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
Łukasz Czajka 55598e0f95
Rust backend (#2787)
* Implements code generation through Rust.
* CLI: adds two `dev` compilation targets: 
  1. `rust` for generating Rust code
  2. `native-rust` for generating a native executable via Rust
* Adds end-to-end tests for compilation from Juvix to native executable
via Rust.
* A target for RISC0 needs to be added in a separate PR building on this
one.
2024-05-29 13:34:04 +02:00

47 lines
1.0 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 :=
if (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;