]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/automath/autCrg.ml
update in helena
[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 UH = U.UriHash
14 module C  = Cps
15 module G  = Options
16 module N  = Layer
17 module E  = Entity
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_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: G.uri_generator;  (* uri generator *)
32 }
33
34 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
35
36 let henv = UH.create henv_size (* optimized global environment *)
37
38 let hcnt = UH.create hcnt_size (* optimized context *)
39
40 (* Internal functions *******************************************************)
41
42 let empty_cnt = D.empty_lenv
43
44 let alpha id =
45    if id.[0] >= '0' && id.[0] <= '9' then !G.alpha ^ id else id
46
47 let attrs_for_appl yv yt =
48    E.appl_attrs ~side:yv ~main:yt !G.restricted
49
50 let attrs_for_abst id yw =
51    let id = if !G.alpha <> "" then alpha id else id in
52    E.bind_attrs ~name:(id, true) ~side:yw ~main:(E.succ yw) ()
53
54 let attrs_for_env y =
55    E.env_attrs ~side:y ()
56
57 let add_abst cnt id yw w =
58    let a = attrs_for_abst id yw in
59    let l = if !G.infinity then N.infinity else N.two in
60    D.EBind (cnt, E.empty_node, a, D.Abst (false, l, w))
61
62 let mk_lref f _ a i = f a.E.b_main (D.TLRef (E.empty_node, i))
63
64 let id_of_name (id, _, _) =
65    if !G.alpha <> "" then alpha id else id
66
67 let mk_qid f lst id path =
68    let str = String.concat "/" path in
69    let str = Filename.concat str id in
70    let str = lst.mk_uri str in
71    f (U.uri_of_string str, id, path)
72
73 let uri_of_qid (uri, _, _) = uri
74
75 let complete_qid f lst (id, is_local, qs) =
76    let f path = C.list_rev_append (mk_qid f lst id) path ~tail:qs in
77    let rec skip f = function
78       | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
79       | _ :: ptl, _ :: _                      -> skip f (ptl, qs)
80       | _                                     -> f []
81    in
82    if is_local then f lst.path else skip f (lst.path, qs)
83
84 let relax_qid f lst (_, id, path) =
85    let f = function
86       | _ :: tl -> C.list_rev (mk_qid f lst id) tl
87       | []      -> assert false
88    in
89    C.list_rev f path
90
91 let relax_opt_qid f lst = function
92    | None     -> f None
93    | Some qid -> let f qid = f (Some qid) in relax_qid f lst qid
94
95 let resolve_gref err f lst qid =
96    try let y, cnt = UH.find henv (uri_of_qid qid) in f qid y cnt
97    with Not_found -> err qid
98
99 let resolve_gref_relaxed f lst qid =
100 (* this is not tail recursive *)
101    let rec err qid = relax_qid (resolve_gref err f lst) lst qid in
102    resolve_gref err f lst qid
103
104 let get_cnt err f lst = function
105    | None             -> f empty_cnt
106    | Some qid as node ->
107       try let cnt = UH.find hcnt (uri_of_qid qid) in f cnt
108       with Not_found -> err node
109
110 let get_cnt_relaxed f lst =
111 (* this is not tail recursive *)
112    let rec err node = relax_opt_qid (get_cnt err f lst) lst node in
113    get_cnt err f lst lst.node
114
115 let push_abst f a w lenv =
116    let bw = D.Abst (false, N.infinity, w) in
117    D.push_bind f E.empty_node a bw lenv
118 (*
119 let rec set_name_y f = function
120    | D.ESort              -> f D.ESort
121    | D.EBind (e, a, y, b) -> set_name_y (D.push_bind f a {y with E.b_name = Some ("Y", true)} b) e
122    | D.EAppl (e, a, v)    -> set_name_y (D.push_appl f a v) e
123    | D.EProj (e, d)       -> let f d = set_name_y (D.push_proj f d) e in set_name_y f d
124 *)
125 let add_proj yt e t = match e with
126    | D.ESort                    -> t
127    | D.EBind (D.ESort, _, a, b) -> D.TBind (E.compose a yt, b, t)
128    | e                          ->
129       D.TProj (D.set_attrs C.start yt e, t)
130
131 (* this is not tail recursive in the GRef branch *)
132 let rec xlate_term f st lst z lenv = function
133    | A.Sort s            ->
134       let k = if s then 0 else 1 in
135       f (k, 0) (D.TSort k)
136    | A.Appl (v, t)       ->
137       let f yv vv yt tt =
138          f yt (D.TAppl (attrs_for_appl yv yt, vv, tt))
139       in
140       let f yv vv = xlate_term (f yv vv) st lst z lenv t in
141       xlate_term f st lst false lenv v
142    | A.Abst (name, w, t) ->
143       let f yw ww =
144          let a = attrs_for_abst name yw in
145          let f yt tt =
146             let l =
147                if !G.cc then match z, snd yt with
148                   | true, _ -> N.one
149                   | _   , 0 -> N.one
150                   | _   , 1 -> N.unknown st
151                   | _   , 2 -> N.two
152                   | _       -> assert false
153                else N.infinity
154             in
155             let b = D.Abst (false, l, ww) in
156 (*            let yt = {yt with E.b_name = Some ("P", true)} in *)
157             f yt (D.TBind (E.compose a yt, b, tt))
158          in
159          let f lenv = xlate_term f st lst z lenv t in
160          push_abst f a ww lenv
161       in
162       xlate_term f st lst true lenv w
163    | A.GRef (name, args) ->
164       let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in
165       let map2 y f arg args =
166          let f yv v = f (D.EAppl (args, attrs_for_appl yv y, v)) in
167          xlate_term f st lst false lenv arg
168       in
169       let g qid y cnt =
170          let gref = D.TGRef (E.empty_node, uri_of_qid qid) in
171          if cnt = D.ESort then f y gref else
172          let f = function
173             | D.EAppl (D.ESort, a, v) -> f y (D.TAppl (a, v, gref))
174             | args                    -> f y (D.TProj (args, gref))
175          in
176          let f args = C.list_fold_right f (map2 y) args D.ESort in
177          D.sub_list_strict (D.fold_names f map1 args) cnt args
178       in
179       let g qid = resolve_gref_relaxed g lst qid in
180       let err () = complete_qid g lst name in
181       D.resolve_lref err (mk_lref f) (id_of_name name) lenv
182
183 let xlate_entity err f st lst = function
184    | A.Section (Some (_, name))     ->
185       err {lst with path = name :: lst.path; nodes = lst.node :: lst.nodes}
186    | A.Section None            ->
187       begin match lst.path, lst.nodes with
188          | _ :: ptl, nhd :: ntl ->
189             err {lst with path = ptl; node = nhd; nodes = ntl}
190          | _                    -> assert false
191       end
192    | A.Context None            ->
193       err {lst with node = None}
194    | A.Context (Some name)     ->
195       let f name = err {lst with node = Some name} in
196       complete_qid f lst name
197    | A.Block (name, w)         ->
198       let f qid =
199          let f cnt =
200             let f yw ww =
201                UH.add hcnt (uri_of_qid qid) (add_abst cnt name yw ww);
202                err {lst with node = Some qid}
203             in
204             xlate_term f st lst true cnt w
205          in
206          get_cnt_relaxed f lst
207       in
208       complete_qid f lst (name, true, [])
209    | A.Decl (name, w)          ->
210       let f lenv =
211          let f qid =
212             let f yw ww =
213                let yv = E.succ yw in
214                let a = attrs_for_env yv in
215                UH.add henv (uri_of_qid qid) (yv, lenv);
216                let t = add_proj yw lenv ww in
217                let na = E.node_attrs ~apix:lst.line () in
218                let entity = E.empty_root, na, uri_of_qid qid, E.abst a t in
219 IFDEF TRACE THEN
220                G.set_current_trace lst.line
221 ELSE () END;
222                f {lst with line = succ lst.line} entity
223             in
224             xlate_term f st lst true lenv w
225          in
226          complete_qid f lst (name, true, [])
227       in
228       let f = if !G.infinity then f else D.set_layer f N.one in
229       get_cnt_relaxed f lst
230    | A.Def (name, w, trans, v) ->
231       let f lenv =
232          let f qid =
233             let f yw ww =
234                let f yv vv =
235                   let a = attrs_for_env yv in
236                   UH.add henv (uri_of_qid qid) (yv, lenv);
237                   let t = if !G.cast then
238                      let f e = D.TCast (add_proj yw e ww, add_proj yv lenv vv) in
239                      if !G.infinity then f lenv else D.set_layer f N.one lenv
240                   else
241                      add_proj yv lenv (D.TCast (ww, vv))
242                   in
243                   let na = E.node_attrs ~apix:lst.line () in
244                   let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in
245                   let entity = ra, na, uri_of_qid qid, E.abbr a t in
246 IFDEF TRACE THEN
247                   G.set_current_trace lst.line
248 ELSE () END;
249                   f {lst with line = succ lst.line} entity
250                in
251                xlate_term f st lst false lenv v
252             in
253             xlate_term f st lst true lenv w
254          in
255          complete_qid f lst (name, true, [])
256       in
257       get_cnt_relaxed f lst
258
259 (* Interface functions ******************************************************)
260
261 let initial_status () =
262    UH.clear henv; UH.clear hcnt; {
263    path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
264 }
265
266 let refresh_status lst = initial_status ()
267
268 let crg_of_aut = xlate_entity