]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/complete_rg/crgOutput.ml
new message reporting system improves performance significatively
[helm.git] / helm / software / helena / src / complete_rg / crgOutput.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 U = NUri
14 module C = Cps
15 module L = Log
16 module H = Hierarchy
17 module E = Entity
18 module N = Level
19 module D = Crg
20
21 (* nodes count **************************************************************)
22
23 let level = 2
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  : D.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 f c e = function
50    | D.TSort _          -> 
51       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
52    | D.TLRef (_, i, j)  -> 
53       begin match D.get e i with
54          | _, _, D.Abbr vs when j < List.length vs ->
55             f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
56          | _                                       ->
57             f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
58       end      
59    | D.TGRef (_, u)     -> 
60       let c =    
61          if C.list_mem ~eq:U.eq u c.uris
62          then {c with nodes = succ c.nodes}
63          else {c with xnodes = succ c.xnodes}
64       in
65       f {c with tgrefs = succ c.tgrefs}
66    | D.TCast (_, v, t)  -> 
67       let c = {c with tcasts = succ c.tcasts} in
68       let f c = count_term f c e t in
69       count_term f c e v
70    | D.TAppl (_, vs, t) -> 
71       let c = {c with tappls = succ c.tappls; nodes = succ c.nodes} in
72       let f c = count_term f c e t in
73       C.list_fold_right f (map1 e) vs c
74    | D.TProj (a, d, t)  ->
75       count_term f c e (D.tshift d t)
76    | D.TBind (a, b, t)  -> 
77       let f c e = count_term f c e t in
78       count_binder f c e a b
79
80 and count_binder f c e a = function
81    | D.Abst (n, ws) ->      
82       let k = List.length ws in
83       let c = {c with tabsts = c.tabsts + k; nodes = c.nodes + k} in
84       let e = D.push_bind C.start e a (D.Abst (n, [])) in
85       let f (c, e) = f c e in
86       C.list_fold_right f map2 ws (c, e)
87    | D.Abbr vs      -> 
88       let k = List.length vs in
89       let c = {c with tabbrs = c.tabbrs + k; xnodes = c.xnodes + k} in
90       let e = D.push_bind C.start e a (D.Abbr []) in
91       let f (c, e) = f c e in
92       C.list_fold_right f map2 vs (c, e)
93    | D.Void k       ->
94       let c = {c with tvoids = c.tvoids + k; xnodes = c.xnodes + k} in   
95       let e = D.push_bind C.start e a (D.Void k) in
96       f c e
97
98 and map1 e f t c = count_term f c e t
99
100 and map2 f t (c, e) =
101    let f c e = f (c, e) in
102    let f c = D.push2 C.err (f c) e ~t () in
103    count_term f c e t
104
105 let count_entity f c = function
106    | _, u, E.Abst (_, w) -> 
107       let c = {c with
108          eabsts = succ c.eabsts; nodes = succ c.nodes; uris = u :: c.uris
109       } in
110       count_term f c D.ESort w
111    | _, _, E.Abbr v      ->  
112       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
113       count_term f c D.ESort v
114    | _, _, E.Void        -> assert false
115
116 let print_counters f c =
117    let terms =
118       c.tsorts + c.tgrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
119       c.tabbrs
120    in
121    let items = c.eabsts + c.eabbrs in
122    let nodes = c.nodes + c.xnodes in
123    L.warn level (P.sprintf "Intermediate representation summary (complete_rg)");
124    L.warn level (P.sprintf "  Total entry items:        %7u" items);
125    L.warn level (P.sprintf "    Declaration items:      %7u" c.eabsts);
126    L.warn level (P.sprintf "    Definition items:       %7u" c.eabbrs);
127    L.warn level (P.sprintf "  Total term items:         %7u" terms);
128    L.warn level (P.sprintf "    Sort items:             %7u" c.tsorts);
129    L.warn level (P.sprintf "    Local reference items:  %7u" c.tlrefs);
130    L.warn level (P.sprintf "    Global reference items: %7u" c.tgrefs);
131    L.warn level (P.sprintf "    Explicit Cast items:    %7u" c.tcasts);
132    L.warn level (P.sprintf "    Application items:      %7u" c.tappls);
133    L.warn level (P.sprintf "    Abstraction items:      %7u" c.tabsts);
134    L.warn level (P.sprintf "    Abbreviation items:     %7u" c.tabbrs);
135    L.warn level (P.sprintf "  Global Int. Complexity:   %7u" c.nodes);
136    L.warn level (P.sprintf "    + Abbreviation nodes:   %7u" nodes);
137    f ()
138
139 (* term/environment pretty printer ******************************************)
140
141 let pp_attrs out a =
142    let map = function
143       | E.Name (s, true)  -> out (P.sprintf "%s;" s)
144       | E.Name (s, false) -> out (P.sprintf "~%s;" s)
145       | E.Apix i          -> out (P.sprintf "+%i;" i)
146       | E.Mark i          -> out (P.sprintf "@%i;" i)
147       | E.Meta _          -> ()
148       | E.Info _          -> ()
149    in
150    List.iter map a
151
152 let rec pp_term out = function
153    | D.TSort (a, l)    -> pp_attrs out a; out (P.sprintf "*%u" l)
154    | D.TLRef (a, i, j) -> pp_attrs out a; out (P.sprintf "#(%u,%u)" i j)
155    | D.TGRef (a, u)    -> pp_attrs out a; out (P.sprintf "$")
156    | D.TCast (a, x, y) -> pp_attrs out a; out "<"; pp_term out x; out ">."; pp_term out y
157    | D.TProj (a, x, y) -> assert false
158    | D.TAppl (a, x, y) -> pp_attrs out a; pp_terms "(" ")" out x; pp_term out y
159    | D.TBind (a, x, y) -> pp_attrs out a; pp_bind out x; pp_term out y
160
161 and pp_terms bg eg out vs =
162    let rec aux = function
163       | []      -> ()
164       | [v]     -> pp_term out v
165       | v :: vs -> pp_term out v; out ", "; aux vs
166    in
167    out bg; aux vs; out (eg ^ ".")
168
169 and pp_bind out = function
170    | D.Abst (n, x) when N.is_infinite n -> pp_terms "[:" "]" out x
171    | D.Abst (n, x)                      -> 
172       pp_terms "[:" (P.sprintf "]^%s" (N.to_string n)) out x
173    | D.Abbr x                           -> pp_terms "[=" "]" out x
174    | D.Void x                           -> out (P.sprintf "[%u]" x)
175
176 let rec pp_lenv out = function
177    | D.ESort           -> ()
178    | D.EProj (x, a, y) -> assert false
179    | D.EBind (x, a, y) -> pp_lenv out x; pp_attrs out a; pp_bind out y