]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/complete_rg/crgAut.ml
Additional contribs.
[helm.git] / helm / software / lambda-delta / complete_rg / crgAut.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 H = U.UriHash
14 module C = Cps
15 module Y = Entity
16 module A = Aut
17 module D = Crg
18
19 (* qualified identifier: uri, name, qualifiers *)
20 type qid = D.uri * D.id * D.id list
21
22 type context = Y.attrs * D.term 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:Y.uri_generator    (* uri generator *) 
32 }
33
34 type resolver = Local of int
35               | Global of context
36
37 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
38
39 let henv = H.create henv_size (* optimized global environment *)
40
41 let hcnt = H.create hcnt_size (* optimized context *)
42
43 (* Internal functions *******************************************************)
44
45 let initial_status mk_uri =
46    H.clear henv; H.clear hcnt; {
47    path = []; node = None; nodes = []; line = 1; mk_uri = mk_uri
48 }
49
50 let empty_cnt = [], []
51
52 let add_abst (a, ws) id w = 
53    Y.Name (id, true) :: a, w :: ws 
54
55 let lenv_of_cnt (a, ws) = 
56    D.push_bind C.start D.empty_lenv a (D.Abst ws)
57
58 let mk_lref f i j k = f (D.TLRef ([Y.Apix k], i, j))
59
60 let id_of_name (id, _, _) = id
61
62 let mk_qid f st id path =
63    let str = String.concat "/" path in
64    let str = Filename.concat str id in 
65    let f str = f (U.uri_of_string str, id, path) in
66    f (st.mk_uri str)
67    
68 let uri_of_qid (uri, _, _) = uri
69
70 let complete_qid f st (id, is_local, qs) =
71    let f path = C.list_rev_append (mk_qid f st id) path ~tail:qs in
72    let rec skip f = function
73       | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
74       | _ :: ptl, _ :: _                      -> skip f (ptl, qs)
75       | _                                     -> f []
76    in
77    if is_local then f st.path else skip f (st.path, qs)
78
79 let relax_qid f st (_, id, path) =
80    let f = function
81       | _ :: tl -> C.list_rev (mk_qid f st id) tl
82       | []      -> assert false
83    in
84    C.list_rev f path
85
86 let relax_opt_qid f st = function
87    | None     -> f None
88    | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
89
90 let resolve_gref err f st qid =
91    try let cnt = H.find henv (uri_of_qid qid) in f qid cnt
92    with Not_found -> err qid 
93
94 let resolve_gref_relaxed f st qid =
95 (* this is not tail recursive *)   
96    let rec err qid = relax_qid (resolve_gref err f st) st qid in
97    resolve_gref err f st qid
98
99 let get_cnt err f st = function
100    | None              -> f empty_cnt
101    | Some qid as node ->
102       try let cnt = H.find hcnt (uri_of_qid qid) in f cnt
103       with Not_found -> err node
104
105 let get_cnt_relaxed f st =
106 (* this is not tail recursive *)   
107    let rec err node = relax_opt_qid (get_cnt err f st) st node in
108    get_cnt err f st st.node
109
110 (* this is not tail recursive in the GRef branch *)
111 let rec xlate_term f st lenv = function
112    | A.Sort s            -> 
113       let f h = f (D.TSort ([], h)) in
114       if s then f 0 else f 1
115    | A.Appl (v, t)       ->
116       let f vv tt = f (D.TAppl ([], [vv], tt)) in
117       let f vv = xlate_term (f vv) st lenv t in
118       xlate_term f st lenv v
119    | A.Abst (name, w, t) ->
120       let f ww = 
121          let a, b = [Y.Name (name, true)], (D.Abst [ww]) in
122          let f tt = f (D.TBind (a, b, tt)) in
123          let f lenv = xlate_term f st lenv t in
124          D.push_bind f lenv a b
125       in
126       xlate_term f st lenv w
127    | A.GRef (name, args) ->
128       let map1 f = function
129             | Y.Name (id, _) -> f (A.GRef ((id, true, []), []))
130             | _              -> C.err ()
131       in
132       let map2 f = xlate_term f st lenv in
133       let g qid (a, _) =
134          let gref = D.TGRef ([], uri_of_qid qid) in
135          match args, a with
136             | [], [] -> f gref
137             | _      -> 
138                let f args = f (D.TAppl ([], args, gref)) in
139                let f args = f (List.rev_map (map2 C.start) args) in
140                let f a = C.list_rev_map_append f map1 a ~tail:args in
141                C.list_sub_strict f a args
142       in
143       let g qid = resolve_gref_relaxed g st qid in
144       let err () = complete_qid g st name in
145       D.resolve_lref err (mk_lref f) (id_of_name name) lenv
146
147 let xlate_entity err f st = function
148    | A.Section (Some (_, name))     ->
149       err {st with path = name :: st.path; nodes = st.node :: st.nodes}
150    | A.Section None            ->
151       begin match st.path, st.nodes with
152          | _ :: ptl, nhd :: ntl -> 
153             err {st with path = ptl; node = nhd; nodes = ntl}
154          | _                    -> assert false
155       end
156    | A.Context None            ->
157       err {st with node = None}
158    | A.Context (Some name)     ->
159       let f name = err {st with node = Some name} in
160       complete_qid f st name 
161    | A.Block (name, w)         ->
162       let f qid = 
163          let f cnt =
164             let lenv = lenv_of_cnt cnt in
165             let ww = xlate_term C.start st lenv w in
166             H.add hcnt (uri_of_qid qid) (add_abst cnt name ww);
167             err {st with node = Some qid}
168          in
169          get_cnt_relaxed f st
170       in
171       complete_qid f st (name, true, [])
172    | A.Decl (name, w)          ->
173       let f cnt =
174          let a, ws = cnt in
175          let lenv = lenv_of_cnt cnt in
176          let f qid = 
177             let ww = xlate_term C.start st lenv w in
178             H.add henv (uri_of_qid qid) cnt;
179             let t = match ws with
180                | [] -> ww
181                | _  -> D.TBind (a, D.Abst ws, ww)
182             in
183 (*
184             print_newline (); CrgOutput.pp_term print_string t;
185 *)
186             let b = Y.Abst t in
187             let entity = [Y.Mark st.line], uri_of_qid qid, b in
188             f {st with line = succ st.line} entity
189          in
190          complete_qid f st (name, true, [])
191       in
192       get_cnt_relaxed f st
193    | A.Def (name, w, trans, v) ->
194       let f cnt = 
195          let a, ws = cnt in
196          let lenv = lenv_of_cnt cnt in
197          let f qid = 
198             let ww = xlate_term C.start st lenv w in
199             let vv = xlate_term C.start st lenv v in
200             H.add henv (uri_of_qid qid) cnt;
201             let t = match ws with
202                | [] -> D.TCast ([], ww, vv)
203                | _  -> D.TBind (a, D.Abst ws, D.TCast ([], ww, vv))
204             in
205 (*
206             print_newline (); CrgOutput.pp_term print_string t;
207 *)
208             let b = Y.Abbr t in
209             let a = Y.Mark st.line :: if trans then [] else [Y.Priv] in
210             let entity = a, uri_of_qid qid, b in
211             f {st with line = succ st.line} entity
212          in
213          complete_qid f st (name, true, [])
214       in
215       get_cnt_relaxed f st
216
217 (* Interface functions ******************************************************)
218
219 let initial_status mk_uri =
220    initial_status mk_uri
221
222 let crg_of_aut = xlate_entity