diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/library/lux/type/resource.lux | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/stdlib/source/library/lux/type/resource.lux b/stdlib/source/library/lux/type/resource.lux index 2a568e6fd..8d389b11e 100644 --- a/stdlib/source/library/lux/type/resource.lux +++ b/stdlib/source/library/lux/type/resource.lux @@ -26,17 +26,25 @@ abstract]]]) (type: .public (Procedure monad input output value) + {#.doc (doc "A computation that takes a sequence of resource access rights as inputs and yields a different sequence as outputs." + "A procedure yields a result value." + "A procedure can make use of monadic effects.")} (-> input (monad [output value]))) (type: .public (Linear monad value) + {#.doc (doc "A procedure that is constant with regards to resource access rights." + "This means no additional resources will be available after the computation is over." + "This also means no previously available resources will have been consumed.")} (All [keys] (Procedure monad keys keys value))) (type: .public (Affine monad permissions value) + {#.doc (doc "A procedure which expands the number of available resources.")} (All [keys] (Procedure monad keys [permissions keys] value))) (type: .public (Relevant monad permissions value) + {#.doc (doc "A procedure which reduces the number of available resources.")} (All [keys] (Procedure monad [permissions keys] keys value))) @@ -66,12 +74,17 @@ [output procedure] (in [keys output])))) -(abstract: .public Ordered {} Any) +(abstract: .public Ordered + {#.doc (doc "The mode of keys which CANNOT be swapped, and for whom order of release/consumption matters.")} + Any) -(abstract: .public Commutative {} Any) +(abstract: .public Commutative + {#.doc (doc "The mode of keys which CAN be swapped, and for whom order of release/consumption DOES NOT matters.")} + Any) (abstract: .public (Key mode key) - {} + {#.doc (doc "The access right for a resource." + "Without the key for a resource existing somewhere among the available ambient rights, one cannot use a resource.")} Any @@ -85,12 +98,14 @@ )) (abstract: .public (Res key value) - {#.doc "A value locked by a key."} + {#.doc (doc "A resource locked by a key." + "The 'key' represents the right to access/consume a resource.")} value (template [<name> <mode> <key>] [(def: .public (<name> monad value) + {#.doc (doc "Makes a value into a resource and adds the key/access-right to it to the ambient keyring for future use.")} (All [! v] (Ex [k] (-> (Monad !) v (Affine ! (Key <mode> k) (Res k v))))) (function (_ keys) (\ monad in [[(<key> []) keys] (:abstraction value)])))] @@ -100,6 +115,7 @@ ) (def: .public (read monad resource) + {#.doc (doc "Access the value of a resource, so long as its key is available.")} (All [! v k m] (-> (Monad !) (Res k v) (Relevant ! (Key m k) v))) (function (_ [key keys]) @@ -132,6 +148,16 @@ (\ monad in [context []]))) (syntax: .public (exchange {swaps ..indices}) + {#.doc (doc "A function that can exchange the keys for resource, so long as they are commutative." + "This keys will be placed at the front of the keyring in the order they are specified." + "The specific keys must be specified based of their index into the current keyring." + (do (..monad !) + [res|left (/.commutative ! pre) + res|right (/.commutative ! post) + _ ((/.exchange [1 0]) !) + left (/.read ! res|left) + right (/.read ! res|right)] + (in (format left right))))} (macro.with_gensyms [g!_ g!context g!!] (case swaps #.End @@ -140,7 +166,7 @@ (#.Item head tail) (do {! meta.monad} [.let [max_idx (list\fold n.max head tail)] - g!inputs (<| (monad.seq !) (list.repeat (inc max_idx)) (macro.gensym "input")) + g!inputs (<| (monad.seq !) (list.repeated (inc max_idx)) (macro.gensym "input")) .let [g!outputs (|> (monad.fold maybe.monad (function (_ from to) (do maybe.monad @@ -171,10 +197,19 @@ (template [<name> <from> <to>] [(syntax: .public (<name> {amount ..amount}) + {#.doc (doc "Group/un-group keys in the keyring into/out-of tuples." + (do (..monad !) + [res|left (/.commutative ! pre) + res|right (/.commutative ! post) + _ ((/.group 2) !) + _ ((/.un_group 2) !) + right (/.read ! res|right) + left (/.read ! res|left)] + (in (format left right))))} (macro.with_gensyms [g!_ g!context g!!] (do {! meta.monad} [g!keys (|> (macro.gensym "keys") - (list.repeat amount) + (list.repeated amount) (monad.seq !))] (in (list (` (: (All [(~ g!!) (~+ g!keys) (~ g!context)] (-> ((~! monad.Monad) (~ g!!)) |