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