aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/type/primitive.lux
blob: 28dd48137184023c841d11ad6d7177e623323e4a (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
(.using
 [library
  [lux "*"
   ["[0]" meta]
   [abstract
    [monad {"+" Monad do}]]
   [control
    ["[0]" exception {"+" exception:}]
    ["<>" parser ("[1]#[0]" monad)
     ["<[0]>" code {"+" Parser}]]]
   [data
    ["[0]" text ("[1]#[0]" equivalence monoid)]
    [collection
     ["[0]" list ("[1]#[0]" functor monoid)]]]
   [macro
    ["^" pattern]
    ["[0]" code]
    [syntax {"+" syntax:}
     ["|[0]|" export]]]
   [meta
    ["[0]" symbol ("[1]#[0]" codec)]]]]
 ["[0]" //])

(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
  (Record
   [#name Text
    #type_vars (List Code)
    #abstraction Code
    #representation Code]))

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

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

       {.#End}
       (undefined)))])

(def: (peek_frames_definition reference source)
  (-> Text (List [Text Global]) (Stack Frame))
  (!peek source reference
         (case head
           {.#Definition [exported? frame_type frame_value]}
           (as (Stack Frame) frame_value)

           (^.or {.#Type _}
                 {.#Alias _}
                 {.#Tag _}
                 {.#Slot _})
           (undefined))))

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

(exception: .public no_active_frames)

(def: (peek! frame)
  (-> (Maybe Text) (Meta Frame))
  (function (_ compiler)
    (let [[reference definition_reference] (symbol ..frames)
          current_frames (peek_frames reference definition_reference (the .#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
  (Meta Frame)
  (..peek! {.#None}))

(def: .public (specific name)
  (-> Text (Meta Frame))
  (..peek! {.#Some name}))

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

       {.#End}
       (undefined)))])

(def: (push_frame_definition reference frame source)
  (-> Text Frame (List [Text Global]) (List [Text Global]))
  (!push source reference
         (case head
           {.#Definition [exported? frames_type frames_value]}
           {.#Definition [exported?
                          frames_type
                          (..push frame (as (Stack Frame) frames_value))]}

           (^.or {.#Type _}
                 {.#Alias _}
                 {.#Tag _}
                 {.#Slot _})
           (undefined))))

(def: (push_frame [module_reference definition_reference] frame source)
  (-> Symbol 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 (symbol ..frames) frame)
                       compiler)
              []]}))

(def: (pop_frame_definition reference source)
  (-> Text (List [Text Global]) (List [Text Global]))
  (!push source reference
         (case head
           {.#Definition [exported? frames_type frames_value]}
           {.#Definition [exported?
                          frames_type
                          (let [current_frames (as (Stack Frame) frames_value)]
                            (case (..pop current_frames)
                              {.#Some current_frames'}
                              current_frames'

                              {.#None}
                              current_frames))]}

           (^.or {.#Type _}
                 {.#Alias _}
                 {.#Tag _}
                 {.#Slot _})
           (undefined))))

(def: (pop_frame [module_reference definition_reference] source)
  (-> Symbol (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 (symbol ..frames))
                       compiler)
              (list)]}))

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

(template [<name> <from> <to>]
  [(syntax: .public (<name> [[frame value] ..cast])
     (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
  (-> Symbol Text)
  symbol#encoded)

(def: representation_definition_name
  (-> Text Text)
  (|>> (all text#composite
            (symbol#encoded (symbol ..#Representation))
            " ")))

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

(def: abstract
  (Parser [Code [Text (List Text)] Code (List Code)])
  (|export|.parser
   (all <>.and
        ..declaration
        <code>.any
        (<>.some <code>.any)
        )))

... TODO: Make sure the generated code always gets optimized away.
... (This applies to uses of "abstraction" and "representation")
(syntax: .public (primitive: [[export_policy [name type_vars] representation_type primitives]
                              ..abstract])
  (do meta.monad
    [current_module meta.current_module_name
     .let [type_varsC (list#each code.local type_vars)
           abstraction_declaration (` ((~ (code.local name)) (~+ type_varsC)))
           representation_declaration (` ((~ (code.local (representation_definition_name name)))
                                          (~+ type_varsC)))]
     _ (..push! [name
                 type_varsC
                 abstraction_declaration
                 representation_declaration])]
    (in (partial_list (` (type: (~ export_policy) (~ abstraction_declaration)
                           (Primitive (~ (code.text (abstraction_type_name [current_module name])))
                                      [(~+ type_varsC)])))
                      (` (type: (~ representation_declaration)
                           (~ representation_type)))
                      (all list#composite
                           primitives
                           (list (` ((~! ..pop!)))))))))

(type: (Selection a)
  (Variant
   {#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)])
  (case selection
    {#Specific specific value}
    (in (list (` (.|> (~ value)
                      (..representation (~ specific))
                      (..abstraction (~ specific))))))
    
    {#Current value}
    (in (list (` (.|> (~ value) ..representation ..abstraction))))))