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 *)
23 type bind = Abst of N.level * term (* level, type *)
24 | Abbr of term (* body *)
27 and term = TSort of attrs * int (* attrs, hierarchy index *)
28 | TLRef of attrs * int (* attrs, position indexe *)
29 | TGRef of attrs * uri (* attrs, reference *)
30 | TCast of attrs * term * term (* attrs, domain, element *)
31 | TAppl of attrs * term * term (* attrs, argument, function *)
32 | TBind of attrs * bind * term (* attrs, binder, scope *)
33 | TProj of attrs * lenv * term (* attrs, closure, member *)
35 and lenv = ESort (* top *)
36 | EBind of lenv * attrs * bind (* environment, attrs, binder *)
37 | EAppl of lenv * attrs * term (* environment, attrs, argument *)
38 | EProj of lenv * attrs * lenv (* environment, attrs, closure *)
40 type entity = term E.entity
42 (* helpers ******************************************************************)
44 let empty_lenv = ESort
46 let push_bind f a b lenv = f (EBind (lenv, a, b))
48 let push_appl f a t lenv = f (EAppl (lenv, a, t))
50 let push_proj f a e lenv = f (EProj (lenv, a, e))
52 let add_bind f a b t = f (TBind (a, b, t))
54 let add_appl f a v t = f (TAppl (a, v, t))
56 let add_proj f a e t = f (TProj (a, e, t))
58 let rec shift f c t = match c with
60 | EBind (e, a, b) -> add_bind (shift f e) a b t
61 | EAppl (e, a, v) -> add_appl (shift f e) a v t
62 | EProj (e, a, d) -> add_proj (shift f e) a d t
64 let rec append f c = function
66 | EBind (e, a, b) -> append (push_bind f a b) c e
67 | EAppl (e, a, t) -> append (push_appl f a t) c e
68 | EProj (e, a, d) -> append (push_proj f a d) c e
70 let resolve_lref err f id lenv =
71 let rec aux i = function
73 | EAppl (tl, _, _) -> aux i tl
75 let f id0 _ = if id0 = id then f i else aux (succ i) tl in
77 | EProj (tl, _, d) -> append (aux i) tl d
81 let rec get_name err f i = function
83 | EAppl (tl, _, _) -> get_name err f i tl
84 | EBind (_, a, _) when i = 0 ->
87 | EBind (tl, _, _) -> get_name err f (pred i) tl
89 let err i = get_name err f i tl in
93 let push2 err f lenv ?attr ?t () = match lenv, attr, t with
94 | EBind (e, a, Abst (n, ws)), Some attr, Some t ->
95 f (EBind (e, (attr :: a), Abst (n, t :: ws)))
96 | EBind (e, a, Abst (n, ws)), None, Some t ->
97 f (EBind (e, a, Abst (n, t :: ws)))
98 | EBind (e, a, Abbr vs), Some attr, Some t ->
99 f (EBind (e, (attr :: a), Abbr (t :: vs)))
100 | EBind (e, a, Abbr vs), None, Some t ->
101 f (EBind (e, a, Abbr (t :: vs)))
102 | EBind (e, a, Void n), Some attr, None ->
103 f (EBind (e, (attr :: a), Void (succ n)))
104 | EBind (e, a, Void n), None, None ->
105 f (EBind (e, a, Void (succ n)))
108 let get_index err f i j lenv =
109 let rec aux f i k = function
111 | EBind (tl, _, Abst (_, []))
112 | EBind (tl, _, Abbr [])
113 | EBind (tl, _, Void 0) -> aux f i k tl
114 | EBind (_, a, _) when i = 0 ->
115 if E.count_names a > j then f (k + j) else err i
116 | EBind (tl, a, _) ->
117 aux f (pred i) (k + E.count_names a) tl
118 | EProj (tl, _, d) -> aux f i k (eshift C.start tl d)
122 let rec names_of_lenv ns = function
124 | EBind (tl, a, _) -> names_of_lenv (E.rev_append_names ns a) tl
125 | EAppl (tl, _, _) -> names_of_lenv ns tl
126 | EProj (tl, _, e) -> names_of_lenv (names_of_lenv ns e) tl
128 let rec get e i = match e with
129 | ESort -> ESort, [], Void
130 | EBind (e, a, b) when i = 0 -> e, a, b
131 | EBind (e, _, _) -> get e (pred i)
132 | EAppl (e, _, _) -> get e i
133 | EProj (e, _, d) -> get (append C.start e d) i
135 let rec sub_list_strict f e l = match e, l with
137 | EBind (e, _, Abst _), _ :: tl -> sub_list_strict f e tl
140 let rec fold_attrs f map a0 = function
142 | EBind (e, a, Abst _) -> fold_attrs f map (map a0 a) e