]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/automath/autCrg.ml
we are optimizing the code by conditional compilation.
[helm.git] / helm / software / helena / src / automath / autCrg.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 module U  = NUri
13 module UH = U.UriHash
14 module C  = Cps
15 module G  = Options
16 module N  = Layer
17 module E  = Entity
18 module A  = Aut
19 module D  = Crg
20
21 (* qualified identifier: uri, name, qualifiers *)
22 type qid = D.uri * D.id * D.id list
23
24 type context_node = qid option (* context node: None = root *)
25
26 type status = {
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 *)
32 }
33
34 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
35
36 let henv = UH.create henv_size (* optimized global environment *)
37
38 let hcnt = UH.create hcnt_size (* optimized context *)
39
40 (* Internal functions *******************************************************)
41
42 let empty_cnt = D.empty_lenv
43
44 let alpha id =
45    if id.[0] >= '0' && id.[0] <= '9' then !G.alpha ^ id else id
46
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.n_main in
50    E.node_attrs ~name:(id, true) ~side:aw.E.n_main ~main ()
51
52 let attrs_for_decl aw =
53    let main = E.succ aw.E.n_main in
54    E.node_attrs ~side:aw.E.n_main ~main ()
55
56 let add_abst cnt id aw w =
57    let a = attrs_for_abst id aw in
58    let l = if !G.infinity then N.infinity else N.two in
59    D.EBind (cnt, a, D.Abst (false, l, w))
60
61 let mk_lref f a i = f a (D.TLRef (a, i))
62
63 let id_of_name (id, _, _) =
64    if !G.alpha <> "" then alpha id else id
65
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)
71
72 let uri_of_qid (uri, _, _) = uri
73
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)
79       | _                                     -> f []
80    in
81    if is_local then f lst.path else skip f (lst.path, qs)
82
83 let relax_qid f lst (_, id, path) =
84    let f = function
85       | _ :: tl -> C.list_rev (mk_qid f lst id) tl
86       | []      -> assert false
87    in
88    C.list_rev f path
89
90 let relax_opt_qid f lst = function
91    | None     -> f None
92    | Some qid -> let f qid = f (Some qid) in relax_qid f lst qid
93
94 let resolve_gref err f lst qid =
95    try let a, cnt = UH.find henv (uri_of_qid qid) in f qid a cnt
96    with Not_found -> err qid
97
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
102
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
108
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
113
114 let push_abst f a w lenv =
115    let bw = D.Abst (false, N.infinity, w) in
116    D.push_bind f a bw lenv
117
118 let add_proj at e t = match e with
119    | D.ESort                 -> t
120    | D.EBind (D.ESort, a, b) -> D.TBind (E.compose a at, b, t) 
121    | _                       ->
122 IFDEF MANAGER OR OBJS THEN
123       D.TProj (at, D.set_attrs C.start at e, t)
124 ELSE
125       D.TProj (at, e, t)
126 END
127
128 (* this is not tail recursive in the GRef branch *)
129 let rec xlate_term f st lst y lenv = function
130    | A.Sort s            ->
131       let h = if s then 0 else 1 in
132       let a = E.node_attrs ~main:(h, 0) () in
133       f a (D.TSort (a, h))
134    | A.Appl (v, t)       ->
135       let f av vv at tt =
136          let at = E.compose av at in
137          f at (D.TAppl (at, !G.extended, vv, tt))
138       in
139       let f av vv = xlate_term (f av vv) st lst y lenv t in
140       xlate_term f st lst false lenv v
141    | A.Abst (name, w, t) ->
142       let f aw ww =
143          let aw = attrs_for_abst name aw in
144          let f at tt =
145             let at = E.compose aw at in
146             let l = 
147                if !G.cc then match y, snd at.E.n_main with
148                   | true, _ -> N.one
149                   | _   , 0 -> N.one
150                   | _   , 1 -> N.unknown st
151                   | _   , 2 -> N.two
152                   | _       -> assert false
153                else N.infinity
154             in
155             let b = D.Abst (false, l, ww) in
156             f at (D.TBind (at, b, tt))
157          in
158          let f lenv = xlate_term f st lst y lenv t in
159          push_abst f aw ww lenv
160       in
161       xlate_term f st lst true lenv w
162    | A.GRef (name, args) ->
163       let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in
164       let map2 f arg args = 
165          let f av v = f (D.EAppl (args, E.shift av, !G.extended, v)) in 
166          xlate_term f st lst false lenv arg
167       in
168       let g qid a cnt =
169          let gref = D.TGRef (a, uri_of_qid qid) in
170          if cnt = D.ESort then f a gref else
171          let f = function 
172             | D.EAppl (D.ESort, av, x, v) ->
173                let a = E.compose av a in
174                f a (D.TAppl (a, x, v, gref))
175             | args                        ->
176 IFDEF MANAGER OR OBJS THEN
177                f a (D.TProj (a, D.set_attrs C.start a args, gref))
178 ELSE               
179                f a (D.TProj (a, args, gref))
180 END
181          in
182          let f args = C.list_fold_right f map2 args D.ESort in
183          D.sub_list_strict (D.fold_names f map1 args) cnt args
184       in
185       let g qid = resolve_gref_relaxed g lst qid in
186       let err () = complete_qid g lst name in
187       D.resolve_lref err (mk_lref f) (id_of_name name) lenv
188
189 let xlate_entity err f st lst = function
190    | A.Section (Some (_, name))     ->
191       err {lst with path = name :: lst.path; nodes = lst.node :: lst.nodes}
192    | A.Section None            ->
193       begin match lst.path, lst.nodes with
194          | _ :: ptl, nhd :: ntl -> 
195             err {lst with path = ptl; node = nhd; nodes = ntl}
196          | _                    -> assert false
197       end
198    | A.Context None            ->
199       err {lst with node = None}
200    | A.Context (Some name)     ->
201       let f name = err {lst with node = Some name} in
202       complete_qid f lst name 
203    | A.Block (name, w)         ->
204       let f qid = 
205          let f cnt =
206             let f aw ww = 
207                UH.add hcnt (uri_of_qid qid) (add_abst cnt name aw ww);
208                err {lst with node = Some qid}
209             in
210             xlate_term f st lst true cnt w
211          in
212          get_cnt_relaxed f lst
213       in
214       complete_qid f lst (name, true, [])
215    | A.Decl (name, w)          ->
216       let f lenv =
217          let f qid = 
218             let f aw ww =
219                let a = attrs_for_decl aw in
220                UH.add henv (uri_of_qid qid) (a, lenv);
221                let t = add_proj aw lenv ww in
222 (*
223             print_newline (); CrgOutput.pp_term print_string t;
224 *)
225                let na = {aw with E.n_apix = lst.line} in
226                let entity = E.empty_root, na, uri_of_qid qid, E.Abst t in
227                G.set_current_trace lst.line;
228                f {lst with line = succ lst.line} entity
229             in
230             xlate_term f st lst true lenv w
231          in
232          complete_qid f lst (name, true, [])
233       in
234       let f = if !G.infinity then f else D.set_layer f N.one in 
235       get_cnt_relaxed f lst
236    | A.Def (name, w, trans, v) ->
237       let f lenv =
238          let f qid = 
239             let f _ ww =
240                let f av vv =
241                   UH.add henv (uri_of_qid qid) (av, lenv);
242                   let t = add_proj av lenv (D.TCast (av, ww, vv)) in
243 (*
244             print_newline (); CrgOutput.pp_term print_string t;
245 *)
246                   let na = {av with E.n_apix = lst.line} in
247                   let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in
248                   let entity = ra, na, uri_of_qid qid, E.Abbr t in
249                   G.set_current_trace lst.line;
250                   f {lst with line = succ lst.line} entity
251                in
252                xlate_term f st lst false lenv v
253             in
254             xlate_term f st lst true lenv w
255          in
256          complete_qid f lst (name, true, [])
257       in
258       get_cnt_relaxed f lst
259
260 (* Interface functions ******************************************************)
261
262 let initial_status () =
263    UH.clear henv; UH.clear hcnt; {
264    path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
265 }
266
267 let refresh_status lst = initial_status ()
268
269 let crg_of_aut = xlate_entity