]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/automath/autCrg.ml
new options activated
[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 add_abst cnt id aw w =
48    let id = if !G.alpha <> "" then alpha id else id in
49    let aw = {aw with E.n_name = Some (id, true); E.n_degr = succ aw.E.n_degr} in 
50    D.EBind (cnt, aw, D.Abst (false, N.two, w))
51
52 let mk_lref f a i = f a (D.TLRef (a, i))
53
54 let id_of_name (id, _, _) =
55    if !G.alpha <> "" then alpha id else id
56
57 let mk_qid f lst id path =
58    let str = String.concat "/" path in
59    let str = Filename.concat str id in 
60    let str = lst.mk_uri str in
61    f (U.uri_of_string str, id, path)
62
63 let uri_of_qid (uri, _, _) = uri
64
65 let complete_qid f lst (id, is_local, qs) =
66    let f path = C.list_rev_append (mk_qid f lst id) path ~tail:qs in
67    let rec skip f = function
68       | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
69       | _ :: ptl, _ :: _                      -> skip f (ptl, qs)
70       | _                                     -> f []
71    in
72    if is_local then f lst.path else skip f (lst.path, qs)
73
74 let relax_qid f lst (_, id, path) =
75    let f = function
76       | _ :: tl -> C.list_rev (mk_qid f lst id) tl
77       | []      -> assert false
78    in
79    C.list_rev f path
80
81 let relax_opt_qid f lst = function
82    | None     -> f None
83    | Some qid -> let f qid = f (Some qid) in relax_qid f lst qid
84
85 let resolve_gref err f lst qid =
86    try let a, cnt = UH.find henv (uri_of_qid qid) in f qid a cnt
87    with Not_found -> err qid
88
89 let resolve_gref_relaxed f lst qid =
90 (* this is not tail recursive *)   
91    let rec err qid = relax_qid (resolve_gref err f lst) lst qid in
92    resolve_gref err f lst qid
93
94 let get_cnt err f lst = function
95    | None             -> f empty_cnt
96    | Some qid as node ->
97       try let cnt = UH.find hcnt (uri_of_qid qid) in f cnt
98       with Not_found -> err node
99
100 let get_cnt_relaxed f lst =
101 (* this is not tail recursive *)   
102    let rec err node = relax_opt_qid (get_cnt err f lst) lst node in
103    get_cnt err f lst lst.node
104
105 let push_abst f a w lenv =
106    let bw = D.Abst (false, N.infinite, w) in
107    D.push_bind f a bw lenv
108
109 let add_proj e t = match e with
110    | D.ESort                 -> t
111    | D.EBind (D.ESort, a, b) -> D.TBind (a, b, t) 
112    | _                       -> D.TProj (E.empty_node, e, t)
113
114 (* this is not tail recursive in the GRef branch *)
115 let rec xlate_term f st lst y lenv = function
116    | A.Sort s            ->
117       let h = if s then 0 else 1 in
118       let a = E.node_attrs ~sort:h () in
119       f a (D.TSort (a, h))
120    | A.Appl (v, t)       ->
121       let f vv at tt = f at (D.TAppl (at, !G.extended, 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
126       let name = Some (name, true) in
127       let f aw ww = 
128          let f at tt =
129             let l = 
130                if !G.cc then match y, at.E.n_degr with
131                   | true, _ -> N.one
132                   | _   , 0 -> N.one
133                   | _   , 1 -> N.unknown st
134                   | _   , 2 -> N.two
135                   | _       -> assert false
136                else N.infinite
137             in
138             let b = D.Abst (false, l, ww) in
139             let at = {at with E.n_name = name} in
140             f at (D.TBind (at, b, tt))
141          in
142          let f lenv = xlate_term f st lst y lenv t in
143          push_abst f {aw with E.n_name = name; E.n_degr = succ aw.E.n_degr} ww lenv
144       in
145       xlate_term f st lst true lenv w
146    | A.GRef (name, args) ->
147       let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in
148       let map2 f arg args = 
149          let f _ arg = f (D.EAppl (args, E.empty_node, !G.extended, arg)) in 
150          xlate_term f st lst false lenv arg
151       in
152       let g qid a cnt =
153          let gref = D.TGRef (a, uri_of_qid qid) in
154          if cnt = D.ESort then f a gref else
155          let f = function 
156             | D.EAppl (D.ESort, _, x, v) -> f a (D.TAppl (a, x, v, gref))
157             | args                       -> f a (D.TProj (a, args, gref))
158          in
159          let f args = C.list_fold_right f map2 args D.ESort in
160          D.sub_list_strict (D.fold_names f map1 args) cnt args
161       in
162       let g qid = resolve_gref_relaxed g lst qid in
163       let err () = complete_qid g lst name in
164       D.resolve_lref err (mk_lref f) (id_of_name name) lenv
165
166 let xlate_entity err f st lst = function
167    | A.Section (Some (_, name))     ->
168       err {lst with path = name :: lst.path; nodes = lst.node :: lst.nodes}
169    | A.Section None            ->
170       begin match lst.path, lst.nodes with
171          | _ :: ptl, nhd :: ntl -> 
172             err {lst with path = ptl; node = nhd; nodes = ntl}
173          | _                    -> assert false
174       end
175    | A.Context None            ->
176       err {lst with node = None}
177    | A.Context (Some name)     ->
178       let f name = err {lst with node = Some name} in
179       complete_qid f lst name 
180    | A.Block (name, w)         ->
181       let f qid = 
182          let f cnt =
183             let f aw ww = 
184                UH.add hcnt (uri_of_qid qid) (add_abst cnt name aw ww);
185                err {lst with node = Some qid}
186             in
187             xlate_term f st lst true cnt w
188          in
189          get_cnt_relaxed f lst
190       in
191       complete_qid f lst (name, true, [])
192    | A.Decl (name, w)          ->
193       let f lenv =
194          let f qid = 
195             let f aw ww =
196                let aw = {aw with E.n_apix = lst.line; E.n_degr = succ aw.E.n_degr} in
197                UH.add henv (uri_of_qid qid) (aw, lenv);
198                let t = add_proj lenv ww in
199 (*
200             print_newline (); CrgOutput.pp_term print_string t;
201 *)
202                let b = E.Abst t in
203                let entity = E.empty_root, aw, uri_of_qid qid, b in
204                G.set_current_trace lst.line;
205                f {lst with line = succ lst.line} entity
206             in
207             xlate_term f st lst true lenv w
208          in
209          complete_qid f lst (name, true, [])
210       in
211       get_cnt_relaxed (D.replace f N.one) lst
212    | A.Def (name, w, trans, v) ->
213       let f lenv =
214          let f qid = 
215             let f _ ww =
216                let f av vv =
217                   let na = {av with E.n_apix = lst.line} in
218                   UH.add henv (uri_of_qid qid) (na, lenv);
219                   let t = add_proj lenv (D.TCast (na, ww, vv)) in
220 (*
221             print_newline (); CrgOutput.pp_term print_string t;
222 *)
223                   let b = E.Abbr t in
224                   let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in
225                   let entity = ra, na, uri_of_qid qid, b in
226                   G.set_current_trace lst.line;
227                   f {lst with line = succ lst.line} entity
228                in
229                xlate_term f st lst false lenv v
230             in
231             xlate_term f st lst true lenv w
232          in
233          complete_qid f lst (name, true, [])
234       in
235       get_cnt_relaxed f lst
236
237 (* Interface functions ******************************************************)
238
239 let initial_status () =
240    UH.clear henv; UH.clear hcnt; {
241    path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
242 }
243
244 let refresh_status lst = initial_status ()
245
246 let crg_of_aut = xlate_entity