]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgOutput.ml
a9cec439ac06c5aafcc1719828e8f034db432244
[helm.git] / helm / software / lambda-delta / basic_rg / brgOutput.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 P = Printf
13 module F = Format
14 module C = Cps
15 module U = NUri
16 module L = Log
17 module Y = Entity
18 module X = Library
19 module H = Hierarchy
20 module O = Output
21 module B = Brg
22
23 (* nodes count **************************************************************)
24
25 type counters = {
26    eabsts: int;
27    eabbrs: int;
28    evoids: int;
29    tsorts: int;
30    tlrefs: int;
31    tgrefs: int;
32    tcasts: int;
33    tappls: int;
34    tabsts: int;
35    tabbrs: int;
36    tvoids: int;
37    uris  : B.uri list;
38    nodes : int;
39    xnodes: int
40 }
41
42 let initial_counters = {
43    eabsts = 0; eabbrs = 0; evoids = 0; 
44    tsorts = 0; tlrefs = 0; tgrefs = 0; tcasts = 0; tappls = 0;
45    tabsts = 0; tabbrs = 0; tvoids = 0;
46    uris = []; nodes = 0; xnodes = 0
47 }
48
49 let rec count_term_binder f c e = function
50    | B.Abst (_, w) ->
51       let c = {c with tabsts = succ c.tabsts; nodes = succ c.nodes} in
52       count_term f c e w
53    | B.Abbr (_, v) -> 
54       let c = {c with tabbrs = succ c.tabbrs; xnodes = succ c.xnodes} in
55       count_term f c e v
56    | B.Void _      ->
57       let c = {c with tvoids = succ c.tvoids; xnodes = succ c.xnodes} in   
58       f c
59
60 and count_term f c e = function
61    | B.Sort _         -> 
62       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
63    | B.LRef (_, i)    -> 
64       let err _ = f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes} in
65       let f _ = function
66          | B.Abst _ 
67          | B.Void _ ->
68             f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
69          | B.Abbr _ ->
70             f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
71       in         
72       B.get err f e i
73    | B.GRef (_, u)    -> 
74       let c =    
75          if Cps.list_mem ~eq:U.eq u c.uris
76          then {c with nodes = succ c.nodes}
77          else {c with xnodes = succ c.xnodes}
78       in
79       f {c with tgrefs = succ c.tgrefs}
80    | B.Cast (_, v, t) -> 
81       let c = {c with tcasts = succ c.tcasts} in
82       let f c = count_term f c e t in
83       count_term f c e v
84    | B.Appl (_, v, t) -> 
85       let c = {c with tappls = succ c.tappls; nodes = succ c.nodes} in
86       let f c = count_term f c e t in
87       count_term f c e v
88    | B.Bind (b, t) -> 
89       let f c = count_term f c (B.push e b) t in
90       count_term_binder f c e b
91
92 let count_entity f c = function
93    | _, u, Y.Abst w -> 
94       let c = {c with
95          eabsts = succ c.eabsts; nodes = succ c.nodes; uris = u :: c.uris
96       } in
97       count_term f c B.empty_lenv w
98    | _, _, Y.Abbr v ->  
99       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
100       count_term f c B.empty_lenv v
101    | _, _, Y.Void -> assert false
102
103 let print_counters f c =
104    let terms =
105       c.tsorts + c.tgrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
106       c.tabbrs
107    in
108    let items = c.eabsts + c.eabbrs in
109    let nodes = c.nodes + c.xnodes in
110    L.warn (P.sprintf "  Kernel representation summary (basic_rg)");
111    L.warn (P.sprintf "    Total entry items:        %7u" items);
112    L.warn (P.sprintf "      Declaration items:      %7u" c.eabsts);
113    L.warn (P.sprintf "      Definition items:       %7u" c.eabbrs);
114    L.warn (P.sprintf "    Total term items:         %7u" terms);
115    L.warn (P.sprintf "      Sort items:             %7u" c.tsorts);
116    L.warn (P.sprintf "      Local reference items:  %7u" c.tlrefs);
117    L.warn (P.sprintf "      Global reference items: %7u" c.tgrefs);
118    L.warn (P.sprintf "      Explicit Cast items:    %7u" c.tcasts);
119    L.warn (P.sprintf "      Application items:      %7u" c.tappls);
120    L.warn (P.sprintf "      Abstraction items:      %7u" c.tabsts);
121    L.warn (P.sprintf "      Abbreviation items:     %7u" c.tabbrs);
122    L.warn (P.sprintf "    Global Int. Complexity:   %7u" c.nodes);
123    L.warn (P.sprintf "      + Abbreviation nodes:   %7u" nodes);
124    f ()
125
126 (* supplementary annotation *************************************************)
127
128 let attrs_of_binder f = function
129    | B.Abst (a, _) 
130    | B.Abbr (a, _)
131    | B.Void a      -> f a
132
133 let rec does_not_occur f n r = function
134    | B.Null           -> f true
135    | B.Cons (c, _, b) ->
136       let f n1 r1 =
137          if n1 = n && r1 = r then f false else does_not_occur f n r c
138       in
139       attrs_of_binder (Y.name C.err f) b 
140
141 let rename f c a =
142    let rec aux f c n r =
143       let f = function
144          | true  -> f n r
145          | false -> aux f c (n ^ "'") r
146       in
147       does_not_occur f n r c
148    in
149    let f n0 r0 =
150       let f n r = if n = n0 && r = r0 then f a else f (Y.Name (n, r) :: a) in
151       aux f c n0 r0 
152    in
153    Y.name C.err f a
154
155 let rename_bind f c = function
156    | B.Abst (a, w) -> let f a = f (B.abst a w) in rename f c a
157    | B.Abbr (a, v) -> let f a = f (B.abbr a v) in rename f c a
158    | B.Void a      -> let f a = f (B.Void a) in rename f c a
159
160 (* lenv/term pretty printing ************************************************)
161
162 let name frm a =
163    let f n = function 
164       | true  -> F.fprintf frm "%s" n
165       | false -> F.fprintf frm "^%s" n
166    in      
167    Y.name C.err f a
168
169 let rec pp_term c frm = function
170    | B.Sort (_, h)             -> 
171       let err _ = F.fprintf frm "@[*%u@]" h in
172       let f s = F.fprintf frm "@[%s@]" s in
173       H.get_sort err f h 
174    | B.LRef (_, i)             -> 
175       let err _ = F.fprintf frm "@[#%u@]" i in
176       let f _ = function
177          | B.Abst (a, _) 
178          | B.Abbr (a, _)
179          | B.Void a      -> F.fprintf frm "@[%a@]" name a
180       in
181       if !O.indexes then err () else B.get err f c i
182    | B.GRef (_, s)             ->
183       F.fprintf frm "@[$%s@]" (U.string_of_uri s)
184    | B.Cast (_, u, t)          ->
185       F.fprintf frm "@[{%a}.%a@]" (pp_term c) u (pp_term c) t
186    | B.Appl (_, v, t)          ->
187       F.fprintf frm "@[(%a).%a@]" (pp_term c) v (pp_term c) t
188    | B.Bind (B.Abst (a, w), t) ->
189       let f a =
190          let cc = B.push c (B.abst a w) in
191          F.fprintf frm "@[[%a:%a].%a@]" name a (pp_term c) w (pp_term cc) t
192       in
193       rename f c a
194    | B.Bind (B.Abbr (a, v), t) ->
195       let f a = 
196          let cc = B.push c (B.abbr a v) in
197          F.fprintf frm "@[[%a=%a].%a@]" name a (pp_term c) v (pp_term cc) t
198       in
199       rename f c a
200    | B.Bind (B.Void a, t)      ->
201       let f a = 
202          let cc = B.push c (B.Void a) in
203          F.fprintf frm "@[[%a].%a@]" name a (pp_term cc) t
204       in
205       rename f c a
206
207 let pp_lenv frm c =
208    let pp_entry f c = function
209       | B.Abst (a, w) -> 
210          let f a = F.fprintf frm "@,@[%a : %a@]" name a (pp_term c) w; f () in
211          rename f c a
212       | B.Abbr (a, v) -> 
213          let f a = F.fprintf frm "@,@[%a = %a@]" name a (pp_term c) v; f () in
214          rename f c a
215       | B.Void a      -> 
216          let f a = F.fprintf frm "@,%a" name a; f () in
217          rename f c a
218    in
219    B.rev_iter C.start pp_entry c
220
221 let specs = {
222    L.pp_term = pp_term; L.pp_lenv = pp_lenv
223 }
224
225 (* term xml printing ********************************************************)
226
227 let rec exp_term e t out tab = match t with
228    | B.Sort (a, l)    ->
229       let a =
230          let err _ = a in
231          let f s = Y.Name (s, true) :: a in
232          H.get_sort err f l
233       in
234       let attrs = [X.position l; X.name a] in
235       X.tag X.sort attrs out tab
236    | B.LRef (a, i)    ->
237       let a = 
238          let err _ = a in
239          let f n r = Y.Name (n, r) :: a in
240          let f _ b = attrs_of_binder (Y.name err f) b in
241          B.get err f e i
242       in
243       let attrs = [X.position i; X.name a] in
244       X.tag X.lref attrs out tab
245    | B.GRef (a, n)    ->
246       let a = Y.Name (U.name_of_uri n, true) :: a in
247       let attrs = [X.uri n; X.name a] in
248       X.tag X.gref attrs out tab
249    | B.Cast (a, u, t) ->
250       let attrs = [] in
251       X.tag X.cast attrs ~contents:(exp_term e u) out tab;
252       exp_term e t out tab
253    | B.Appl (a, v, t) ->
254       let attrs = [] in
255       X.tag X.appl attrs ~contents:(exp_term e v) out tab;
256       exp_term e t out tab
257    | B.Bind (b, t)    ->
258       let b = rename_bind C.start e b in
259       exp_bind e b out tab; 
260       exp_term (B.push e b) t out tab 
261
262 and exp_bind e b out tab = match b with
263    | B.Abst (a, w) ->
264       let attrs = [X.name a; X.mark a] in
265       X.tag X.abst attrs ~contents:(exp_term e w) out tab
266    | B.Abbr (a, v) ->
267       let attrs = [X.name a; X.mark a] in
268       X.tag X.abbr attrs ~contents:(exp_term e v) out tab
269    | B.Void a ->
270       let attrs = [X.name a; X.mark a] in
271       X.tag X.void attrs out tab
272
273 let export_term = exp_term B.empty_lenv