2022-04-22 11:06:34 +03:00
|
|
|
module Polymorphism;
|
|
|
|
|
|
|
|
inductive Pair (A : Type) (B : Type) {
|
|
|
|
mkPair : A → B → Pair A B;
|
|
|
|
};
|
|
|
|
|
|
|
|
inductive Nat {
|
|
|
|
zero : Nat;
|
|
|
|
suc : Nat → Nat;
|
|
|
|
};
|
|
|
|
|
|
|
|
inductive List (A : Type) {
|
|
|
|
nil : List A;
|
2022-06-21 18:53:35 +03:00
|
|
|
cons : A → List A → List A;
|
2022-04-22 11:06:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
inductive Bool {
|
|
|
|
false : Bool;
|
|
|
|
true : Bool;
|
|
|
|
};
|
|
|
|
|
|
|
|
id : (A : Type) → A → A;
|
|
|
|
id _ a ≔ a;
|
|
|
|
|
2022-05-30 14:40:52 +03:00
|
|
|
terminating
|
2022-04-22 11:06:34 +03:00
|
|
|
undefined : (A : Type) → A;
|
|
|
|
undefined A ≔ undefined A;
|
|
|
|
|
2022-05-04 11:50:03 +03:00
|
|
|
add : Nat → Nat → Nat;
|
|
|
|
add zero b ≔ b;
|
|
|
|
add (suc a) b ≔ suc (add a b);
|
|
|
|
|
2022-04-22 11:06:34 +03:00
|
|
|
nil' : (E : Type) → List E;
|
2022-06-13 15:25:22 +03:00
|
|
|
nil' A ≔ nil;
|
2022-04-22 11:06:34 +03:00
|
|
|
|
|
|
|
-- currying
|
|
|
|
nil'' : (E : Type) → List E;
|
2022-06-13 15:25:22 +03:00
|
|
|
nil'' E ≔ nil;
|
2022-04-22 11:06:34 +03:00
|
|
|
|
|
|
|
fst : (A : Type) → (B : Type) → Pair A B → A;
|
|
|
|
fst _ _ (mkPair a b) ≔ a;
|
|
|
|
|
|
|
|
p : Pair Bool Bool;
|
2022-06-13 15:25:22 +03:00
|
|
|
p ≔ mkPair true false;
|
2022-04-22 11:06:34 +03:00
|
|
|
|
|
|
|
swap : (A : Type) → (B : Type) → Pair A B → Pair B A;
|
2022-06-13 15:25:22 +03:00
|
|
|
swap A B (mkPair a b) ≔ mkPair b a;
|
2022-04-22 11:06:34 +03:00
|
|
|
|
|
|
|
curry : (A : Type) → (B : Type) → (C : Type)
|
|
|
|
→ (Pair A B → C) → A → B → C;
|
2022-06-13 15:25:22 +03:00
|
|
|
curry A B C f a b ≔ f (mkPair a b) ;
|
2022-04-22 11:06:34 +03:00
|
|
|
|
|
|
|
ap : (A : Type) → (B : Type)
|
|
|
|
→ (A → B) → A → B;
|
|
|
|
ap A B f a ≔ f a;
|
|
|
|
|
|
|
|
ite : (A : Type) → Bool → A → A → A;
|
|
|
|
ite _ true tt _ ≔ tt;
|
|
|
|
ite _ false _ ff ≔ ff;
|
|
|
|
|
2022-05-04 11:50:03 +03:00
|
|
|
headDef : (A : Type) → A → List A → A;
|
|
|
|
headDef _ d nil ≔ d;
|
|
|
|
headDef A _ (cons h _) ≔ h;
|
|
|
|
|
2022-04-22 11:06:34 +03:00
|
|
|
filter : (A : Type) → (A → Bool) → List A → List A;
|
2022-06-13 15:25:22 +03:00
|
|
|
filter A f nil ≔ nil;
|
|
|
|
filter A f (cons x xs) ≔ ite (List A) (f x) (cons x (filter A f xs)) (filter A f xs);
|
2022-04-22 11:06:34 +03:00
|
|
|
|
|
|
|
map : (A : Type) → (B : Type) →
|
|
|
|
(A → B) → List A → List B;
|
2022-06-13 15:25:22 +03:00
|
|
|
map A B f nil ≔ nil ;
|
|
|
|
map A B f (cons x xs) ≔ cons (f x) (map A B f xs);
|
2022-04-22 11:06:34 +03:00
|
|
|
|
|
|
|
zip : (A : Type) → (B : Type)
|
|
|
|
→ List A → List B → List (Pair A B);
|
2022-06-13 15:25:22 +03:00
|
|
|
zip A B nil _ ≔ nil;
|
|
|
|
zip A B _ nil ≔ nil;
|
|
|
|
zip A B (cons a as) (cons b bs) ≔ nil;
|
2022-04-22 11:06:34 +03:00
|
|
|
|
|
|
|
zipWith : (A : Type) → (B : Type) → (C : Type)
|
|
|
|
→ (A → B → C)
|
|
|
|
→ List A → List B → List C;
|
2022-06-13 15:25:22 +03:00
|
|
|
zipWith A B C f nil _ ≔ nil;
|
|
|
|
zipWith A B C f _ nil ≔ nil;
|
|
|
|
zipWith A B C f (cons a as) (cons b bs) ≔ cons (f a b) (zipWith A B C f as bs);
|
2022-04-22 11:06:34 +03:00
|
|
|
|
|
|
|
rankn : ((A : Type) → A → A) → Bool → Nat → Pair Bool Nat;
|
2022-06-13 15:25:22 +03:00
|
|
|
rankn f b n ≔ mkPair (f Bool b) (f Nat n);
|
2022-04-22 11:06:34 +03:00
|
|
|
|
|
|
|
-- currying
|
|
|
|
trankn : Pair Bool Nat;
|
|
|
|
trankn ≔ rankn id false zero;
|
|
|
|
|
2022-05-04 11:50:03 +03:00
|
|
|
l1 : List Nat;
|
2022-06-13 15:25:22 +03:00
|
|
|
l1 ≔ cons zero nil;
|
2022-05-04 11:50:03 +03:00
|
|
|
|
|
|
|
pairEval : (A : Type) → (B : Type) → Pair (A → B) A → B;
|
|
|
|
pairEval _ _ (mkPair f x) ≔ f x;
|
|
|
|
|
|
|
|
main : Nat;
|
2022-06-13 15:25:22 +03:00
|
|
|
main ≔ headDef Nat (pairEval Nat Nat (mkPair (add zero) zero))
|
2022-05-04 11:50:03 +03:00
|
|
|
(zipWith Nat Nat Nat add l1 l1);
|
|
|
|
|
2022-04-22 11:06:34 +03:00
|
|
|
end;
|