diff options
-rw-r--r-- | stdlib/source/lux/codata/cont.lux | 8 | ||||
-rw-r--r-- | stdlib/source/lux/codata/struct/stream.lux | 4 | ||||
-rw-r--r-- | stdlib/test/test/lux/codata/env.lux | 32 | ||||
-rw-r--r-- | stdlib/test/test/lux/codata/io.lux | 32 | ||||
-rw-r--r-- | stdlib/test/test/lux/codata/state.lux | 51 | ||||
-rw-r--r-- | stdlib/test/test/lux/codata/struct/stream.lux | 150 | ||||
-rw-r--r-- | stdlib/test/tests.lux | 8 |
7 files changed, 170 insertions, 115 deletions
diff --git a/stdlib/source/lux/codata/cont.lux b/stdlib/source/lux/codata/cont.lux index b851d417c..ddc15f1f3 100644 --- a/stdlib/source/lux/codata/cont.lux +++ b/stdlib/source/lux/codata/cont.lux @@ -5,10 +5,10 @@ (;module: lux - (lux (macro (ast #as ast)) - (control (functor #as F #refer #all) - (applicative #as A #refer #all) - (monad #as M #refer #all)) + (lux (macro [ast]) + (control functor + applicative + monad) (data (struct list))) (.. function)) diff --git a/stdlib/source/lux/codata/struct/stream.lux b/stdlib/source/lux/codata/struct/stream.lux index 2fd962b38..b76db0f39 100644 --- a/stdlib/source/lux/codata/struct/stream.lux +++ b/stdlib/source/lux/codata/struct/stream.lux @@ -86,8 +86,8 @@ [(#;Cons [x tail]) next]) [(list) xs])))] - [take-while drop-while split-with (-> a Bool) (pred x) pred] - [take drop split Nat (n.> +0 pred) (n.dec pred)] + [take-while drop-while split-while (-> a Bool) (pred x) pred] + [take drop split Nat (n.> +0 pred) (n.dec pred)] ) (def: #export (unfold step init) diff --git a/stdlib/test/test/lux/codata/env.lux b/stdlib/test/test/lux/codata/env.lux index 0e24ddf75..a2a90a3af 100644 --- a/stdlib/test/test/lux/codata/env.lux +++ b/stdlib/test/test/lux/codata/env.lux @@ -1,3 +1,8 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + (;module: lux (lux (codata [io]) @@ -6,18 +11,19 @@ text/format [number]) (codata function - env)) + ["&" env])) lux/test) -(test: "lux/codata/env exports" - (test-all (match 123 (run 123 ask)) - (match 246 (run 123 (local (i.* 2) ask))) - (match 134 (run 123 (:: Functor<Env> map i.inc (i.+ 10)))) - (match 10 (run 123 (:: Applicative<Env> wrap 10))) - (match 30 (run 123 (let [(^open) Applicative<Env>] - (apply (wrap (i.+ 10)) (wrap 20))))) - (match 30 (run 123 (do Monad<Env> - [f (wrap i.+) - x (wrap 10) - y (wrap 20)] - (wrap (f x y))))))) +(test: "Envs" + ($_ seq + (assert "" (i.= 123 (&;run 123 &;ask))) + (assert "" (i.= 246 (&;run 123 (&;local (i.* 2) &;ask)))) + (assert "" (i.= 134 (&;run 123 (:: &;Functor<Env> map i.inc (i.+ 10))))) + (assert "" (i.= 10 (&;run 123 (:: &;Applicative<Env> wrap 10)))) + (assert "" (i.= 30 (&;run 123 (let [(^open "&/") &;Applicative<Env>] + (&/apply (&/wrap (i.+ 10)) (&/wrap 20)))))) + (assert "" (i.= 30 (&;run 123 (do &;Monad<Env> + [f (wrap i.+) + x (wrap 10) + y (wrap 20)] + (wrap (f x y)))))))) diff --git a/stdlib/test/test/lux/codata/io.lux b/stdlib/test/test/lux/codata/io.lux index 916609226..7965869d0 100644 --- a/stdlib/test/test/lux/codata/io.lux +++ b/stdlib/test/test/lux/codata/io.lux @@ -1,21 +1,27 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + (;module: lux (lux (control monad) - (data [text "Text/" Monoid<Text>] + (data [text "Text/" Monoid<Text> Eq<Text>] text/format [number]) (codata function - io)) + ["&" io])) lux/test) -(test: "lux/codata/io exports" - (test-all (match "YOLO" (run (io "YOLO"))) - (match 11 (run (:: Functor<IO> map i.inc (io 10)))) - (match 10 (run (:: Applicative<IO> wrap 10))) - (match 30 (run (let [(^open) Applicative<IO>] - (apply (wrap (i.+ 10)) (wrap 20))))) - (match 30 (run (do Monad<IO> - [f (wrap i.+) - x (wrap 10) - y (wrap 20)] - (wrap (f x y))))))) +(test: "I/O" + ($_ seq + (assert "" (Text/= "YOLO" (&;run (&;io "YOLO")))) + (assert "" (i.= 11 (&;run (:: &;Functor<IO> map i.inc (&;io 10))))) + (assert "" (i.= 10 (&;run (:: &;Applicative<IO> wrap 10)))) + (assert "" (i.= 30 (&;run (let [(^open "&/") &;Applicative<IO>] + (&/apply (&/wrap (i.+ 10)) (&/wrap 20)))))) + (assert "" (i.= 30 (&;run (do &;Monad<IO> + [f (wrap i.+) + x (wrap 10) + y (wrap 20)] + (wrap (f x y)))))))) diff --git a/stdlib/test/test/lux/codata/state.lux b/stdlib/test/test/lux/codata/state.lux index 84c90b5b1..4c4b808b3 100644 --- a/stdlib/test/test/lux/codata/state.lux +++ b/stdlib/test/test/lux/codata/state.lux @@ -1,3 +1,8 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + (;module: lux (lux (codata [io]) @@ -7,28 +12,28 @@ [number] [product]) (codata function - state)) + ["&" state])) lux/test) -(test: "lux/codata/state exports" - (test-all (match 123 (product;right (run 123 get))) - (match 321 (product;right (run 123 (do Monad<State> - [_ (put 321)] - get)))) - (match 369 (product;right (run 123 (do Monad<State> - [_ (update (i.* 3))] - get)))) - (match 124 (product;right (run 123 (use i.inc)))) - (match 246 (product;right (run 123 (local (i.* 2) get)))) - (match 124 (product;right (run 123 (:: Functor<State> map i.inc get)))) - (match 10 (product;right (run 123 (:: Applicative<State> wrap 10)))) - (match 30 (product;right (run 123 (let [(^open) Applicative<State>] - (apply (wrap (i.+ 10)) (wrap 20)))))) - (match 30 (product;right (run 123 (: (State Int Int) - (do Monad<State> - [f (wrap i.+) - x (wrap 10) - y (wrap 20)] - - (wrap (f x y))))))) - )) +(test: "State" + ($_ seq + (assert "" (i.= 123 (product;right (&;run 123 &;get)))) + (assert "" (i.= 321 (product;right (&;run 123 (do &;Monad<State> + [_ (&;put 321)] + &;get))))) + (assert "" (i.= 369 (product;right (&;run 123 (do &;Monad<State> + [_ (&;update (i.* 3))] + &;get))))) + (assert "" (i.= 124 (product;right (&;run 123 (&;use i.inc))))) + (assert "" (i.= 246 (product;right (&;run 123 (&;local (i.* 2) &;get))))) + (assert "" (i.= 124 (product;right (&;run 123 (:: &;Functor<State> map i.inc &;get))))) + (assert "" (i.= 10 (product;right (&;run 123 (:: &;Applicative<State> wrap 10))))) + (assert "" (i.= 30 (product;right (&;run 123 (let [(^open "&/") &;Applicative<State>] + (&/apply (&/wrap (i.+ 10)) (&/wrap 20))))))) + (assert "" (i.= 30 (product;right (&;run 123 (: (&;State Int Int) + (do &;Monad<State> + [f (wrap i.+) + x (wrap 10) + y (wrap 20)] + (wrap (f x y)))))))) + )) diff --git a/stdlib/test/test/lux/codata/struct/stream.lux b/stdlib/test/test/lux/codata/struct/stream.lux index 1da81aa02..c9397b1a3 100644 --- a/stdlib/test/test/lux/codata/struct/stream.lux +++ b/stdlib/test/test/lux/codata/struct/stream.lux @@ -1,3 +1,8 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + (;module: lux (lux (codata [io]) @@ -5,64 +10,97 @@ comonad) (data [text "Text/" Monoid<Text>] text/format - [number "Int/" Codec<Text,Int>]) + (struct [list]) + [number "Nat/" Codec<Text,Nat>]) (codata function [cont] - (struct stream))) + (struct ["&" stream])) + (math ["R" random]) + pipe) lux/test) -(test: "lux/codata/stream exports" - (let% [<take+drop+split> (do-template [<take> <drop> <split> <arg>] - [(match (^ (list 0 1 2)) - (<take> <arg> (iterate i.inc 0))) - (match (^=> (^stream& w x y z ...) - {[w x y z] [3 4 5 6]}) - (<drop> <arg> (iterate i.inc 0))) - (match (^=> (^ [(list 0 1 2) _stream_]) - {_stream_ (^stream& w x y z ...)} - {[w x y z] [3 4 5 6]}) - (<split> <arg> (iterate i.inc 0)))] +(test: "Streams" + [size (|> R;nat (:: @ map (|>. (n.% +100) (n.max +1)))) + offset (|> R;nat (:: @ map (n.% +100))) + factor (|> R;nat (:: @ map (|>. (n.% +100) (n.max +1)))) + elem R;nat + cycle-seed (R;list size R;nat) + cycle-sample-idx (|> R;nat (:: @ map (n.% +1000))) + #let [(^open "List/") (list;Eq<List> number;Eq<Nat>) + sample0 (&;iterate n.inc +0) + sample1 (&;iterate n.inc offset)]] + ($_ seq + (assert "Can move along a stream and take slices off it." + (and (and (List/= (list;n.range +0 size) + (&;take size sample0)) + (List/= (list;n.range offset (n.+ offset size)) + (&;take size (&;drop offset sample0))) + (let [[drops takes...] (&;split size sample0)] + (and (List/= (list;n.range +0 size) + drops) + (List/= (list;n.range size (n.* +2 size)) + (&;take size takes...))))) + (and (List/= (list;n.range +0 size) + (&;take-while (n.< size) sample0)) + (List/= (list;n.range offset (n.+ offset size)) + (&;take-while (n.< (n.+ offset size)) + (&;drop-while (n.< offset) sample0))) + (let [[drops takes...] (&;split-while (n.< size) sample0)] + (and (List/= (list;n.range +0 size) + drops) + (List/= (list;n.range size (n.* +2 size)) + (&;take-while (n.< (n.* +2 size)) takes...))))) + )) + + (assert "Can repeat any element and infinite number of times." + (n.= elem (&;at offset (&;repeat elem)))) + + (assert "Can obtain the head & tail of a stream." + (and (n.= offset (&;head sample1)) + (List/= (list;n.range offset (n.+ offset size)) + (&;take size (&;tail sample1))))) + + (assert "Can filter streams." + (and (n.= (n.* +2 offset) + (&;at offset + (&;filter n.even? sample0))) + (let [[evens odds] (&;partition n.even? (&;iterate n.inc +0))] + (and (n.= (n.* +2 offset) + (&;at offset evens)) + (n.= (n.inc (n.* +2 offset)) + (&;at offset odds)))))) + + (assert "Functor goes over 'all' elements in a stream." + (let [(^open "&/") &;Functor<Stream> + there (&/map (n.* factor) sample0) + back-again (&/map (n./ factor) there)] + (and (not (List/= (&;take size sample0) + (&;take size there))) + (List/= (&;take size sample0) + (&;take size back-again))))) + + (assert "CoMonad produces a value for every element in a stream." + (let [(^open "&/") &;Functor<Stream>] + (List/= (&;take size (&/map (n.* factor) sample1)) + (&;take size + (be &;CoMonad<Stream> + [inputs sample1] + (n.* factor (&;head inputs))))))) + + (assert "'unfold' generalizes 'iterate'." + (let [(^open "&/") &;Functor<Stream> + (^open "List/") (list;Eq<List> text;Eq<Text>)] + (List/= (&;take size + (&/map Nat/encode (&;iterate n.inc offset))) + (&;take size + (&;unfold (lambda [n] [(n.inc n) (Nat/encode n)]) + offset))))) - [take drop split +3] - [take-while drop-while split-with (i.< 3)]) - ] - (test-all (match (^=> (^stream& w x y z ...) - {[w x y z] [0 1 2 3]}) - (iterate i.inc 0)) - (match (^=> (^stream& w x y z ...) - {[w x y z] [0 0 0 0]}) - (repeat 0)) - (match (^=> (#;Some the-stream) - {the-stream (^stream& w x y z ...)} - {[w x y z] [0 1 0 1]}) - (cycle (list 0 1))) - (match 0 (head (iterate i.inc 0))) - (match (^=> (^stream& w x y z ...) - {[w x y z] [1 2 3 4]}) - (tail (iterate i.inc 0))) - (match 9 (at +9 (iterate i.inc 0))) - (match 0 (at +0 (iterate i.inc 0))) - <take+drop+split> - (match (^=> (^stream& w x y z ...) - {[w x y z] ["0" "1" "2" "3"]}) - (unfold (lambda [n] [(i.inc n) (Int/encode n)]) - 0)) - (match (^=> (^stream& w x y z ...) - {[w x y z] [0 2 4 6]}) - (filter even? (iterate i.inc 0))) - (match (^=> [e_stream o_stream] - {e_stream (^stream& w x y z ...)} - {o_stream (^stream& a b c d ...)} - {[w x y z a b c d] [0 2 4 6 1 3 5 7]}) - (partition even? (iterate i.inc 0))) - (match (^=> (^stream& w x y z ...) - {[w x y z] [0 1 4 9]}) - (let [square (lambda [n] (i.* n n))] - (:: Functor<Stream> map square (iterate i.inc 0)))) - (match (^=> (^stream& w x y z ...) - {[w x y z] [4 9 16 25]}) - (let [square (lambda [n] (i.* n n))] - (be CoMonad<Stream> - [inputs (iterate i.inc 2)] - (square (head inputs))))) - ))) + (assert "Can cycle over the same elements as an infinite stream." + (|> (&;cycle cycle-seed) + (default (undefined)) + (&;at cycle-sample-idx) + (n.= (default (undefined) + (list;at (n.% size cycle-sample-idx) + cycle-seed))))) + )) diff --git a/stdlib/test/tests.lux b/stdlib/test/tests.lux index 5314c2923..de1487b34 100644 --- a/stdlib/test/tests.lux +++ b/stdlib/test/tests.lux @@ -12,6 +12,10 @@ [test]) (test lux (lux ["_;" cli] + (codata ["_;" io] + [env] + [state] + (struct [stream])) (data [bit] [bool] [char] @@ -38,10 +42,6 @@ ) (text [format]) ) - ## (codata ["_;" io] - ## [env] - ## [state] - ## (struct [stream])) ## (macro [ast] ## [syntax]) ## [type] |