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 / prefix.osc

23 lines · 684 bytes Raw Blame History
1// entry: prefix_main
2// expect-run: prefix_main 1 1 0 7 -> 36
3// expect-run: prefix_main 1 1 2 3 -> 7
4// expect-run: prefix_main 1 1 0 8 -> trap=BOUNDS
5// prefix sums in i32 (checked) over an owned array; queries a range sum.
6fn prefix(a: &mut [i32; 8]) {
7 for i in 1 .. 8 {
8 a[i] = a[i] + a[i - 1];
9 }
10}
11
12fn range_sum(p: &[i32; 8], lo: u8, hi: u8) -> i32 {
13 if lo == 0 { return p[hi]; }
14 return p[hi] - p[lo - 1];
15}
16
17fn prefix_main(x: i32, step: i16, lo: u8, hi: u8) -> i32 {
18 let a: own [i32; 8] = alloc(x);
19 for i in 0 .. 8 { a[i] = a[i] + (step as i32) * (i as i32); }
20 prefix(&mut a);
21 if lo > hi { return 0; }
22 return range_sum(&a, lo, hi);
23}