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_______________________________________________________________ *)
22 (* qualified identifier: uri, name, qualifiers *)
23 type qid = D.uri * D.id * D.id list
25 type context_node = qid option (* context node: None = root *)
28 path: D.id list; (* current section path *)
29 node: context_node; (* current context node *)
30 nodes: context_node list; (* context node list *)
31 line: int; (* line number *)
32 mk_uri: G.uri_generator; (* uri generator *)
35 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
37 let henv = UH.create henv_size (* optimized global environment *)
39 let hcnt = UH.create hcnt_size (* optimized context *)
41 (* Internal functions *******************************************************)
43 let empty_cnt = D.empty_lenv
46 if id.[0] >= '0' && id.[0] <= '9' then !G.alpha ^ id else id
48 let add_abst cnt id d w =
49 let id = if !G.alpha <> "" then alpha id else id in
50 let a = E.node_attrs ~name:(id, true) ~degr:(succ d) () in
51 D.EBind (cnt, a, D.Abst (N.two, w))
53 let mk_lref f a i = f a.E.n_degr (D.TLRef (E.empty_node, i))
55 let id_of_name (id, _, _) =
56 if !G.alpha <> "" then alpha id else id
58 let mk_qid f lst id path =
59 let str = String.concat "/" path in
60 let str = Filename.concat str id in
61 let str = lst.mk_uri str in
62 f (U.uri_of_string str, id, path)
64 let uri_of_qid (uri, _, _) = uri
66 let complete_qid f lst (id, is_local, qs) =
67 let f path = C.list_rev_append (mk_qid f lst id) path ~tail:qs in
68 let rec skip f = function
69 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
70 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
73 if is_local then f lst.path else skip f (lst.path, qs)
75 let relax_qid f lst (_, id, path) =
77 | _ :: tl -> C.list_rev (mk_qid f lst id) tl
82 let relax_opt_qid f lst = function
84 | Some qid -> let f qid = f (Some qid) in relax_qid f lst qid
86 let resolve_gref err f lst qid =
87 try let a, cnt = UH.find henv (uri_of_qid qid) in f qid a cnt
88 with Not_found -> err qid
90 let resolve_gref_relaxed f lst qid =
91 (* this is not tail recursive *)
92 let rec err qid = relax_qid (resolve_gref err f lst) lst qid in
93 resolve_gref err f lst qid
95 let get_cnt err f lst = function
98 try let cnt = UH.find hcnt (uri_of_qid qid) in f cnt
99 with Not_found -> err node
101 let get_cnt_relaxed f lst =
102 (* this is not tail recursive *)
103 let rec err node = relax_opt_qid (get_cnt err f lst) lst node in
104 get_cnt err f lst lst.node
106 let push_abst f a w lenv =
107 let bw = D.Abst (N.infinite, w) in
108 D.push_bind f a bw lenv
110 let add_proj e t = match e with
112 | D.EBind (D.ESort, a, b) -> D.TBind (a, b, t)
113 | _ -> D.TProj (E.empty_node, e, t)
115 (* this is not tail recursive in the GRef branch *)
116 let rec xlate_term f st lst y lenv = function
118 let f h = f 0 (D.TSort (E.empty_node, h)) in
119 if s then f 0 else f 1
121 let f vv d tt = f d (D.TAppl (E.empty_node, vv, tt)) in
122 let f _ vv = xlate_term (f vv) st lst y lenv t in
123 xlate_term f st lst false lenv v
124 | A.Abst (name, w, t) ->
125 let name = if !G.alpha <> "" then alpha name else name in
127 let a = E.node_attrs ~name:(name, true) () in
129 (* let a = {a with E.n_degr = dt} in *)
130 let l = if !G.cc then match y, dt with
133 | _ , 1 -> N.unknown st.S.lenv
138 let b = D.Abst (l, ww) in
139 f dt (D.TBind (a, b, tt))
141 let f lenv = xlate_term f st lst y lenv t in
142 push_abst f {a with E.n_degr = succ dw} ww lenv
144 xlate_term f st lst true lenv w
145 | A.GRef (name, args) ->
146 let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in
147 let map2 f arg args =
148 let f _ arg = f (D.EAppl (args, E.empty_node, arg)) in
149 xlate_term f st lst false lenv arg
152 let gref = D.TGRef (a, uri_of_qid qid) in
153 if cnt = D.ESort then f a.E.n_degr gref else
155 | D.EAppl (D.ESort, b, v) -> f a.E.n_degr (D.TAppl (b, v, gref))
156 | args -> f a.E.n_degr (D.TProj (E.empty_node, args, gref))
158 let f args = C.list_fold_right f map2 args D.ESort in
159 D.sub_list_strict (D.fold_names f map1 args) cnt args
161 let g qid = resolve_gref_relaxed g lst qid in
162 let err () = complete_qid g lst name in
163 D.resolve_lref err (mk_lref f) (id_of_name name) lenv
165 let xlate_entity err f st lst = function
166 | A.Section (Some (_, name)) ->
167 err {lst with path = name :: lst.path; nodes = lst.node :: lst.nodes}
169 begin match lst.path, lst.nodes with
170 | _ :: ptl, nhd :: ntl ->
171 err {lst with path = ptl; node = nhd; nodes = ntl}
175 err {lst with node = None}
176 | A.Context (Some name) ->
177 let f name = err {lst with node = Some name} in
178 complete_qid f lst name
179 | A.Block (name, w) ->
183 UH.add hcnt (uri_of_qid qid) (add_abst cnt name d ww);
184 err {lst with node = Some qid}
186 xlate_term f st lst true cnt w
188 get_cnt_relaxed f lst
190 complete_qid f lst (name, true, [])
191 | A.Decl (name, w) ->
195 let a = E.node_attrs ~apix:lst.line ~degr:(succ d) () in
196 UH.add henv (uri_of_qid qid) (a, lenv);
197 let t = add_proj lenv ww in
199 print_newline (); CrgOutput.pp_term print_string t;
202 let entity = E.empty_root, a, uri_of_qid qid, b in
203 f {lst with line = succ lst.line} entity
205 xlate_term f st lst true lenv w
207 complete_qid f lst (name, true, [])
209 get_cnt_relaxed (D.sta f) lst
210 | A.Def (name, w, trans, v) ->
215 let na = E.node_attrs ~apix:lst.line ~degr:d () in
216 UH.add henv (uri_of_qid qid) (na, lenv);
217 let t = add_proj lenv (D.TCast (E.empty_node, ww, vv)) in
219 print_newline (); CrgOutput.pp_term print_string t;
222 let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in
223 let entity = ra, na, uri_of_qid qid, b in
224 f {lst with line = succ lst.line} entity
226 xlate_term f st lst false lenv v
228 xlate_term f st lst true lenv w
230 complete_qid f lst (name, true, [])
232 get_cnt_relaxed f lst
234 (* Interface functions ******************************************************)
236 let initial_status () =
237 UH.clear henv; UH.clear hcnt; {
238 path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
241 let refresh_status lst = {lst with
242 mk_uri = G.get_mk_uri ()
245 let crg_of_aut = xlate_entity