]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/complete_rg/crgOutput.ml
new intermediate language complete_rg,
[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 E = Entity
17 module N = Level
18 module D = Crg
19
20 (* nodes count **************************************************************)
21
22 let level = 2
23
24 type counters = {
25    eabsts: int;
26    eabbrs: int;
27    evoids: int;
28    tsorts: int;
29    tlrefs: int;
30    tgrefs: int;
31    tcasts: int;
32    tappls: int;
33    tabsts: int;
34    tabbrs: int;
35    tvoids: int;
36    uris  : D.uri list;
37    nodes : int;
38    xnodes: int
39 }
40
41 let initial_counters = {
42    eabsts = 0; eabbrs = 0; evoids = 0; 
43    tsorts = 0; tlrefs = 0; tgrefs = 0; tcasts = 0; tappls = 0;
44    tabsts = 0; tabbrs = 0; tvoids = 0;
45    uris = []; nodes = 0; xnodes = 0
46 }
47
48 let rec count_term f c e = function
49    | D.TSort _          -> 
50       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
51    | D.TLRef (_, i)     -> 
52       begin match D.get e i with
53          | _, _, D.Abbr _ ->
54             f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
55          | _              ->
56             f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
57       end      
58    | D.TGRef (_, u)     -> 
59       let c =    
60          if C.list_mem ~eq:U.eq u c.uris
61          then {c with nodes = succ c.nodes}
62          else {c with xnodes = succ c.xnodes}
63       in
64       f {c with tgrefs = succ c.tgrefs}
65    | D.TCast (_, v, t)  -> 
66       let c = {c with tcasts = succ c.tcasts} in
67       let f c = count_term f c e t in
68       count_term f c e v
69    | D.TAppl (_, v, t)  -> 
70       let c = {c with tappls = succ c.tappls} in
71       let f c = count_term f c e t in
72       count_term f c e v
73    | D.TProj (_, d, t)  ->
74       D.shift (count_term f c e) d t
75    | D.TBind (a, b, t)  -> 
76       let f c e = count_term f c e t in
77       count_binder f c e a b
78
79 and count_binder f c e a b = match b with
80    | D.Abst (_, w) ->
81       let c = {c with tabsts = succ c.tabsts; nodes = succ c.nodes} in
82       let f c = D.push_bind (f c) a b e in
83       count_term f c e w
84    | D.Abbr v      -> 
85       let c = {c with tabbrs = succ c.tabbrs; xnodes = succ c.xnodes} in
86       let f c = D.push_bind (f c) a b e in
87       count_term f c e v     
88    | D.Void        ->
89       let c = {c with tvoids = succ c.tvoids; xnodes = succ c.xnodes} in
90       D.push_bind (f c) a b e
91
92 let count_entity f c = function
93    | _, u, E.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 D.ESort w
98    | _, _, E.Abbr v      ->  
99       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
100       count_term f c D.ESort v
101    | _, _, E.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 level (P.sprintf "Intermediate representation summary (complete_rg)");
111    L.warn level (P.sprintf "  Total entry items:        %7u" items);
112    L.warn level (P.sprintf "    Declaration items:      %7u" c.eabsts);
113    L.warn level (P.sprintf "    Definition items:       %7u" c.eabbrs);
114    L.warn level (P.sprintf "  Total term items:         %7u" terms);
115    L.warn level (P.sprintf "    Sort items:             %7u" c.tsorts);
116    L.warn level (P.sprintf "    Local reference items:  %7u" c.tlrefs);
117    L.warn level (P.sprintf "    Global reference items: %7u" c.tgrefs);
118    L.warn level (P.sprintf "    Explicit Cast items:    %7u" c.tcasts);
119    L.warn level (P.sprintf "    Application items:      %7u" c.tappls);
120    L.warn level (P.sprintf "    Abstraction items:      %7u" c.tabsts);
121    L.warn level (P.sprintf "    Abbreviation items:     %7u" c.tabbrs);
122    L.warn level (P.sprintf "  Global Int. Complexity:   %7u" c.nodes);
123    L.warn level (P.sprintf "    + Abbreviation nodes:   %7u" nodes);
124    f ()
125
126 (* term/environment pretty printer ******************************************)
127
128 let pp_attrs out a =
129    let map = function
130       | E.Name (s, true)  -> out (P.sprintf "%s;" s)
131       | E.Name (s, false) -> out (P.sprintf "~%s;" s)
132       | E.Apix i          -> out (P.sprintf "+%i;" i)
133       | E.Mark i          -> out (P.sprintf "@%i;" i)
134       | E.Meta _          -> ()
135       | E.Info _          -> ()
136    in
137    List.iter map a
138
139 let rec pp_term out = function
140    | D.TSort (a, l)    -> pp_attrs out a; out (P.sprintf "*%u" l)
141    | D.TLRef (a, i   ) -> pp_attrs out a; out (P.sprintf "#%u" i)
142    | D.TGRef (a, u)    -> pp_attrs out a; out (P.sprintf "$")
143    | D.TCast (a, x, y) -> pp_attrs out a; out "<"; pp_term out x; out ">."; pp_term out y
144    | D.TAppl (a, x, y) -> pp_attrs out a; out "("; pp_term out x; out ")."; pp_term out y
145    | D.TBind (a, x, y) -> pp_attrs out a; pp_bind out x; out "."; pp_term out y
146    | D.TProj (a, x, y) -> pp_attrs out a; out "{"; pp_lenv out x; out "}."; pp_term out y
147
148 and pp_bind out = function
149    | D.Abst (n, x) ->
150       out "[:"; pp_term out x; out "]";
151       if N.is_infinite n then () else out (N.to_string n)  
152    | D.Abbr x      -> out "[="; pp_term out x; out "]";
153    | D.Void        -> out "[]"
154
155 and pp_lenv out = function
156    | D.ESort           -> ()
157    | D.EBind (x, a, y) -> pp_lenv out x; pp_attrs out a; pp_bind out y
158    | D.EAppl (x, a, y) -> pp_lenv out x; out "("; pp_term out y; out ")"
159    | D.EProj (x, a, y) -> pp_lenv out x; out "{"; pp_lenv out y; out "}"