2023-03-14 18:24:07 +03:00
|
|
|
-- Church numerals with pattern matching
|
|
|
|
|
|
|
|
def czero : Π A : Type, (A → A) → A → A :=
|
|
|
|
λ(A : Type) λ(f : A → A) λ(x : A) x;
|
|
|
|
|
|
|
|
def csuc : (Π A : Type, (A → A) → A → A) → Π A : Type, (A → A) → A → A :=
|
|
|
|
λ(n : Π A : Type, (A → A) → A → A) λ(A : Type) λ(f : A → A) λ(x : A) f (n A f x);
|
|
|
|
|
2023-03-15 18:41:39 +03:00
|
|
|
def num : Nat → Π A : Type, (A → A) → A → A :=
|
|
|
|
λ(n : Nat) match n : Nat with : Π A : Type, (A → A) → A → A {
|
2023-03-14 18:24:07 +03:00
|
|
|
zero := czero;
|
2023-03-15 18:41:39 +03:00
|
|
|
suc (n : Nat) := csuc (num n);
|
2023-03-14 18:24:07 +03:00
|
|
|
};
|
|
|
|
|
2023-03-15 18:41:39 +03:00
|
|
|
def toNat : (Π A : Type, (A → A) → A → A) → Nat :=
|
|
|
|
λ(n : Π A : Type, (A → A) → A → A) n Nat suc zero;
|
2023-03-14 18:24:07 +03:00
|
|
|
|
2023-03-15 18:41:39 +03:00
|
|
|
def toInt : Nat → Int :=
|
|
|
|
λ(n : Nat) case n of {
|
2023-03-14 18:24:07 +03:00
|
|
|
zero := 0;
|
|
|
|
suc n := toInt n + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
toInt (toNat (num (suc (suc (suc zero)))))
|