From 5933f4c3b16385c1d6459095f4f952c8fb73ae6d Mon Sep 17 00:00:00 2001 From: Dmitry Boulytchev Date: Thu, 20 Feb 2020 12:43:52 +0300 Subject: [PATCH] Cosmetics in stdlib --- src/version.ml | 2 +- stdlib/Array.lama | 7 ++-- stdlib/Collection.lama | 4 +-- stdlib/Expr.lama | 66 ---------------------------------- stdlib/Fun.lama | 5 +++ stdlib/Lazy.lama | 13 ++++++- stdlib/List.lama | 5 +++ stdlib/Ostap.lama | 68 +++++++++++++++++++++++++++++++++++ stdlib/Ref.lama | 8 +++++ stdlib/regression/test11.lama | 1 - stdlib/regression/test12.lama | 1 - stdlib/regression/test13.lama | 1 - stdlib/regression/test14.lama | 1 - stdlib/regression/test15.lama | 1 - stdlib/regression/test16.lama | 1 - 15 files changed, 105 insertions(+), 79 deletions(-) delete mode 100644 stdlib/Expr.lama diff --git a/src/version.ml b/src/version.ml index 56b50f381..a86b61425 100644 --- a/src/version.ml +++ b/src/version.ml @@ -1 +1 @@ -let version = "Version 1.00, b7271d16, Tue Feb 18 14:08:39 2020 +0300" +let version = "Version 1.00, 7fd85f27, Wed Feb 19 21:21:18 2020 +0300" diff --git a/stdlib/Array.lama b/stdlib/Array.lama index f9e3f0d79..29c0a9967 100644 --- a/stdlib/Array.lama +++ b/stdlib/Array.lama @@ -1,8 +1,9 @@ --- Array package. +-- Arrays. -- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020 -- --- This package provides a simplistic implementation of immutable set/map/hashtable --- data structures. +-- This unit provides a set of array-manipulation primitives. Note, some of these +-- primitives can be applied to any boxed value in Lama, not necessarily arrays +-- per se. import List; diff --git a/stdlib/Collection.lama b/stdlib/Collection.lama index 01ecfe8a5..b3623b021 100644 --- a/stdlib/Collection.lama +++ b/stdlib/Collection.lama @@ -1,7 +1,7 @@ --- Collection package. +-- Collections. -- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020 -- --- This package provides a simplistic implementation of immutable set/map/hashtable +-- This unit provides a simplistic implementation of immutable set/map/hashtable -- data structures. import List; diff --git a/stdlib/Expr.lama b/stdlib/Expr.lama deleted file mode 100644 index bd51361d6..000000000 --- a/stdlib/Expr.lama +++ /dev/null @@ -1,66 +0,0 @@ -import Ostap; -import List; -import Fun; - -public fun left (f) { - fun (c, x) { - fun (y) { - f (c (x), y) - } - } -} - -public fun right (f) { - fun (c, x) { - fun (y) { - c (f (x, y)) - } - } -} - ---- ops -> fun (x, y) {x `op` y} -fun altl (level) { - case level of - [assoc, ps] -> - local assfun = case assoc of Left -> left | Right -> right | Nona -> left esac; - case map (fun (p) { - case p of - [op, sema] -> op @ lift(assfun (sema)) - esac - }, ps) of - p : ps -> foldl (infix |, p, ps) - esac - esac -} - -public fun expr (ops, opnd) { - fun inner (ops) { - case ops of - {} -> fun (c) {opnd @ c} - | level : tl -> - local lops = altl (level), - next = inner (tl); - - case level.fst of - Nona -> - fun this (c) { - next (id) |> fun (l) {lops |> fun (op) {next (id) @ fun (r) {c (op)(id, l)(r)}}} - | next (id) @ c - } - - this - - | _ -> - fun this (c) { - next (id) |> fun (l) {lops |> fun (op) {this (op (c, l))}} - | next (id) @ c - } - - this - esac - esac - } - - inner (ops) -} - diff --git a/stdlib/Fun.lama b/stdlib/Fun.lama index 2c88bdc07..01bb24525 100644 --- a/stdlib/Fun.lama +++ b/stdlib/Fun.lama @@ -1,3 +1,8 @@ +-- Fun. +-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020 +-- +-- This unit provides some convenient function-manipulation stuff. + import Ref; public fun id (x) { diff --git a/stdlib/Lazy.lama b/stdlib/Lazy.lama index 9086f235a..84c4cfbec 100644 --- a/stdlib/Lazy.lama +++ b/stdlib/Lazy.lama @@ -1,10 +1,21 @@ +-- Lazy. +-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020 +-- +-- Deferred computations. + public fun makeLazy (f) { local value, set = false; fun () { if set then value - else set := true; value := f (); value + else + local c; -- need this temporary since in value := f () value would + -- create an intermediate managed pointer + set := true; + c := f (); + value := c; + c fi } } diff --git a/stdlib/List.lama b/stdlib/List.lama index 2075eb081..0804522f5 100644 --- a/stdlib/List.lama +++ b/stdlib/List.lama @@ -1,3 +1,8 @@ +-- Lists. +-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020 +-- +-- This unit provides a set of list-manipulation primitives. + public fun singleton (x) { x : {} } diff --git a/stdlib/Ostap.lama b/stdlib/Ostap.lama index ce9190e63..db0c38e9c 100644 --- a/stdlib/Ostap.lama +++ b/stdlib/Ostap.lama @@ -1,3 +1,9 @@ +-- Ostap. +-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020 +-- +-- This unit provides an implementation of monadic parser combinators in CPS with +-- memoization. + import List; import Collection; import Ref; @@ -226,4 +232,66 @@ public fun parseString (p, s) { acc.result } +public fun left (f) { + fun (c, x) { + fun (y) { + f (c (x), y) + } + } +} + +public fun right (f) { + fun (c, x) { + fun (y) { + c (f (x, y)) + } + } +} + +--- ops -> fun (x, y) {x `op` y} +fun altl (level) { + case level of + [assoc, ps] -> + local assfun = case assoc of Left -> left | Right -> right | Nona -> left esac; + case map (fun (p) { + case p of + [op, sema] -> op @ lift(assfun (sema)) + esac + }, ps) of + p : ps -> foldl (infix |, p, ps) + esac + esac +} + +public fun expr (ops, opnd) { + fun inner (ops) { + case ops of + {} -> fun (c) {opnd @ c} + | level : tl -> + local lops = altl (level), + next = inner (tl); + + case level.fst of + Nona -> + fun this (c) { + next (id) |> fun (l) {lops |> fun (op) {next (id) @ fun (r) {c (op)(id, l)(r)}}} + | next (id) @ c + } + + this + + | _ -> + fun this (c) { + next (id) |> fun (l) {lops |> fun (op) {this (op (c, l))}} + | next (id) @ c + } + + this + esac + esac + } + + inner (ops) +} + initOstap () diff --git a/stdlib/Ref.lama b/stdlib/Ref.lama index d0d6a5cae..ec56f506f 100644 --- a/stdlib/Ref.lama +++ b/stdlib/Ref.lama @@ -1,11 +1,19 @@ +-- Emulation of first-class references. +-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020 +-- +-- This unit provides an implementation for first-class references emulation. + +-- Creates a new reference cell with contents x public fun ref (x) { [x] } +-- Returns the contents of the cell x public fun deref (x) { x[0] } +-- Assigns a new value y into the cell x public infix ::= before := (x, y) { x[0] := y } \ No newline at end of file diff --git a/stdlib/regression/test11.lama b/stdlib/regression/test11.lama index daaffcb4e..62bf27501 100644 --- a/stdlib/regression/test11.lama +++ b/stdlib/regression/test11.lama @@ -1,5 +1,4 @@ import Ostap; -import Expr; import Fun; local a = token ("a"), diff --git a/stdlib/regression/test12.lama b/stdlib/regression/test12.lama index 973d4ac98..9e9afaccd 100644 --- a/stdlib/regression/test12.lama +++ b/stdlib/regression/test12.lama @@ -1,5 +1,4 @@ import Ostap; -import Expr; import Fun; fun gen (depth) { diff --git a/stdlib/regression/test13.lama b/stdlib/regression/test13.lama index 9f784827f..b6277bef9 100644 --- a/stdlib/regression/test13.lama +++ b/stdlib/regression/test13.lama @@ -1,5 +1,4 @@ import Ostap; -import Expr; import Fun; local a = token ("a"), diff --git a/stdlib/regression/test14.lama b/stdlib/regression/test14.lama index 9e529156d..4eb50b462 100644 --- a/stdlib/regression/test14.lama +++ b/stdlib/regression/test14.lama @@ -1,5 +1,4 @@ import Ostap; -import Expr; import Fun; import List; diff --git a/stdlib/regression/test15.lama b/stdlib/regression/test15.lama index 19809e42a..63395235d 100644 --- a/stdlib/regression/test15.lama +++ b/stdlib/regression/test15.lama @@ -1,5 +1,4 @@ import Ostap; -import Expr; import Fun; import List; diff --git a/stdlib/regression/test16.lama b/stdlib/regression/test16.lama index 425736f53..7cbd09ec9 100644 --- a/stdlib/regression/test16.lama +++ b/stdlib/regression/test16.lama @@ -1,5 +1,4 @@ import Ostap; -import Expr; import Fun; import List;