(* ||M|| This file is part of HELM, an Hypertextual, Electronic ||A|| Library of Mathematics, developed at the Computer Science ||T|| Department, University of Bologna, Italy. ||I|| ||T|| HELM is free software; you can redistribute it and/or ||A|| modify it under the terms of the GNU General Public License \ / version 2 or (at your option) any later version. \ / This software is distributed as is, NO WARRANTY. V_______________________________________________________________ *) module U = NUri module UH = U.UriHash module C = Cps module G = Options module N = Layer module E = Entity module A = Aut module D = Crg (* qualified identifier: uri, name, qualifiers *) type qid = D.uri * D.id * D.id list type context_node = qid option (* context node: None = root *) type status = { path: D.id list; (* current section path *) node: context_node; (* current context node *) nodes: context_node list; (* context node list *) line: int; (* line number *) mk_uri: G.uri_generator; (* uri generator *) } let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *) let henv = UH.create henv_size (* optimized global environment *) let hcnt = UH.create hcnt_size (* optimized context *) (* Internal functions *******************************************************) let empty_cnt = D.empty_lenv let alpha id = if id.[0] >= '0' && id.[0] <= '9' then !G.alpha ^ id else id let attrs_for_appl yv yt = E.appl_attrs ~side:yv ~main:yt !G.restricted let attrs_for_abst id yw = let id = if !G.alpha <> "" then alpha id else id in E.bind_attrs ~name:(id, true) ~side:yw ~main:(E.succ yw) () let attrs_for_env y = E.env_attrs ~side:y () let add_abst cnt id yw w = let a = attrs_for_abst id yw in let l = if !G.infinity then N.infinity else N.two in D.EBind (cnt, E.empty_node, a, D.Abst (false, l, w)) let mk_lref f _ a i = f a.E.b_main (D.TLRef (E.empty_node, i)) let id_of_name (id, _, _) = if !G.alpha <> "" then alpha id else id let mk_qid f lst id path = let str = String.concat "/" path in let str = Filename.concat str id in let str = lst.mk_uri str in f (U.uri_of_string str, id, path) let uri_of_qid (uri, _, _) = uri let complete_qid f lst (id, is_local, qs) = let f path = C.list_rev_append (mk_qid f lst id) path ~tail:qs in let rec skip f = function | phd :: ptl, qshd :: _ when phd = qshd -> f ptl | _ :: ptl, _ :: _ -> skip f (ptl, qs) | _ -> f [] in if is_local then f lst.path else skip f (lst.path, qs) let relax_qid f lst (_, id, path) = let f = function | _ :: tl -> C.list_rev (mk_qid f lst id) tl | [] -> assert false in C.list_rev f path let relax_opt_qid f lst = function | None -> f None | Some qid -> let f qid = f (Some qid) in relax_qid f lst qid let resolve_gref err f lst qid = try let y, cnt = UH.find henv (uri_of_qid qid) in f qid y cnt with Not_found -> err qid let resolve_gref_relaxed f lst qid = (* this is not tail recursive *) let rec err qid = relax_qid (resolve_gref err f lst) lst qid in resolve_gref err f lst qid let get_cnt err f lst = function | None -> f empty_cnt | Some qid as node -> try let cnt = UH.find hcnt (uri_of_qid qid) in f cnt with Not_found -> err node let get_cnt_relaxed f lst = (* this is not tail recursive *) let rec err node = relax_opt_qid (get_cnt err f lst) lst node in get_cnt err f lst lst.node let push_abst f a w lenv = let bw = D.Abst (false, N.infinity, w) in D.push_bind f E.empty_node a bw lenv (* let rec set_name_y f = function | D.ESort -> f D.ESort | D.EBind (e, a, y, b) -> set_name_y (D.push_bind f a {y with E.b_name = Some ("Y", true)} b) e | D.EAppl (e, a, v) -> set_name_y (D.push_appl f a v) e | D.EProj (e, d) -> let f d = set_name_y (D.push_proj f d) e in set_name_y f d *) let add_proj yt e t = match e with | D.ESort -> t | D.EBind (D.ESort, _, a, b) -> D.TBind (E.compose a yt, b, t) | e -> D.TProj (D.set_attrs C.start yt e, t) (* this is not tail recursive in the GRef branch *) let rec xlate_term f st lst z lenv = function | A.Sort s -> let k = if s then 0 else 1 in f (k, 0) (D.TSort k) | A.Appl (v, t) -> let f yv vv yt tt = f yt (D.TAppl (attrs_for_appl yv yt, vv, tt)) in let f yv vv = xlate_term (f yv vv) st lst z lenv t in xlate_term f st lst false lenv v | A.Abst (name, w, t) -> let f yw ww = let a = attrs_for_abst name yw in let f yt tt = let l = if !G.cc then match z, snd yt with | true, _ -> N.one | _ , 0 -> N.one | _ , 1 -> N.unknown st | _ , 2 -> N.two | _ -> assert false else N.infinity in let b = D.Abst (false, l, ww) in (* let yt = {yt with E.b_name = Some ("P", true)} in *) f yt (D.TBind (E.compose a yt, b, tt)) in let f lenv = xlate_term f st lst z lenv t in push_abst f a ww lenv in xlate_term f st lst true lenv w | A.GRef (name, args) -> let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in let map2 y f arg args = let f yv v = f (D.EAppl (args, attrs_for_appl yv y, v)) in xlate_term f st lst false lenv arg in let g qid y cnt = let gref = D.TGRef (E.empty_node, uri_of_qid qid) in if cnt = D.ESort then f y gref else let f = function | D.EAppl (D.ESort, a, v) -> f y (D.TAppl (a, v, gref)) | args -> f y (D.TProj (args, gref)) in let f args = C.list_fold_right f (map2 y) args D.ESort in D.sub_list_strict (D.fold_names f map1 args) cnt args in let g qid = resolve_gref_relaxed g lst qid in let err () = complete_qid g lst name in D.resolve_lref err (mk_lref f) (id_of_name name) lenv let xlate_entity err f st lst = function | A.Section (Some (_, name)) -> err {lst with path = name :: lst.path; nodes = lst.node :: lst.nodes} | A.Section None -> begin match lst.path, lst.nodes with | _ :: ptl, nhd :: ntl -> err {lst with path = ptl; node = nhd; nodes = ntl} | _ -> assert false end | A.Context None -> err {lst with node = None} | A.Context (Some name) -> let f name = err {lst with node = Some name} in complete_qid f lst name | A.Block (name, w) -> let f qid = let f cnt = let f yw ww = UH.add hcnt (uri_of_qid qid) (add_abst cnt name yw ww); err {lst with node = Some qid} in xlate_term f st lst true cnt w in get_cnt_relaxed f lst in complete_qid f lst (name, true, []) | A.Decl (name, w) -> let f lenv = let f qid = let f yw ww = let yv = E.succ yw in let a = attrs_for_env yv in UH.add henv (uri_of_qid qid) (yv, lenv); let t = add_proj yw lenv ww in let na = E.node_attrs ~apix:lst.line () in let entity = E.empty_root, na, uri_of_qid qid, E.abst a t in IFDEF TRACE THEN G.set_current_trace lst.line ELSE () END; f {lst with line = succ lst.line} entity in xlate_term f st lst true lenv w in complete_qid f lst (name, true, []) in let f = if !G.infinity then f else D.set_layer f N.one in get_cnt_relaxed f lst | A.Def (name, w, trans, v) -> let f lenv = let f qid = let f yw ww = let f yv vv = let a = attrs_for_env yv in UH.add henv (uri_of_qid qid) (yv, lenv); let t = if !G.cast then let f e = D.TCast (add_proj yw e ww, add_proj yv lenv vv) in if !G.infinity then f lenv else D.set_layer f N.one lenv else add_proj yv lenv (D.TCast (ww, vv)) in let na = E.node_attrs ~apix:lst.line () in let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in let entity = ra, na, uri_of_qid qid, E.abbr a t in IFDEF TRACE THEN G.set_current_trace lst.line ELSE () END; f {lst with line = succ lst.line} entity in xlate_term f st lst false lenv v in xlate_term f st lst true lenv w in complete_qid f lst (name, true, []) in get_cnt_relaxed f lst (* Interface functions ******************************************************) let initial_status () = UH.clear henv; UH.clear hcnt; { path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri (); } let refresh_status lst = initial_status () let crg_of_aut = xlate_entity