This repository shows how to implement a minimalist type theory of the kind that is called "spartan" by some people. The implementation was presented at the School and Workshop on Univalent Mathematics which took place at the University of Birmingham in December 2017. The video recording of the lecture How to implement type theory in an hour is now available.
The dependent type theory spartan has the following ingredients:
- A universe
TypewithType : Type. - Dependent products written as
forall (x : T₁), T₂or∀ (x : T₁), T₂or∏ (x : T₁), T₂. - Dependent sums written as
exists (x : T₁), T₂orΣ (x : T₁), T₂. - Functions written as
fun (x : T) => eorλ (x : T) ⇒ e. The typing annotation may be omitted. - Application written as
e₁ e₂. - Pairs written as
(e₁, e₂) - First projection written as
fst e. - Second projection written as
snd e. - Type ascription written as
e : T. - Natural numbers type
nat : Typewithzeroandsucc n. - Eliminator for the natural numbers used as
match expr with zero -> e₁ | succ m -> e₂.
Top-level commands:
Definition x := e.-- define a valueAxiom x : T.-- assume a constantxof typeTCheck e.-- print the type ofeEval e.-- evaluateea la call-by-valueLoad "⟨file⟩".-- load a file
-
The OPAM packages
menhirandsedlex:opam install menhir opam install sedlex -
It is recommended that you also install the
rlwraporleditcommand line wrapper.
You can type:
maketo make thespartan.nativeexecutable.make byteto make the bytecodespartan.byteexecutable.make cleanto clean up.make docto generate HTML documentation (see the generatedspartan.docdir/index.html).
The purpose of the implementation is to keep the source uncomplicated and short. The essential bits of source code can be found in the following files. It should be possible for you to just read the entire source code. You should start with the core:
syntax.ml-- abstract syntax of the inputcontext.ml-- contextsequal.ml-- normalizationtypecheck.ml-- type checking and conversion from abstract syntax to core type theoryTT.ml-- the core type theory
and continue with the infrastructure
spartan.ml-- interactive top levelprint.ml-- printing and message supportdesugar.ml-- conversion from parsed syntax to abstract syntaxlexer.mlandparser.mly-- parsing into concrete syntax
There are many things you can try, for example try adding dependent sums, or basic types
unit, bool and nat.