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