]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/automath/autCrg.ml
- bug fix in the static analyzer allows better Pi/forall separation (exportation...
[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       let e = if !G.export || !G.manager <> G.Quiet then D.set_attrs C.start at e else e in
123       D.TProj (at, e, t)
124
125 (* this is not tail recursive in the GRef branch *)
126 let rec xlate_term f st lst y lenv = function
127    | A.Sort s            ->
128       let h = if s then 0 else 1 in
129       let a = E.node_attrs ~main:(h, 0) () in
130       f a (D.TSort (a, h))
131    | A.Appl (v, t)       ->
132       let f av vv at tt =
133          let at = E.compose av at in
134          f at (D.TAppl (at, !G.extended, vv, tt))
135       in
136       let f av vv = xlate_term (f av vv) st lst y lenv t in
137       xlate_term f st lst false lenv v
138    | A.Abst (name, w, t) ->
139       let f aw ww =
140          let aw = attrs_for_abst name aw in
141          let f at tt =
142             let at = E.compose aw at in
143             let l = 
144                if !G.cc then match y, snd at.E.n_main with
145                   | true, _ -> N.one
146                   | _   , 0 -> N.one
147                   | _   , 1 -> N.unknown st
148                   | _   , 2 -> N.two
149                   | _       -> assert false
150                else N.infinity
151             in
152             let b = D.Abst (false, l, ww) in
153             f at (D.TBind (at, b, tt))
154          in
155          let f lenv = xlate_term f st lst y lenv t in
156          push_abst f aw ww lenv
157       in
158       xlate_term f st lst true lenv w
159    | A.GRef (name, args) ->
160       let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in
161       let map2 f arg args = 
162          let f av v = f (D.EAppl (args, E.shift av, !G.extended, v)) in 
163          xlate_term f st lst false lenv arg
164       in
165       let g qid a cnt =
166          let gref = D.TGRef (a, uri_of_qid qid) in
167          if cnt = D.ESort then f a gref else
168          let f = function 
169             | D.EAppl (D.ESort, av, x, v) ->
170                let a = E.compose av a in
171                f a (D.TAppl (a, x, v, gref))
172             | args                        ->
173                let args = if !G.export || !G.manager <> G.Quiet then D.set_attrs C.start a args else args in
174                f a (D.TProj (a, args, gref))
175          in
176          let f args = C.list_fold_right f map2 args D.ESort in
177          D.sub_list_strict (D.fold_names f map1 args) cnt args
178       in
179       let g qid = resolve_gref_relaxed g lst qid in
180       let err () = complete_qid g lst name in
181       D.resolve_lref err (mk_lref f) (id_of_name name) lenv
182
183 let xlate_entity err f st lst = function
184    | A.Section (Some (_, name))     ->
185       err {lst with path = name :: lst.path; nodes = lst.node :: lst.nodes}
186    | A.Section None            ->
187       begin match lst.path, lst.nodes with
188          | _ :: ptl, nhd :: ntl -> 
189             err {lst with path = ptl; node = nhd; nodes = ntl}
190          | _                    -> assert false
191       end
192    | A.Context None            ->
193       err {lst with node = None}
194    | A.Context (Some name)     ->
195       let f name = err {lst with node = Some name} in
196       complete_qid f lst name 
197    | A.Block (name, w)         ->
198       let f qid = 
199          let f cnt =
200             let f aw ww = 
201                UH.add hcnt (uri_of_qid qid) (add_abst cnt name aw ww);
202                err {lst with node = Some qid}
203             in
204             xlate_term f st lst true cnt w
205          in
206          get_cnt_relaxed f lst
207       in
208       complete_qid f lst (name, true, [])
209    | A.Decl (name, w)          ->
210       let f lenv =
211          let f qid = 
212             let f aw ww =
213                let a = attrs_for_decl aw in
214                UH.add henv (uri_of_qid qid) (a, lenv);
215                let t = add_proj aw lenv ww in
216 (*
217             print_newline (); CrgOutput.pp_term print_string t;
218 *)
219                let na = {aw with E.n_apix = lst.line} in
220                let entity = E.empty_root, na, uri_of_qid qid, E.Abst t in
221                G.set_current_trace lst.line;
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 _ ww =
234                let f av vv =
235                   UH.add henv (uri_of_qid qid) (av, lenv);
236                   let t = add_proj av lenv (D.TCast (av, ww, vv)) in
237 (*
238             print_newline (); CrgOutput.pp_term print_string t;
239 *)
240                   let na = {av with E.n_apix = lst.line} in
241                   let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in
242                   let entity = ra, na, uri_of_qid qid, E.Abbr t in
243                   G.set_current_trace lst.line;
244                   f {lst with line = succ lst.line} entity
245                in
246                xlate_term f st lst false lenv v
247             in
248             xlate_term f st lst true lenv w
249          in
250          complete_qid f lst (name, true, [])
251       in
252       get_cnt_relaxed f lst
253
254 (* Interface functions ******************************************************)
255
256 let initial_status () =
257    UH.clear henv; UH.clear hcnt; {
258    path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
259 }
260
261 let refresh_status lst = initial_status ()
262
263 let crg_of_aut = xlate_entity