]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/automath/autCrg.ml
- the disambiguation of unified binders continues
[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 K = U.UriHash
14 module C = Cps
15 module N = Level
16 module E = Entity
17 module G = Options
18 module S = Status
19 module A = Aut
20 module D = Crg
21
22 (* qualified identifier: uri, name, qualifiers *)
23 type qid = D.uri * D.id * D.id list
24
25 type context_node = qid option (* context node: None = root *)
26
27 type status = {
28    path: D.id list;          (* current section path *) 
29    node: context_node;       (* current context node *)
30    nodes: context_node list; (* context node list *)
31    line: int;                (* line number *)
32    mk_uri: G.uri_generator;  (* uri generator *)
33 }
34
35 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
36
37 let henv = K.create henv_size (* optimized global environment *)
38
39 let hcnt = K.create hcnt_size (* optimized context *)
40
41 (* Internal functions *******************************************************)
42
43 let empty_cnt = D.ESort
44
45 let add_abst cnt id d w =
46    let a = E.node_attrs ~name:(id, true) ~degr:(succ d) () in 
47    D.EBind (cnt, a, D.Abst (N.two, w)) 
48
49 let mk_lref f a i = f a.E.n_degr (D.TLRef (E.empty_node, i))
50
51 let id_of_name (id, _, _) = id
52
53 let mk_qid f lst id path =
54    let str = String.concat "/" path in
55    let str = Filename.concat str id in 
56    let str = lst.mk_uri str in
57    f (U.uri_of_string str, id, path)
58
59 let uri_of_qid (uri, _, _) = uri
60
61 let complete_qid f lst (id, is_local, qs) =
62    let f path = C.list_rev_append (mk_qid f lst id) path ~tail:qs in
63    let rec skip f = function
64       | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
65       | _ :: ptl, _ :: _                      -> skip f (ptl, qs)
66       | _                                     -> f []
67    in
68    if is_local then f lst.path else skip f (lst.path, qs)
69
70 let relax_qid f lst (_, id, path) =
71    let f = function
72       | _ :: tl -> C.list_rev (mk_qid f lst id) tl
73       | []      -> assert false
74    in
75    C.list_rev f path
76
77 let relax_opt_qid f lst = function
78    | None     -> f None
79    | Some qid -> let f qid = f (Some qid) in relax_qid f lst qid
80
81 let resolve_gref err f lst qid =
82    try let a, cnt = K.find henv (uri_of_qid qid) in f qid a cnt
83    with Not_found -> err qid
84
85 let resolve_gref_relaxed f lst qid =
86 (* this is not tail recursive *)   
87    let rec err qid = relax_qid (resolve_gref err f lst) lst qid in
88    resolve_gref err f lst qid
89
90 let get_cnt err f lst = function
91    | None             -> f empty_cnt
92    | Some qid as node ->
93       try let cnt = K.find hcnt (uri_of_qid qid) in f cnt
94       with Not_found -> err node
95
96 let get_cnt_relaxed f lst =
97 (* this is not tail recursive *)   
98    let rec err node = relax_opt_qid (get_cnt err f lst) lst node in
99    get_cnt err f lst lst.node
100
101 let push_abst f a w lenv =
102    let bw = D.Abst (N.infinite, w) in
103    D.push_bind f a bw lenv
104
105 let add_proj e t = match e with
106    | D.ESort                 -> t
107    | D.EBind (D.ESort, a, b) -> D.TBind (a, b, t) 
108    | _                       -> D.TProj (E.empty_node, e, t)
109
110 (* this is not tail recursive in the GRef branch *)
111 let rec xlate_term f st lst y lenv = function
112    | A.Sort s            ->
113       let f h = f 0 (D.TSort (E.empty_node, h)) in
114       if s then f 0 else f 1
115    | A.Appl (v, t)       ->
116       let f vv d tt = f d (D.TAppl (E.empty_node, vv, tt)) in
117       let f _ vv = xlate_term (f vv) st lst y lenv t in
118       xlate_term f st lst false lenv v
119    | A.Abst (name, w, t) ->
120       let f d ww = 
121          let a = E.node_attrs ~name:(name, true) () in
122          let f d tt =
123             let l = if !G.cc then match y, d with
124                | true, _ -> N.one
125                | _   , 0 -> N.one
126                | _   , 1 -> N.unknown st.S.lenv
127                | _   , 2 -> N.two
128                | _       -> assert false
129                else N.infinite
130             in
131             let b = D.Abst (l, ww) in
132             f d (D.TBind (a, b, tt))
133          in
134          let f lenv = xlate_term f st lst y lenv t in
135          push_abst f {a with E.n_degr = succ d} ww lenv
136       in
137       xlate_term f st lst true lenv w
138    | A.GRef (name, args) ->
139       let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in
140       let map2 f arg args = 
141          let f _ arg = f (D.EAppl (args, E.empty_node, arg)) in 
142          xlate_term f st lst false lenv arg
143       in
144       let g qid a cnt =
145          let gref = D.TGRef (a, uri_of_qid qid) in
146          if cnt = D.ESort then f a.E.n_degr gref else
147          let f = function 
148             | D.EAppl (D.ESort, a, v) -> f a.E.n_degr (D.TAppl (a, v, gref))
149             | args                    -> f a.E.n_degr (D.TProj (E.empty_node, args, gref))
150          in
151          let f args = C.list_fold_right f map2 args D.ESort in
152          D.sub_list_strict (D.fold_names f map1 args) cnt args
153       in
154       let g qid = resolve_gref_relaxed g lst qid in
155       let err () = complete_qid g lst name in
156       D.resolve_lref err (mk_lref f) (id_of_name name) lenv
157
158 let xlate_entity err f st lst = function
159    | A.Section (Some (_, name))     ->
160       err {lst with path = name :: lst.path; nodes = lst.node :: lst.nodes}
161    | A.Section None            ->
162       begin match lst.path, lst.nodes with
163          | _ :: ptl, nhd :: ntl -> 
164             err {lst with path = ptl; node = nhd; nodes = ntl}
165          | _                    -> assert false
166       end
167    | A.Context None            ->
168       err {lst with node = None}
169    | A.Context (Some name)     ->
170       let f name = err {lst with node = Some name} in
171       complete_qid f lst name 
172    | A.Block (name, w)         ->
173       let f qid = 
174          let f cnt =
175             let f d ww = 
176                K.add hcnt (uri_of_qid qid) (add_abst cnt name d ww);
177                err {lst with node = Some qid}
178             in
179             xlate_term f st lst true cnt w
180          in
181          get_cnt_relaxed f lst
182       in
183       complete_qid f lst (name, true, [])
184    | A.Decl (name, w)          ->
185       let f lenv =
186          let f qid = 
187             let f d ww =
188                let a = E.node_attrs ~apix:lst.line ~degr:(succ d) () in
189                K.add henv (uri_of_qid qid) (a, lenv);
190                let t = add_proj lenv ww in
191 (*
192             print_newline (); CrgOutput.pp_term print_string t;
193 *)
194                let b = E.Abst t in
195                let entity = E.empty_root, a, uri_of_qid qid, b in
196                f {lst with line = succ lst.line} entity
197             in
198             xlate_term f st lst true lenv w
199          in
200          complete_qid f lst (name, true, [])
201       in
202       get_cnt_relaxed (D.sta f) lst
203    | A.Def (name, w, trans, v) ->
204       let f lenv =
205          let f qid = 
206             let f _ ww =
207                let f d vv =
208                   let na = E.node_attrs ~apix:lst.line ~degr:d () in
209                   K.add henv (uri_of_qid qid) (na, lenv);
210                   let t = add_proj lenv (D.TCast (E.empty_node, ww, vv)) in
211 (*
212             print_newline (); CrgOutput.pp_term print_string t;
213 *)
214                   let b = E.Abbr t in
215                   let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in
216                   let entity = ra, na, uri_of_qid qid, b in
217                   f {lst with line = succ lst.line} entity
218                in
219                xlate_term f st lst false lenv v
220             in
221             xlate_term f st lst true lenv w
222          in
223          complete_qid f lst (name, true, [])
224       in
225       get_cnt_relaxed f lst
226
227 (* Interface functions ******************************************************)
228
229 let initial_status () =
230    K.clear henv; K.clear hcnt; {
231    path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
232 }
233
234 let refresh_status lst = {lst with
235    mk_uri = G.get_mk_uri ()
236 }
237
238 let crg_of_aut = xlate_entity