]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/toplevel/metaAut.ml
6a45518b5bbf1210f5f46d6ffa620a66e000fc3e
[helm.git] / helm / software / lambda-delta / src / toplevel / metaAut.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 M = Meta
18 module A = Aut
19
20 (* qualified identifier: uri, name, qualifiers *)
21 type qid = M.uri * M.id * M.id list
22
23 type context_node = qid option (* context node: None = root *)
24
25 type status = {
26    path: M.id list;          (* current section path *) 
27    node: context_node;       (* current context node *)
28    nodes: context_node list; (* context node list *)
29    line: int;                (* line number *)
30    cover: string             (* initial segment of URI hierarchy *) 
31 }
32
33 type resolver = Local of int
34               | Global of M.pars
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 id_of_name (id, _, _) = id
45
46 let mk_qid st id path =
47    let uripath = if st.cover = "" then path else st.cover :: path in
48    let str = String.concat "/" uripath in
49    let str = Filename.concat str id in 
50    U.uri_of_string ("ld:/" ^ str ^ ".ld"), id, path
51
52 let uri_of_qid (uri, _, _) = uri
53
54 let complete_qid f st (id, is_local, qs) =
55    let f qs = f (mk_qid st id qs) in
56    let f path = C.list_rev_append f path ~tail:qs in
57    let rec skip f = function
58       | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
59       | _ :: ptl, _ :: _                      -> skip f (ptl, qs)
60       | _                                     -> f []
61    in
62    if is_local then f st.path else skip f (st.path, qs)
63
64 let relax_qid f st (_, id, path) =
65    let f path = f (mk_qid st id path) in
66    let f = function
67       | _ :: tl -> C.list_rev f tl
68       | []      -> assert false
69    in
70    C.list_rev f path
71
72 let relax_opt_qid f st = function
73    | None     -> f None
74    | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
75
76 let resolve_lref f st l lenv id =
77    let rec aux f i = function
78      | []                            -> f None
79      | (name, _) :: _ when name = id -> f (Some (M.LRef (l, i)))
80      | _ :: tl                       -> aux f (succ i) tl
81    in
82    aux f 0 lenv
83
84 let resolve_lref_strict f st l lenv id =
85    let f = function
86       | Some t -> f t
87       | None   -> assert false
88    in
89    resolve_lref f st l lenv id
90
91 let resolve_gref f st qid =
92    try let args = K.find henv (uri_of_qid qid) in f qid (Some args)
93    with Not_found -> f qid None
94
95 let resolve_gref_relaxed f st qid =
96 (* this is not tail recursive *)
97    let rec g qid = function
98       | None      -> relax_qid (resolve_gref g st) st qid
99       | Some args -> f qid args
100    in
101    resolve_gref g st qid
102
103 let get_pars f st = function
104    | None              -> f [] None
105    | Some qid as node ->
106       try let pars = K.find hcnt (uri_of_qid qid) in f pars None
107       with Not_found -> f [] (Some node)
108
109 let get_pars_relaxed f st =
110 (* this is not tail recursive *)
111    let rec g pars = function
112       | None      -> f pars 
113       | Some node -> relax_opt_qid (get_pars g st) st node
114    in
115    get_pars g st st.node
116
117 (* this is not tail recursive on the GRef branch *)
118 let rec xlate_term f st lenv = function
119    | A.Sort sort         -> 
120       f (M.Sort sort)
121    | A.Appl (v, t)       ->
122       let f vv tt = f (M.Appl (vv, tt)) in
123       let f vv = xlate_term (f vv) st lenv t in
124       xlate_term f st lenv v
125    | A.Abst (name, w, t) ->
126       let add name w lenv = (name, w) :: lenv in
127       let f ww tt = f (M.Abst (name, ww, tt)) in
128       let f ww = xlate_term (f ww) st (add name ww lenv) t in
129       xlate_term f st lenv w
130    | A.GRef (name, args) ->
131       let l = List.length lenv in
132       let g qid defs =
133          let map1 f = xlate_term f st lenv in       
134          let map2 f (id, _) = resolve_lref_strict f st l lenv id in
135          let f tail = 
136             let f args = f (M.GRef (l, uri_of_qid qid, args)) in
137             let f defs = C.list_rev_map_append f map2 defs ~tail in
138             C.list_sub_strict f defs args
139          in   
140          C.list_map f map1 args
141       in
142       let g qid = resolve_gref_relaxed g st qid in
143       let f = function
144          | Some t -> f t
145          | None   -> complete_qid g st name
146       in
147       resolve_lref f st l lenv (id_of_name name)
148
149 let xlate_entity err f st = function
150    | A.Section (Some (_, name))     ->
151       err {st with path = name :: st.path; nodes = st.node :: st.nodes}
152    | A.Section None            ->
153       begin match st.path, st.nodes with
154          | _ :: ptl, nhd :: ntl -> 
155             err {st with path = ptl; node = nhd; nodes = ntl}
156          | _                    -> assert false
157       end
158    | A.Context None            ->
159       err {st with node = None}
160    | A.Context (Some name)     ->
161       let f name = err {st with node = Some name} in
162       complete_qid f st name 
163    | A.Block (name, w)         ->
164       let f qid = 
165          let f pars =
166             let f ww = 
167                K.add hcnt (uri_of_qid qid) ((name, ww) :: pars);
168                err {st with node = Some qid}
169             in
170             xlate_term f st pars w
171          in
172          get_pars_relaxed f st
173       in
174       complete_qid f st (name, true, [])
175    | A.Decl (name, w)          ->
176       let f pars = 
177          let f qid = 
178             let f ww =
179                K.add henv (uri_of_qid qid) pars;
180                let a = [E.Mark st.line] in
181                let entry = pars, ww, None in
182                let entity = a, uri_of_qid qid, E.Abst entry in
183                f {st with line = succ st.line} entity
184             in
185             xlate_term f st pars w
186          in
187          complete_qid f st (name, true, [])
188       in
189       get_pars_relaxed f st
190    | A.Def (name, w, trans, v) ->
191       let f pars = 
192          let f qid = 
193             let f ww vv = 
194                K.add henv (uri_of_qid qid) pars;
195                let a = E.Mark st.line :: if trans then [] else [E.Priv] in 
196                let entry = pars, ww, Some vv in
197                let entity = a, uri_of_qid qid, E.Abbr entry in
198                f {st with line = succ st.line} entity
199             in
200             let f ww = xlate_term (f ww) st pars v in
201             xlate_term f st pars w
202          in
203          complete_qid f st (name, true, [])
204       in
205       get_pars_relaxed f st
206
207 (* Interface functions ******************************************************)
208
209 let initial_status () =
210    K.clear henv; K.clear hcnt; {
211    path = []; node = None; nodes = []; line = 1; cover = !G.cover
212 }
213
214 let refresh_status st = {st with
215   cover = !G.cover
216 }  
217
218 let meta_of_aut = xlate_entity