aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/type/opaque.lux
blob: 3b50fcbc2195059c24534a6951b8776bbf4b30b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(;module:
  lux
  (lux (control [applicative]
                [monad #+ do Monad]
                ["p" parser])
       (data [text "text/" Eq<Text> Monoid<Text>]
             ["E" error]
             (coll [list "list/" Functor<List> Monoid<List>]))
       [macro]
       (macro [code]
              ["s" syntax #+ syntax:]
              (syntax ["cs" common]
                      (common ["csr" reader]
                              ["csw" writer])))))

(def: (get k plist)
  (All [a]
    (-> Text (List [Text a]) (Maybe a)))
  (case plist
    #;Nil
    #;None

    (#;Cons [k' v] plist')
    (if (text/= k k')
      (#;Some v)
      (get k plist'))))

(def: (put k v plist)
  (All [a]
    (-> Text a (List [Text a]) (List [Text a])))
  (case plist
    #;Nil
    (list [k v])

    (#;Cons [k' v'] plist')
    (if (text/= k k')
      (#;Cons [k' v] plist')
      (#;Cons [k' v'] (put k v plist')))))

(def: (remove k plist)
  (All [a]
    (-> Text (List [Text a]) (List [Text a])))
  (case plist
    #;Nil
    #;Nil

    (#;Cons [k' v'] plist')
    (if (text/= k k')
      plist'
      (#;Cons [k' v'] (remove k plist')))))

(def: down-cast Text "@opaque")
(def: up-cast Text "@repr")
(def: macro-anns Code (' {#;macro? true}))

(def: representation-name
  (-> Text Text)
  (|>. ($_ text/compose "{" kind "@" module "}")
       (let [[module kind] (ident-for #;;Representation)])))

(def: (install-casts' this-module-name name type-vars)
  (-> Text Text (List Text) (Meta Unit))
  (do macro;Monad<Meta>
    [this-module (macro;find-module this-module-name)
     #let [type-varsC (list/map code;local-symbol type-vars)
           opaque-declaration (` ((~ (code;local-symbol name)) (~@ type-varsC)))
           representation-declaration (` ((~ (code;local-symbol (representation-name name))) (~@ type-varsC)))
           this-module (|> this-module
                           (update@ #;defs (put down-cast (: Def
                                                             [Macro macro-anns
                                                              (function [tokens]
                                                                (case tokens
                                                                  (^ (list value))
                                                                  (wrap (list (` ((: (All [(~@ type-varsC)]
                                                                                       (-> (~ representation-declaration) (~ opaque-declaration)))
                                                                                     (|>. :!!))
                                                                                  (~ value)))))

                                                                  _
                                                                  (macro;fail ($_ text/compose "Wrong syntax for " down-cast))))])))
                           (update@ #;defs (put up-cast (: Def
                                                           [Macro macro-anns
                                                            (function [tokens]
                                                              (case tokens
                                                                (^ (list value))
                                                                (wrap (list (` ((: (All [(~@ type-varsC)]
                                                                                     (-> (~ opaque-declaration) (~ representation-declaration)))
                                                                                   (|>. :!!))
                                                                                (~ value)))))

                                                                _
                                                                (macro;fail ($_ text/compose "Wrong syntax for " up-cast))))]))))]]
    (function [compiler]
      (#E;Success [(update@ #;modules (put this-module-name this-module) compiler)
                   []]))))

(def: (un-install-casts' this-module-name)
  (-> Text (Meta Unit))
  (do macro;Monad<Meta>
    [this-module (macro;find-module this-module-name)
     #let [this-module (|> this-module
                           (update@ #;defs (remove down-cast))
                           (update@ #;defs (remove up-cast)))]]
    (function [compiler]
      (#E;Success [(update@ #;modules (put this-module-name this-module) compiler)
                   []]))))

(syntax: #hidden (install-casts [name s;local-symbol]
                                [type-vars (s;tuple (p;some s;local-symbol))])
  (do @
    [this-module-name macro;current-module-name
     ?down-cast (macro;find-macro [this-module-name down-cast])
     ?up-cast (macro;find-macro [this-module-name up-cast])]
    (case [?down-cast ?up-cast]
      [#;None #;None]
      (do @
        [_ (install-casts' this-module-name name type-vars)]
        (wrap (list)))

      _
      (macro;fail ($_ text/compose
                      "Cannot temporarily define casting functions ("
                      down-cast " & " up-cast
                      ") because definitions like that already exist.")))))

(syntax: #hidden (un-install-casts)
  (do macro;Monad<Meta>
    [this-module-name macro;current-module-name
     ?down-cast (macro;find-macro [this-module-name down-cast])
     ?up-cast (macro;find-macro [this-module-name up-cast])]
    (case [?down-cast ?up-cast]
      [(#;Some _) (#;Some _)]
      (do @
        [_ (un-install-casts' this-module-name)]
        (wrap (list)))

      _
      (macro;fail ($_ text/compose
                      "Cannot un-define casting functions ("
                      down-cast " & " up-cast
                      ") because they do not exist.")))))

(def: declaration
  (s;Syntax [Text (List Text)])
  (p;either (s;form (p;seq s;local-symbol (p;some s;local-symbol)))
            (p;seq s;local-symbol (:: p;Monad<Parser> wrap (list)))))

(syntax: #export (opaque: [export csr;export]
                   [[name type-vars] declaration]
                   [annotations (p;default cs;empty-annotations csr;annotations)]
                   representation-type
                   [primitives (p;some s;any)])
  (let [hidden-name (code;local-symbol (representation-name name))
        type-varsC (list/map code;local-symbol type-vars)
        opaque-declaration (` ((~ (code;local-symbol name)) (~@ type-varsC)))
        representation-declaration (` ((~ hidden-name) (~@ type-varsC)))]
    (wrap (list& (` (type: (~@ (csw;export export)) (~ opaque-declaration)
                      (~ (csw;annotations annotations))
                      (primitive (~ hidden-name) [(~@ type-varsC)])))
                 (` (type: (~@ (csw;export export)) (~ representation-declaration)
                      (~ representation-type)))
                 (` (install-casts (~ (code;local-symbol name)) [(~@ type-varsC)]))
                 (list/compose primitives
                               (list (` (un-install-casts))))))))