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_requires.osc

48 lines · 1521 bytes Raw Blame History
1// entry: safe_sub abs8 inc use_arr give
2// OSC-2 requires clauses that fail at run time: TRAP REQUIRES at entry of the
3// callee, after which the run ends with no destructors (owners stay live in
4// the pool log, as for every OSC-1 trap). The clause obeys the checked
5// semantics: signed overflow inside it traps OVERFLOW, a dynamic index BOUNDS.
6fn safe_sub(a: u32, b: u32) -> u32 requires a >= b {
7 return a - b;
8}
9
10fn abs8(x: i8) -> i8 requires x != -128 ensures result >= 0 {
11 if x < 0 { return -x; }
12 return x;
13}
14
15// `x + 1` itself overflows for x = i32 max: OVERFLOW, not REQUIRES
16fn inc(x: i32) -> i32 requires x + 1 > x ensures result - 1 == x {
17 return x + 1;
18}
19
20// element reads through a borrow parameter
21fn first_pos(a: &[i32; 4]) -> i32 requires a[0] > 0 ensures result == a[0] {
22 return a[0];
23}
24
25fn use_arr(x: i32) -> i32 {
26 let a: own [i32; 4] = alloc(x);
27 return first_pos(&a);
28}
29
30// element read through an own parameter, dynamic index (BOUNDS inside the clause)
31fn take(a: own [u8; 2], i: u8) -> u8 requires a[i] == 7 {
32 return a[1];
33}
34
35fn give(i: u8) -> u8 {
36 let a: own [u8; 2] = alloc(7);
37 return take(a, i);
38}
39// expect-run: safe_sub 5 7 -> trap=REQUIRES
40// expect-run: safe_sub 9 7 -> 2
41// expect-run: abs8 -128 -> trap=REQUIRES
42// expect-run: abs8 -5 -> 5
43// expect-run: inc 2147483647 -> trap=OVERFLOW
44// expect-run: inc 4 -> 5
45// expect-run: use_arr 0 -> trap=REQUIRES
46// expect-run: use_arr 5 -> 5
47// expect-run: give 1 -> 7
48// expect-run: give 5 -> trap=BOUNDS