aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/type/resource.lux
blob: 05eb845e5785b60830f7d874180b67fd95d57f1d (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
(.using
  [library
   [lux "*"
    ["[0]" meta]
    [abstract
     ["[0]" monad {"+" Monad do}
      [indexed {"+" IxMonad}]]]
    [control
     ["[0]" maybe]
     ["[0]" exception {"+" exception:}]
     ["<>" parser
      ["<[0]>" code {"+" Parser}]]]
    [data
     [text
      ["%" format {"+" format}]]
     [collection
      ["[0]" set]
      ["[0]" sequence {"+" Sequence}]
      ["[0]" list ("[1]#[0]" functor mix)]]]
    ["[0]" macro
     [syntax {"+" syntax:}]]
    [math
     [number
      ["n" nat]]]
    [type
     abstract]]])

(type: .public (Procedure monad input output value)
  (-> input (monad [output value])))

(type: .public (Linear monad value)
  (All (_ keys)
    (Procedure monad keys keys value)))

(type: .public (Affine monad permissions value)
  (All (_ keys)
    (Procedure monad keys [permissions keys] value)))

(type: .public (Relevant monad permissions value)
  (All (_ keys)
    (Procedure monad [permissions keys] keys value)))

(implementation: .public (monad monad)
  (All (_ !) (-> (Monad !) (IxMonad (Procedure !))))
  
  (def: (in value)
    (function (_ keys)
      (# monad in [keys value])))

  (def: (then f input)
    (function (_ keysI)
      (do monad
        [[keysT value] (input keysI)]
        ((f value) keysT)))))

(def: .public (run! monad procedure)
  (All (_ ! v) (-> (Monad !) (Linear ! v) (! v)))
  (do monad
    [[_ output] (procedure [])]
    (in output)))

(def: .public (lifted monad procedure)
  (All (_ ! v) (-> (Monad !) (! v) (Linear ! v)))
  (function (_ keys)
    (do monad
      [output procedure]
      (in [keys output]))))

(abstract: .public Ordered Any)
(abstract: .public Commutative Any)

(abstract: .public (Key mode key)
  Any

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

    [ordered_key     Ordered]
    [commutative_key Commutative]
    ))

(abstract: .public (Res key value)
  value

  (template [<name> <mode> <key>]
    [(def: .public (<name> monad value)
       (All (_ ! v) (Ex (_ k) (-> (Monad !) v (Affine ! (Key <mode> k) (Res k v)))))
       (function (_ keys)
         (# monad in [[(<key> []) keys] (:abstraction value)])))]

    [ordered     Ordered     ..ordered_key]
    [commutative Commutative ..commutative_key]
    )

  (def: .public (read monad resource)
    (All (_ ! v k m)
      (-> (Monad !) (Res k v) (Relevant ! (Key m k) v)))
    (function (_ [key keys])
      (#  monad in [keys (:representation resource)])))
  )

(exception: .public (index_cannot_be_repeated [index Nat])
  (exception.report
   ["Index" (%.nat index)]))

(exception: .public amount_cannot_be_zero)

(def: indices
  (Parser (List Nat))
  (<code>.tuple (loop [seen (set.empty n.hash)]
                  (do [! <>.monad]
                    [done? <code>.end?]
                    (if done?
                      (in (list))
                      (do !
                        [head <code>.nat
                         _ (<>.assertion (exception.error ..index_cannot_be_repeated head)
                                         (not (set.member? seen head)))
                         tail (again (set.has head seen))]
                        (in (list& head tail))))))))

(def: (no_op monad)
  (All (_ m) (-> (Monad m) (Linear m Any)))
  (function (_ context)
    (# monad in [context []])))

(syntax: .public (exchange [swaps ..indices])
  (macro.with_symbols [g!_ g!context g!!]
    (case swaps
      {.#End}
      (in (list (` (~! no_op))))

      {.#Item head tail}
      (do [! meta.monad]
        [.let [max_idx (list#mix n.max head tail)]
         g!inputs (<| (monad.all !) (list.repeated (++ max_idx)) (macro.symbol "input"))
         .let [g!outputs (|> (monad.mix maybe.monad
                                        (function (_ from to)
                                          (do maybe.monad
                                            [input (list.item from g!inputs)]
                                            (in (sequence.suffix input to))))
                                        (: (Sequence Code) sequence.empty)
                                        swaps)
                             maybe.trusted
                             sequence.list)
               g!inputsT+ (list#each (|>> (~) (..Key ..Commutative) (`)) g!inputs)
               g!outputsT+ (list#each (|>> (~) (..Key ..Commutative) (`)) g!outputs)]]
        (in (list (` (: (All ((~ g!_) (~ g!!) (~+ g!inputs) (~ g!context))
                          (-> ((~! monad.Monad) (~ g!!))
                              (Procedure (~ g!!)
                                         [(~+ g!inputsT+) (~ g!context)]
                                         [(~+ g!outputsT+) (~ g!context)]
                                         .Any)))
                        (function ((~ g!_) (~ g!!) [(~+ g!inputs) (~ g!context)])
                          (# (~ g!!) (~' in) [[(~+ g!outputs) (~ g!context)] []]))))))))))

(def: amount
  (Parser Nat)
  (do <>.monad
    [raw <code>.nat
     _ (<>.assertion (exception.error ..amount_cannot_be_zero [])
                     (n.> 0 raw))]
    (in raw)))

(template [<name> <from> <to>]
  [(syntax: .public (<name> [amount ..amount])
     (macro.with_symbols [g!_ g!context g!!]
       (do [! meta.monad]
         [g!keys (|> (macro.symbol "keys")
                     (list.repeated amount)
                     (monad.all !))]
         (in (list (` (: (All ((~ g!_) (~ g!!) (~+ g!keys) (~ g!context))
                           (-> ((~! monad.Monad) (~ g!!))
                               (Procedure (~ g!!)
                                          [<from> (~ g!context)]
                                          [<to> (~ g!context)]
                                          .Any)))
                         (function ((~ g!_) (~ g!!) [<from> (~ g!context)])
                           (# (~ g!!) (~' in) [[<to> (~ g!context)] []])))))))))]

  [group    (~+ g!keys)   [(~+ g!keys)]]
  [un_group [(~+ g!keys)] (~+ g!keys)]
  )