Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add a simple example for displaying the basic syntax and semantics #55

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/calc/rebar.config
Original file line number Diff line number Diff line change
@@ -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"}]}.
4 changes: 4 additions & 0 deletions examples/calc/rebar.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[{<<"sesterl_stdlib">>,
{git,"https://github.com/gfngfn/sesterl_stdlib",
{ref,"0c45578e2d91d3b127135ada704e9f80b3997fd7"}},
0}].
26 changes: 26 additions & 0 deletions examples/calc/sesterl.yaml
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions examples/calc/src/BinTree.sest
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions examples/calc/src/calc.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{application, calc, [
{description, "calc"},
{vsn, "0.1.0"},
{registered, []},
{modules, []},
{applications, [
kernel,
stdlib
]},
{env, []},
{licenses, ["MIT"]},
{links, []}
]}.
29 changes: 29 additions & 0 deletions examples/calc/src/calc.sest
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Calc :> sig
val fact : fun(int) -> int
val sum : fun(list<int>) -> int
val id<$a> : fun($a) -> $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

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 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