navid/haze / commit
111bf5c1dba3
Navid M <navidmomtahen@gmail.com> · 2026-12-02
parents: ef88b55c38be
Browse files at this commit · Download source
--- /dev/null+++ b/samples/baseline/01_hello.xyz@@ -0,0 +1,5 @@+// 01: the smallest program.++fn main() {+ print("hello, xyz") // hello, xyz+}--- /dev/null+++ b/samples/baseline/02_values.xyz@@ -0,0 +1,16 @@+// 02: constants, variables, integer arithmetic.++fn main() {+ const a = 7+ const b = 3+ var total = a + b * 2+ print(total) // 13+ total = total - 1+ print(total) // 12+ total += 10+ print(total) // 22+ print(a / b) // 2+ print(a % b) // 1+ print(-a) // -7+ print("done") // done+}--- /dev/null+++ b/samples/baseline/03_functions.xyz@@ -0,0 +1,22 @@+// 03: functions, expression bodies vs block bodies, calls.++fn add(a i32, b i32) i32 => a + b++fn square(x i32) i32 {+ return x * x+}++fn sum_of_squares(a i32, b i32) i32 => add(square(a), square(b))++fn greet(name str) {+ print("hello")+ print(name)+}++fn main() {+ print(add(2, 3)) // 5+ print(square(7)) // 49+ print(sum_of_squares(3, 4)) // 25+ greet("world") // hello+ // world+}--- /dev/null+++ b/samples/baseline/04_conditionals.xyz@@ -0,0 +1,38 @@+// 04: if / else if / else, as a statement and as an expression.++fn abs(x i32) i32 {+ if x < 0 {+ return -x+ }+ return x+}++fn max(a i32, b i32) i32 => if a > b { a } else { b }++fn sign(x i32) i32 => if x < 0 { -1 } else if x == 0 { 0 } else { 1 }++fn is_even(n i32) bool => n % 2 == 0++fn in_range(x i32, lo i32, hi i32) bool => x >= lo && x <= hi++fn main() {+ print(abs(-5)) // 5+ print(abs(5)) // 5+ print(max(3, 9)) // 9+ print(max(9, 3)) // 9+ print(sign(-4)) // -1+ print(sign(0)) // 0+ print(sign(12)) // 1++ if is_even(10) {+ print("even") // even+ } else {+ print("odd")+ }++ if in_range(15, 1, 10) {+ print("in")+ } else {+ print("out") // out+ }+}--- /dev/null+++ b/samples/baseline/05_loops.xyz@@ -0,0 +1,44 @@+// 05: `for x in a..b` (half-open), `for cond`, break, continue.++fn sum_below(n i32) i32 {+ var total = 0+ for i in 0..n {+ total += i+ }+ return total+}++fn count_digits(n i32) i32 {+ var digits = 1+ var rest = n+ for rest >= 10 {+ rest = rest / 10+ digits+++ }+ return digits+}++fn first_square_over(limit i32) i32 {+ var i = 1+ for i * i <= limit {+ i+++ }+ return i * i+}++fn main() {+ print(sum_below(5)) // 10+ print(count_digits(12345)) // 5+ print(count_digits(7)) // 1+ print(first_square_over(50)) // 64++ for i in 0..10 {+ if i % 2 == 0 {+ continue+ }+ if i > 7 {+ break+ }+ print(i) // 1, 3, 5, 7 (one per line)+ }+}--- /dev/null+++ b/samples/baseline/06_lists.xyz@@ -0,0 +1,28 @@+// 06: list literals, .len, indexing, for-in.++fn sum(xs list[i32]) i32 {+ var total = 0+ for x in xs {+ total += x+ }+ return total+}++fn count_above(xs list[i32], limit i32) i32 {+ var n = 0+ for x in xs {+ if x > limit {+ n+++ }+ }+ return n+}++fn main() {+ const xs = [4, 8, 15, 16, 23, 42]+ print(xs.len) // 6+ print(xs[0]) // 4+ print(xs[5]) // 42+ print(sum(xs)) // 108+ print(count_above(xs, 15)) // 3+}--- /dev/null+++ b/samples/baseline/07_records.xyz@@ -0,0 +1,22 @@+// 07: records. Data is immutable: "changing" a value builds a new one.++type Point {+ x i32+ y i32+}++fn moved(p Point, dx i32, dy i32) Point => Point{ x = p.x + dx, y = p.y + dy }++fn add(a Point, b Point) Point => Point{ x = a.x + b.x, y = a.y + b.y }++fn dist2(p Point) i32 => p.x * p.x + p.y * p.y++fn main() {+ const p = Point{ x = 3, y = 4 }+ const q = moved(p, 1, 1)+ print(p.x) // 3 (p is unchanged)+ print(q.x) // 4+ print(q.y) // 5+ print(dist2(p)) // 25+ print(add(p, q).y) // 9+}--- /dev/null+++ b/samples/baseline/08_sum_types.xyz@@ -0,0 +1,28 @@+// 08: sum types and match (as an expression and as a statement).++type Shape =+ | Square(side i32)+ | Rect(w i32, h i32)+ | Dot++fn area(s Shape) i32 => match s {+ Square(side) => side * side+ Rect(w, h) => w * h+ Dot => 0+}++fn describe(s Shape) {+ match s {+ Square(_) => print("square")+ Rect(_, _) => print("rect")+ Dot => print("dot")+ }+}++fn main() {+ print(area(Square(5))) // 25+ print(area(Rect(3, 4))) // 12+ print(area(Dot)) // 0+ describe(Rect(1, 2)) // rect+ describe(Dot) // dot+}--- /dev/null+++ b/samples/baseline/09_contracts.xyz@@ -0,0 +1,37 @@+// 09: inline contracts. One `: pre` / `: post` clause per line, before the `{`.+// A milestone-1 compiler can turn each clause into a runtime assertion.++fn clamp(x i32, lo i32, hi i32) i32+ : pre lo <= hi+ : post r => r >= lo && r <= hi+{+ if x < lo {+ return lo+ }+ if x > hi {+ return hi+ }+ return x+}++fn safe_div(a i32, b i32) i32+ : pre a >= 0+ : pre b > 0+ : post r => r >= 0 && r <= a+{+ return a / b+}++fn main() {+ print(clamp(15, 0, 10)) // 10+ print(clamp(-3, 0, 10)) // 0+ print(clamp(4, 0, 10)) // 4+ print(safe_div(17, 5)) // 3+ print(safe_div(0, 9)) // 0++ // Each of these must be rejected (compile time, or a runtime panic at milestone 1):+ //+ // clamp(1, 5, 2) // pre `lo <= hi` violated+ // safe_div(10, 0) // pre `b > 0` violated+ // safe_div(-1, 2) // pre `a >= 0` violated+}--- /dev/null+++ b/samples/baseline/10_named_contracts.xyz@@ -0,0 +1,47 @@+// 10: named contracts, pure functions in contracts, contract reuse.++pure fn even(n i32) bool => n % 2 == 0++contract Even(n i32) {+ pre even(n)+}++fn half(n i32) i32+ : Even(n)+ : post r => r * 2 == n+{+ return n / 2+}++// FactorialCond from the original sketch.+// Note: a static verifier cannot prove the multiplication stays in i32 from+// `r >= 1` alone; as runtime assertions it is checked on every call.+contract FactorialCond(n i32) {+ pre {+ n >= 0+ n <= 12+ }+ post r => r >= 1+}++fn factorial(n i32) i32+ : FactorialCond(n)+{+ if n <= 1 {+ return 1+ }+ return n * factorial(n - 1)+}++fn main() {+ print(half(10)) // 5+ print(factorial(0)) // 1+ print(factorial(5)) // 120+ print(factorial(12)) // 479001600++ // Must be rejected:+ //+ // half(7) // pre `even(n)` violated+ // factorial(13) // pre `n <= 12` violated+ // factorial(-1) // pre `n >= 0` violated+}--- /dev/null+++ b/samples/baseline_2/main.xyz@@ -0,0 +1,64 @@+// Functional chains and modules.+//+// Module rules used here:+// - one file is one module, named after the file (no declaration line)+// - items are private unless marked `pub`+// - `import m` gives qualified access only: m::name+// - `import m::{a, b}` brings names into scope unqualified, so they work+// as plain calls and as dot-calls (UFCS): xs.a(...)+// - `::` is for module paths; `.` stays for fields and UFCS++use seq+use seq {range, map, filter, fold, take, all}+use orders {Order, total, is_big}++fn add(a i32, b i32) i32 => a + b++fn main() {+ const squares = range(1, 11)+ .filter(fn(n) => n % 2 == 0)+ .map(fn(n) => n * n)+ .fold(0, add)+ print(squares) // 220++ const orders = [+ Order{ id = 1, qty = 2, price = 30 },+ Order{ id = 2, qty = 1, price = 15 },+ Order{ id = 3, qty = 5, price = 40 },+ Order{ id = 4, qty = 10, price = 2 },+ ]++ print(orders.map(total).fold(0, add)) // 295+ print(orders.filter(is_big).map(total).fold(0, add)) // 200++ const bulk_ids = orders+ .filter(fn(o) => o.qty > 1)+ .map(fn(o) => o.id)++ for id in bulk_ids {+ print(id)+ }++ const firsts = range(0, 100)+ .map(fn(n) => n * 3)+ .filter(fn(n) => n % 2 == 1)+ .take(4)++ for n in firsts {+ print(n) // 3, 9, 15, 21 (one per line)+ }++ print(seq::fold(seq::range(1, 6), 1, fn(acc, n) => acc * n)) // 120++ if orders.all(fn(o) => o.qty > 0) {+ print("all positive") // all positive+ } else {+ print("some empty")+ }++ // Each of these must be rejected:+ //+ // orders::BIG_ORDER // error: `BIG_ORDER` is private to `orders`+ // range(5, 1) // pre `lo <= hi` violated+ // orders.take(-1) // pre `n >= 0` violated+}--- /dev/null+++ b/samples/baseline_2/orders.xyz@@ -0,0 +1,13 @@+// Module `orders`: a small domain model.++const BIG_ORDER i32 = 100 // private: not visible outside this file++pub type Order {+ id i32+ qty i32+ price i32+}++pub pure fn total(o Order) i32 => o.qty * o.price++pub pure fn is_big(o Order) bool => total(o) >= BIG_ORDER--- /dev/null+++ b/samples/baseline_2/seq.xyz@@ -0,0 +1,77 @@+// Module `seq`: list functions, written in xyz itself (no stdlib assumed).+// Every `pub` item is exported; anything without `pub` is private to this file.+// A pub function's contract may only mention pub names, because callers have+// to be able to read and reason about it.++pub pure fn min(a i32, b i32) i32 => if a < b { a } else { b }++pub fn range(lo i32, hi i32) list[i32]+ : pre lo >= 0 // keeps `hi - lo` from overflowing+ : pre lo <= hi+ : post r => r.len == hi - lo+{+ var out list[i32] = []+ for i in lo..hi+ : invariant out.len == i - lo+ {+ out = out + [i]+ }+ return out+}++pub fn map[T, U](xs list[T], f fn(T) U) list[U]+ : post r => r.len == xs.len+{+ var out list[U] = []+ for i in 0..xs.len+ : invariant out.len == i+ {+ out = out + [f(xs[i])]+ }+ return out+}++pub fn filter[T](xs list[T], keep fn(T) bool) list[T]+ : post r => r.len <= xs.len+{+ var out list[T] = []+ for i in 0..xs.len+ : invariant out.len <= i+ {+ if keep(xs[i]) {+ out = out + [xs[i]]+ }+ }+ return out+}++pub fn fold[T, A](xs list[T], init A, f fn(A, T) A) A {+ var acc = init+ for x in xs {+ acc = f(acc, x)+ }+ return acc+}++pub fn take[T](xs list[T], n i32) list[T]+ : pre n >= 0+ : post r => r.len == min(n, xs.len)+{+ const count = min(n, xs.len)+ var out list[T] = []+ for i in 0..count+ : invariant out.len == i+ {+ out = out + [xs[i]]+ }+ return out+}++pub fn all[T](xs list[T], ok fn(T) bool) bool {+ for x in xs {+ if !ok(x) {+ return false+ }+ }+ return true+}--- /dev/null+++ b/samples/baseline_3/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/baseline_3/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/baseline_3/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/baseline_3/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+}--- a/samples/sample1.xyz+++ /dev/null@@ -1,27 +0,0 @@-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)-}--- a/samples/sample2.xyz+++ /dev/null@@ -1,61 +0,0 @@-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)-}--- a/samples/sample3.xyz+++ /dev/null@@ -1,49 +0,0 @@-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))-}--- a/samples/sample4.xyz+++ /dev/null@@ -1,106 +0,0 @@-// 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-}