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