hm/test.hm

66 lines
1.7 KiB
Plaintext
Raw Normal View History

2022-01-23 12:00:17 +01:00
-- currently all parameterised types are commented out until kinds are being implemented
2022-01-20 21:05:25 +01:00
-- no pattern matching, instead a recursor is given based on your inductive definition
-- for example: the following
2022-01-23 12:00:17 +01:00
{-
2022-01-20 21:05:25 +01:00
type List A
| nil : List A
| cons : A → List A → List A
2022-01-23 12:00:17 +01:00
-}
2022-01-20 21:05:25 +01:00
-- will bring the following into scope
-- rec[List] : B → (A → B → B) → List A → B
-- map could then be written as
2022-01-23 12:00:17 +01:00
{-
2022-01-20 21:05:25 +01:00
map : (A → B) → List A → List B
:= rec[List] nil (λx xs. cons (f x) xs)
2022-01-23 12:00:17 +01:00
-}
2022-01-20 21:05:25 +01:00
-- one could then define the naturals as follows
type Nat
| zero : Nat
| succ : Nat → Nat
-- 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
-- since | rec[Nat] : B → (Nat → B → B) → Nat → B
-- which generalizes to | rec[Nat] : (Nat → Nat) → (Nat → (Nat → Nat) → (Nat → Nat)) → Nat → Nat → Nat
-- ^ adding 0 ^ recursively adding one more ^ resulting addition type
-- 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.
-- multiplication is defined similairly
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
-- here's an example of a simpler type
type Bool
| true : Bool
| 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
-- rec[Expr] : (Nat → B) → (B → B → B) → (B → B → B) → Expr → B
eval : Expr → Nat
:= rec[Expr] (λx. x) add mul