mirror of
https://github.com/anoma/juvix.git
synced 2024-12-12 14:28:08 +03:00
55598e0f95
* 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.
60 lines
1.4 KiB
Plaintext
60 lines
1.4 KiB
Plaintext
-- Iterators
|
||
module test054;
|
||
|
||
import Stdlib.Prelude open;
|
||
|
||
syntax iterator myfor;
|
||
myfor : {A B : Type} → (A → B → A) → A → List B → A :=
|
||
foldl {_} {_};
|
||
|
||
syntax iterator mymap {init := 0};
|
||
mymap : {A B : Type} → (A → B) → List A → List B
|
||
| f nil := nil
|
||
| f (x :: xs) := f x :: mymap f xs;
|
||
|
||
sum : List Nat → Nat
|
||
| xs := myfor (acc := 0) (x in xs) {acc + x};
|
||
|
||
sum' : List Nat → Nat
|
||
| xs := myfor λ {acc x := acc + x} 0 xs;
|
||
|
||
lst : List Nat := 1 :: 2 :: 3 :: 4 :: 5 :: nil;
|
||
|
||
syntax iterator myfor2 {init := 1; range := 2};
|
||
myfor2
|
||
: {A B C : Type}
|
||
→ (A → B → C → A)
|
||
→ A
|
||
→ List B
|
||
→ List C
|
||
→ A
|
||
| f acc xs ys :=
|
||
myfor (acc' := acc) (x in xs)
|
||
myfor (acc'' := acc') (y in ys)
|
||
f acc'' x y;
|
||
|
||
syntax iterator myzip2 {init := 2; range := 2};
|
||
myzip2
|
||
: {A A' B C : Type}
|
||
→ (A → A' → B → C → A × A')
|
||
→ A
|
||
→ A'
|
||
→ List B
|
||
→ List C
|
||
→ A × A'
|
||
| f a b xs ys :=
|
||
myfor (acc, acc' := a, b) (x, y in zip xs ys)
|
||
f acc acc' x y;
|
||
|
||
main : Nat :=
|
||
sum lst
|
||
+ sum' lst
|
||
+ fst (myfor (a, b := 0, 0) (x in lst) b + x, a)
|
||
+ (myfor2 (acc := 0) (x in lst; y in 1 :: 2 :: nil)
|
||
acc + x + y)
|
||
+ fst
|
||
(myzip2 (a := 0; b := 0) (x in lst; y in reverse lst)
|
||
a + x * y, b + y)
|
||
+ myfor (a := 0) (x, y in mymap (x in lst) {x, x + 1})
|
||
a + x * y;
|