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