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
omega-mv / tests / compiler / progs / contracts_basic.osc
| 1 | // entry: safe_sub seven always use_safe_sub |
| 2 | // OSC-2 contracts that hold for every call in range: requires / ensures are |
| 3 | // enforced at run time; constant-true clauses and constant returns that |
| 4 | // satisfy ensures are elided (docs/osc/OSC-2-DESIGN.md section 1). |
| 5 | fn safe_sub(a: u32, b: u32) -> u32 |
| 6 | requires a >= b |
| 7 | ensures result <= a |
| 8 | { |
| 9 | return a - b; |
| 10 | } |
| 11 | |
| 12 | // the first return folds `result == 7` to true: no check there |
| 13 | fn seven(x: u8) -> u8 ensures result == 7 || result == x { |
| 14 | if x > 3 { return 7; } |
| 15 | return x; |
| 16 | } |
| 17 | |
| 18 | // both clauses fold to true on their own: no runtime check at all |
| 19 | fn always(x: u8) -> u8 requires true || false ensures !false { |
| 20 | return x; |
| 21 | } |
| 22 | |
| 23 | // constant arguments that satisfy the callee's requires are accepted |
| 24 | fn use_safe_sub(x: u32) -> u32 { |
| 25 | let k: u32 = safe_sub(10, 3); |
| 26 | if x >= k { return safe_sub(x, k); } |
| 27 | return k; |
| 28 | } |
| 29 | // expect-run: safe_sub 7 5 -> 2 |
| 30 | // expect-run: safe_sub 5 5 -> 0 |
| 31 | // expect-run: seven 9 -> 7 |
| 32 | // expect-run: seven 2 -> 2 |
| 33 | // expect-run: always 200 -> 200 |
| 34 | // expect-run: use_safe_sub 3 -> 7 |
| 35 | // expect-run: use_safe_sub 10 -> 3 |