navid/haze / commit

ef88b55c38be

Navid M <navidmomtahen@gmail.com> · 2026-12-02

Browse files at this commit · Download source

all: init
--- /dev/null+++ b/samples/sample1.xyz@@ -0,0 +1,27 @@+contract FactorialCond(n i32) {+    pre {+        n >= 0 &&+        n <= 12+    }+    post r => {+        r >= 1+    }+}++// The ':' means "conforms to" in this scenario+fn factorial(n i32) i32 : FactorialCond(n) {+    if n <= 1 {+        return 1+    }+    return n * factorial(n - 1)+}++fn main(args list[str]) {+    for arg in args {+        print(arg)+    }+    const doubled = [1, 2, 3, 4, 5]+            .map(fn(x) => x * 2)+            .filter(fn(x) => x > 4)+    print(doubled)+}--- /dev/null+++ b/samples/sample2.xyz@@ -0,0 +1,61 @@+type List[T] =+    | Nil+    | Cons(head T, tail List[T])++interface Ord {+    fn lt(self Self, other Self) bool+}++impl Ord with i32 {+    fn lt(self i32, other i32) bool => self < other+}++pure fn len[T](xs List[T]) i32 => match xs {+    Nil        => 0+    Cons(_, t) => 1 + len(t)+}++pure fn sorted[T Ord](xs List[T]) bool => match xs {+    Cons(a, Cons(b, _) as rest) => !b.lt(a) && sorted(rest)+    _                           => true+}++contract Inserted[T Ord](x T, xs List[T]) {+    pre sorted(xs)+    post r => {+        sorted(r)+        len(r) == len(xs) + 1+    }+}++// Do some stuff...+fn insert[T Ord](x T, xs List[T]) List[T] : Inserted(x, xs) => match xs {+    Nil => Cons(x, Nil)+    Cons(h, t) => {+        if x.lt(h) {+            return Cons(x, xs)+        }+        return Cons(h, insert(x, t))+    }+}++fn sort[T Ord](xs List[T]) List[T] : { post r => sorted(r) && len(r) == len(xs) } => match xs {+    Nil        => Nil+    Cons(h, t) => insert(h, sort(t))+}++fn print_list(xs List[i32]) {+    match xs {+        Nil        => print("end")+        Cons(h, t) => {+            print(h)+            print_list(t)+        }+    }+}++fn main() {+    const xs = Cons(3, Cons(1, Cons(2, Nil)))+    const ys = sort(xs)+    print_list(ys)+}--- /dev/null+++ b/samples/sample3.xyz@@ -0,0 +1,49 @@+trait Ord {+    fn lt(self Self, other Self) bool+}++impl i32 : Ord {+    fn lt(self i32, other i32) bool => self < other+}++type Counter {+    count i32+    limit i32+}++fn bump(c Counter) : pre c.count < c.limit {+    c.count+++}++fn count_to(limit i32) i32 : pre limit >= 0 : post r => r == limit {+    var c = Counter{count = 0, limit = limit}+    for c.count < c.limit {+        bump(c)+    }+    return c.count+}++fn find(xs list[i32], target i32) i32 : { post r => r >= -1 && r < xs.len } {+    for i in 0..xs.len {+        if xs[i] == target {+            return i+        }+    }+    return -1+}++fn max[T Ord](a T, b T) T {+    if a.lt(b) { return b }+    return a+}++fn main() {+    var c = Counter{ count = 0, limit = 3 }+    bump(c)+    print(c.count)+    print(count_to(5))+    const xs = [4, 8, 15, 16, 23, 42]+    print(find(xs, 15))+    print(find(xs, 99))+    print(max(3, 7))+}--- /dev/null+++ b/samples/sample4.xyz@@ -0,0 +1,106 @@+// Bank account: contracts over immutable data, braced style.++const MAX_BALANCE i64 = 1_000_000_000++type Account {+    id      i32+    balance i64+}++type Transfer {+    src Account+    dst Account+}++pure fn solvent(a Account) bool => a.balance >= 0 && a.balance <= MAX_BALANCE++contract Solvent(a Account) {+    pre solvent(a)+}++contract Funded(a Account, amt i64) {+    pre {+        amt > 0+        amt <= a.balance+    }+}++contract Room(a Account, amt i64) {+    pre {+        amt > 0+        a.balance + amt <= MAX_BALANCE+    }+}++fn deposit(a Account, amt i64) Account+    : Solvent(a)+    : Room(a, amt)+    : post r => r.id == a.id+    : post r => r.balance == a.balance + amt+    : post r => solvent(r)+    =>  Account{ id = a.id, balance = a.balance + amt }++fn withdraw(a Account, amt i64) Account+    : Solvent(a)+    : Funded(a, amt)+    : post r => r.id == a.id+    : post r => r.balance == a.balance - amt+    : post r => solvent(r)+    => Account{ id = a.id, balance = a.balance - amt }++fn transfer(src Account, dst Account, amt i64) Transfer+    : Solvent(src)+    : Solvent(dst)+    : Funded(src, amt)+    : Room(dst, amt)+    : pre src.id != dst.id+    : post r => r.src.id == src.id && r.dst.id == dst.id+    : post r => r.src.balance == src.balance - amt+    : post r => r.dst.balance == dst.balance + amt+    : post r => solvent(r.src) && solvent(r.dst)+    => Transfer{ src = withdraw(src, amt), dst = deposit(dst, amt) }+++// Deposit `amt` once a month for `months` months.+fn save(a Account, amt i64, months i64) Account+    : Solvent(a)+    : pre amt > 0+    : pre months >= 0+    : pre a.balance + amt * months <= MAX_BALANCE+    : post r => r.id == a.id+    : post r => r.balance == a.balance + amt * months+    : post r => solvent(r)+{+    var acc = a+    for i in 0..months+        : invariant acc.balance == a.balance + amt * i+    {+        acc = deposit(acc, amt)+    }+    return acc+}++fn main() {+    const alice = Account{ id = 1, balance = 100 }+    const bob   = Account{ id = 2, balance = 50 }++    const a1 = deposit(alice, 25)+    print(a1.balance)               // 125++    const a2 = withdraw(a1, 100)+    print(a2.balance)               // 25++    const t = transfer(a2, bob, 20)+    print(t.src.balance)            // 5+    print(t.dst.balance)            // 70++    const s = save(t.src, 10, 3)+    print(s.balance)                // 35++    // Each of these should be rejected at compile time:+    //+    // withdraw(a2, 1000)           // pre `amt <= a.balance` not provable (a2.balance == 25)+    // transfer(a2, a2, 5)          // pre `src.id != dst.id` not provable+    // deposit(a2, 0)               // pre `amt > 0` not provable+    // save(a2, 1000000000, 2)      // pre `a.balance + amt * months <= MAX_BALANCE` not provable+}