aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/type/abstract.lux
blob: aa00fa4fde347fa3395f3845a16a815564cb88ae (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
(.module:
  [lux (#- Scope)
   [abstract
    [monad (#+ Monad do)]]
   [control
    ["ex" exception (#+ exception:)]
    ["<>" parser ("#@." monad)
     ["<c>" code (#+ Parser)]]]
   [data
    ["." name ("#@." codec)]
    ["." text ("#@." equivalence monoid)]
    [collection
     ["." list ("#@." functor monoid)]
     ["." stack (#+ Stack)]]]
   ["." macro ("#@." monad)
    ["." code]
    [syntax (#+ syntax:)
     ["cs" common
      ["csr" reader]
      ["csw" writer]]]]
   [type (#+ :cast)]])

(type: Scope
  {#name Text
   #type-vars (List Code)
   #abstraction Code
   #representation Code})

(def: scopes
  (Stack Scope)
  stack.empty)

(template: (!peek <source> <reference> <then>)
  (loop [entries <source>]
    (case entries
      (#.Cons [head-name head] tail)
      (if (text@= <reference> head-name)
        <then>
        (recur tail))

      #.Nil
      (undefined))))

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

           (#.Right [exported? scope-type scope-anns scope-value])
           (:coerce (Stack Scope) scope-value))))

(def: (peek-scopes reference definition-reference source)
  (-> Text Text (List [Text Module]) (Stack Scope))
  (!peek source reference
         (peek-scopes-definition definition-reference (get@ #.definitions head))))

(exception: #export (no-active-scopes)
  "")

(def: (peek! scope)
  (-> (Maybe Text) (Meta Scope))
  (function (_ compiler)
    (let [[reference definition-reference] (name-of ..scopes)
          current-scopes (peek-scopes reference definition-reference (get@ #.modules compiler))]
      (case (case scope
              (#.Some scope)
              (list.find (function (_ [actual _])
                           (text@= scope actual))
                         current-scopes)
              
              #.None
              (stack.peek current-scopes))
        (#.Some scope)
        (#.Right [compiler scope])
        
        #.None
        (ex.throw no-active-scopes [])))))

(template: (!push <source> <reference> <then>)
  (loop [entries <source>]
    (case entries
      (#.Cons [head-name head] tail)
      (if (text@= <reference> head-name)
        (#.Cons [head-name <then>]
                tail)
        (#.Cons [head-name head]
                (recur tail)))

      #.Nil
      (undefined))))

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

           (#.Right [exported? scopes-type scopes-anns scopes-value])
           (#.Right [exported?
                     scopes-type
                     scopes-anns
                     (stack.push scope (:coerce (Stack Scope) scopes-value))]))))

(def: (push-scope [module-reference definition-reference] scope source)
  (-> Name Scope (List [Text Module]) (List [Text Module]))
  (!push source module-reference
         (|> head (update@ #.definitions (push-scope-definition definition-reference scope)))))

(def: (push! scope)
  (-> Scope (Meta Any))
  (function (_ compiler)
    (#.Right [(update@ #.modules
                       (..push-scope (name-of ..scopes) scope)
                       compiler)
              []])))

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

           (#.Right [exported? scopes-type scopes-anns scopes-value])
           (#.Right [exported?
                     scopes-type
                     scopes-anns
                     (let [current-scopes (:coerce (Stack Scope) scopes-value)]
                       (case (stack.pop current-scopes)
                         (#.Some current-scopes')
                         current-scopes'

                         #.None
                         current-scopes))]))))

(def: (pop-scope [module-reference definition-reference] source)
  (-> Name (List [Text Module]) (List [Text Module]))
  (!push source module-reference
         (|> head (update@ #.definitions (pop-scope-definition definition-reference)))))

(syntax: (pop!)
  (function (_ compiler)
    (#.Right [(update@ #.modules
                       (..pop-scope (name-of ..scopes))
                       compiler)
              (list)])))

(def: cast
  (Parser [(Maybe Text) Code])
  (<>.either (<>.and (<>.maybe <c>.local-identifier) <c>.any)
             (<>.and (<>@wrap #.None) <c>.any)))

(template [<name> <from> <to>]
  [(syntax: #export (<name> {[scope value] cast})
     (do @
       [[name type-vars abstraction representation] (peek! scope)]
       (wrap (list (` ((~! :cast) [(~+ 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 (<c>.form (<>.and <c>.local-identifier (<>.some <c>.local-identifier)))
             (<>.and <c>.local-identifier (:: <>.monad wrap (list)))))

## TODO: Make sure the generated code always gets optimized away.
## (This applies to uses of ":abstraction" and ":representation")
(syntax: #export (abstract:
                   {export csr.export}
                   {[name type-vars] declaration}
                   {annotations (<>.default cs.empty-annotations csr.annotations)}
                   representation-type
                   {primitives (<>.some <c>.any)})
  (do @
    [current-module macro.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])]
    (wrap (list& (` (type: (~+ (csw.export export)) (~ abstraction-declaration)
                      (~ (csw.annotations annotations))
                      (primitive (~ (code.text (abstraction-type-name [current-module name])))
                                 [(~+ type-varsC)])))
                 (` (type: (~ representation-declaration)
                      (~ representation-type)))
                 ($_ list@compose
                     primitives
                     (list (` ((~! ..pop!)))))))))

(syntax: #export (:transmutation value)
  (wrap (list (` (..:abstraction (..:representation (~ value)))))))

(syntax: #export (^:representation {name (<c>.form <c>.local-identifier)}
                                   body
                                   {branches (<>.some <c>.any)})
  (let [g!var (code.local-identifier name)]
    (wrap (list& g!var
                 (` (.let [(~ g!var) (..:representation (~ g!var))]
                      (~ body)))
                 branches))))