From acf69d0cb86a862437084735db22b9fa15ae65ba Mon Sep 17 00:00:00 2001 From: gfngfn Date: Sat, 20 Nov 2021 16:41:00 +0900 Subject: [PATCH 1/2] add 'examples/calc' --- examples/calc/rebar.config | 7 +++++++ examples/calc/rebar.lock | 4 ++++ examples/calc/sesterl.yaml | 26 ++++++++++++++++++++++++++ examples/calc/src/calc.app.src | 13 +++++++++++++ examples/calc/src/calc.sest | 30 ++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 examples/calc/rebar.config create mode 100644 examples/calc/rebar.lock create mode 100644 examples/calc/sesterl.yaml create mode 100644 examples/calc/src/calc.app.src create mode 100644 examples/calc/src/calc.sest diff --git a/examples/calc/rebar.config b/examples/calc/rebar.config new file mode 100644 index 00000000..8d49e30a --- /dev/null +++ b/examples/calc/rebar.config @@ -0,0 +1,7 @@ +{plugins, [{rebar_sesterl, {git, "https://github.com/gfngfn/rebar_sesterl_plugin.git", {branch, "master"}}}]}. +{src_dirs, ["./_generated", "./src"]}. +{deps, [{sesterl_stdlib, {git, "https://github.com/gfngfn/sesterl_stdlib", {tag, "v0.4.1"}}}]}. +{profiles, [{test, [{deps, []}]}]}. +{eunit_tests, [{dir, "_generated_test"}]}. +{relx, [{release, {calc, "0.1.0"}, [calc]}, {dev_mode, false}]}. +{sesterl_opts, [{output_dir, "./_generated"},{test_output_dir, "_generated_test"}]}. diff --git a/examples/calc/rebar.lock b/examples/calc/rebar.lock new file mode 100644 index 00000000..d48c9573 --- /dev/null +++ b/examples/calc/rebar.lock @@ -0,0 +1,4 @@ +[{<<"sesterl_stdlib">>, + {git,"https://github.com/gfngfn/sesterl_stdlib", + {ref,"0c45578e2d91d3b127135ada704e9f80b3997fd7"}}, + 0}]. diff --git a/examples/calc/sesterl.yaml b/examples/calc/sesterl.yaml new file mode 100644 index 00000000..561cd93a --- /dev/null +++ b/examples/calc/sesterl.yaml @@ -0,0 +1,26 @@ +package: "calc" +language: "v0.2.0" + +main_module: "Calc" + +source_directories: + - "./src" + +dependencies: + - name: "sesterl_stdlib" + source: + type: "git" + repository: "https://github.com/gfngfn/sesterl_stdlib" + spec: + type: "tag" + value: "v0.4.1" + +erlang: + output_directory: "./_generated" + relx: + release: + name: "calc" + version: "0.1.0" + applications: + - "calc" + dev_mode: true diff --git a/examples/calc/src/calc.app.src b/examples/calc/src/calc.app.src new file mode 100644 index 00000000..3b13ff0a --- /dev/null +++ b/examples/calc/src/calc.app.src @@ -0,0 +1,13 @@ +{application, calc, [ + {description, "calc"}, + {vsn, "0.1.0"}, + {registered, []}, + {modules, []}, + {applications, [ + kernel, + stdlib + ]}, + {env, []}, + {licenses, ["MIT"]}, + {links, []} +]}. diff --git a/examples/calc/src/calc.sest b/examples/calc/src/calc.sest new file mode 100644 index 00000000..5a80bb03 --- /dev/null +++ b/examples/calc/src/calc.sest @@ -0,0 +1,30 @@ +module Calc :> sig + val fact : fun(int) -> int + val sum : fun(list) -> int + val id<$a> : fun($a) -> $a + val first<$a, $b> : fun({$a, $b}) -> $a + val have_same_length<$a, $b> : fun(list<$a>, list<$b>) -> bool +end = struct + open Stdlib + + val fact(n) = + let rec aux(acc, n) = + if n <= 0 then acc else aux(n * acc, n - 1) + in + aux(1, n) + + val sum(ns) = + List.foldl( + fun(acc, n) -> acc + n end, + 0, + ns) + + val id(x) = x + + val first({x, _}) = x + + val have_same_length<$a, $b>(xs : list<$a>, ys : list<$b>) : bool = + List.length(xs) == List.length(ys) + + +end From 920ced26e355b704735bc0b87da2afd9b4a2d9dd Mon Sep 17 00:00:00 2001 From: gfngfn Date: Sun, 21 Nov 2021 16:43:53 +0900 Subject: [PATCH 2/2] update 'examples/calc' --- examples/calc/src/BinTree.sest | 24 ++++++++++++++++++++++++ examples/calc/src/calc.sest | 19 +++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 examples/calc/src/BinTree.sest diff --git a/examples/calc/src/BinTree.sest b/examples/calc/src/BinTree.sest new file mode 100644 index 00000000..8f137b3b --- /dev/null +++ b/examples/calc/src/BinTree.sest @@ -0,0 +1,24 @@ +module BinTree :> sig + type t :: (o) -> o + val leaf<$a> : fun($a) -> t<$a> + val node<$a> : fun($a, t<$a>, t<$a>) -> t<$a> + val tree_size<$a> : fun(t<$a>) -> int +end = struct + + type t<$a> = + | Empty + | Node($a, t<$a>, t<$a>) + + val leaf(x) = + Node(x, Empty, Empty) + + val node(x, tree1, tree2) = + Node(x, tree1, tree2) + + val rec tree_size(tree) = + case tree of + | Empty -> 0 + | Node(_, tree1, tree2) -> 1 + tree_size(tree1) + tree_size(tree2) + end + +end diff --git a/examples/calc/src/calc.sest b/examples/calc/src/calc.sest index 5a80bb03..990df131 100644 --- a/examples/calc/src/calc.sest +++ b/examples/calc/src/calc.sest @@ -2,7 +2,7 @@ module Calc :> sig val fact : fun(int) -> int val sum : fun(list) -> int val id<$a> : fun($a) -> $a - val first<$a, $b> : fun({$a, $b}) -> $a + val swap<$a, $b> : fun({$a, $b}) -> {$b, $a} val have_same_length<$a, $b> : fun(list<$a>, list<$b>) -> bool end = struct open Stdlib @@ -14,17 +14,16 @@ end = struct aux(1, n) val sum(ns) = - List.foldl( - fun(acc, n) -> acc + n end, - 0, - ns) + List.foldl(fun(acc, n) -> acc + n end, 0, ns) val id(x) = x - val first({x, _}) = x - - val have_same_length<$a, $b>(xs : list<$a>, ys : list<$b>) : bool = - List.length(xs) == List.length(ys) - + val swap({x, y}) = {y, x} + val rec have_same_length<$a, $b>(xs : list<$a>, ys : list<$b>) : bool = + case {xs, ys} of + | {[], []} -> true + | {_ :: xtail, _ :: ytail} -> have_same_length(xtail, ytail) + | _ -> false + end end