aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/type/resource.lux
blob: 16bb08f50e45a77e5bd65674decdea895dea1095 (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
(.module:
  [lux #*
   [abstract
    ["." monad (#+ Monad do)
     [indexed (#+ IxMonad)]]]
   [control
    ["p" parser
     ["s" code (#+ Parser)]]
    ["." exception (#+ exception:)]
    ["." io (#+ IO)]
    [concurrency
     ["." promise (#+ Promise)]]]
   [data
    ["." identity (#+ Identity)]
    ["." maybe]
    ["." product]
    [number
     ["n" nat]]
    [text
     ["%" format (#+ format)]]
    [collection
     ["." set]
     ["." row (#+ Row)]
     ["." list ("#@." functor fold)]]]
   ["." macro
    [syntax (#+ syntax:)]]
   [type
    abstract]])

(type: #export (Procedure monad input output value)
  (-> input (monad [output value])))

(type: #export (Linear monad value)
  (All [keys]
    (Procedure monad keys keys value)))

(type: #export (Affine monad permissions value)
  (All [keys]
    (Procedure monad keys [permissions keys] value)))

(type: #export (Relevant monad permissions value)
  (All [keys]
    (Procedure monad [permissions keys] keys value)))

(structure: (indexed Monad<m>)
  (All [m] (-> (Monad m) (IxMonad (Procedure m))))
  
  (def: (wrap value)
    (function (_ keys)
      (:: Monad<m> wrap [keys value])))

  (def: (bind f input)
    (function (_ keysI)
      (do Monad<m>
        [[keysT value] (input keysI)]
        ((f value) keysT)))))

(template [<name> <m> <monad> <execute> <lift>]
  [(def: #export <name>
     (IxMonad (Procedure <m>))
     (..indexed <monad>))

   (def: #export (<execute> procedure)
     (All [v] (-> (Linear <m> v) (<m> v)))
     (do <monad>
       [[_ output] (procedure [])]
       (wrap output)))

   (def: #export (<lift> procedure)
     (All [v] (-> (<m> v) (Linear <m> v)))
     (function (_ keys)
       (do <monad>
         [output procedure]
         (wrap [keys output]))))]

  [pure  Identity identity.monad run-pure  lift-pure]
  [sync  IO       io.monad       run-sync  lift-sync]
  [async Promise  promise.monad  run-async lift-async]
  )

(abstract: #export Ordered [])

(abstract: #export Commutative [])

(abstract: #export (Key mode key)
  []

  (template [<name> <mode>]
    [(def: <name>
       (Ex [k] (-> [] (Key <mode> k)))
       (|>> :abstraction))]

    [ordered-key     Ordered]
    [commutative-key Commutative]
    ))

(type: #export OK (Key Ordered))
(type: #export CK (Key Commutative))

(abstract: #export (Res key value)
  value

  {#.doc "A value locked by a key."}

  (template [<name> <m> <monad> <mode> <key>]
    [(def: #export (<name> value)
       (All [v] (Ex [k] (-> v (Affine <m> (Key <mode> k) (Res k v)))))
       (function (_ keys)
         (:: <monad> wrap [[(<key> []) keys] (:abstraction value)])))]

    [ordered-pure      Identity identity.monad Ordered     ordered-key]
    [ordered-sync      IO       io.monad       Ordered     ordered-key]
    [ordered-async     Promise  promise.monad  Ordered     ordered-key]
    [commutative-sync  IO       io.monad       Commutative commutative-key]
    [commutative-pure  Identity identity.monad Commutative commutative-key]
    [commutative-async Promise  promise.monad  Commutative commutative-key]
    )

  (template [<name> <m> <monad>]
    [(def: #export (<name> resource)
       (All [v k m]
         (-> (Res k v) (Relevant <m> (Key m k) v)))
       (function (_ [key keys])
         (:: <monad> wrap [keys (:representation resource)])))]

    [read-pure  Identity identity.monad]
    [read-sync  IO       io.monad]
    [read-async Promise  promise.monad]
    ))

(exception: #export (index-cannot-be-repeated {index Nat})
  (exception.report
   ["Index" (%.nat index)]))

(exception: #export amount-cannot-be-zero)

(def: indices
  (Parser (List Nat))
  (s.tuple (loop [seen (set.new n.hash)]
             (do {@ p.monad}
               [done? s.end?]
               (if done?
                 (wrap (list))
                 (do @
                   [head s.nat
                    _ (p.assert (exception.construct index-cannot-be-repeated head)
                                (not (set.member? seen head)))
                    tail (recur (set.add head seen))]
                   (wrap (list& head tail))))))))

(def: (no-op Monad<m>)
  (All [m] (-> (Monad m) (Linear m Any)))
  (function (_ context) (:: Monad<m> wrap [context []])))

(template [<name> <m> <monad>]
  [(syntax: #export (<name> {swaps ..indices})
     (macro.with-gensyms [g!_ g!context]
       (case swaps
         #.Nil
         (wrap (list (` ((~! no-op) <monad>))))

         (#.Cons head tail)
         (do {@ macro.monad}
           [#let [max-idx (list@fold n.max head tail)]
            g!inputs (<| (monad.seq @) (list.repeat (inc max-idx)) (macro.gensym "input"))
            #let [g!outputs (|> (monad.fold maybe.monad
                                            (function (_ from to)
                                              (do maybe.monad
                                                [input (list.nth from g!inputs)]
                                                (wrap (row.add input to))))
                                            (: (Row Code) row.empty)
                                            swaps)
                                maybe.assume
                                row.to-list)
                  g!inputsT+ (list@map (|>> (~) ..CK (`)) g!inputs)
                  g!outputsT+ (list@map (|>> (~) ..CK (`)) g!outputs)]]
           (wrap (list (` (: (All [(~+ g!inputs) (~ g!context)]
                               (Procedure (~! <m>)
                                          [(~+ g!inputsT+) (~ g!context)]
                                          [(~+ g!outputsT+) (~ g!context)]
                                          .Any))
                             (function ((~ g!_) [(~+ g!inputs) (~ g!context)])
                               (:: (~! <monad>) (~' wrap) [[(~+ g!outputs) (~ g!context)] []]))))))))))]

  [exchange-pure  Identity identity.monad]
  [exchange-sync  IO       io.monad]
  [exchange-async Promise  promise.monad]
  )

(def: amount
  (Parser Nat)
  (do p.monad
    [raw s.nat
     _ (p.assert (exception.construct amount-cannot-be-zero [])
                 (n.> 0 raw))]
    (wrap raw)))

(template [<name> <m> <monad> <from> <to>]
  [(syntax: #export (<name> {amount ..amount})
     (macro.with-gensyms [g!_ g!context]
       (do {@ macro.monad}
         [g!keys (<| (monad.seq @) (list.repeat amount) (macro.gensym "keys"))]
         (wrap (list (` (: (All [(~+ g!keys) (~ g!context)]
                             (Procedure (~! <m>)
                                        [<from> (~ g!context)]
                                        [<to> (~ g!context)]
                                        .Any))
                           (function ((~ g!_) [<from> (~ g!context)])
                             (:: (~! <monad>) (~' wrap) [[<to> (~ g!context)] []])))))))))]

  [group-pure     Identity identity.monad (~+ g!keys)   [(~+ g!keys)]]
  [group-sync     IO       io.monad       (~+ g!keys)   [(~+ g!keys)]]
  [group-async    Promise  promise.monad  (~+ g!keys)   [(~+ g!keys)]]
  [un-group-pure  Identity identity.monad [(~+ g!keys)] (~+ g!keys)]
  [un-group-sync  IO       io.monad       [(~+ g!keys)] (~+ g!keys)]
  [un-group-async Promise  promise.monad  [(~+ g!keys)] (~+ g!keys)]
  )