2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
12 (* kernel version: complete, relative, global *)
13 (* note : fragment of complete lambda-delta serving as abstract layer *)
17 type attrs = Entity.attrs
19 type bind = Abst of term list (* domains *)
20 | Abbr of term list (* bodies *)
21 | Void of int (* number of exclusions *)
23 and term = TSort of attrs * int (* attrs, hierarchy index *)
24 | TLRef of attrs * int * int (* attrs, position indexes *)
25 | TGRef of attrs * uri (* attrs, reference *)
26 | TCast of attrs * term * term (* attrs, domain, element *)
27 | TAppl of attrs * term list * term (* attrs, arguments, function *)
28 | TProj of attrs * lenv * term (* attrs, closure, member *)
29 | TBind of attrs * bind * term (* attrs, binder, scope *)
31 and lenv = ESort (* top *)
32 | EProj of lenv * attrs * lenv (* environment, attrs, closure *)
33 | EBind of lenv * attrs * bind (* environment, attrs, binder *)
35 type entity = term Entity.entity
37 (* helpers ******************************************************************)
39 let mk_uri si root s =
40 let kernel = if si then "crg-si" else "crg" in
41 String.concat "/" ["ld:"; kernel; root; s ^ ".ld"]
43 let empty_lenv = ESort
45 let push_bind f lenv a b = f (EBind (lenv, a, b))
47 let push_proj f lenv a e = f (EProj (lenv, a, e))
49 let push2 err f lenv attr ?t () = match lenv, t with
50 | EBind (e, a, Abst ws), Some t -> f (EBind (e, (attr :: a), Abst (t :: ws)))
51 | EBind (e, a, Abbr vs), Some t -> f (EBind (e, (attr :: a), Abbr (t :: vs)))
52 | EBind (e, a, Void n), None -> f (EBind (e, (attr :: a), Void (succ n)))
55 (* this id not tail recursive *)
56 let resolve_lref err f id lenv =
57 let rec aux f i k = function
59 | EBind (tl, _, Abst [])
60 | EBind (tl, _, Abbr [])
61 | EBind (tl, _, Void 0) -> aux f i k tl
63 let err kk = aux f (succ i) (k + kk) tl in
64 let f j = f i j (k + j) in
65 Entity.resolve err f id a
66 | EProj _ -> assert false (* TODO *)
70 let rec get_name err f i j = function
72 | EBind (tl, _, Abst [])
73 | EBind (tl, _, Abbr [])
74 | EBind (tl, _, Void 0) -> get_name err f i j tl
75 | EBind (_, a, _) when i = 0 ->
77 Entity.get_name err f j a
79 get_name err f (pred i) j tl
81 let err i = get_name err f i j tl in
84 let get_index err f i j lenv =
85 let rec aux f i k = function
87 | EBind (tl, _, Abst [])
88 | EBind (tl, _, Abbr [])
89 | EBind (tl, _, Void 0) -> aux f i k tl
90 | EBind (_, a, _) when i = 0 ->
91 if Entity.count_names a > j then f (k + j) else err i
93 aux f (pred i) (k + Entity.count_names a) tl
94 | EProj _ -> assert false (* TODO *)