2022-01-28 21:25:16 +01:00
|
|
|
-- one could define the naturals as follows
|
|
|
|
type Nat
|
|
|
|
| zero : Nat
|
|
|
|
| succ : Nat → Nat
|
2022-01-20 21:05:25 +01:00
|
|
|
|
2022-01-28 21:25:16 +01:00
|
|
|
-- which will bring a recursor for the type into scope
|
|
|
|
-- rec[Nat] : A → (A → A) → Nat → A
|
2022-01-20 21:05:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
-- defining addition as
|
|
|
|
add : Nat → Nat → Nat
|
2022-01-28 20:50:02 +01:00
|
|
|
:= rec[Nat] (λx. x) (λf n. succ (f n))
|
2022-01-20 21:05:25 +01:00
|
|
|
|
2022-01-28 21:25:16 +01:00
|
|
|
-- since | rec[Nat] : A → (A → A) → Nat → A
|
|
|
|
-- which generalizes to | rec[Nat] : (Nat → Nat) → ((Nat → Nat) → (Nat → Nat)) → Nat → (Nat → Nat)
|
|
|
|
-- ^ adding 0 ^ recursively adding one more ^ resulting addition type
|
2022-01-20 21:05:25 +01:00
|
|
|
|
|
|
|
-- id is used to not add anything, the second function takes the last addition function and adds a layer
|
|
|
|
-- of succ onto it, this way it generates a function for adding the right amount.
|
|
|
|
|
|
|
|
|
2022-01-28 21:25:16 +01:00
|
|
|
|
|
|
|
-- multiplication is defined similairly
|
2022-01-20 21:05:25 +01:00
|
|
|
mul : Nat → Nat → Nat
|
2022-01-28 20:50:02 +01:00
|
|
|
:= rec[Nat] (λx. zero) (λf n. add n (f n))
|
2022-01-20 21:05:25 +01:00
|
|
|
|
|
|
|
|
2022-01-28 21:25:16 +01:00
|
|
|
|
|
|
|
-- here's an example of a simpler type
|
2022-01-20 21:05:25 +01:00
|
|
|
type Bool
|
2022-01-28 21:25:16 +01:00
|
|
|
| true : Bool
|
2022-01-20 21:05:25 +01:00
|
|
|
| false : Bool
|
|
|
|
|
|
|
|
not : Bool → Bool
|
|
|
|
:= rec[Bool] false true
|
|
|
|
|
|
|
|
-- now, let's look at a bit more interesting example
|
|
|
|
type Expr
|
2022-01-28 20:50:02 +01:00
|
|
|
| ENat : Nat → Expr
|
|
|
|
| EAdd : Expr → Expr → Expr
|
|
|
|
| EMul : Expr → Expr → Expr
|
2022-01-20 21:05:25 +01:00
|
|
|
|
|
|
|
-- this generates the following recursor
|
2022-01-28 21:25:16 +01:00
|
|
|
-- rec[Expr] : (Nat → A) → (A → A → A) → (A → A → A) → Expr → A
|
|
|
|
|
|
|
|
|
2022-01-20 21:05:25 +01:00
|
|
|
|
|
|
|
eval : Expr → Nat
|
|
|
|
:= rec[Expr] (λx. x) add mul
|
2022-01-28 21:25:16 +01:00
|
|
|
|
|
|
|
-- here's some other functions
|
|
|
|
isEven : Nat → Bool
|
|
|
|
:= rec[Nat] true not
|
2022-01-29 14:01:25 +01:00
|
|
|
|
|
|
|
type bot
|
|
|
|
|
|
|
|
absurd : bot -> A := rec[bot]
|