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 list (* level, domains *)
24 | Abbr of term list (* bodies *)
25 | Void of int (* number of exclusions *)
27 and term = TSort of attrs * int (* attrs, hierarchy index *)
28 | TLRef of attrs * int * int (* attrs, position indexes *)
29 | TGRef of attrs * uri (* attrs, reference *)
30 | TCast of attrs * term * term (* attrs, domain, element *)
31 | TAppl of attrs * term list * term (* attrs, arguments, function *)
32 | TProj of attrs * lenv * term (* attrs, closure, member *)
33 | TBind of attrs * bind * term (* attrs, binder, scope *)
35 and lenv = ESort (* top *)
36 | EProj of lenv * attrs * lenv (* environment, attrs, closure *)
37 | EBind of lenv * attrs * bind (* environment, attrs, binder *)
39 type entity = term E.entity
41 (* helpers ******************************************************************)
43 let rec tshift t = function
45 | EBind (e, a, b) -> tshift (TBind (a, b, t)) e
46 | EProj (e, a, d) -> tshift (TProj (a, d, t)) e
48 let tshift c t = tshift t c
50 let rec eshift f c = function
52 | EBind (e, a, b) -> let f ee = f (EBind (ee, a, b)) in eshift f c e
53 | EProj (e, a, d) -> let f ee = f (EProj (ee, a, d)) in eshift f c e
55 let empty_lenv = ESort
57 let push_bind f lenv a b = f (EBind (lenv, a, b))
59 let push_proj f lenv a e = f (EProj (lenv, a, e))
61 let push2 err f lenv ?attr ?t () = match lenv, attr, t with
62 | EBind (e, a, Abst (n, ws)), Some attr, Some t ->
63 f (EBind (e, (attr :: a), Abst (n, t :: ws)))
64 | EBind (e, a, Abst (n, ws)), None, Some t ->
65 f (EBind (e, a, Abst (n, t :: ws)))
66 | EBind (e, a, Abbr vs), Some attr, Some t ->
67 f (EBind (e, (attr :: a), Abbr (t :: vs)))
68 | EBind (e, a, Abbr vs), None, Some t ->
69 f (EBind (e, a, Abbr (t :: vs)))
70 | EBind (e, a, Void n), Some attr, None ->
71 f (EBind (e, (attr :: a), Void (succ n)))
72 | EBind (e, a, Void n), None, None ->
73 f (EBind (e, a, Void (succ n)))
76 (* this id not tail recursive *)
77 let resolve_lref err f id lenv =
78 let rec aux f i k = function
80 | EBind (tl, _, Abst (_, []))
81 | EBind (tl, _, Abbr [])
82 | EBind (tl, _, Void 0) -> aux f i k tl
84 let err kk = aux f (succ i) (k + kk) tl in
85 let f j = f i j (k + j) in
87 | EProj (tl, _, d) -> aux f i k (eshift C.start tl d)
91 let rec get_name err f i j = function
93 | EBind (tl, _, Abst (_, []))
94 | EBind (tl, _, Abbr [])
95 | EBind (tl, _, Void 0) -> get_name err f i j tl
96 | EBind (_, a, _) when i = 0 ->
100 get_name err f (pred i) j tl
101 | EProj (tl, _, e) ->
102 let err i = get_name err f i j tl in
105 let get_index err f i j lenv =
106 let rec aux f i k = function
108 | EBind (tl, _, Abst (_, []))
109 | EBind (tl, _, Abbr [])
110 | EBind (tl, _, Void 0) -> aux f i k tl
111 | EBind (_, a, _) when i = 0 ->
112 if E.count_names a > j then f (k + j) else err i
113 | EBind (tl, a, _) ->
114 aux f (pred i) (k + E.count_names a) tl
115 | EProj (tl, _, d) -> aux f i k (eshift C.start tl d)
119 let rec names_of_lenv ns = function
121 | EBind (tl, a, _) -> names_of_lenv (E.rev_append_names ns a) tl
122 | EProj (tl, _, e) -> names_of_lenv (names_of_lenv ns e) tl
124 let rec get i = function
125 | ESort -> ESort, [], Void 0
126 | EBind (e, _, Abst (_, []))
127 | EBind (e, _, Abbr [])
128 | EBind (e, _, Void 0) -> get i e
129 | EBind (e, a, b) when i = 0 -> e, a, b
130 | EBind (e, _, _) -> get (pred i) e
131 | EProj (e, _, d) -> get i (eshift C.start e d)
133 let get e i = get i e