From e978beda66408179585fe271bfec1900f21df8b5 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 1 May 2018 21:13:08 -0400 Subject: - Re-named lux/control/cont to lux/control/continuation. --- stdlib/source/lux/control/cont.lux | 89 --------------------------- stdlib/source/lux/control/continuation.lux | 89 +++++++++++++++++++++++++++ stdlib/source/lux/data/coll/stream.lux | 20 +++--- stdlib/test/test/lux/control/cont.lux | 77 ----------------------- stdlib/test/test/lux/control/continuation.lux | 77 +++++++++++++++++++++++ stdlib/test/test/lux/data/coll/stream.lux | 3 +- stdlib/test/tests.lux | 2 +- 7 files changed, 178 insertions(+), 179 deletions(-) delete mode 100644 stdlib/source/lux/control/cont.lux create mode 100644 stdlib/source/lux/control/continuation.lux delete mode 100644 stdlib/test/test/lux/control/cont.lux create mode 100644 stdlib/test/test/lux/control/continuation.lux (limited to 'stdlib') diff --git a/stdlib/source/lux/control/cont.lux b/stdlib/source/lux/control/cont.lux deleted file mode 100644 index 35f549ee7..000000000 --- a/stdlib/source/lux/control/cont.lux +++ /dev/null @@ -1,89 +0,0 @@ -(.module: - lux - (lux (control [functor #+ Functor] - [applicative #+ Applicative] - monad) - function - [macro #+ with-gensyms] - (macro [code] - [syntax #+ syntax:]))) - -(type: #export (Cont i o) - {#.doc "Continuations."} - (-> (-> i o) o)) - -(def: #export (continue k cont) - {#.doc "Forces a continuation thunk to be evaluated."} - (All [i o] (-> (-> i o) (Cont i o) o)) - (cont k)) - -(def: #export (run cont) - {#.doc "Forces a continuation thunk to be evaluated."} - (All [a] (-> (Cont a a) a)) - (cont id)) - -(struct: #export Functor (All [o] (Functor (All [i] (Cont i o)))) - (def: (map f fv) - (function (_ k) (fv (compose k f))))) - -(struct: #export Applicative (All [o] (Applicative (All [i] (Cont i o)))) - (def: functor Functor) - - (def: (wrap value) - (function (_ k) (k value))) - - (def: (apply ff fv) - (function (_ k) - (|> (k (f v)) - (function (_ v)) fv - (function (_ f)) ff)))) - -(struct: #export Monad (All [o] (Monad (All [i] (Cont i o)))) - (def: applicative Applicative) - - (def: (join ffa) - (function (_ k) - (ffa (continue k))))) - -(def: #export (call/cc f) - {#.doc "Call with current continuation."} - (All [a b z] - (-> (-> (-> a (Cont b z)) - (Cont a z)) - (Cont a z))) - (function (_ k) - (f (function (_ a) (function (_ _) (k a))) - k))) - -(syntax: #export (pending expr) - {#.doc (doc "Turns any expression into a function that is pending a continuation." - (pending (some-function some-input)))} - (with-gensyms [g!_ g!k] - (wrap (list (` (.function ((~ g!_) (~ g!k)) ((~ g!k) (~ expr)))))))) - -(def: #export (portal init) - (All [i o z] - (-> i - (Cont [(-> i (Cont o z)) - i] - z))) - (call/cc (function (_ k) - (do Monad - [#let [nexus (function (nexus val) - (k [nexus val]))] - _ (k [nexus init])] - (wrap (undefined)))))) - -(def: #export (reset scope) - (All [i o] (-> (Cont i i) (Cont i o))) - (function (_ k) - (k (run scope)))) - -(def: #export (shift f) - (All [a] - (-> (-> (-> a (Cont a a)) - (Cont a a)) - (Cont a a))) - (function (_ oc) - (f (function (_ a) (function (_ ic) (ic (oc a)))) - id))) diff --git a/stdlib/source/lux/control/continuation.lux b/stdlib/source/lux/control/continuation.lux new file mode 100644 index 000000000..35f549ee7 --- /dev/null +++ b/stdlib/source/lux/control/continuation.lux @@ -0,0 +1,89 @@ +(.module: + lux + (lux (control [functor #+ Functor] + [applicative #+ Applicative] + monad) + function + [macro #+ with-gensyms] + (macro [code] + [syntax #+ syntax:]))) + +(type: #export (Cont i o) + {#.doc "Continuations."} + (-> (-> i o) o)) + +(def: #export (continue k cont) + {#.doc "Forces a continuation thunk to be evaluated."} + (All [i o] (-> (-> i o) (Cont i o) o)) + (cont k)) + +(def: #export (run cont) + {#.doc "Forces a continuation thunk to be evaluated."} + (All [a] (-> (Cont a a) a)) + (cont id)) + +(struct: #export Functor (All [o] (Functor (All [i] (Cont i o)))) + (def: (map f fv) + (function (_ k) (fv (compose k f))))) + +(struct: #export Applicative (All [o] (Applicative (All [i] (Cont i o)))) + (def: functor Functor) + + (def: (wrap value) + (function (_ k) (k value))) + + (def: (apply ff fv) + (function (_ k) + (|> (k (f v)) + (function (_ v)) fv + (function (_ f)) ff)))) + +(struct: #export Monad (All [o] (Monad (All [i] (Cont i o)))) + (def: applicative Applicative) + + (def: (join ffa) + (function (_ k) + (ffa (continue k))))) + +(def: #export (call/cc f) + {#.doc "Call with current continuation."} + (All [a b z] + (-> (-> (-> a (Cont b z)) + (Cont a z)) + (Cont a z))) + (function (_ k) + (f (function (_ a) (function (_ _) (k a))) + k))) + +(syntax: #export (pending expr) + {#.doc (doc "Turns any expression into a function that is pending a continuation." + (pending (some-function some-input)))} + (with-gensyms [g!_ g!k] + (wrap (list (` (.function ((~ g!_) (~ g!k)) ((~ g!k) (~ expr)))))))) + +(def: #export (portal init) + (All [i o z] + (-> i + (Cont [(-> i (Cont o z)) + i] + z))) + (call/cc (function (_ k) + (do Monad + [#let [nexus (function (nexus val) + (k [nexus val]))] + _ (k [nexus init])] + (wrap (undefined)))))) + +(def: #export (reset scope) + (All [i o] (-> (Cont i i) (Cont i o))) + (function (_ k) + (k (run scope)))) + +(def: #export (shift f) + (All [a] + (-> (-> (-> a (Cont a a)) + (Cont a a)) + (Cont a a))) + (function (_ oc) + (f (function (_ a) (function (_ ic) (ic (oc a)))) + id))) diff --git a/stdlib/source/lux/data/coll/stream.lux b/stdlib/source/lux/data/coll/stream.lux index 3e76e9d5c..b6964d95f 100644 --- a/stdlib/source/lux/data/coll/stream.lux +++ b/stdlib/source/lux/data/coll/stream.lux @@ -3,7 +3,7 @@ (lux (control functor monad comonad - [cont #+ pending Cont] + [continuation #+ pending Cont] ["p" parser]) [macro #+ with-gensyms] (macro [code] @@ -50,7 +50,7 @@ (do-template [ ] [(def: #export ( s) (All [a] (-> (Stream a) )) - (let [[h t] (cont.run s)] + (let [[h t] (continuation.run s)] ))] [head a h] @@ -58,7 +58,7 @@ (def: #export (nth idx s) (All [a] (-> Nat (Stream a) a)) - (let [[h t] (cont.run s)] + (let [[h t] (continuation.run s)] (if (n/> +0 idx) (nth (n/dec idx) t) h))) @@ -67,7 +67,7 @@ [(def: #export ( pred xs) (All [a] (-> (Stream a) (List a))) - (let [[x xs'] (cont.run xs)] + (let [[x xs'] (continuation.run xs)] (if (list& x ( xs')) (list)))) @@ -75,7 +75,7 @@ (def: #export ( pred xs) (All [a] (-> (Stream a) (Stream a))) - (let [[x xs'] (cont.run xs)] + (let [[x xs'] (continuation.run xs)] (if ( xs') xs))) @@ -83,7 +83,7 @@ (def: #export ( pred xs) (All [a] (-> (Stream a) [(List a) (Stream a)])) - (let [[x xs'] (cont.run xs)] + (let [[x xs'] (continuation.run xs)] (if (let [[tail next] ( xs')] [(#.Cons [x tail]) next]) @@ -102,7 +102,7 @@ (def: #export (filter p xs) (All [a] (-> (-> a Bool) (Stream a) (Stream a))) - (let [[x xs'] (cont.run xs)] + (let [[x xs'] (continuation.run xs)] (if (p x) (pending [x (filter p xs')]) (filter p xs')))) @@ -119,14 +119,14 @@ ## [Structures] (struct: #export _ (Functor Stream) (def: (map f fa) - (let [[h t] (cont.run fa)] + (let [[h t] (continuation.run fa)] (pending [(f h) (map f t)])))) (struct: #export _ (CoMonad Stream) (def: functor Functor) (def: unwrap head) (def: (split wa) - (let [[head tail] (cont.run wa)] + (let [[head tail] (continuation.run wa)] (pending [wa (split tail)])))) ## [Pattern-matching] @@ -138,7 +138,7 @@ (with-gensyms [g!stream] (let [body+ (` (let [(~+ (List/join (List/map (function (_ pattern) (list (` [(~ pattern) (~ g!stream)]) - (` ((~! cont.run) (~ g!stream))))) + (` ((~! continuation.run) (~ g!stream))))) patterns)))] (~ body)))] (wrap (list& g!stream body+ branches))))) diff --git a/stdlib/test/test/lux/control/cont.lux b/stdlib/test/test/lux/control/cont.lux deleted file mode 100644 index 7afc84fc2..000000000 --- a/stdlib/test/test/lux/control/cont.lux +++ /dev/null @@ -1,77 +0,0 @@ -(.module: - lux - (lux [io] - (control ["M" monad #+ do Monad] - ["&" cont]) - (data [text "Text/" Monoid] - text/format - [number] - [product] - (coll [list])) - ["r" math/random]) - lux/test) - -(context: "Continuations" - (<| (times +100) - (do @ - [sample r.nat - #let [(^open "&/") &.Monad] - elems (r.list +3 r.nat)] - ($_ seq - (test "Can run continuations to compute their values." - (n/= sample (&.run (&/wrap sample)))) - - (test "Can use functor." - (n/= (n/inc sample) (&.run (&/map n/inc (&/wrap sample))))) - - (test "Can use applicative." - (n/= (n/inc sample) (&.run (&/apply (&/wrap n/inc) (&/wrap sample))))) - - (test "Can use monad." - (n/= (n/inc sample) (&.run (do &.Monad - [func (wrap n/inc) - arg (wrap sample)] - (wrap (func arg)))))) - - (test "Can use the current-continuation as a escape hatch." - (n/= (n/* +2 sample) - (&.run (do &.Monad - [value (&.call/cc - (function (_ k) - (do @ - [temp (k sample)] - ## If this code where to run, - ## the output would be - ## (n/* +4 sample) - (k temp))))] - (wrap (n/* +2 value)))))) - - (test "Can use the current-continuation to build a time machine." - (n/= (n/+ +100 sample) - (&.run (do &.Monad - [[restart [output idx]] (&.portal [sample +0])] - (if (n/< +10 idx) - (restart [(n/+ +10 output) (n/inc idx)]) - (wrap output)))))) - - (test "Can use delimited continuations with shifting." - (let [(^open "&/") &.Monad - (^open "L/") (list.Eq number.Eq) - visit (: (-> (List Nat) - (&.Cont (List Nat) (List Nat))) - (function (visit xs) - (case xs - #.Nil - (&/wrap #.Nil) - - (#.Cons x xs') - (do &.Monad - [output (&.shift (function (_ k) - (do @ - [tail (k xs')] - (wrap (#.Cons x tail)))))] - (visit output)))))] - (L/= elems - (&.run (&.reset (visit elems)))) - )) - )))) diff --git a/stdlib/test/test/lux/control/continuation.lux b/stdlib/test/test/lux/control/continuation.lux new file mode 100644 index 000000000..db274189e --- /dev/null +++ b/stdlib/test/test/lux/control/continuation.lux @@ -0,0 +1,77 @@ +(.module: + lux + (lux [io] + (control ["M" monad #+ do Monad] + ["&" continuation]) + (data [text "Text/" Monoid] + text/format + [number] + [product] + (coll [list])) + ["r" math/random]) + lux/test) + +(context: "Continuations" + (<| (times +100) + (do @ + [sample r.nat + #let [(^open "&/") &.Monad] + elems (r.list +3 r.nat)] + ($_ seq + (test "Can run continuations to compute their values." + (n/= sample (&.run (&/wrap sample)))) + + (test "Can use functor." + (n/= (n/inc sample) (&.run (&/map n/inc (&/wrap sample))))) + + (test "Can use applicative." + (n/= (n/inc sample) (&.run (&/apply (&/wrap n/inc) (&/wrap sample))))) + + (test "Can use monad." + (n/= (n/inc sample) (&.run (do &.Monad + [func (wrap n/inc) + arg (wrap sample)] + (wrap (func arg)))))) + + (test "Can use the current-continuation as a escape hatch." + (n/= (n/* +2 sample) + (&.run (do &.Monad + [value (&.call/cc + (function (_ k) + (do @ + [temp (k sample)] + ## If this code where to run, + ## the output would be + ## (n/* +4 sample) + (k temp))))] + (wrap (n/* +2 value)))))) + + (test "Can use the current-continuation to build a time machine." + (n/= (n/+ +100 sample) + (&.run (do &.Monad + [[restart [output idx]] (&.portal [sample +0])] + (if (n/< +10 idx) + (restart [(n/+ +10 output) (n/inc idx)]) + (wrap output)))))) + + (test "Can use delimited continuations with shifting." + (let [(^open "&/") &.Monad + (^open "L/") (list.Eq number.Eq) + visit (: (-> (List Nat) + (&.Cont (List Nat) (List Nat))) + (function (visit xs) + (case xs + #.Nil + (&/wrap #.Nil) + + (#.Cons x xs') + (do &.Monad + [output (&.shift (function (_ k) + (do @ + [tail (k xs')] + (wrap (#.Cons x tail)))))] + (visit output)))))] + (L/= elems + (&.run (&.reset (visit elems)))) + )) + )))) diff --git a/stdlib/test/test/lux/data/coll/stream.lux b/stdlib/test/test/lux/data/coll/stream.lux index 1fe325782..cbdfcab49 100644 --- a/stdlib/test/test/lux/data/coll/stream.lux +++ b/stdlib/test/test/lux/data/coll/stream.lux @@ -2,8 +2,7 @@ lux (lux [io] (control [monad #+ do Monad] - comonad - [cont]) + comonad) (data [maybe] [text "Text/" Monoid] text/format diff --git a/stdlib/test/tests.lux b/stdlib/test/tests.lux index 850abc865..05118f921 100644 --- a/stdlib/test/tests.lux +++ b/stdlib/test/tests.lux @@ -18,7 +18,7 @@ (control ["_." exception] ["_." interval] ["_." pipe] - ["_." cont] + ["_." continuation] ["_." reader] ["_." writer] ["_." state] -- cgit v1.2.3