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 empty_lenv = ESort
44 let push_bind f lenv a b = f (EBind (lenv, a, b))
46 let push_proj f lenv a e = f (EProj (lenv, a, e))
48 let push2 err f lenv ?attr ?t () = match lenv, attr, t with
49 | EBind (e, a, Abst (n, ws)), Some attr, Some t ->
50 f (EBind (e, (attr :: a), Abst (n, t :: ws)))
51 | EBind (e, a, Abst (n, ws)), None, Some t ->
52 f (EBind (e, a, Abst (n, t :: ws)))
53 | EBind (e, a, Abbr vs), Some attr, Some t ->
54 f (EBind (e, (attr :: a), Abbr (t :: vs)))
55 | EBind (e, a, Abbr vs), None, Some t ->
56 f (EBind (e, a, Abbr (t :: vs)))
57 | EBind (e, a, Void n), Some attr, None ->
58 f (EBind (e, (attr :: a), Void (succ n)))
59 | EBind (e, a, Void n), None, None ->
60 f (EBind (e, a, Void (succ n)))
63 (* this id not tail recursive *)
64 let resolve_lref err f id lenv =
65 let rec aux f i k = function
67 | EBind (tl, _, Abst (_, []))
68 | EBind (tl, _, Abbr [])
69 | EBind (tl, _, Void 0) -> aux f i k tl
71 let err kk = aux f (succ i) (k + kk) tl in
72 let f j = f i j (k + j) in
74 | EProj _ -> assert false (* TODO *)
78 let rec get_name err f i j = function
80 | EBind (tl, _, Abst (_, []))
81 | EBind (tl, _, Abbr [])
82 | EBind (tl, _, Void 0) -> get_name err f i j tl
83 | EBind (_, a, _) when i = 0 ->
87 get_name err f (pred i) j tl
89 let err i = get_name err f i j tl in
92 let get_index err f i j lenv =
93 let rec aux f i k = function
95 | EBind (tl, _, Abst (_, []))
96 | EBind (tl, _, Abbr [])
97 | EBind (tl, _, Void 0) -> aux f i k tl
98 | EBind (_, a, _) when i = 0 ->
99 if E.count_names a > j then f (k + j) else err i
100 | EBind (tl, a, _) ->
101 aux f (pred i) (k + E.count_names a) tl
102 | EProj _ -> assert false (* TODO *)
106 let rec names_of_lenv ns = function
108 | EBind (tl, a, _) -> names_of_lenv (E.rev_append_names ns a) tl
109 | EProj (tl, _, e) -> names_of_lenv (names_of_lenv ns e) tl
111 let rec get i = function
112 | ESort -> ESort, [], Void 0
113 | EBind (e, _, Abst (_, []))
114 | EBind (e, _, Abbr [])
115 | EBind (e, _, Void 0) -> get i e
116 | EBind (e, a, b) when i = 0 -> e, a, b
117 | EBind (e, _, _) -> get (pred i) e
118 | EProj _ -> assert false (* TODO *)
120 let get e i = get i e