]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_rg/brgOutput.ml
new message reporting system improves performance significatively
[helm.git] / helm / software / helena / src / 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 C  = Cps
14 module U  = NUri
15 module L  = Log
16 module G  = Options
17 module E  = Entity
18 module H  = Hierarchy
19 module N  = Level
20 module XD = XmlCrg
21 module B  = Brg
22 module BD = BrgCrg
23
24 (* nodes count **************************************************************)
25
26 type counters = {
27    eabsts: int;
28    eabbrs: int;
29    evoids: int;
30    tsorts: int;
31    tlrefs: int;
32    tgrefs: int;
33    tcasts: int;
34    tappls: int;
35    tabsts: int;
36    tabbrs: int;
37    tvoids: int;
38    uris  : B.uri list;
39    nodes : int;
40    xnodes: int
41 }
42
43 let level = 2
44
45 let initial_counters = {
46    eabsts = 0; eabbrs = 0; evoids = 0; 
47    tsorts = 0; tlrefs = 0; tgrefs = 0; tcasts = 0; tappls = 0;
48    tabsts = 0; tabbrs = 0; tvoids = 0;
49    uris = []; nodes = 0; xnodes = 0
50 }
51
52 let rec count_term_binder f c e = function
53    | B.Abst (_, w) ->
54       let c = {c with tabsts = succ c.tabsts; nodes = succ c.nodes} in
55       count_term f c e w
56    | B.Abbr v      -> 
57       let c = {c with tabbrs = succ c.tabbrs; xnodes = succ c.xnodes} in
58       count_term f c e v
59    | B.Void        ->
60       let c = {c with tvoids = succ c.tvoids; xnodes = succ c.xnodes} in   
61       f c
62
63 and count_term f c e = function
64    | B.Sort _         -> 
65       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
66    | B.LRef (_, i)    -> 
67       begin match B.get e i with
68          | _, _, _, B.Abst _
69          | _, _, _, B.Void   ->
70             f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
71          | _, _, _, B.Abbr _ ->
72             f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
73       end      
74    | B.GRef (_, u)    -> 
75       let c =    
76          if Cps.list_mem ~eq:U.eq u c.uris
77          then {c with nodes = succ c.nodes}
78          else {c with xnodes = succ c.xnodes}
79       in
80       f {c with tgrefs = succ c.tgrefs}
81    | B.Cast (_, v, t) -> 
82       let c = {c with tcasts = succ c.tcasts} in
83       let f c = count_term f c e t in
84       count_term f c e v
85    | B.Appl (_, v, t) -> 
86       let c = {c with tappls = succ c.tappls; nodes = succ c.nodes} in
87       let f c = count_term f c e t in
88       count_term f c e v
89    | B.Bind (a, b, t) -> 
90       let f c = count_term f c (B.push e B.empty a b) t in
91       count_term_binder f c e b
92
93 let count_entity f c = function
94    | _, u, E.Abst (_, w) -> 
95       let c = {c with
96          eabsts = succ c.eabsts; nodes = succ c.nodes; uris = u :: c.uris
97       } in
98       count_term f c B.empty w
99    | _, _, E.Abbr v      ->  
100       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
101       count_term f c B.empty v
102    | _, _, E.Void        -> assert false
103
104 let print_counters f c =
105    let terms =
106       c.tsorts + c.tgrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
107       c.tabbrs
108    in
109    let items = c.eabsts + c.eabbrs in
110    let nodes = c.nodes + c.xnodes in
111    L.warn level (P.sprintf "Kernel representation summary (basic_rg)");
112    L.warn level (P.sprintf "  Total entry items:        %7u" items);
113    L.warn level (P.sprintf "    Declaration items:      %7u" c.eabsts);
114    L.warn level (P.sprintf "    Definition items:       %7u" c.eabbrs);
115    L.warn level (P.sprintf "  Total term items:         %7u" terms);
116    L.warn level (P.sprintf "    Sort items:             %7u" c.tsorts);
117    L.warn level (P.sprintf "    Local reference items:  %7u" c.tlrefs);
118    L.warn level (P.sprintf "    Global reference items: %7u" c.tgrefs);
119    L.warn level (P.sprintf "    Explicit Cast items:    %7u" c.tcasts);
120    L.warn level (P.sprintf "    Application items:      %7u" c.tappls);
121    L.warn level (P.sprintf "    Abstraction items:      %7u" c.tabsts);
122    L.warn level (P.sprintf "    Abbreviation items:     %7u" c.tabbrs);
123    L.warn level (P.sprintf "  Global Int. Complexity:   %7u" c.nodes);
124    L.warn level (P.sprintf "    + Abbreviation nodes:   %7u" nodes);
125    f ()
126
127 (* supplementary annotation *************************************************)
128
129 let rec does_not_occur f n r = function
130    | B.Null              -> f true
131    | B.Cons (e, _, a, _) ->
132       let f n1 r1 =
133          if n1 = n && r1 = r then f false else does_not_occur f n r e
134       in
135       E.name C.err f a 
136
137 let rename f e a =
138    let rec aux f e n r =
139       let f = function
140          | true  -> f n r
141          | false -> aux f e (n ^ "_") r
142       in
143       does_not_occur f n r e
144    in
145    let f n0 r0 =
146       let f n r = if n = n0 && r = r0 then f a else f (E.Name (n, r) :: a) in
147       aux f e n0 r0 
148    in
149    E.name C.err f a
150
151 (* lenv/term pretty printing ************************************************)
152
153 let name err och a =
154    let f n = function 
155       | true  -> P.fprintf och "%s" n
156       | false -> P.fprintf och "-%s" n
157    in      
158    E.name err f a
159
160 let pp_level och n =
161    if N.is_infinite n then () else P.fprintf och "^%s" (N.to_string n)
162
163 let rec pp_term e och = function
164    | B.Sort (_, h)           -> 
165       let err _ = P.fprintf och "*%u" h in
166       let f s = P.fprintf och "%s" s in
167       H.string_of_sort err f h 
168    | B.LRef (_, i)           -> 
169       let err _ = P.fprintf och "#%u" i in
170       if !G.indexes then err () else      
171       let _, _, a, b = B.get e i in
172       P.fprintf och "%a" (name err) a
173    | B.GRef (_, s)           ->
174       P.fprintf och "$%s" (U.string_of_uri s)
175    | B.Cast (_, u, t)        ->
176       P.fprintf och "{%a}.%a" (pp_term e) u (pp_term e) t
177    | B.Appl (_, v, t)        ->
178       P.fprintf och "(%a).%a" (pp_term e) v (pp_term e) t
179    | B.Bind (a, B.Abst (n, w), t) ->
180       let f a =
181          let ee = B.push e B.empty a (B.abst n w) in
182          P.fprintf och "[%a:%a]%a.%a" (name C.err) a (pp_term e) w pp_level n (pp_term ee) t
183       in
184       rename f e a
185    | B.Bind (a, B.Abbr v, t) ->
186       let f a = 
187          let ee = B.push e B.empty a (B.abbr v) in
188          P.fprintf och "[%a=%a].%a" (name C.err) a (pp_term e) v (pp_term ee) t
189       in
190       rename f e a
191    | B.Bind (a, B.Void, t)   ->
192       let f a = 
193          let ee = B.push e B.empty a B.Void in
194          P.fprintf och "[%a].%a" (name C.err) a (pp_term ee) t
195       in
196       rename f e a
197
198 let pp_lenv och e =
199    let pp_entry f e c a b x = f x (* match b with
200       | B.Abst (a, w) -> 
201          let f a = P.fprintf och "%a : %a\n" (name C.err) a (pp_term e) w; f a in
202          rename f x a
203       | B.Abbr (a, v) -> 
204          let f a = P.fprintf och "%a = %a\n" (name C.err) a (pp_term e) v; f a in
205          rename f c a
206       | B.Void a      -> 
207          let f a = P.fprintf och "%a\n" (name C.err) a; f a in
208          rename f c a
209 *)   in
210    let e = B.empty in
211    if e = B.empty then P.fprintf och "%s\n" "not shown" else
212    B.fold_right ignore pp_entry e B.empty
213
214 let specs = {
215    L.pp_term = pp_term; L.pp_lenv = pp_lenv
216 }
217
218 (* term xml printing ********************************************************)
219
220 let export_term =
221    BD.crg_of_brg XD.export_term