]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_rg/brgOutput.ml
- simpler attribute system
[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 let rec count_term_binder f c e = function
56    | B.Abst (_, _, w) ->
57       let c = {c with tabsts = succ c.tabsts; nodes = succ c.nodes} in
58       count_term f c e w
59    | B.Abbr v         -> 
60       let c = {c with tabbrs = succ c.tabbrs; xnodes = succ c.xnodes} in
61       count_term f c e v
62    | B.Void           ->
63       let c = {c with tvoids = succ c.tvoids; xnodes = succ c.xnodes} in   
64       f c
65
66 and count_term f c e = function
67    | B.Sort _         ->
68       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
69    | B.LRef (_, i)    ->
70       begin match B.get e i with
71          | _, _, _, _, B.Abst _
72          | _, _, _, _, B.Void   ->
73             f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
74          | _, _, _, _, B.Abbr _ ->
75             f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
76       end      
77    | B.GRef (_, u)    -> 
78       let c =    
79          if Cps.list_mem ~eq:U.eq u c.uris
80          then {c with nodes = succ c.nodes}
81          else {c with xnodes = succ c.xnodes}
82       in
83       f {c with tgrefs = succ c.tgrefs}
84    | B.Cast (v, t)    -> 
85       let c = {c with tcasts = succ c.tcasts} in
86       let f c = count_term f c e t in
87       count_term f c e v
88    | B.Appl (_, v, t) -> 
89       let c = {c with tappls = succ c.tappls; nodes = succ c.nodes} in
90       let f c = count_term f c e t in
91       count_term f c e v
92    | B.Bind (y, b, t) -> 
93       let f c = count_term f c (B.push e B.empty E.empty_node y b) t in
94       count_term_binder f c e b
95
96 let count_entity f c = function
97    | _, _, u, E.Abst w -> 
98       let c = {c with
99          eabsts = succ c.eabsts; nodes = succ c.nodes; uris = u :: c.uris
100       } in
101       count_term f c B.empty w
102    | _, _, _, E.Abbr v ->  
103       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
104       count_term f c B.empty v
105    | _, _, _, E.Void   -> assert false
106
107 let print_counters f c =
108    let terms =
109       c.tsorts + c.tlrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
110       c.tabbrs
111    in
112    let items = c.eabsts + c.eabbrs in
113    let nodes = c.nodes + c.xnodes in
114    L.warn level (KP.sprintf "Kernel representation summary (basic_rg)");
115    L.warn level (KP.sprintf "  Total entry items:        %7u" items);
116    L.warn level (KP.sprintf "    Declaration items:      %7u" c.eabsts);
117    L.warn level (KP.sprintf "    Definition items:       %7u" c.eabbrs);
118    L.warn level (KP.sprintf "  Total term items:         %7u" terms);
119    L.warn level (KP.sprintf "    Sort items:             %7u" c.tsorts);
120    L.warn level (KP.sprintf "    Local reference items:  %7u" c.tlrefs);
121    L.warn level (KP.sprintf "    Global reference items: %7u" c.tgrefs);
122    L.warn level (KP.sprintf "    Explicit Cast items:    %7u" c.tcasts);
123    L.warn level (KP.sprintf "    Application items:      %7u" c.tappls);
124    L.warn level (KP.sprintf "    Abstraction items:      %7u" c.tabsts);
125    L.warn level (KP.sprintf "    Abbreviation items:     %7u" c.tabbrs);
126    L.warn level (KP.sprintf "  Global Int. Complexity:   %7u" c.nodes);
127    L.warn level (KP.sprintf "    + Abbreviation nodes:   %7u" nodes);
128    f ()
129
130 (* lenv/term pretty printing ************************************************)
131
132 let name err och a =
133    let f n = function 
134       | true  -> KP.fprintf och "%s" n
135       | false -> KP.fprintf och "-%s" n
136    in
137    E.name err f a
138
139 let pp_reduced och x =
140    if x then KP.fprintf och "%s" "^"
141
142 let pp_level st och n =
143    KP.fprintf och "%s" (N.to_string st n)
144
145 let rec pp_term st e och = function
146    | B.Sort k                        -> 
147       let err _ = KP.fprintf och "*%u" k in
148       let f s = KP.fprintf och "%s" s in
149       H.string_of_sort err f k 
150    | B.LRef (_, i)                   -> 
151       let err _ = KP.fprintf och "#%u" i in
152       if !G.indexes then err () else      
153       let _, _, _, y, b = B.get e i in
154       KP.fprintf och "%a" (name err) y
155    | B.GRef (_, s)                   ->
156       let u = U.string_of_uri s in
157       KP.fprintf och "$%s" (if !G.short then KF.basename u else u)
158    | B.Cast (u, t)                   ->
159       KP.fprintf och "<%a>.%a" (pp_term st e) u (pp_term st e) t
160    | B.Appl (_, v, t)                ->
161       KP.fprintf och "(%a).%a" (pp_term st e) v (pp_term st e) t
162    | B.Bind (y, B.Abst (r, n, w), t) ->
163       let y = R.alpha B.mem e y in
164       let ee = B.push e B.empty E.empty_node y (B.abst r n w) in
165       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
166    | B.Bind (y, B.Abbr v, t)         ->
167       let y = R.alpha B.mem e y in
168       let ee = B.push e B.empty E.empty_node y (B.abbr v) in
169       KP.fprintf och "[%a=%a].%a" (name C.start) y (pp_term st e) v (pp_term st ee) t
170    | B.Bind (y, B.Void, t)           ->
171       let y = R.alpha B.mem e y in
172       let ee = B.push e B.empty E.empty_node y B.Void in
173       KP.fprintf och "[%a].%a" (name C.start) y (pp_term st ee) t
174
175 let pp_lenv st och e =
176    let pp_entry f c a y b x =
177       let y = R.alpha B.mem e y in
178       let x = B.push x c a y b in
179       match b with
180          | B.Abst (_, _, w) ->
181             KP.fprintf och "[%a : %a] " (name C.start) y (pp_term st c) w; f x
182          | B.Abbr v            ->
183             KP.fprintf och "[%a = %a] " (name C.start) y (pp_term st c) v; f x
184          | B.Void           ->
185             KP.fprintf och "[%a]" (name C.start) y; f x
186    in
187    if e = B.empty then KP.fprintf och "%s" "empty" else
188    B.fold_right ignore pp_entry e B.empty
189
190 let specs = {
191    L.pp_term = pp_term; L.pp_lenv = pp_lenv
192 }
193
194 (* term xml printing ********************************************************)
195
196 let export_term st =
197    BD.crg_of_brg (XD.export_term st)