Skip to content

Commit 08c7fde

Browse files
committed
OCaml Henk
1 parent 199e7bd commit 08c7fde

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

src/ocaml/henk.ml

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
(* Henk: CoC 1988 with infinite hierarchy of predicative universes *)
22

33
type term =
4-
| Var of string
5-
| Universe of int
6-
| Pi of string * term * term (* ∀ (x : a), b *)
7-
| Lam of string * term * term (* λ (x : a), b *)
8-
| App of term * term
4+
| Var of string
5+
| Universe of int
6+
| Pi of string * term * term (* ∀ (x : a), b *)
7+
| Lam of string * term * term (* λ (x : a), b *)
8+
| App of term * term
99

10-
type context = (string * term) list
10+
let rec string_of_term = function
11+
| Var x -> x
12+
| Universe u -> "U_" ^ string_of_int u
13+
| Pi (x, a, b) -> "∀ (" ^ x ^ " : " ^ string_of_term a ^ "), " ^ string_of_term b
14+
| Lam (x, a, t) -> "λ (" ^ x ^ " : " ^ string_of_term a ^ "), " ^ string_of_term t
15+
| App (t1, t2) -> "(" ^ string_of_term t1 ^ " " ^ string_of_term t2 ^ ")"
1116

17+
type context = (string * term) list
1218
exception TypeError of string
1319

20+
let universe = function
21+
| Universe i -> if i < 0 then raise (TypeError "Negative universe level"); i
22+
| ty -> raise (TypeError (Printf.sprintf "Expected a universe, got: %s" (string_of_term ty)))
23+
1424
let rec subst x s = function
1525
| Var y -> if x = y then s else Var y
1626
| Pi (y, a, b) when x <> y -> Pi (y, subst x s a, subst x s b)
@@ -53,18 +63,6 @@ and infer ctx t =
5363
| App (f, arg) -> match infer ctx f with | Pi (x, a, b) -> subst x arg b | ty -> raise (TypeError "Application requires a Pi type.")
5464
in normalize ctx res
5565

56-
let universe ty =
57-
match ty with
58-
| Universe i -> if i < 0 then raise (TypeError "Negative universe level"); i
59-
| ty -> raise (TypeError (Printf.sprintf "Expected a universe, got: %s" (string_of_term ty)))
60-
61-
let rec string_of_term = function
62-
| Var x -> x
63-
| Universe u -> "U_" ^ string_of_int u
64-
| Pi (x, a, b) -> "∀ (" ^ x ^ " : " ^ string_of_term a ^ "), " ^ string_of_term b
65-
| Lam (x, a, t) -> "λ (" ^ x ^ " : " ^ string_of_term a ^ "), " ^ string_of_term t
66-
| App (t1, t2) -> "(" ^ string_of_term t1 ^ " " ^ string_of_term t2 ^ ")"
67-
6866
(* Test Suite *)
6967

7068
let nat_type =

0 commit comments

Comments
 (0)