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_______________________________________________________________ *)
21 (* qualified identifier: uri, name, qualifiers *)
22 type qid = D.uri * D.id * D.id list
24 type context_node = qid option (* context node: None = root *)
27 path: D.id list; (* current section path *)
28 node: context_node; (* current context node *)
29 nodes: context_node list; (* context node list *)
30 line: int; (* line number *)
31 mk_uri: G.uri_generator; (* uri generator *)
34 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
36 let henv = UH.create henv_size (* optimized global environment *)
38 let hcnt = UH.create hcnt_size (* optimized context *)
40 (* Internal functions *******************************************************)
42 let empty_cnt = D.empty_lenv
45 if id.[0] >= '0' && id.[0] <= '9' then !G.alpha ^ id else id
47 let attrs_for_appl yv yt =
48 E.appl_attrs ~side:yv ~main:yt !G.restricted
50 let attrs_for_abst id yw =
51 let id = if !G.alpha <> "" then alpha id else id in
52 E.bind_attrs ~name:(id, true) ~side:yw ~main:(E.succ yw) ()
55 E.env_attrs ~side:y ()
57 let add_abst cnt id yw w =
58 let a = attrs_for_abst id yw in
59 let l = if !G.infinity then N.infinity else N.two in
60 D.EBind (cnt, E.empty_node, a, D.Abst (false, l, w))
62 let mk_lref f _ a i = f a.E.b_main (D.TLRef (E.empty_node, i))
64 let id_of_name (id, _, _) =
65 if !G.alpha <> "" then alpha id else id
67 let mk_qid f lst id path =
68 let str = String.concat "/" path in
69 let str = Filename.concat str id in
70 let str = lst.mk_uri str in
71 f (U.uri_of_string str, id, path)
73 let uri_of_qid (uri, _, _) = uri
75 let complete_qid f lst (id, is_local, qs) =
76 let f path = C.list_rev_append (mk_qid f lst id) path ~tail:qs in
77 let rec skip f = function
78 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
79 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
82 if is_local then f lst.path else skip f (lst.path, qs)
84 let relax_qid f lst (_, id, path) =
86 | _ :: tl -> C.list_rev (mk_qid f lst id) tl
91 let relax_opt_qid f lst = function
93 | Some qid -> let f qid = f (Some qid) in relax_qid f lst qid
95 let resolve_gref err f lst qid =
96 try let y, cnt = UH.find henv (uri_of_qid qid) in f qid y cnt
97 with Not_found -> err qid
99 let resolve_gref_relaxed f lst qid =
100 (* this is not tail recursive *)
101 let rec err qid = relax_qid (resolve_gref err f lst) lst qid in
102 resolve_gref err f lst qid
104 let get_cnt err f lst = function
105 | None -> f empty_cnt
106 | Some qid as node ->
107 try let cnt = UH.find hcnt (uri_of_qid qid) in f cnt
108 with Not_found -> err node
110 let get_cnt_relaxed f lst =
111 (* this is not tail recursive *)
112 let rec err node = relax_opt_qid (get_cnt err f lst) lst node in
113 get_cnt err f lst lst.node
115 let push_abst f a w lenv =
116 let bw = D.Abst (false, N.infinity, w) in
117 D.push_bind f E.empty_node a bw lenv
119 let rec set_name_y f = function
120 | D.ESort -> f D.ESort
121 | D.EBind (e, a, y, b) -> set_name_y (D.push_bind f a {y with E.b_name = Some ("Y", true)} b) e
122 | D.EAppl (e, a, v) -> set_name_y (D.push_appl f a v) e
123 | D.EProj (e, d) -> let f d = set_name_y (D.push_proj f d) e in set_name_y f d
125 let add_proj yt e t = match e with
127 | D.EBind (D.ESort, _, a, b) -> D.TBind (E.compose a yt, b, t)
129 D.TProj (D.set_attrs C.start yt e, t)
131 (* this is not tail recursive in the GRef branch *)
132 let rec xlate_term f st lst z lenv = function
134 let k = if s then 0 else 1 in
138 f yt (D.TAppl (attrs_for_appl yv yt, vv, tt))
140 let f yv vv = xlate_term (f yv vv) st lst z lenv t in
141 xlate_term f st lst false lenv v
142 | A.Abst (name, w, t) ->
144 let a = attrs_for_abst name yw in
147 if !G.cc then match z, snd yt with
150 | _ , 1 -> N.unknown st
155 let b = D.Abst (false, l, ww) in
156 (* let yt = {yt with E.b_name = Some ("P", true)} in *)
157 f yt (D.TBind (E.compose a yt, b, tt))
159 let f lenv = xlate_term f st lst z lenv t in
160 push_abst f a ww lenv
162 xlate_term f st lst true lenv w
163 | A.GRef (name, args) ->
164 let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in
165 let map2 y f arg args =
166 let f yv v = f (D.EAppl (args, attrs_for_appl yv y, v)) in
167 xlate_term f st lst false lenv arg
170 let gref = D.TGRef (E.empty_node, uri_of_qid qid) in
171 if cnt = D.ESort then f y gref else
173 | D.EAppl (D.ESort, a, v) -> f y (D.TAppl (a, v, gref))
174 | args -> f y (D.TProj (args, gref))
176 let f args = C.list_fold_right f (map2 y) args D.ESort in
177 D.sub_list_strict (D.fold_names f map1 args) cnt args
179 let g qid = resolve_gref_relaxed g lst qid in
180 let err () = complete_qid g lst name in
181 D.resolve_lref err (mk_lref f) (id_of_name name) lenv
183 let xlate_entity err f st lst = function
184 | A.Section (Some (_, name)) ->
185 err {lst with path = name :: lst.path; nodes = lst.node :: lst.nodes}
187 begin match lst.path, lst.nodes with
188 | _ :: ptl, nhd :: ntl ->
189 err {lst with path = ptl; node = nhd; nodes = ntl}
193 err {lst with node = None}
194 | A.Context (Some name) ->
195 let f name = err {lst with node = Some name} in
196 complete_qid f lst name
197 | A.Block (name, w) ->
201 UH.add hcnt (uri_of_qid qid) (add_abst cnt name yw ww);
202 err {lst with node = Some qid}
204 xlate_term f st lst true cnt w
206 get_cnt_relaxed f lst
208 complete_qid f lst (name, true, [])
209 | A.Decl (name, w) ->
213 let yv = E.succ yw in
214 let a = attrs_for_env yv in
215 UH.add henv (uri_of_qid qid) (yv, lenv);
216 let t = add_proj yw lenv ww in
217 let na = E.node_attrs ~apix:lst.line () in
218 let entity = E.empty_root, na, uri_of_qid qid, E.abst a t in
220 G.set_current_trace lst.line
222 f {lst with line = succ lst.line} entity
224 xlate_term f st lst true lenv w
226 complete_qid f lst (name, true, [])
228 let f = if !G.infinity then f else D.set_layer f N.one in
229 get_cnt_relaxed f lst
230 | A.Def (name, w, trans, v) ->
235 let a = attrs_for_env yv in
236 UH.add henv (uri_of_qid qid) (yv, lenv);
237 let t = if !G.cast then
238 let f e = D.TCast (add_proj yw e ww, add_proj yv lenv vv) in
239 if !G.infinity then f lenv else D.set_layer f N.one lenv
241 add_proj yv lenv (D.TCast (ww, vv))
243 let na = E.node_attrs ~apix:lst.line () in
244 let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in
245 let entity = ra, na, uri_of_qid qid, E.abbr a t in
247 G.set_current_trace lst.line
249 f {lst with line = succ lst.line} entity
251 xlate_term f st lst false lenv v
253 xlate_term f st lst true lenv w
255 complete_qid f lst (name, true, [])
257 get_cnt_relaxed f lst
259 (* Interface functions ******************************************************)
261 let initial_status () =
262 UH.clear henv; UH.clear hcnt; {
263 path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
266 let refresh_status lst = initial_status ()
268 let crg_of_aut = xlate_entity