1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-12 14:28:08 +03:00
juvix/tests/Rust/Compilation/positive/test028.juvix
2024-07-02 10:03:06 +02:00

47 lines
1.1 KiB
Plaintext

-- streams without memoization
module test028;
import Stdlib.Prelude open;
type Stream := cons : Nat → (Unit → Stream) → Stream;
force : (Unit → Stream) → Stream
| f := f unit;
terminating
sfilter : (Nat → Bool) → (Unit → Stream) → Unit → Stream
| p s unit :=
case force s of {cons h t :=
ite (p h) (cons h (sfilter p t)) (force (sfilter p t))};
shead : Stream → Nat
| (cons h _) := h;
stail : Stream → Unit → Stream
| (cons _ t) := t;
snth : Nat → (Unit → Stream) → Nat
| zero s := shead (force s)
| (suc n) s := snth n (stail (force s));
terminating
numbers : Nat → Unit → Stream
| n unit := cons n (numbers (suc n));
indivisible : Nat → Nat → Bool
| n x := not (mod x n == 0);
terminating
eratostenes : (Unit → Stream) → Unit → Stream
| s unit :=
case force s of {cons n t :=
cons n (eratostenes (sfilter (indivisible n) t))};
primes : Unit → Stream := eratostenes (numbers 2);
main : Nat :=
snth 10 primes
+ snth 50 primes
+ snth 100 primes
+ snth 200 primes;