aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/type/abstract.lux
blob: 0cbe49087d1071f21a4b0d7d11fe2bc1ced2efc5 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
(.module:
  lux
  (lux (control [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 ":abstraction")
(def: up-cast Text ":representation")
(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 Any))
  (do macro.Monad<Meta>
    [this-module (macro.find-module this-module-name)
     #let [type-varsC (list/map code.local-symbol type-vars)
           abstract-declaration (` ((~ (code.local-symbol name)) (~+ type-varsC)))
           representation-declaration (` ((~ (code.local-symbol (representation-name name))) (~+ type-varsC)))
           this-module (|> this-module
                           (update@ #.definitions (put down-cast (: Definition
                                                                    [Macro macro-anns
                                                                     (: Macro
                                                                        (function (_ tokens)
                                                                          (case tokens
                                                                            (^ (list value))
                                                                            (wrap (list (` ((: (All [(~+ type-varsC)]
                                                                                                 (-> (~ representation-declaration) (~ abstract-declaration)))
                                                                                               (|>> :assume))
                                                                                            (~ value)))))

                                                                            _
                                                                            (macro.fail ($_ text/compose "Wrong syntax for " down-cast)))))])))
                           (update@ #.definitions (put up-cast (: Definition
                                                                  [Macro macro-anns
                                                                   (: Macro
                                                                      (function (_ tokens)
                                                                        (case tokens
                                                                          (^ (list value))
                                                                          (wrap (list (` ((: (All [(~+ type-varsC)]
                                                                                               (-> (~ abstract-declaration) (~ representation-declaration)))
                                                                                             (|>> :assume))
                                                                                          (~ 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 Any))
  (do macro.Monad<Meta>
    [this-module (macro.find-module this-module-name)
     #let [this-module (|> this-module
                           (update@ #.definitions (remove down-cast))
                           (update@ #.definitions (remove up-cast)))]]
    (function (_ compiler)
      (#E.Success [(update@ #.modules (put this-module-name this-module) compiler)
                   []]))))

(syntax: (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: (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 (abstract:
                   {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 (representation-name name)
        type-varsC (list/map code.local-symbol type-vars)
        abstract-declaration (` ((~ (code.local-symbol name)) (~+ type-varsC)))
        representation-declaration (` ((~ (code.local-symbol hidden-name)) (~+ type-varsC)))]
    (wrap (list& (` (type: (~+ (csw.export export)) (~ abstract-declaration)
                      (~ (csw.annotations annotations))
                      (primitive (~ (code.text 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)))))))))

(syntax: #export (^:representation {name (s.form s.local-symbol)}
                                   body
                                   {branches (p.some s.any)})
  (let [g!representation (code.local-symbol name)]
    (do @
      [current-module macro.current-module-name
       #let [g!:representation (code.symbol [current-module up-cast])]]
      (wrap (list& g!representation
                   (` (.let [(~ g!representation) ((~ g!:representation) (~ g!representation))]
                        (~ body)))
                   branches)))))