]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/automath/autCrg.ml
new intermediate language complete_rg,
[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 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 = D.ESort
48
49 let add_abst cnt id w = 
50    D.EBind (cnt, [E.Name (id, true)], D.Abst (N.infinite, w)) 
51
52 let mk_lref f i = f (D.TLRef ([], i))
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 age, cnt = K.find henv (uri_of_qid qid) in f qid age 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 let push_abst f a w lenv =
105    let bw = D.Abst (N.infinite, w) in
106    D.push_bind f a bw lenv
107
108 let add_proj e t = match e with
109    | D.ESort                 -> t
110    | D.EBind (D.ESort, a, b) -> D.TBind (a, b, t) 
111    | _                       -> D.TProj ([], e, t)
112
113 let lenv_of_cnt cnt = cnt
114
115 (* this is not tail recursive in the GRef branch *)
116 let rec xlate_term f st lenv = function
117    | A.Sort s            -> 
118       let f h = f (D.TSort ([], h)) in
119       if s then f 0 else f 1
120    | A.Appl (v, t)       ->
121       let f vv tt = f (D.TAppl ([], vv, tt)) in
122       let f vv = xlate_term (f vv) st lenv t in
123       xlate_term f st lenv v
124    | A.Abst (name, w, t) ->
125       let f ww = 
126          let a = [E.Name (name, true)] in
127          let f tt =
128             let b = D.Abst (N.infinite, ww) in
129             f (D.TBind (a, b, tt))
130          in
131          let f lenv = xlate_term f st lenv t in
132          push_abst f a ww lenv
133       in
134       xlate_term f st lenv w
135    | A.GRef (name, args) ->
136       let map1 args a =
137          let f id _ = A.GRef ((id, true, []), []) :: args in
138          E.name C.err f a
139       in
140       let map2 f arg args = 
141          let f arg = f (D.EAppl (args, [], arg)) in 
142          xlate_term f st lenv arg
143       in
144       let g qid age cnt =
145          let gref = D.TGRef ([age], uri_of_qid qid) in
146          if cnt = D.ESort then f gref else
147          let f = function 
148             | D.EAppl (D.ESort, a, v) -> f (D.TAppl (a, v, gref))
149             | args                    -> f (D.TProj ([], 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_attrs f map1 args) cnt args
153       in
154       let g qid = resolve_gref_relaxed g st qid in
155       let err () = complete_qid g st name in
156       D.resolve_lref err (mk_lref f) (id_of_name name) lenv
157
158 let xlate_entity err f st = function
159    | A.Section (Some (_, name))     ->
160       err {st with path = name :: st.path; nodes = st.node :: st.nodes}
161    | A.Section None            ->
162       begin match st.path, st.nodes with
163          | _ :: ptl, nhd :: ntl -> 
164             err {st with path = ptl; node = nhd; nodes = ntl}
165          | _                    -> assert false
166       end
167    | A.Context None            ->
168       err {st with node = None}
169    | A.Context (Some name)     ->
170       let f name = err {st with node = Some name} in
171       complete_qid f st name 
172    | A.Block (name, w)         ->
173       let f qid = 
174          let f cnt =
175             let f ww = 
176                K.add hcnt (uri_of_qid qid) (add_abst cnt name ww);
177                err {st with node = Some qid}
178             in
179             xlate_term f st cnt w
180          in
181          get_cnt_relaxed f st
182       in
183       complete_qid f st (name, true, [])
184    | A.Decl (name, w)          ->
185       let f cnt =
186          let lenv = lenv_of_cnt cnt in
187          let f qid = 
188             let f ww =
189                let age = E.Apix st.line in
190                K.add henv (uri_of_qid qid) (age, cnt);
191                let t = add_proj lenv ww in
192 (*
193             print_newline (); CrgOutput.pp_term print_string t;
194 *)
195                let b = E.Abst (N.infinite, t) in
196                let entity = [age], uri_of_qid qid, b in
197                f {st with line = succ st.line} entity
198             in
199             xlate_term f st lenv w
200          in
201          complete_qid f st (name, true, [])
202       in
203       get_cnt_relaxed f st
204    | A.Def (name, w, trans, v) ->
205       let f cnt =
206          let lenv = lenv_of_cnt cnt in
207          let f qid = 
208             let f ww =
209                let f vv =
210                   let age = E.Apix st.line in
211                   K.add henv (uri_of_qid qid) (age, cnt);
212                   let t = add_proj lenv (D.TCast ([], ww, vv)) in
213 (*
214             print_newline (); CrgOutput.pp_term print_string t;
215 *)
216                   let b = E.Abbr t in
217                   let a = age :: if trans then [] else [E.Meta [E.Private]] in
218                   let entity = a, uri_of_qid qid, b in
219                   f {st with line = succ st.line} entity
220                in
221                xlate_term f st lenv v
222             in
223             xlate_term f st lenv w
224          in
225          complete_qid f st (name, true, [])
226       in
227       get_cnt_relaxed f st
228
229 (* Interface functions ******************************************************)
230
231 let initial_status () =
232    K.clear henv; K.clear hcnt; {
233    path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ()
234 }
235
236 let refresh_status st = {st with
237    mk_uri = G.get_mk_uri ()
238 }
239
240 let crg_of_aut = xlate_entity