mirror of
https://github.com/anoma/juvix.git
synced 2024-11-30 05:42:26 +03:00
21d5034e60
In this PR, we ran the Juvix formatter so that we can now freely run `make format`, `make check`, or `make pre-commit` without any unexpected file changes. This goes after: - https://github.com/anoma/juvix/pull/2486
117 lines
2.3 KiB
Plaintext
117 lines
2.3 KiB
Plaintext
module Polymorphism;
|
|
|
|
type Pair (A : Type) (B : Type) :=
|
|
mkPair : A → B → Pair A B;
|
|
|
|
type Nat :=
|
|
| zero : Nat
|
|
| suc : Nat → Nat;
|
|
|
|
type List (A : Type) :=
|
|
| nil : List A
|
|
| cons : A → List A → List A;
|
|
|
|
type Bool :=
|
|
| false : Bool
|
|
| true : Bool;
|
|
|
|
id : (A : Type) → A → A
|
|
| _ a := a;
|
|
|
|
terminating
|
|
undefined : (A : Type) → A
|
|
| A := undefined A;
|
|
|
|
add : Nat → Nat → Nat
|
|
| zero b := b
|
|
| (suc a) b := suc (add a b);
|
|
|
|
nil' : (E : Type) → List E
|
|
| A := nil;
|
|
|
|
-- currying
|
|
nil'' : (E : Type) → List E
|
|
| E := nil;
|
|
|
|
fst : (A : Type) → (B : Type) → Pair A B → A
|
|
| _ _ (mkPair a b) := a;
|
|
|
|
p : Pair Bool Bool := mkPair true false;
|
|
|
|
swap : (A : Type) → (B : Type) → Pair A B → Pair B A
|
|
| A B (mkPair a b) := mkPair b a;
|
|
|
|
curry
|
|
: (A : Type)
|
|
→ (B : Type)
|
|
→ (C : Type)
|
|
→ (Pair A B → C)
|
|
→ A
|
|
→ B
|
|
→ C
|
|
| A B C f a b := f (mkPair a b);
|
|
|
|
ap : (A : Type) → (B : Type) → (A → B) → A → B
|
|
| A B f a := f a;
|
|
|
|
ite : (A : Type) → Bool → A → A → A
|
|
| _ true tt _ := tt
|
|
| _ false _ ff := ff;
|
|
|
|
headDef : (A : Type) → A → List A → A
|
|
| _ d nil := d
|
|
| A _ (cons h _) := h;
|
|
|
|
filter : (A : Type) → (A → Bool) → List A → List A
|
|
| A f nil := nil
|
|
| A f (cons x xs) :=
|
|
ite
|
|
(List A)
|
|
(f x)
|
|
(cons x (filter A f xs))
|
|
(filter A f xs);
|
|
|
|
map : (A : Type) → (B : Type) → (A → B) → List A → List B
|
|
| A B f nil := nil
|
|
| A B f (cons x xs) := cons (f x) (map A B f xs);
|
|
|
|
zip
|
|
: (A : Type)
|
|
→ (B : Type)
|
|
→ List A
|
|
→ List B
|
|
→ List (Pair A B)
|
|
| A B nil _ := nil
|
|
| A B _ nil := nil
|
|
| A B (cons a as) (cons b bs) := nil;
|
|
|
|
zipWith
|
|
: (A : Type)
|
|
→ (B : Type)
|
|
→ (C : Type)
|
|
→ (A → B → C)
|
|
→ List A
|
|
→ List B
|
|
→ List C
|
|
| A B C f nil _ := nil
|
|
| A B C f _ nil := nil
|
|
| A B C f (cons a as) (cons b bs) :=
|
|
cons (f a b) (zipWith A B C f as bs);
|
|
|
|
rankn : ((A : Type) → A → A) → Bool → Nat → Pair Bool Nat
|
|
| f b n := mkPair (f Bool b) (f Nat n);
|
|
|
|
-- currying
|
|
trankn : Pair Bool Nat := rankn id false zero;
|
|
|
|
l1 : List Nat := cons zero nil;
|
|
|
|
pairEval : (A : Type) → (B : Type) → Pair (A → B) A → B
|
|
| _ _ (mkPair f x) := f x;
|
|
|
|
main : Nat :=
|
|
headDef
|
|
Nat
|
|
(pairEval Nat Nat (mkPair (add zero) zero))
|
|
(zipWith Nat Nat Nat add l1 l1);
|