diff options
Diffstat (limited to 'stdlib/source/library/lux/control/function')
4 files changed, 47 insertions, 8 deletions
diff --git a/stdlib/source/library/lux/control/function/contract.lux b/stdlib/source/library/lux/control/function/contract.lux index 149053230..414445ac1 100644 --- a/stdlib/source/library/lux/control/function/contract.lux +++ b/stdlib/source/library/lux/control/function/contract.lux @@ -34,8 +34,9 @@ "Otherwise, an error is raised." (pre (i.= +4 (i.+ +2 +2)) (foo +123 +456 +789)))} - (wrap (list (` (exec ((~! ..assert!) (~ (code.text (exception.construct ..pre_condition_failed test))) - (~ test)) + (wrap (list (` (exec + ((~! ..assert!) (~ (code.text (exception.construct ..pre_condition_failed test))) + (~ test)) (~ expr)))))) (syntax: #export (post test expr) @@ -47,6 +48,7 @@ (i.+ +2 +2)))} (with_gensyms [g!output] (wrap (list (` (let [(~ g!output) (~ expr)] - (exec ((~! ..assert!) (~ (code.text (exception.construct ..post_condition_failed test))) - ((~ test) (~ g!output))) + (exec + ((~! ..assert!) (~ (code.text (exception.construct ..post_condition_failed test))) + ((~ test) (~ g!output))) (~ g!output)))))))) diff --git a/stdlib/source/library/lux/control/function/memo.lux b/stdlib/source/library/lux/control/function/memo.lux index 4c50a0695..e2b734cae 100644 --- a/stdlib/source/library/lux/control/function/memo.lux +++ b/stdlib/source/library/lux/control/function/memo.lux @@ -41,7 +41,7 @@ (All [i o] (:let [Memory (Dictionary i o)] (-> (Memo i o) (-> [Memory i] [Memory o])))) - (let [memo (//.mixin (//.inherit ..memoization (//.from_recursive memo)))] + (let [memo (//.mixin (//.inherit ..memoization (//.of_recursive memo)))] (function (_ [memory input]) (|> input memo (state.run memory))))) @@ -50,7 +50,7 @@ "Memoized results will be re-used during recursive invocations, but cannot be accessed after the main invocation has ended.")} (All [i o] (-> (Hash i) (Memo i o) (-> i o))) - (let [memo (//.mixin (//.inherit ..memoization (//.from_recursive memo))) + (let [memo (//.mixin (//.inherit ..memoization (//.of_recursive memo))) empty (dictionary.new hash)] (|>> memo (state.run empty) product.right))) @@ -59,6 +59,6 @@ "This is useful as a test control when measuring the effect of using memoization.")} (All [i o] (-> (Hash i) (Memo i o) (-> i o))) - (let [memo (//.mixin (//.from_recursive memo)) + (let [memo (//.mixin (//.of_recursive memo)) empty (dictionary.new hash)] (|>> memo (state.run empty) product.right))) diff --git a/stdlib/source/library/lux/control/function/mixin.lux b/stdlib/source/library/lux/control/function/mixin.lux index 8c3443339..8248b2055 100644 --- a/stdlib/source/library/lux/control/function/mixin.lux +++ b/stdlib/source/library/lux/control/function/mixin.lux @@ -10,19 +10,23 @@ [monad (#+ Monad do)]]]]) (type: #export (Mixin i o) + {#.doc (doc "A function which can be mixed with others to inherit their behavior.")} (-> (-> i o) (-> i o) (-> i o))) (def: #export (mixin f) + {#.doc (doc "Given a mixin, produces a normal function.")} (All [i o] (-> (Mixin i o) (-> i o))) (function (mix input) ((f mix mix) input))) (def: #export nothing + {#.doc (doc "A mixin that does nothing and just delegates work to the next mixin.")} Mixin (function (_ delegate recur) delegate)) (def: #export (inherit parent child) + {#.doc (doc "Produces a new mixin, where the behavior of the child can make use of the behavior of the parent.")} (All [i o] (-> (Mixin i o) (Mixin i o) (Mixin i o))) (function (_ delegate recur) (parent (child delegate recur) recur))) @@ -34,6 +38,7 @@ (def: compose ..inherit)) (def: #export (advice when then) + {#.doc (doc "Only apply then mixin when the input meets some criterion.")} (All [i o] (-> (Predicate i) (Mixin i o) (Mixin i o))) (function (_ delegate recur input) (if (when input) @@ -41,6 +46,7 @@ (delegate input)))) (def: #export (before monad action) + {#.doc (doc "Executes an action before doing the main work.")} (All [! i o] (-> (Monad !) (-> i (! Any)) (Mixin i (! o)))) (function (_ delegate recur input) (do monad @@ -48,6 +54,7 @@ (delegate input)))) (def: #export (after monad action) + {#.doc (doc "Executes an action after doing the main work.")} (All [! i o] (-> (Monad !) (-> i o (! Any)) (Mixin i (! o)))) (function (_ delegate recur input) (do monad @@ -56,9 +63,11 @@ (wrap output)))) (type: #export (Recursive i o) + {#.doc (doc "An indirectly recursive function.")} (-> (-> i o) (-> i o))) -(def: #export (from_recursive recursive) +(def: #export (of_recursive recursive) + {#.doc (doc "Transform an indirectly recursive function into a mixin.")} (All [i o] (-> (Recursive i o) (Mixin i o))) (function (_ delegate recur) (recursive recur))) diff --git a/stdlib/source/library/lux/control/function/mutual.lux b/stdlib/source/library/lux/control/function/mutual.lux index dcc4791e1..4ceaaa61f 100644 --- a/stdlib/source/library/lux/control/function/mutual.lux +++ b/stdlib/source/library/lux/control/function/mutual.lux @@ -1,4 +1,5 @@ (.module: + {#.doc (.doc "Macros for implementing mutually-recursive functions.")} [library [lux (#- Definition let def:) ["." meta] @@ -54,6 +55,20 @@ (syntax: #export (let {functions (<code>.tuple (<>.some ..mutual))} body) + {#.doc (doc "Locally-defined mutually-recursive functions." + (let [(even? number) + (-> Nat Bit) + (case number + 0 true + _ (odd? (dec number))) + + (odd? number) + (-> Nat Bit) + (case number + 0 false + _ (even? (dec number)))] + (and (even? 4) + (odd? 5))))} (case functions #.Nil (wrap (list body)) @@ -105,6 +120,19 @@ ..mutual))) (syntax: #export (def: {functions (<>.many ..definition)}) + {#.doc (doc "Globally-defined mutually-recursive functions." + (def: + [#export (even? number) + (-> Nat Bit) + (case number + 0 true + _ (odd? (dec number)))] + + [#export (odd? number) + (-> Nat Bit) + (case number + 0 false + _ (even? (dec number)))]))} (case functions #.Nil (wrap (list)) |