A Martin-Lof dependent type theory kernel, running in your browser. Bidirectional type checking over normalisation by evaluation, with intensional identity types eliminated by the J rule. The prelude of path lemmas below is not built in: every one of them is an ordinary term that the checker had to accept.
Honest naming: this is MLTT, not HoTT proper. There is no univalence and there are no higher inductive types. See limitations. If you want the plain-language version first, Start Here.
Types as spaces, terms as points, equalities as paths
This is the idea the whole subject is named after, and it is worth having in front of you before you read any of the code below.
A TYPE IS A SPACE. ITS TERMS ARE THE POINTS IN IT.
A : Type
+--------------------------------------------+
| |
| . x . z |
| |
| . y |
| |
+--------------------------------------------+
x : A y : A z : A
three points, i.e. three terms of the type A
AN EQUALITY IS A PATH BETWEEN TWO POINTS.
p : Id A x y
x .~~~~~~~~~~~~~~~~~~~. y
p
Not "x and y are the same symbol".
A path, an object in its own right, that you can carry around.
THE GROUPOID LAWS ARE JUST THINGS PATHS DO.
sym A x y p : Id A y x walk the path backwards
x .<~~~~~~~~~~~~~~~~~. y
sym p
trans A x y z p q : Id A x z join two paths end to end
x .~~~~~~~~~~. y ~~~~~~~~~~. z
p q
\______________________________/
trans p q
Nobody adds these as rules. They are derived, each by a single
application of J, and you can read their normal forms below.
ap SENDS A PATH THROUGH A FUNCTION.
f : A -> B p : Id A x y
ap A B f x y p : Id B (f x) (f y)
A B
x .~~~~~~. y --f--> f x .~~~~~~. f y
p ap f p
A function cannot tear a path. Continuity, for free,
because there is no rule that would let you build a tear.
transport CARRIES A POINT ALONG A PATH, IN A FAMILY OF SPACES.
P : A -> Type p : Id A x y u : P x
transport A P x y p u : P y
P x P y
+---------+ +---------+
| . u | ==p==> | . u' |
+---------+ +---------+
| |
x .~~~~~~~~~~~~~~~~~~~~~~. y
p
This is substitution of equals for equals. In topology it is
lifting a path along a fibration. Same rule, two readings.
PATHS BETWEEN PATHS. THIS IS WHERE IT GETS INTERESTING.
p, q : Id A x y two different paths, same endpoints
h : Id (Id A x y) p q a path between those paths
p
..~~~~~~~~~..
x .< | h >. y
''~~~~~~~~~''
q
Nothing in the kernel treats this specially. Id is simply applied
to an Id, and the same J works at every level. That uniformity is
the entire content of "types behave like spaces".
And h is NOT automatically available: UIP, the claim that any two
such p and q are equal, is not derivable here. Load the "uniqueness
of identity proofs" example below and watch the kernel refuse it.
That refusal is the room where univalence would live.
Worked examples
Each is checked on top of the prelude by the real kernel. Two of them are refuted on purpose. Nothing is precomputed.
[verified] Composing a path with the constant path leaves it unchanged. One side holds by computation alone; the other genuinely needs path induction.
[verified] A path followed by its reverse is the constant path, and reversing twice does nothing.
[verified] A proof that two paths are equal is itself a path, one dimension up. Applying ap to it is what makes a type behave like an infinity-groupoid rather than merely a groupoid.
[verified] The type of pairs (y, p) with p a path from x to y has a centre that every element is connected to.
[verified] Because the kernel evaluates while it checks, some equations need no proof beyond refl, while their mirror images need induction.
[verified] Given a family of types indexed by A, a path in A moves you between the fibres. This is subst in logic and a lift along a fibration in topology.
[verified] A family of types defined by recursion on a numeral turns a false equation into the empty type.
[refuted] A deliberately false equation, so you can watch the kernel refuse it.
[refuted] Two paths between the same endpoints need not be equal, and the obvious attempt to prove otherwise fails.
What this example is for
Composing on the right is definitional, because J applied to refl is a reduction and both sides have the same normal form, so refl proves it. Composing on the left is not, and needs induction on the path. That asymmetry is the first place the groupoid structure of a type shows up.
Proof source
Declarations are checked in order against the prelude signature. Use def name : TYPE := TERM or axiom name : TYPE. -- starts a comment. A failing declaration is reported and skipped; later ones that mention it fail with "unknown constant", which keeps errors local.
Kernel output
declarations 2
accepted 2
refused 0
axioms used none - derived from J alone
VERDICT VERIFIED
Per declaration
Result
Name
Type as checked
Axioms
[PASS]
unitRight
(A : Type) -> (x : A) -> (y : A) -> (p : Id A x y) -> Id (Id A x y) (trans A x y y p (refl A y)) p
none
[PASS]
unitLeft
(A : Type) -> (x : A) -> (y : A) -> (p : Id A x y) -> Id (Id A x y) (trans A x x y (refl A x) p) p
none
Normal forms
The kernel evaluates while it checks. These are the fully normalised bodies, with every definition unfolded and every J-on-refl reduction fired. Two terms are definitionally equal exactly when these agree.
Artifact digest
SHA-256 of the canonical JSON of the declarations, their types, the sorted axiom list and the verdict. A development that leans on a postulate hashes differently from one that does not, deliberately.
computing...
The prelude it is checked against
47 declarations, 0 failures, checked by the kernel when this page loaded. Nothing in it is built in — every lemma is an ordinary term the checker had to accept, and the only path primitives available are refl and J.
Available constants
id const compose Not sym trans ap transport transportBack pathInd transReflRight transReflLeft symInvolutive transSymRight transSymLeft transAssoc apRefl apTrans apSym transportRefl transportBackForth Singleton center contraction isContr singletonIsContr pairPath isProp isSet emptyIsProp pred add mul addZeroRight addSuccRight addZeroLeft addSuccLeft addAssoc addComm succInjective Code zeroNotSucc succNotZero twoPlusTwo threeTimesFour funext addZeroIsIdentity
Axioms in the prelude
funext
funext is function extensionality. It is genuinely not provable in this
theory, so it is postulated rather than smuggled in. Exactly one prelude
lemma depends on it, and the "Axioms" column above reports that dependency
for every declaration you check.
Show the full prelude source
-- ---------------------------------------------------------------------------
-- Functions
-- ---------------------------------------------------------------------------
def id : (A : Type) -> A -> A
:= \A x. x
def const : (A : Type) -> (B : Type) -> A -> B -> A
:= \A B x y. x
def compose : (A : Type) -> (B : Type) -> (C : Type) -> (B -> C) -> (A -> B) -> A -> C
:= \A B C g f x. g (f x)
def Not : Type -> Type
:= \A. A -> Empty
-- ---------------------------------------------------------------------------
-- The identity type is an equivalence relation
-- ---------------------------------------------------------------------------
def sym : (A : Type) -> (x : A) -> (y : A) -> Id A x y -> Id A y x
:= \A x y p. J A x (\b q. Id A b x) (refl A x) y p
def trans : (A : Type) -> (x : A) -> (y : A) -> (z : A) -> Id A x y -> Id A y z -> Id A x z
:= \A x y z p q. J A y (\b r. Id A x b) p z q
def ap : (A : Type) -> (B : Type) -> (f : A -> B) -> (x : A) -> (y : A)
-> Id A x y -> Id B (f x) (f y)
:= \A B f x y p. J A x (\b q. Id B (f x) (f b)) (refl B (f x)) y p
def transport : (A : Type) -> (P : A -> Type) -> (x : A) -> (y : A) -> Id A x y -> P x -> P y
:= \A P x y p u. J A x (\b q. P b) u y p
def transportBack : (A : Type) -> (P : A -> Type) -> (x : A) -> (y : A) -> Id A x y -> P y -> P x
:= \A P x y p u. transport A P y x (sym A x y p) u
-- Unbased path induction, recovered from the based form.
def pathInd : (A : Type) -> (C : (x : A) -> (y : A) -> Id A x y -> Type)
-> ((x : A) -> C x x (refl A x))
-> (x : A) -> (y : A) -> (p : Id A x y) -> C x y p
:= \A C d x y p. J A x (\b q. C x b q) (d x) y p
-- ---------------------------------------------------------------------------
-- Groupoid laws for paths
-- ---------------------------------------------------------------------------
-- Right unit holds by computation alone: J on refl is a reduction.
def transReflRight : (A : Type) -> (x : A) -> (y : A) -> (p : Id A x y)
-> Id (Id A x y) (trans A x y y p (refl A y)) p
:= \A x y p. refl (Id A x y) p
-- Left unit genuinely needs induction on the path.
def transReflLeft : (A : Type) -> (x : A) -> (y : A) -> (p : Id A x y)
-> Id (Id A x y) (trans A x x y (refl A x) p) p
:= \A x y p. J A x (\b q. Id (Id A x b) (trans A x x b (refl A x) q) q)
(refl (Id A x x) (refl A x)) y p
def symInvolutive : (A : Type) -> (x : A) -> (y : A) -> (p : Id A x y)
-> Id (Id A x y) (sym A y x (sym A x y p)) p
:= \A x y p. J A x (\b q. Id (Id A x b) (sym A b x (sym A x b q)) q)
(refl (Id A x x) (refl A x)) y p
def transSymRight : (A : Type) -> (x : A) -> (y : A) -> (p : Id A x y)
-> Id (Id A x x) (trans A x y x p (sym A x y p)) (refl A x)
:= \A x y p. J A x (\b q. Id (Id A x x) (trans A x b x q (sym A x b q)) (refl A x))
(refl (Id A x x) (refl A x)) y p
def transSymLeft : (A : Type) -> (x : A) -> (y : A) -> (p : Id A x y)
-> Id (Id A y y) (trans A y x y (sym A x y p) p) (refl A y)
:= \A x y p. J A x (\b q. Id (Id A b b) (trans A b x b (sym A x b q) q) (refl A b))
(refl (Id A x x) (refl A x)) y p
def transAssoc : (A : Type) -> (w : A) -> (x : A) -> (y : A) -> (z : A)
-> (p : Id A w x) -> (q : Id A x y) -> (r : Id A y z)
-> Id (Id A w z) (trans A w y z (trans A w x y p q) r)
(trans A w x z p (trans A x y z q r))
:= \A w x y z p q r.
J A y (\b s. Id (Id A w b) (trans A w y b (trans A w x y p q) s)
(trans A w x b p (trans A x y b q s)))
(refl (Id A w y) (trans A w x y p q)) z r
-- ---------------------------------------------------------------------------
-- Functoriality of ap and transport
-- ---------------------------------------------------------------------------
def apRefl : (A : Type) -> (B : Type) -> (f : A -> B) -> (x : A)
-> Id (Id B (f x) (f x)) (ap A B f x x (refl A x)) (refl B (f x))
:= \A B f x. refl (Id B (f x) (f x)) (refl B (f x))
def apTrans : (A : Type) -> (B : Type) -> (f : A -> B) -> (x : A) -> (y : A) -> (z : A)
-> (p : Id A x y) -> (q : Id A y z)
-> Id (Id B (f x) (f z))
(ap A B f x z (trans A x y z p q))
(trans B (f x) (f y) (f z) (ap A B f x y p) (ap A B f y z q))
:= \A B f x y z p q.
J A y (\b s. Id (Id B (f x) (f b))
(ap A B f x b (trans A x y b p s))
(trans B (f x) (f y) (f b) (ap A B f x y p) (ap A B f y b s)))
(refl (Id B (f x) (f y)) (ap A B f x y p)) z q
def apSym : (A : Type) -> (B : Type) -> (f : A -> B) -> (x : A) -> (y : A) -> (p : Id A x y)
-> Id (Id B (f y) (f x)) (ap A B f y x (sym A x y p)) (sym B (f x) (f y) (ap A B f x y p))
:= \A B f x y p.
J A x (\b q. Id (Id B (f b) (f x)) (ap A B f b x (sym A x b q))
(sym B (f x) (f b) (ap A B f x b q)))
(refl (Id B (f x) (f x)) (refl B (f x))) y p
def transportRefl : (A : Type) -> (P : A -> Type) -> (x : A) -> (u : P x)
-> Id (P x) (transport A P x x (refl A x) u) u
:= \A P x u. refl (P x) u
def transportBackForth : (A : Type) -> (P : A -> Type) -> (x : A) -> (y : A)
-> (p : Id A x y) -> (u : P x)
-> Id (P x) (transportBack A P x y p (transport A P x y p u)) u
:= \A P x y p u.
J A x (\b q. Id (P x) (transportBack A P x b q (transport A P x b q u)) u)
(refl (P x) u) y p
-- ---------------------------------------------------------------------------
-- Dependent pairs and the contractibility of singletons
-- ---------------------------------------------------------------------------
def Singleton : (A : Type) -> A -> Type
:= \A x. (y : A) * Id A x y
def center : (A : Type) -> (x : A) -> Singleton A x
:= \A x. (x, refl A x)
-- Relies on eta for pairs: the result type mentions (fst s, snd s), which is
-- definitionally s.
def contraction : (A : Type) -> (x : A) -> (s : Singleton A x)
-> Id (Singleton A x) (center A x) s
:= \A x s. J A x (\b q. Id (Singleton A x) (center A x) (b, q))
(refl (Singleton A x) (center A x)) (fst s) (snd s)
def isContr : Type -> Type
:= \A. (c : A) * ((x : A) -> Id A c x)
def singletonIsContr : (A : Type) -> (x : A) -> isContr (Singleton A x)
:= \A x. (center A x, contraction A x)
def pairPath : (A : Type) -> (B : A -> Type) -> (x : A) -> (y : A) -> (u : B x) -> (p : Id A x y)
-> Id ((a : A) * B a) (x, u) (y, transport A B x y p u)
:= \A B x y u p.
J A x (\b q. Id ((a : A) * B a) (x, u) (b, transport A B x b q u))
(refl ((a : A) * B a) (x, u)) y p
-- ---------------------------------------------------------------------------
-- Propositions and sets
-- ---------------------------------------------------------------------------
def isProp : Type -> Type
:= \A. (x : A) -> (y : A) -> Id A x y
def isSet : Type -> Type
:= \A. (x : A) -> (y : A) -> (p : Id A x y) -> (q : Id A x y) -> Id (Id A x y) p q
def emptyIsProp : isProp Empty
:= \x y. absurd (Id Empty x y) x
-- ---------------------------------------------------------------------------
-- Arithmetic
-- ---------------------------------------------------------------------------
def pred : Nat -> Nat
:= \n. natElim (\k. Nat) zero (\k r. k) n
def add : Nat -> Nat -> Nat
:= \m n. natElim (\k. Nat) m (\k r. succ r) n
def mul : Nat -> Nat -> Nat
:= \m n. natElim (\k. Nat) zero (\k r. add m r) n
-- These two hold by computation, because add recurses on its second argument.
def addZeroRight : (n : Nat) -> Id Nat (add n zero) n
:= \n. refl Nat n
def addSuccRight : (m : Nat) -> (n : Nat) -> Id Nat (add m (succ n)) (succ (add m n))
:= \m n. refl Nat (succ (add m n))
-- These two need induction.
def addZeroLeft : (n : Nat) -> Id Nat (add zero n) n
:= \n. natElim (\k. Id Nat (add zero k) k)
(refl Nat zero)
(\k ih. ap Nat Nat (\t. succ t) (add zero k) k ih)
n
def addSuccLeft : (m : Nat) -> (n : Nat) -> Id Nat (add (succ m) n) (succ (add m n))
:= \m n. natElim (\k. Id Nat (add (succ m) k) (succ (add m k)))
(refl Nat (succ m))
(\k ih. ap Nat Nat (\t. succ t) (add (succ m) k) (succ (add m k)) ih)
n
def addAssoc : (m : Nat) -> (n : Nat) -> (k : Nat)
-> Id Nat (add (add m n) k) (add m (add n k))
:= \m n k. natElim (\j. Id Nat (add (add m n) j) (add m (add n j)))
(refl Nat (add m n))
(\j ih. ap Nat Nat (\t. succ t) (add (add m n) j) (add m (add n j)) ih)
k
def addComm : (m : Nat) -> (n : Nat) -> Id Nat (add m n) (add n m)
:= \m n. natElim (\k. Id Nat (add m k) (add k m))
(sym Nat (add zero m) m (addZeroLeft m))
(\k ih. trans Nat (add m (succ k)) (succ (add k m)) (add (succ k) m)
(ap Nat Nat (\t. succ t) (add m k) (add k m) ih)
(sym Nat (add (succ k) m) (succ (add k m)) (addSuccLeft k m)))
n
def succInjective : (m : Nat) -> (n : Nat) -> Id Nat (succ m) (succ n) -> Id Nat m n
:= \m n p. ap Nat Nat (\t. pred t) (succ m) (succ n) p
-- Large elimination: a family of types defined by recursion on a numeral,
-- which is what makes zero and succ provably distinct.
def Code : Nat -> Type
:= \n. natElim (\k. Type) Unit (\k c. Empty) n
def zeroNotSucc : (n : Nat) -> Not (Id Nat zero (succ n))
:= \n p. J Nat zero (\b q. Code b) tt (succ n) p
def succNotZero : (n : Nat) -> Not (Id Nat (succ n) zero)
:= \n p. zeroNotSucc n (sym Nat (succ n) zero p)
-- Closed arithmetic facts that the evaluator settles by reduction.
def twoPlusTwo : Id Nat (add 2 2) 4
:= refl Nat 4
def threeTimesFour : Id Nat (mul 3 4) 12
:= refl Nat 12
-- ---------------------------------------------------------------------------
-- An axiom, and something that depends on it
-- ---------------------------------------------------------------------------
axiom funext : (A : Type) -> (B : A -> Type)
-> (f : (x : A) -> B x) -> (g : (x : A) -> B x)
-> ((x : A) -> Id (B x) (f x) (g x))
-> Id ((x : A) -> B x) f g
def addZeroIsIdentity : Id (Nat -> Nat) (\n. add zero n) (\n. n)
:= funext Nat (\k. Nat) (\n. add zero n) (\n. n) (\n. addZeroLeft n)
The rule everything comes from
C : (b : A) -> Id A a b -> Type i
d : C a (refl A a)
---------------------------------------------
J A a C d b p : C b p for p : Id A a b
computation rule:
J A a C d a (refl A a) == d
To prove something about an arbitrary path out of a, it is enough to prove it for the constant path at a. Geometrically: the space of paths starting at a fixed point is contractible, so there is nowhere else to check. The "based paths out of a point are contractible" example above proves exactly that inside the system.