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_abst id aw =
48 let id = if !G.alpha <> "" then alpha id else id in
49 let main = E.succ aw.E.b_main in
50 E.bind_attrs ~name:(id, true) ~side:aw.E.b_main ~main ()
52 let attrs_for_decl aw =
53 let main = E.succ aw.E.b_main in
54 E.bind_attrs ~side:aw.E.b_main ~main ()
56 let add_abst cnt id aw w =
57 let y = attrs_for_abst id aw in
58 let l = if !G.infinity then N.infinity else N.two in
59 D.EBind (cnt, E.empty_node, y, D.Abst (false, l, w))
61 let mk_lref f _ y i = f y (D.TLRef (E.empty_node, i))
63 let id_of_name (id, _, _) =
64 if !G.alpha <> "" then alpha id else id
66 let mk_qid f lst id path =
67 let str = String.concat "/" path in
68 let str = Filename.concat str id in
69 let str = lst.mk_uri str in
70 f (U.uri_of_string str, id, path)
72 let uri_of_qid (uri, _, _) = uri
74 let complete_qid f lst (id, is_local, qs) =
75 let f path = C.list_rev_append (mk_qid f lst id) path ~tail:qs in
76 let rec skip f = function
77 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
78 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
81 if is_local then f lst.path else skip f (lst.path, qs)
83 let relax_qid f lst (_, id, path) =
85 | _ :: tl -> C.list_rev (mk_qid f lst id) tl
90 let relax_opt_qid f lst = function
92 | Some qid -> let f qid = f (Some qid) in relax_qid f lst qid
94 let resolve_gref err f lst qid =
95 try let y, cnt = UH.find henv (uri_of_qid qid) in f qid y cnt
96 with Not_found -> err qid
98 let resolve_gref_relaxed f lst qid =
99 (* this is not tail recursive *)
100 let rec err qid = relax_qid (resolve_gref err f lst) lst qid in
101 resolve_gref err f lst qid
103 let get_cnt err f lst = function
104 | None -> f empty_cnt
105 | Some qid as node ->
106 try let cnt = UH.find hcnt (uri_of_qid qid) in f cnt
107 with Not_found -> err node
109 let get_cnt_relaxed f lst =
110 (* this is not tail recursive *)
111 let rec err node = relax_opt_qid (get_cnt err f lst) lst node in
112 get_cnt err f lst lst.node
114 let push_abst f y w lenv =
115 let bw = D.Abst (false, N.infinity, w) in
116 D.push_bind f E.empty_node y bw lenv
118 let rec set_name_y f = function
119 | D.ESort -> f D.ESort
120 | D.EBind (e, a, y, b) -> set_name_y (D.push_bind f a {y with E.b_name = Some ("Y", true)} b) e
121 | D.EAppl (e, x, v) -> set_name_y (D.push_appl f x v) e
122 | D.EProj (e, d) -> let f d = set_name_y (D.push_proj f d) e in set_name_y f d
124 let add_proj yt e t = match e with
126 | D.EBind (D.ESort, _, y, b) -> D.TBind (E.compose y yt, b, t)
128 D.TProj (D.set_attrs C.start yt e, t)
130 (* this is not tail recursive in the GRef branch *)
131 let rec xlate_term f st lst z lenv = function
133 let k = if s then 0 else 1 in
134 let y = E.bind_attrs ~main:(k, 0) () in
138 f yt (D.TAppl (!G.extended, vv, tt))
140 let f _ vv = xlate_term (f vv) st lst z lenv t in
141 xlate_term f st lst false lenv v
142 | A.Abst (name, w, t) ->
144 let yw = attrs_for_abst name yw in
146 let yt = E.compose yw yt in
148 if !G.cc then match z, snd yt.E.b_main with
151 | _ , 1 -> N.unknown st
156 let b = D.Abst (false, l, ww) in
157 (* let yt = {yt with E.b_name = Some ("P", true)} in *)
158 f yt (D.TBind (yt, b, tt))
160 let f lenv = xlate_term f st lst z lenv t in
161 push_abst f yw ww lenv
163 xlate_term f st lst true lenv w
164 | A.GRef (name, args) ->
165 let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in
166 let map2 f arg args =
167 let f _ v = f (D.EAppl (args, !G.extended, v)) in
168 xlate_term f st lst false lenv arg
171 let gref = D.TGRef (E.empty_node, uri_of_qid qid) in
172 if cnt = D.ESort then f y gref else
174 | D.EAppl (D.ESort, x, v) -> f y (D.TAppl (x, v, gref))
175 | args -> f y (D.TProj (args, gref))
177 let f args = C.list_fold_right f map2 args D.ESort in
178 D.sub_list_strict (D.fold_names f map1 args) cnt args
180 let g qid = resolve_gref_relaxed g lst qid in
181 let err () = complete_qid g lst name in
182 D.resolve_lref err (mk_lref f) (id_of_name name) lenv
184 let xlate_entity err f st lst = function
185 | A.Section (Some (_, name)) ->
186 err {lst with path = name :: lst.path; nodes = lst.node :: lst.nodes}
188 begin match lst.path, lst.nodes with
189 | _ :: ptl, nhd :: ntl ->
190 err {lst with path = ptl; node = nhd; nodes = ntl}
194 err {lst with node = None}
195 | A.Context (Some name) ->
196 let f name = err {lst with node = Some name} in
197 complete_qid f lst name
198 | A.Block (name, w) ->
202 UH.add hcnt (uri_of_qid qid) (add_abst cnt name aw ww);
203 err {lst with node = Some qid}
205 xlate_term f st lst true cnt w
207 get_cnt_relaxed f lst
209 complete_qid f lst (name, true, [])
210 | A.Decl (name, w) ->
214 let y = attrs_for_decl yw in
215 UH.add henv (uri_of_qid qid) (y, 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 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 UH.add henv (uri_of_qid qid) (yv, lenv);
236 let t = add_proj yv lenv (D.TCast (ww, vv)) in
238 let lenv0 = D.set_layer C.start N.one lenv in
239 let t = D.TCast (add_proj yw lenv0 ww, add_proj yv lenv vv) in
241 let na = E.node_attrs ~apix:lst.line () in
242 let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in
243 let entity = ra, na, uri_of_qid qid, E.Abbr t in
245 G.set_current_trace lst.line
247 f {lst with line = succ lst.line} entity
249 xlate_term f st lst false lenv v
251 xlate_term f st lst true lenv w
253 complete_qid f lst (name, true, [])
255 get_cnt_relaxed f lst
257 (* Interface functions ******************************************************)
259 let initial_status () =
260 UH.clear henv; UH.clear hcnt; {
261 path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
264 let refresh_status lst = initial_status ()
266 let crg_of_aut = xlate_entity