Basic Syntax
(; Comments ;)
;; Line comments
(* Values *)
x = 42; // Integer binding
s = "hello"; // String binding
b = true; // Boolean binding
(* Functions *)
f x = x + 1; // Simple function
g (x : int) = x + 1; // With type annotation
h = fun x => x + 1; // Lambda syntax
Types and Modules
(* Type Definitions *)
type t = int;
type pair a b = {fst: a; snd: b};
(* Module Types *)
type STACK = {
type t a;
empty 'a : t a;
push 'a : a -> t a -> t a;
};
(* Module Implementation *)
Stack :> STACK = {
type t a = List.list a;
empty = List.nil;
push = List.cons;
};
Pattern Matching
(* Pattern Matching *)
caseopt opt_val
(fun () => /* handle None */)
(fun x => /* handle Some x */);
caselist lst
(fun () => /* handle Nil */)
(fun (head, tail) => /* handle Cons */);
if condition
then expr1
else expr2;
Common Operations
(* Printing *)
do Int.print 42;
do Text.print "hello";
(* List Operations *)
lst = List.cons 1 (List.cons 2 List.nil);
len = List.length lst;
rev = List.rev lst;
(* Function Application *)
result = f (g x);
Advanced Features
(* Optional Values *)
type OPT = {
type opt a;
none 'a : opt a;
some 'a : a -> opt a;
};
(* Using Options *)
maybeValue = some 42;
result = caseopt maybeValue
(fun () => 0) // handle None case
(fun x => x + 1); // handle Some case
(* Higher-Order Functions *)
map f lst = foldr lst nil (fun x => cons (f x));
filter p lst = foldr lst nil
(fun x ys => if p x then cons x ys else ys);
(* Type Abstraction *)
Set (Elem : ORD) :> SET with (elem = Elem.t) = {
type elem = Elem.t;
type set = (int, elem -> bool);
// ... implementation
};