navid/haze / 410cf353c8bebfa56a4099714ad9c7b96a5835655bb2a68a9416269d91a86ce7 / samples/baseline_3/sample3.hz

sample3.hz

← samples/baseline_3 · 896 bytes · raw · download

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