aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/type/row.lux
blob: 9965c79e8402d795e94592009d59cbfd96849264 (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
... https://en.wikipedia.org/wiki/Row_polymorphism
(.require
 [library
  [lux (.except Slot macro type the has revised)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["?" parser (.use "[1]#[0]" monad)]
    ["[0]" maybe (.use "[1]#[0]" monad)]
    ["[0]" exception (.only Exception)]]
   [data
    ["[0]" product]
    ["[0]" text (.only)
     ["%" \\format]]
    [collection
     ["[0]" list (.use "[1]#[0]" monad mix)]
     ["[0]" dictionary (.only Dictionary)]
     ["[0]" set]]]
   [math
    [number
     ["n" nat]]]
   ["[0]" meta (.only)
    ["[0]" symbol]
    ["[0]" type]
    ["[0]" code (.only)
     ["?[1]" \\parser (.only Parser)]]
    ["[0]" macro (.only with_symbols)
     ["[0]" vocabulary]
     [syntax (.only syntax)
      ["[0]" export]]]]]])

(vocabulary.def
  [.public Slot]
  [.public slot]
  [.private macro]
  [.private by_name])

(.type .public (Row content extra)
  (Record
   [#content content
    #extra extra]))

(def declaration
  (Parser [Text (List Text)])
  (all ?.either
       (?.and ?code.local (?#in (list)))
       (?code.form (?.and ?code.local (?.some ?code.local)))
       ))

(def un_paired
  (All (_ value)
    (-> (List [value value])
        (List value)))
  (|>> (list#each (function (_ [left right])
                    (list left right)))
       list#conjoint))

(def record
  (Parser (List [Text Code]))
  (?code.tuple (?.many (?.and ?code.local ?code.any))))

(def super
  (Parser Code)
  ?code.any)

(def definition
  (Parser [(Maybe Code) (List [Text Code])])
  (all ?.either
       (?.and (?#in {.#None}) ..record)
       (?.and (?#each maybe#in ..super) ..record)
       ))

(def row_module (symbol.module (symbol ..Row)))
(def row_short (symbol.short (symbol ..Row)))

(def (nesting it)
  (-> Type Nat)
  (when it
    {.#Named _ it}
    (nesting it)

    {.#ExQ (list) un_quantified}
    (when (type.flat_application un_quantified)
      [{.#Named [..row_module ..row_short] _} _]
      1

      [super _]
      (++ (nesting super)))

    _
    0))

(exception.def .public (cannot_repeat_slot_names slots)
  (Exception (List Text))
  (exception.report
   (list ["Slots" (exception.listing %.text slots)])))

(def (unique_slots? it)
  (-> (List Text) Bit)
  (|> it
      (set.of_list text.hash)
      set.size
      (n.= (list.size it))))

(def .public type
  (syntax (_ [[export_policy [name parameters]] (export.with ..declaration)
              [super slots] ..definition])
    (let [slot_names (list#each product.left slots)]
      (if (unique_slots? slot_names)
        (do [! meta.monad]
          [.let [parameters (list#each code.local parameters)]
           name' (macro.symbol name)
           slots' (monad.each ! (function (_ [slot type])
                                  (do !
                                    [slot' (macro.symbol slot)]
                                    (in [slot' type])))
                              slots)
           :super: (when super
                     {.#Some super}
                     (do !
                       [super (meta.eval Type
                                         (` (type_literal (, super))))]
                       (in {.#Some (as Type super)}))

                     _
                     (in {.#None}))]
          (with_symbols [g!_ g!&]
            (let [self (` (Row ((, name') (,* parameters))
                               (, g!&)))]
              (in (list.partial (` (.type (, export_policy) ((, name') (,* parameters))
                                     (Record
                                      [(,* (un_paired slots'))])))
                                (` (.type (, export_policy) ((, (code.local name)) (,* parameters))
                                     (Ex ((, g!_) (, g!&))
                                       (, (when super
                                            {.#Some super}
                                            (` ((, super) (, self)))

                                            _
                                            self)))))
                                (list#each (function (_ [[row_slot _] [record_slot _]])
                                             (` (def (, export_policy) (, (code.local row_slot))
                                                  (..slot
                                                   (syntax ((, g!_) [])
                                                     (of meta.monad (,' in)
                                                         (list (,* (list.repeated (|> :super:
                                                                                      (maybe#each nesting)
                                                                                      (maybe.else 0))
                                                                                  (` (` ..#extra))))
                                                               (` ..#content)
                                                               (` (, record_slot)))))))))
                                           (list.zipped_2 slots slots'))
                                )))))
        (meta.of_try (exception.except ..cannot_repeat_slot_names [slot_names]))))))

(def .public the
  (syntax (_ [slot ?code.symbol
              row ?code.any])
    (do meta.monad
      [slot (by_name slot)
       path ((macro.function slot) (list))]
      (in (list (` (.the [(,* path)] (, row))))))))

(def .public has
  (syntax (_ [slot ?code.symbol
              value ?code.any
              row ?code.any])
    (do meta.monad
      [slot (by_name slot)
       path ((macro.function slot) (list))]
      (in (list (` (.has [(,* path)] (, value) (, row))))))))

(def .public revised
  (syntax (_ [slot ?code.symbol
              revision ?code.any
              row ?code.any])
    (do meta.monad
      [slot (by_name slot)
       path ((macro.function slot) (list))]
      (in (list (` (.revised [(,* path)] (, revision) (, row))))))))

(exception.def .public (missing_level it)
  (Exception Nat)
  (exception.report
   (list ["Level" (%.nat it)])))

(def .public row
  (syntax (_ [slots (?code.tuple (?.many (?.and ?code.symbol ?code.any)))])
    (do [! meta.monad]
      [record (monad.each ! (function (_ [slot value])
                              (do !
                                [slot (by_name slot)
                                 path ((macro.function slot) (list))
                                 .let [nesting (n.- 2 (list.size path))
                                       slot (maybe.trusted (list.last path))]]
                                (in [nesting [slot value]])))
                          slots)
       .let [levels (list#mix (function (_ [nesting slot,value] levels)
                                (dictionary.revised' nesting
                                                     (list)
                                                     (|>> (list.partial slot,value))
                                                     levels))
                              (is (Dictionary Nat (List [Code Code]))
                                  (dictionary.empty n.hash))
                              record)]
       row (monad.mix ! (function (_ level extra)
                          (when (dictionary.value level levels)
                            {.#Some record}
                            (in (` [..#content [(,* (un_paired record))]
                                    ..#extra (, extra)]))
                            
                            {.#None}
                            (meta.of_try (exception.except ..missing_level [level]))))
                      (` [])
                      (|> (dictionary.size levels)
                          list.indices
                          list.reversed))]
      (in (list row)))))