1
1
mirror of https://github.com/anoma/juvix.git synced 2024-11-30 14:13:27 +03:00
juvix/tests/Compilation/positive/test028.juvix
2024-07-02 10:03:06 +02:00

49 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 : IO :=
printNatLn (snth 10 primes)
>>> printNatLn (snth 50 primes)
>>> printNatLn (snth 100 primes)
>>> printNatLn (snth 200 primes);