aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/type/abstract.lux
blob: 9b657cb1994c633ae32299b2342f445ecc7262d6 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
(.module:
  [library
   [lux #*
    ["." meta]
    [abstract
     [monad (#+ Monad do)]]
    [control
     ["." exception (#+ exception:)]
     ["<>" parser ("#\." monad)
      ["<.>" code (#+ Parser)]]]
    [data
     ["." name ("#\." codec)]
     ["." text ("#\." equivalence monoid)]
     [collection
      ["." list ("#\." functor monoid)]]]
    [macro
     ["." code]
     [syntax (#+ syntax:)
      ["|.|" annotations]]]]]
  ["." //])

(type: Stack
  List)

(def: peek
  (All [a] (-> (Stack a) (Maybe a)))
  list.head)

(def: (push value stack)
  (All [a] (-> a (Stack a) (Stack a)))
  (#.Item value stack))

(def: pop
  (All [a] (-> (Stack a) (Maybe (Stack a))))
  list.tail)

(type: .public Frame
  {#.doc (example "Meta-data about an abstract/nominal type in a stack of them.")}
  {#name Text
   #type_vars (List Code)
   #abstraction Code
   #representation Code})

(def: frames
  (Stack Frame)
  #.End)

(template: (!peek <source> <reference> <then>)
  [(loop [entries <source>]
     (case entries
       (#.Item [head_name head] tail)
       (if (text\= <reference> head_name)
         <then>
         (recur tail))

       #.End
       (undefined)))])

(def: (peek_frames_definition reference source)
  (-> Text (List [Text Global]) (Stack Frame))
  (!peek source reference
         (case head
           (#.Left _)
           (undefined)

           (#.Right [exported? frame_type frame_anns frame_value])
           (:as (Stack Frame) frame_value))))

(def: (peek_frames reference definition_reference source)
  (-> Text Text (List [Text Module]) (Stack Frame))
  (!peek source reference
         (peek_frames_definition definition_reference (value@ #.definitions head))))

(exception: .public no_active_frames)

(def: (peek! frame)
  (-> (Maybe Text) (Meta Frame))
  (function (_ compiler)
    (let [[reference definition_reference] (name_of ..frames)
          current_frames (peek_frames reference definition_reference (value@ #.modules compiler))]
      (case (case frame
              (#.Some frame)
              (list.example (function (_ [actual _])
                              (text\= frame actual))
                            current_frames)
              
              #.None
              (..peek current_frames))
        (#.Some frame)
        (#.Right [compiler frame])
        
        #.None
        (exception.except ..no_active_frames [])))))

(def: .public current
  {#.doc (example "The currently-being-defined abstract/nominal type.")}
  (Meta Frame)
  (..peek! #.None))

(def: .public (specific name)
  {#.doc (example "A specific abstract/nominal type still being defined somewhere in the scope.")}
  (-> Text (Meta Frame))
  (..peek! (#.Some name)))

(template: (!push <source> <reference> <then>)
  [(loop [entries <source>]
     (case entries
       (#.Item [head_name head] tail)
       (if (text\= <reference> head_name)
         (#.Item [head_name <then>]
                 tail)
         (#.Item [head_name head]
                 (recur tail)))

       #.End
       (undefined)))])

(def: (push_frame_definition reference frame source)
  (-> Text Frame (List [Text Global]) (List [Text Global]))
  (!push source reference
         (case head
           (#.Left _)
           (undefined)

           (#.Right [exported? frames_type frames_anns frames_value])
           (#.Right [exported?
                     frames_type
                     frames_anns
                     (..push frame (:as (Stack Frame) frames_value))]))))

(def: (push_frame [module_reference definition_reference] frame source)
  (-> Name Frame (List [Text Module]) (List [Text Module]))
  (!push source module_reference
         (revised@ #.definitions (push_frame_definition definition_reference frame) head)))

(def: (push! frame)
  (-> Frame (Meta Any))
  (function (_ compiler)
    (#.Right [(revised@ #.modules
                        (..push_frame (name_of ..frames) frame)
                        compiler)
              []])))

(def: (pop_frame_definition reference source)
  (-> Text (List [Text Global]) (List [Text Global]))
  (!push source reference
         (case head
           (#.Left _)
           (undefined)

           (#.Right [exported? frames_type frames_anns frames_value])
           (#.Right [exported?
                     frames_type
                     frames_anns
                     (let [current_frames (:as (Stack Frame) frames_value)]
                       (case (..pop current_frames)
                         (#.Some current_frames')
                         current_frames'

                         #.None
                         current_frames))]))))

(def: (pop_frame [module_reference definition_reference] source)
  (-> Name (List [Text Module]) (List [Text Module]))
  (!push source module_reference
         (|> head (revised@ #.definitions (pop_frame_definition definition_reference)))))

(syntax: (pop! [])
  (function (_ compiler)
    (#.Right [(revised@ #.modules
                        (..pop_frame (name_of ..frames))
                        compiler)
              (list)])))

(def: cast
  (Parser [(Maybe Text) Code])
  (<>.either (<>.and (<>.maybe <code>.local_identifier) <code>.any)
             (<>.and (<>\in #.None) <code>.any)))

(template [<name> <from> <to>]
  [(syntax: .public (<name> [[frame value] ..cast])
     {#.doc (example "Type-casting macro for abstract/nominal types."
                     (: <to>
                        (<name> (: <from>
                                   value))))}
     (do meta.monad
       [[name type_vars abstraction representation] (peek! frame)]
       (in (list (` ((~! //.:as) [(~+ type_vars)] (~ <from>) (~ <to>)
                     (~ value)))))))]

  [:abstraction representation abstraction]
  [:representation abstraction representation]
  )

(def: abstraction_type_name
  (-> Name Text)
  (|>> name\encode
       ($_ text\compose
           (name\encode (name_of #..Abstraction))
           " ")))

(def: representation_definition_name
  (-> Text Text)
  (|>> ($_ text\compose
           (name\encode (name_of #..Representation))
           " ")))

(def: declaration
  (Parser [Text (List Text)])
  (<>.either (<code>.form (<>.and <code>.local_identifier (<>.some <code>.local_identifier)))
             (<>.and <code>.local_identifier (\ <>.monad in (list)))))

(def: abstract
  (Parser [Code [Text (List Text)] |annotations|.Annotations Code (List Code)])
  (let [private (: (Parser [[Text (List Text)] |annotations|.Annotations Code (List Code)])
                   ($_ <>.and
                       ..declaration
                       |annotations|.parser
                       <code>.any
                       (<>.some <code>.any)
                       ))]
    ($_ <>.either
        (<>.and <code>.any private)
        (<>.and (<>\in (` .private)) private)
        )))

... TODO: Make sure the generated code always gets optimized away.
... (This applies to uses of ":abstraction" and ":representation")
(syntax: .public (abstract: [[export_policy [name type_vars] annotations representation_type primitives]
                             ..abstract])
  {#.doc (example "Define abstract/nominal types which hide their representation details."
                  "You can convert between the abstraction and its representation selectively to access the value, while hiding it from others."
                  (abstract: String
                    {#.doc "An opaque text."}

                    Text

                    (def: (string value)
                      (-> Text String)
                      (:abstraction value))

                    (def: (text value)
                      (-> String Text)
                      (:representation value)))

                  "Type-parameters are optional."
                  (abstract: (Duplicate a)
                    {}

                    [a a]

                    (def: (duplicate value)
                      (All [a] (-> a (Duplicate a)))
                      (:abstraction [value value])))

                  "Definitions can be nested."
                  (abstract: (Single a)
                    {}

                    a

                    (def: (single value)
                      (All [a] (-> a (Single a)))
                      (:abstraction value))

                    (abstract: (Double a)
                      {}

                      [a a]

                      (def: (double value)
                        (All [a] (-> a (Double a)))
                        (:abstraction [value value]))

                      (def: (single' value)
                        (All [a] (-> a (Single a)))
                        (:abstraction Single [value value]))

                      (let [value 0123]
                        (same? value
                               (|> value
                                   single'
                                   (:representation Single)
                                   double
                                   :representation)))))

                  "Type-parameters do not necessarily have to be used in the representation type."
                  "If they are not used, they become phantom types and can be used to customize types without changing the representation."
                  (abstract: (JavaScript a)
                    {}

                    Text

                    (abstract: Expression {} Any)
                    (abstract: Statement {} Any)

                    (def: (+ x y)
                      (-> (JavaScript Expression) (JavaScript Expression) (JavaScript Expression))
                      (:abstraction
                       (format "(" (:representation x) "+" (:representation y) ")")))

                    (def: (while test body)
                      (-> (JavaScript Expression) (JavaScript Statement) (JavaScript Statement))
                      (:abstraction
                       (format "while(" (:representation test) ") {"
                               (:representation body)
                               "}"))))
                  )}
  (do meta.monad
    [current_module meta.current_module_name
     .let [type_varsC (list\map code.local_identifier type_vars)
           abstraction_declaration (` ((~ (code.local_identifier name)) (~+ type_varsC)))
           representation_declaration (` ((~ (code.local_identifier (representation_definition_name name)))
                                          (~+ type_varsC)))]
     _ (..push! [name
                 type_varsC
                 abstraction_declaration
                 representation_declaration])]
    (in (list& (` (type: (~ export_policy) (~ abstraction_declaration)
                    (~ (|annotations|.format annotations))
                    (primitive (~ (code.text (abstraction_type_name [current_module name])))
                               [(~+ type_varsC)])))
               (` (type: (~ representation_declaration)
                    (~ representation_type)))
               ($_ list\compose
                   primitives
                   (list (` ((~! ..pop!)))))))))

(type: (Selection a)
  (#Specific Code a)
  (#Current a))

(def: (selection parser)
  (All [a] (-> (Parser a) (Parser (Selection a))))
  (<>.or (<>.and <code>.any parser)
         parser))

(syntax: .public (:transmutation [selection (..selection <code>.any)])
  {#.doc (example "Transmutes an abstract/nominal type's phantom types."
                  (abstract: (JavaScript a)
                    {}

                    Text

                    (abstract: Expression {} Any)
                    (abstract: Statement {} Any)

                    (def: (statement expression)
                      (-> (JavaScript Expression) (JavaScript Statement))
                      (:transmutation expression))

                    (def: (statement' expression)
                      (-> (JavaScript Expression) (JavaScript Statement))
                      (:transmutation JavaScript expression))))}
  (case selection
    (#Specific specific value)
    (in (list (` (.|> (~ value)
                      (..:representation (~ specific))
                      (..:abstraction (~ specific))))))
    
    (#Current value)
    (in (list (` (.|> (~ value) ..:representation ..:abstraction))))))

(syntax: .public (^:representation [selection (<code>.form (..selection <code>.local_identifier))
                                    body <code>.any
                                    branches (<>.some <code>.any)])
  {#.doc (example "Pattern-matching macro to easily extract a representation."
                  (def: (computation abstraction)
                    (All [a] (-> (Abstract a) ???))
                    (let [(^:representation value) abstraction]
                      (foo (bar (baz value))))))}
  (case selection
    (#Specific specific name)
    (let [g!var (code.local_identifier name)]
      (in (list& g!var
                 (` (.let [(~ g!var) (..:representation (~ specific) (~ g!var))]
                      (~ body)))
                 branches)))
    
    (#Current name)
    (let [g!var (code.local_identifier name)]
      (in (list& g!var
                 (` (.let [(~ g!var) (..:representation (~ g!var))]
                      (~ body)))
                 branches)))))