interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / compiler / progs / contracts_ensures.osc

39 lines · 1248 bytes Raw Blame History
1// entry: half_up void_check pick sum_first
2// OSC-2 ensures clauses that fail at run time: TRAP ENSURES after the return
3// value is computed and before the scope releases (no destructors run).
4// `half_up` is deliberately wrong for odd inputs.
5fn half_up(x: u16) -> u16 ensures result * 2 >= x {
6 return x / 2;
7}
8
9// void function: the check runs at the fall-off end, before `a` is released
10fn void_check(n: i64) ensures n >= 0 {
11 let a: own [i64; 4] = alloc(n);
12 a[0] = 1;
13}
14
15// checked at every return; the early return is wrong when x == 100
16fn pick(x: u8, y: u8) -> u8 ensures result != 100 {
17 if x == 100 { return x; }
18 if y > x { return y - x; }
19 return x - y;
20}
21
22// ensures reads elements through a shared borrow parameter
23fn first(a: &[u32; 3], k: u32) -> u32 ensures result == a[0] {
24 if k > 5 { return k; }
25 return a[0];
26}
27
28fn sum_first(k: u32) -> u32 {
29 let a: own [u32; 3] = alloc(4);
30 return first(&a, k);
31}
32// expect-run: half_up 7 -> trap=ENSURES
33// expect-run: half_up 8 -> 4
34// expect-run: void_check -1 -> trap=ENSURES
35// expect-run: void_check 3 -> 0
36// expect-run: pick 100 1 -> trap=ENSURES
37// expect-run: pick 9 1 -> 8
38// expect-run: sum_first 6 -> trap=ENSURES
39// expect-run: sum_first 5 -> 4