]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/automath/autCrg.ml
5ed896306dfcf0558d6858ad499f57398b3f94f0
[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.b_main in
50    E.bind_attrs ~name:(id, true) ~side:aw.E.b_main ~main ()
51
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 ()
55
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))
60
61 let mk_lref f _ y i = f y (D.TLRef (E.empty_node, 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 y, cnt = UH.find henv (uri_of_qid qid) in f qid y 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 y w lenv =
115    let bw = D.Abst (false, N.infinity, w) in
116    D.push_bind f E.empty_node y bw lenv
117 (*
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
123 *)
124 let add_proj yt e t = match e with
125    | D.ESort                    -> t
126    | D.EBind (D.ESort, _, y, b) -> D.TBind (E.compose y yt, b, t) 
127    | e                          ->
128       D.TProj (D.set_attrs C.start yt e, t)
129
130 (* this is not tail recursive in the GRef branch *)
131 let rec xlate_term f st lst z lenv = function
132    | A.Sort s            ->
133       let k = if s then 0 else 1 in
134       let y = E.bind_attrs ~main:(k, 0) () in
135       f y (D.TSort k)
136    | A.Appl (v, t)       ->
137       let f vv yt tt =
138          f yt (D.TAppl (!G.extended, vv, tt))
139       in
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) ->
143       let f yw ww =
144          let yw = attrs_for_abst name yw in
145          let f yt tt =
146             let yt = E.compose yw yt in
147             let l = 
148                if !G.cc then match z, snd yt.E.b_main with
149                   | true, _ -> N.one
150                   | _   , 0 -> N.one
151                   | _   , 1 -> N.unknown st
152                   | _   , 2 -> N.two
153                   | _       -> assert false
154                else N.infinity
155             in
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))
159          in
160          let f lenv = xlate_term f st lst z lenv t in
161          push_abst f yw ww lenv
162       in
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
169       in
170       let g qid y cnt =
171          let gref = D.TGRef (E.empty_node, uri_of_qid qid) in
172          if cnt = D.ESort then f y gref else
173          let f = function 
174             | D.EAppl (D.ESort, x, v) -> f y (D.TAppl (x, v, gref))
175             | args                    -> f y (D.TProj (args, gref))
176          in
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
179       in
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
183
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}
187    | A.Section None            ->
188       begin match lst.path, lst.nodes with
189          | _ :: ptl, nhd :: ntl -> 
190             err {lst with path = ptl; node = nhd; nodes = ntl}
191          | _                    -> assert false
192       end
193    | A.Context None            ->
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)         ->
199       let f qid = 
200          let f cnt =
201             let f aw ww = 
202                UH.add hcnt (uri_of_qid qid) (add_abst cnt name aw ww);
203                err {lst with node = Some qid}
204             in
205             xlate_term f st lst true cnt w
206          in
207          get_cnt_relaxed f lst
208       in
209       complete_qid f lst (name, true, [])
210    | A.Decl (name, w)          ->
211       let f lenv =
212          let f qid = 
213             let f yw ww =
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
219 IFDEF TRACE THEN
220                G.set_current_trace lst.line
221 ELSE () END;
222                f {lst with line = succ lst.line} entity
223             in
224             xlate_term f st lst true lenv w
225          in
226          complete_qid f lst (name, true, [])
227       in
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) ->
231       let f lenv =
232          let f qid = 
233             let f yw ww =
234                let f yv vv =
235                   UH.add henv (uri_of_qid qid) (yv, lenv);
236                   let t = add_proj yv lenv (D.TCast (ww, vv)) in
237 (*
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
240 *)
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
244 IFDEF TRACE THEN
245                   G.set_current_trace lst.line
246 ELSE () END;
247                   f {lst with line = succ lst.line} entity
248                in
249                xlate_term f st lst false lenv v
250             in
251             xlate_term f st lst true lenv w
252          in
253          complete_qid f lst (name, true, [])
254       in
255       get_cnt_relaxed f lst
256
257 (* Interface functions ******************************************************)
258
259 let initial_status () =
260    UH.clear henv; UH.clear hcnt; {
261    path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
262 }
263
264 let refresh_status lst = initial_status ()
265
266 let crg_of_aut = xlate_entity