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 +++---- 3 files changed, 99 insertions(+), 99 deletions(-) delete mode 100644 stdlib/source/lux/control/cont.lux create mode 100644 stdlib/source/lux/control/continuation.lux (limited to 'stdlib/source') 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))))) -- cgit v1.2.3