]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/complete_rg/crgOutput.ml
c8d3bad88ec4448ba8590f2ad993e6f6eccd8fde
[helm.git] / helm / software / lambda-delta / 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 type counters = {
24    eabsts: int;
25    eabbrs: int;
26    evoids: int;
27    tsorts: int;
28    tlrefs: int;
29    tgrefs: int;
30    tcasts: int;
31    tappls: int;
32    tabsts: int;
33    tabbrs: int;
34    tvoids: int;
35    uris  : D.uri list;
36    nodes : int;
37    xnodes: int
38 }
39
40 let initial_counters = {
41    eabsts = 0; eabbrs = 0; evoids = 0; 
42    tsorts = 0; tlrefs = 0; tgrefs = 0; tcasts = 0; tappls = 0;
43    tabsts = 0; tabbrs = 0; tvoids = 0;
44    uris = []; nodes = 0; xnodes = 0
45 }
46
47 let rec shift t = function
48    | D.ESort           -> t
49    | D.EBind (e, a, b) -> shift (D.TBind (a, b, t)) e
50    | D.EProj (e, a, d) -> shift (D.TProj (a, d, t)) e
51
52 let rec count_term f c e = function
53    | D.TSort _          -> 
54       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
55    | D.TLRef (_, i, j)  -> 
56       begin match D.get e i with
57          | _, _, D.Abbr vs when j < List.length vs ->
58             f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
59          | _                                       ->
60             f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
61       end      
62    | D.TGRef (_, u)     -> 
63       let c =    
64          if C.list_mem ~eq:U.eq u c.uris
65          then {c with nodes = succ c.nodes}
66          else {c with xnodes = succ c.xnodes}
67       in
68       f {c with tgrefs = succ c.tgrefs}
69    | D.TCast (_, v, t)  -> 
70       let c = {c with tcasts = succ c.tcasts} in
71       let f c = count_term f c e t in
72       count_term f c e v
73    | D.TAppl (_, vs, t) -> 
74       let c = {c with tappls = succ c.tappls; nodes = succ c.nodes} in
75       let f c = count_term f c e t in
76       C.list_fold_right f (map1 e) vs c
77    | D.TProj (a, d, t)  ->
78       count_term f c e (shift t d)
79    | D.TBind (a, b, t)  -> 
80       let f c e = count_term f c e t in
81       count_binder f c e a b
82
83 and count_binder f c e a = function
84    | D.Abst (n, ws) ->      
85       let k = List.length ws in
86       let c = {c with tabsts = c.tabsts + k; nodes = c.nodes + k} in
87       let e = D.push_bind C.start e a (D.Abst (n, [])) in
88       let f (c, e) = f c e in
89       C.list_fold_right f map2 ws (c, e)
90    | D.Abbr vs      -> 
91       let k = List.length vs in
92       let c = {c with tabbrs = c.tabbrs + k; xnodes = c.xnodes + k} in
93       let e = D.push_bind C.start e a (D.Abbr []) in
94       let f (c, e) = f c e in
95       C.list_fold_right f map2 vs (c, e)
96    | D.Void k       ->
97       let c = {c with tvoids = c.tvoids + k; xnodes = c.xnodes + k} in   
98       let e = D.push_bind C.start e a (D.Void k) in
99       f c e
100
101 and map1 e f t c = count_term f c e t
102
103 and map2 f t (c, e) =
104    let f c e = f (c, e) in
105    let f c = D.push2 C.err (f c) e ~t () in
106    count_term f c e t
107
108 let count_entity f c = function
109    | _, u, E.Abst (_, w) -> 
110       let c = {c with
111          eabsts = succ c.eabsts; nodes = succ c.nodes; uris = u :: c.uris
112       } in
113       count_term f c D.ESort w
114    | _, _, E.Abbr v      ->  
115       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
116       count_term f c D.ESort v
117    | _, _, E.Void        -> assert false
118
119 let print_counters f c =
120    let terms =
121       c.tsorts + c.tgrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
122       c.tabbrs
123    in
124    let items = c.eabsts + c.eabbrs in
125    let nodes = c.nodes + c.xnodes in
126    L.warn (P.sprintf "  Intermediate representation summary (crg)");
127    L.warn (P.sprintf "    Total entry items:        %7u" items);
128    L.warn (P.sprintf "      Declaration items:      %7u" c.eabsts);
129    L.warn (P.sprintf "      Definition items:       %7u" c.eabbrs);
130    L.warn (P.sprintf "    Total term items:         %7u" terms);
131    L.warn (P.sprintf "      Sort items:             %7u" c.tsorts);
132    L.warn (P.sprintf "      Local reference items:  %7u" c.tlrefs);
133    L.warn (P.sprintf "      Global reference items: %7u" c.tgrefs);
134    L.warn (P.sprintf "      Explicit Cast items:    %7u" c.tcasts);
135    L.warn (P.sprintf "      Application items:      %7u" c.tappls);
136    L.warn (P.sprintf "      Abstraction items:      %7u" c.tabsts);
137    L.warn (P.sprintf "      Abbreviation items:     %7u" c.tabbrs);
138    L.warn (P.sprintf "    Global Int. Complexity:   %7u" c.nodes);
139    L.warn (P.sprintf "      + Abbreviation nodes:   %7u" nodes);
140    f ()
141
142 (* term/environment pretty printer ******************************************)
143
144 let pp_attrs out a =
145    let map = function
146       | E.Name (s, true)  -> out (P.sprintf "%s;" s)
147       | E.Name (s, false) -> out (P.sprintf "~%s;" s)
148       | E.Apix i          -> out (P.sprintf "+%i;" i)
149       | E.Mark i          -> out (P.sprintf "@%i;" i)
150       | E.Meta s          -> out (P.sprintf "\"%s\";" s)
151       | E.Priv            -> out (P.sprintf "%s;" "~")
152    in
153    List.iter map a
154
155 let rec pp_term out = function
156    | D.TSort (a, l)    -> pp_attrs out a; out (P.sprintf "*%u" l)
157    | D.TLRef (a, i, j) -> pp_attrs out a; out (P.sprintf "#(%u,%u)" i j)
158    | D.TGRef (a, u)    -> pp_attrs out a; out (P.sprintf "$")
159    | D.TCast (a, x, y) -> pp_attrs out a; out "<"; pp_term out x; out ">."; pp_term out y
160    | D.TProj (a, x, y) -> assert false
161    | D.TAppl (a, x, y) -> pp_attrs out a; pp_terms "(" ")" out x; pp_term out y
162    | D.TBind (a, x, y) -> pp_attrs out a; pp_bind out x; pp_term out y
163
164 and pp_terms bg eg out vs =
165    let rec aux = function
166       | []      -> ()
167       | [v]     -> pp_term out v
168       | v :: vs -> pp_term out v; out ", "; aux vs
169    in
170    out bg; aux vs; out (eg ^ ".")
171
172 and pp_bind out = function
173    | D.Abst (n, x) when N.is_infinite n -> pp_terms "[:" "]" out x
174    | D.Abst (n, x)                      -> 
175       pp_terms "[:" (P.sprintf "]^%s" (N.to_string n)) out x
176    | D.Abbr x                           -> pp_terms "[=" "]" out x
177    | D.Void x                           -> out (P.sprintf "[%u]" x)
178
179 let rec pp_lenv out = function
180    | D.ESort           -> ()
181    | D.EProj (x, a, y) -> assert false
182    | D.EBind (x, a, y) -> pp_lenv out x; pp_attrs out a; pp_bind out y