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 *)
22 type bind = Abst of N.level * term list (* level, domains *)
23 | Abbr of term list (* bodies *)
24 | Void of int (* number of exclusions *)
26 and term = TSort of attrs * int (* attrs, hierarchy index *)
27 | TLRef of attrs * int * int (* attrs, position indexes *)
28 | TGRef of attrs * uri (* attrs, reference *)
29 | TCast of attrs * term * term (* attrs, domain, element *)
30 | TAppl of attrs * term list * term (* attrs, arguments, function *)
31 | TProj of attrs * lenv * term (* attrs, closure, member *)
32 | TBind of attrs * bind * term (* attrs, binder, scope *)
34 and lenv = ESort (* top *)
35 | EProj of lenv * attrs * lenv (* environment, attrs, closure *)
36 | EBind of lenv * attrs * bind (* environment, attrs, binder *)
38 type entity = term E.entity
40 (* helpers ******************************************************************)
42 let mk_uri si root s =
43 let kernel = if si then "crg-si" else "crg" in
44 String.concat "/" ["ld:"; kernel; root; s ^ ".ld"]
46 let empty_lenv = ESort
48 let push_bind f lenv a b = f (EBind (lenv, a, b))
50 let push_proj f lenv a e = f (EProj (lenv, a, e))
52 let push2 err f lenv attr ?t () = match lenv, t with
53 | EBind (e, a, Abst (n, ws)), Some t ->
54 f (EBind (e, (attr :: a), Abst (n, t :: ws)))
55 | EBind (e, a, Abbr vs), Some t ->
56 f (EBind (e, (attr :: a), Abbr (t :: vs)))
57 | EBind (e, a, Void n), None ->
58 f (EBind (e, (attr :: a), Void (succ n)))
61 (* this id not tail recursive *)
62 let resolve_lref err f id lenv =
63 let rec aux f i k = function
65 | EBind (tl, _, Abst (_, []))
66 | EBind (tl, _, Abbr [])
67 | EBind (tl, _, Void 0) -> aux f i k tl
69 let err kk = aux f (succ i) (k + kk) tl in
70 let f j = f i j (k + j) in
72 | EProj _ -> assert false (* TODO *)
76 let rec get_name err f i j = function
78 | EBind (tl, _, Abst (_, []))
79 | EBind (tl, _, Abbr [])
80 | EBind (tl, _, Void 0) -> get_name err f i j tl
81 | EBind (_, a, _) when i = 0 ->
85 get_name err f (pred i) j tl
87 let err i = get_name err f i j tl in
90 let get_index err f i j lenv =
91 let rec aux f i k = function
93 | EBind (tl, _, Abst (_, []))
94 | EBind (tl, _, Abbr [])
95 | EBind (tl, _, Void 0) -> aux f i k tl
96 | EBind (_, a, _) when i = 0 ->
97 if E.count_names a > j then f (k + j) else err i
99 aux f (pred i) (k + E.count_names a) tl
100 | EProj _ -> assert false (* TODO *)
104 let rec names_of_lenv ns = function
106 | EBind (tl, a, _) -> names_of_lenv (E.rev_append_names ns a) tl
107 | EProj (tl, _, e) -> names_of_lenv (names_of_lenv ns e) tl