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