forked from andrejbauer/spartan-type-theory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.ml
54 lines (42 loc) · 1.56 KB
/
context.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(** Typing context and definitional equalities. *)
(** Each entry in the context is bound to an atom and a type. *)
type entry = TT.atom * TT.ty
(** Each definitiona equality maps an atom to an expression. *)
type definition = TT.atom * TT.expr
(** A typing context is a list of known identifiers and definitional equalities. *)
type context =
{
idents : entry list ;
defs : definition list
}
(** The initial, empty typing context. *)
let initial = { idents = [] ; defs = [] }
(** The list of names which should not be used for printing bound variables. *)
let penv {idents; _} = List.map (fun ((x, _), _) -> x) idents
(** Extend the context with an identifier. *)
let extend_ident a t ctx = { ctx with idents = (a, t) :: ctx.idents }
(** Extend the context with a definitional equality. *)
let extend_def a e ctx = { ctx with defs = (a, e) :: ctx.defs }
(** Lookup the type and value of the given de Bruijn index [k] *)
let lookup k {idents; _} =
let rec search m = function
| [] -> None
| et :: lst -> if m = 0 then Some et else search (m - 1) lst
in
search k idents
(** Lookup the type of the given atom [a]. *)
let lookup_atom_ty a {idents; _} =
try
Some (List.assoc a idents)
with
Not_found -> None
(** Lookup the definitional equality of the given atom [a]. *)
let lookup_def a {defs; _} =
try
let e = List.assoc a defs in
Some e
with Not_found -> None
let print_entry (a, ty) ppf =
Print.print ppf "%t : %t" (TT.print_atom a) (TT.print_ty ~penv:[] ty)
let print_context ctx ppf =
Print.sequence print_entry ", " ctx.idents ppf