3. Type of natural numbers
Haskell
data Nat = Zero | Suc Nat
Scala
sealed trait Nat
case object Zero extends Nat
case class Suc(n: Nat) extends Nat
Java
public interface Nat {}
public class Zero implements Nat {}
public class Suc implements Nat {
private final Nat n;
}
Dmytro Mitin Type of natural numbers
4. Type of natural numbers
1 Type Formation
Γ ctx
Γ Nat : ∗
2 Constructors
Γ ctx
Γ zero : Nat
Γ n : Nat
Γ suc n : Nat
3 Eliminator
Γ c1 : C Γ, n : Nat c2 : C
Γ, x : Nat caseNat(x)(c1, λn.c2) : C
x match { case Zero => c1; case Suc(n) => c2(n) }
Dmytro Mitin Type of natural numbers
5. Type of natural numbers
4 Computation rules (”β-reduction“)
Γ c1 : C Γ, n : Nat c2 : C
Γ caseNat(zero)(c1, λn.c2) ≡ c1 : C
Γ c1 : C Γ, n : Nat c2 : C Γ n : Nat
Γ caseNat(suc n )(c1, λn.c2) ≡ c2[n ← n ] : C
5 Uniqueness principle (”η-conversion“)
Dmytro Mitin Type of natural numbers
6. ProvingGround
git clone --recursive https://github.com/siddhartha-gadgil/ProvingGround.git
cd ProvingGround
mill -i mantle.repl
import provingground.HoTT.
import provingground.induction.TLImplicits.
import shapeless.
val Nat = "Nat" :: Type
val NatInd = ("0" ::: Nat) |: ("succ" ::: Nat -->>: Nat) =: Nat
val zero :: succ :: HNil = NatInd.intros
val n = "n" :: Nat
val m = "m" :: Nat
val one = succ(zero)
val two = succ(one)
val three = succ(two)
val four = succ(three)
val five = succ(four)
Dmytro Mitin Type of natural numbers
7. ProvingGround
val recNN = NatInd.rec(Nat)
val double = recNN(zero)(n :-> (m :-> succ(succ(m)) ))
double(two) == four
val recNNN = NatInd.rec(Nat ->: Nat)
val addn ="add(n)" :: Nat ->: Nat
val add = recNNN(m :-> m)(n :-> (addn :-> (m :->
succ(addn(m)) )))
add(two)(three) == five
Dmytro Mitin Type of natural numbers