aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/type/linear.lux
blob: 89080dbb41d71b7c690399f30f80ec4df9e9d642 (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
(.require
 [library
  [lux (.except)
   [abstract
    ["[0]" monad (.only Monad do)
     ["[0]" indexed]]]
   [control
    ["<>" parser]
    ["[0]" maybe]
    ["[0]" exception (.only Exception)]]
   [data
    [text
     ["%" \\format (.only format)]]
    [collection
     ["[0]" set]
     ["[0]" sequence (.only Sequence)]
     ["[0]" list (.use "[1]#[0]" functor mix)]]]
   [math
    [number
     ["n" nat]]]
   ["[0]" meta (.only)
    ["[0]" code
     ["<[1]>" \\parser (.only Parser)]]
    ["[0]" macro (.only)
     [syntax (.only syntax)]]]]]
 [//
  ["[0]" nominal (.except def)]])

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

(type .public (Linear ! value)
  (All (_ keys)
    (Procedure ! keys keys value)))

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

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

(def .public (monad monad)
  (All (_ !) (-> (Monad !) (indexed.Monad (Procedure !))))
  (implementation
   (def (in value)
     (function (_ keys)
       (at 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]))))

(nominal.def .public Ordered Any)
(nominal.def .public Commutative Any)

(nominal.def .public (Key mode key)
  Any

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

    [ordered_key     Ordered]
    [commutative_key Commutative]
    ))

(nominal.def .public (Res key value)
  value

  (with_template [<name> <mode> <key>]
    [(def .public (<name> monad value)
       (All (_ ! v) (Ex (_ k) (-> (Monad !) v (Affine ! (Key <mode> k) (Res k v)))))
       (function (_ keys)
         (at 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])
      (at  monad in [keys (representation resource)])))
  )

(exception.def .public (index_cannot_be_repeated index)
  (Exception Nat)
  (exception.report
   (list ["Index" (%.nat index)])))

(exception.def .public amount_cannot_be_zero)

(def indices
  (Parser (List Nat))
  (<code>.tuple (loop (again [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.partial head tail))))))))

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

(def .public exchange
  (syntax (_ [swaps ..indices])
    (macro.with_symbols [g!_ g!context g!!]
      (when 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))))
                                          (is (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 (` (is (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)])
                             (at (, 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)))

(with_template [<name> <from> <to>]
  [(def .public <name>
     (syntax (_ [amount ..amount])
       (macro.with_symbols [g!_ g!context g!!]
         (do [! meta.monad]
           [g!keys (|> (macro.symbol "keys")
                       (list.repeated amount)
                       (monad.all !))]
           (in (list (` (is (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)])
                              (at (, g!!) (,' in) [[<to> (, g!context)] []]))))))))))]

  [group    (,* g!keys)   [(,* g!keys)]]
  [un_group [(,* g!keys)] (,* g!keys)]
  )