From e529e0e48c999fccb216bd3f3bc948abe22dbe76 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Tue, 6 Dec 2022 17:37:43 +0200 Subject: Clean up examples, stdlib --- examples/effects-concept.milch | 3 ++ examples/import.milch | 2 -- examples/impure-concept.milch | 19 +++++++++++-- examples/purity-test.milch | 25 ----------------- examples/records-concept.milch | 24 ---------------- examples/test.milch | 62 +----------------------------------------- examples/types-concept.milch | 3 ++ stdlib/maybe-with-unions.milch | 42 ---------------------------- stdlib/maybe.milch | 43 ++++++++++++----------------- 9 files changed, 41 insertions(+), 182 deletions(-) delete mode 100644 examples/import.milch delete mode 100644 examples/purity-test.milch delete mode 100644 stdlib/maybe-with-unions.milch diff --git a/examples/effects-concept.milch b/examples/effects-concept.milch index 0bde72f..6091945 100644 --- a/examples/effects-concept.milch +++ b/examples/effects-concept.milch @@ -1,3 +1,6 @@ +; Probably not going to implement this, +; just writing down ideas + ; FUNCTIONS (let prompt-input (\[] diff --git a/examples/import.milch b/examples/import.milch deleted file mode 100644 index 47a7477..0000000 --- a/examples/import.milch +++ /dev/null @@ -1,2 +0,0 @@ -(import "stdlib/common.milch") -(import M "stdlib/maybe.milch") diff --git a/examples/impure-concept.milch b/examples/impure-concept.milch index c767914..75c3bb7 100644 --- a/examples/impure-concept.milch +++ b/examples/impure-concept.milch @@ -5,8 +5,21 @@ ; The top-level context is impure but has special rules ; related to `import`, `let`, and other builtins. -; pure function +; this is a pure function (let f (\[x] x)) -; impure function -(let g! (\![x] (print! x))) +(f 10) ; works + +; this is an impure function +(let g! (\![x] (println! x))) + +(g! "foo") ; works + +; this is a pure function that tries to call an impure function +(let h (\[x] (g! x))) + +(h "bar") +; error: cannot call impure function in pure context +; when calling function g! at examples/purity-test.milch:19:15 +; in a function definition at examples/purity-test.milch:19:12 +; when calling function h at examples/purity-test.milch:21:1 \ No newline at end of file diff --git a/examples/purity-test.milch b/examples/purity-test.milch deleted file mode 100644 index f7c77e5..0000000 --- a/examples/purity-test.milch +++ /dev/null @@ -1,25 +0,0 @@ -; utils - -(let println! (\![x] - (print! (fmt "{0}\n" [x])))) - -; pure vs. impure demo - -; this is a pure function -(let f (\[x] x)) - -(f 10) ; works - -; this is an impure function -(let g! (\![x] (println! x))) - -(g! "foo") ; works - -; this is a pure function that tries to call an impure function -(let h (\[x] (g! x))) - -(h "bar") -; error: cannot call impure function in pure context -; when calling function g! at examples/purity-test.milch:19:15 -; in a function definition at examples/purity-test.milch:19:12 -; when calling function h at examples/purity-test.milch:21:1 diff --git a/examples/records-concept.milch b/examples/records-concept.milch index 1686d75..dcb4a49 100644 --- a/examples/records-concept.milch +++ b/examples/records-concept.milch @@ -22,30 +22,6 @@ (Ns/User/set-phone-number unit user) ; => (Ns/User name:"Rick" phone-number:) - - - -; Unions are unions of records with an identifier. `union!` is a builtin function -; that generates functions for manipulating the records in the union. - -; `union!` can only be called in the top-level context. - -(union! Ns/Maybe - (just value) - (nothing)) - -; Generates the following functions: - -(let m1 (Ns/Maybe/just 10)) ; m1 ~> (Ns/Maybe/Just value:10) -(let m2 (Ns/Maybe/nothing)) ; m2 ~> (Ns/Maybe/Nothing) - -(Ns/Maybe/Just/get-value m1) ; => 10 - - - - ; The builtin function `kind` returns the identifier as a string for any record value -(kind m1) ; => "Ns/Maybe/Just" -(kind m2) ; => "Ns/Maybe/Nothing" (kind user) ; => "Ns/User" diff --git a/examples/test.milch b/examples/test.milch index fc41266..d8d0077 100644 --- a/examples/test.milch +++ b/examples/test.milch @@ -1,71 +1,11 @@ -(let compose (\[f g] - (\[x] (f (g x))))) - -((compose (+ 1) (+ 2)) 3) - -(let id (\[a] a)) - -(let mod (\[n k] - (- n (* k (/ n k))))) - -(let not (\[b] - (match b - true false - false true))) - -(let is-even (\[n] - (match (mod n 2) - 0 true - 1 false))) - -(let is-odd (compose not is-even)) - -;; map :: (a -> b) -> [a] -> [b] -(let map (\[f lst] - (match lst - [] - [] - otherwise - (prepend (f (head lst)) (map f (tail lst)))))) +(import "stdlib/common.milch") (map (+ 1) [1 2 3]) -;; foldr :: (a -> b -> b) -> b -> [a] -> b -(let foldr (\[f accumulator lst] - (match lst - [] - accumulator - otherwise - (f (head lst) (foldr f accumulator (tail lst)))))) - (foldr + 0 [1 2 3]) -;; filter :: (a -> Bool) -> [a] -> [a] -(let filter (\[pred lst] - (match lst - [] [] - otherwise (match (pred (head lst)) - true - (prepend (head lst) (filter pred (tail lst))) - false - (filter pred (tail lst)))))) - (filter is-even [0 1 2 3 4 5]) -(let reverse_ (\[v a] - (let lazy x (head v)) - (let lazy xs (tail v)) - (let lazy xa (prepend x a)) - (match v - [] a - _ (reverse_ xs xa)))) - -(let reverse (\[v] - (reverse_ v []))) - (reverse [1 2 3]) -(let flow (\[fs] (foldr compose id (reverse fs)))) -(let pipe (\[x fs] ((flow fs) x))) - (pipe 10 [(+ 1) (+ 2)]) diff --git a/examples/types-concept.milch b/examples/types-concept.milch index 9496ffe..476c96b 100644 --- a/examples/types-concept.milch +++ b/examples/types-concept.milch @@ -1,3 +1,6 @@ +; Probably not going to implement this, +; just writing down ideas + ; lowercase types: polymorphic ; everything else: monomorphic diff --git a/stdlib/maybe-with-unions.milch b/stdlib/maybe-with-unions.milch deleted file mode 100644 index 8a15471..0000000 --- a/stdlib/maybe-with-unions.milch +++ /dev/null @@ -1,42 +0,0 @@ -; LIB - -(union! Maybe - (just value) - (nothing)) - -(let Maybe/map (\[f m] - (match (kind m) - "Maybe/Just" (Maybe/just (f (Maybe/Just/get-value m))) - "Maybe/Nothing" (Maybe/nothing)))) - -(let Maybe/and-then (\[f m] - (match (kind m) - "Maybe/Just" (f (Maybe/Just/get-value m)) - "Maybe/Nothing" (Maybe/nothing)))) - -; TESTING - -;; (let print-line! (\[s] -;; (print! (concat s "\n")))) - -;; (let a (Maybe/just 10)) -;; (let b (Maybe/nothing)) - -;; (Maybe/map (+ 5) a) -;; (Maybe/map (+ 5) b) - -;; (Maybe/and-then (\[n] (Maybe/just (+ n 5))) a) -;; (Maybe/and-then (\[n] (Maybe/just (+ n 5))) b) - -;; (let m (Maybe/just 10)) -;; (match m -;; (Maybe/just _) -;; (print-line! (fmt "found just {0}!" [(Maybe/Just/get-value m)])) -;; (Maybe/nothing) -;; (print-line! "found nothing!")) - -;; (let m (Maybe/just 10)) -;; (pipe m [ -;; (Maybe/map (+ 5)) -;; (Maybe/and-then (\[val] (Maybe/just (- val 3)))) -;; ]) diff --git a/stdlib/maybe.milch b/stdlib/maybe.milch index 4114e22..ef9e906 100644 --- a/stdlib/maybe.milch +++ b/stdlib/maybe.milch @@ -1,33 +1,20 @@ ; LIB -(let Maybe/just (\[a] - ["Maybe" "just" a])) -(let Maybe/nothing (\[] - ["Maybe" "nothing"])) - -(let Maybe/#at (\[n seq] - (match seq - [] - (fatal! "Maybe/#at out of bounds") - otherwise - (match n - 0 - (head seq) - otherwise - (Maybe/#at (- n 1) (tail seq)))))) - -(let Maybe/unpack (Maybe/#at 2)) -(let Maybe/kind (Maybe/#at 1)) +(record Maybe/Just value) +(record Maybe/Nothing) + +(let Maybe/just Maybe/Just/create) +(let Maybe/nothing Maybe/Nothing/create) (let Maybe/map (\[f m] - (match (Maybe/kind m) - "just" (Maybe/just (f (Maybe/unpack m))) - "nothing" (Maybe/nothing)))) + (match (kind m) + "Maybe/Just" (Maybe/just (f (Maybe/Just/get-value m))) + "Maybe/Nothing" (Maybe/nothing)))) (let Maybe/and-then (\[f m] - (match (Maybe/kind m) - "just" (f (Maybe/unpack m)) - "nothing" (Maybe/nothing)))) + (match (kind m) + "Maybe/Just" (f (Maybe/Just/get-value m)) + "Maybe/Nothing" (Maybe/nothing)))) ; TESTING @@ -46,6 +33,12 @@ ;; (let m (Maybe/just 10)) ;; (match m ;; (Maybe/just _) -;; (print-line! (fmt "found just {0}!" [(Maybe/unpack m)])) +;; (print-line! (fmt "found just {0}!" [(Maybe/Just/get-value m)])) ;; (Maybe/nothing) ;; (print-line! "found nothing!")) + +;; (let m (Maybe/just 10)) +;; (pipe m [ +;; (Maybe/map (+ 5)) +;; (Maybe/and-then (\[val] (Maybe/just (- val 3)))) +;; ]) -- cgit v1.3