]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_rg/brgOutput.ml
- we are moving from old (patched) management of sort inclusion
[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
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 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 level = 2
45
46 let initial_counters = {
47    eabsts = 0; eabbrs = 0; evoids = 0; 
48    tsorts = 0; tlrefs = 0; tgrefs = 0; tcasts = 0; tappls = 0;
49    tabsts = 0; tabbrs = 0; tvoids = 0;
50    uris = []; nodes = 0; xnodes = 0
51 }
52
53 let rec count_term_binder f c e = function
54    | B.Abst (_, w) ->
55       let c = {c with tabsts = succ c.tabsts; nodes = succ c.nodes} in
56       count_term f c e w
57    | B.Abbr v      -> 
58       let c = {c with tabbrs = succ c.tabbrs; xnodes = succ c.xnodes} in
59       count_term f c e v
60    | B.Void        ->
61       let c = {c with tvoids = succ c.tvoids; xnodes = succ c.xnodes} in   
62       f c
63
64 and count_term f c e = function
65    | B.Sort _         -> 
66       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
67    | B.LRef (_, i)    -> 
68       begin match B.get e i with
69          | _, _, _, B.Abst _
70          | _, _, _, B.Void   ->
71             f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
72          | _, _, _, B.Abbr _ ->
73             f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
74       end      
75    | B.GRef (_, u)    -> 
76       let c =    
77          if Cps.list_mem ~eq:U.eq u c.uris
78          then {c with nodes = succ c.nodes}
79          else {c with xnodes = succ c.xnodes}
80       in
81       f {c with tgrefs = succ c.tgrefs}
82    | B.Cast (_, v, t) -> 
83       let c = {c with tcasts = succ c.tcasts} in
84       let f c = count_term f c e t in
85       count_term f c e v
86    | B.Appl (_, v, t) -> 
87       let c = {c with tappls = succ c.tappls; nodes = succ c.nodes} in
88       let f c = count_term f c e t in
89       count_term f c e v
90    | B.Bind (a, b, t) -> 
91       let f c = count_term f c (B.push e B.empty a b) t in
92       count_term_binder f c e b
93
94 let count_entity f c = function
95    | _, _, u, E.Abst w -> 
96       let c = {c with
97          eabsts = succ c.eabsts; nodes = succ c.nodes; uris = u :: c.uris
98       } in
99       count_term f c B.empty w
100    | _, _, _, E.Abbr v ->  
101       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
102       count_term f c B.empty v
103    | _, _, _, E.Void   -> assert false
104
105 let print_counters f c =
106    let terms =
107       c.tsorts + c.tgrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
108       c.tabbrs
109    in
110    let items = c.eabsts + c.eabbrs in
111    let nodes = c.nodes + c.xnodes in
112    L.warn level (P.sprintf "Kernel representation summary (basic_rg)");
113    L.warn level (P.sprintf "  Total entry items:        %7u" items);
114    L.warn level (P.sprintf "    Declaration items:      %7u" c.eabsts);
115    L.warn level (P.sprintf "    Definition items:       %7u" c.eabbrs);
116    L.warn level (P.sprintf "  Total term items:         %7u" terms);
117    L.warn level (P.sprintf "    Sort items:             %7u" c.tsorts);
118    L.warn level (P.sprintf "    Local reference items:  %7u" c.tlrefs);
119    L.warn level (P.sprintf "    Global reference items: %7u" c.tgrefs);
120    L.warn level (P.sprintf "    Explicit Cast items:    %7u" c.tcasts);
121    L.warn level (P.sprintf "    Application items:      %7u" c.tappls);
122    L.warn level (P.sprintf "    Abstraction items:      %7u" c.tabsts);
123    L.warn level (P.sprintf "    Abbreviation items:     %7u" c.tabbrs);
124    L.warn level (P.sprintf "  Global Int. Complexity:   %7u" c.nodes);
125    L.warn level (P.sprintf "    + Abbreviation nodes:   %7u" nodes);
126    f ()
127
128 (* supplementary annotation *************************************************)
129
130 let rec does_not_occur f n r = function
131    | B.Null              -> f true
132    | B.Cons (e, _, a, _) ->
133       let f n1 r1 =
134          if n1 = n && r1 = r then f false else does_not_occur f n r e
135       in
136       E.name C.err f a 
137
138 let rename f e a =
139    let rec aux f e n r =
140       let f = function
141          | true  -> f n r
142          | false -> aux f e (n ^ "_") r
143       in
144       does_not_occur f n r e
145    in
146    let f n0 r0 =
147       let f n r = if n = n0 && r = r0 then f a else f {a with E.n_name = Some (n, r)} in
148       aux f e n0 r0 
149    in
150    E.name C.err f a
151
152 (* lenv/term pretty printing ************************************************)
153
154 let name err och a =
155    let f n = function 
156       | true  -> P.fprintf och "%s" n
157       | false -> P.fprintf och "-%s" n
158    in      
159    E.name err f a
160
161 let pp_level st och n =
162    P.fprintf och "%s" (N.to_string st n)
163
164 let rec pp_term st e och = function
165    | B.Sort (_, h)           -> 
166       let err _ = P.fprintf och "*%u" h in
167       let f s = P.fprintf och "%s" s in
168       H.string_of_sort err f h 
169    | B.LRef (_, i)           -> 
170       let err _ = P.fprintf och "#%u" i in
171       if !G.indexes then err () else      
172       let _, _, a, b = B.get e i in
173       P.fprintf och "%a" (name err) a
174    | B.GRef (_, s)           ->
175       P.fprintf och "$%s" (U.string_of_uri s)
176    | B.Cast (_, u, t)        ->
177       P.fprintf och "{%a}.%a" (pp_term st e) u (pp_term st e) t
178    | B.Appl (_, v, t)        ->
179       P.fprintf och "(%a).%a" (pp_term st e) v (pp_term st e) t
180    | B.Bind (a, B.Abst (n, w), t) ->
181       let f a =
182          let ee = B.push e B.empty a (B.abst n w) in
183          P.fprintf och "%a[%a:%a].%a" (pp_level st) n (name C.err) a (pp_term st e) w (pp_term st ee) t
184       in
185       rename f e a
186    | B.Bind (a, B.Abbr v, t) ->
187       let f a = 
188          let ee = B.push e B.empty a (B.abbr v) in
189          P.fprintf och "[%a=%a].%a" (name C.err) a (pp_term st e) v (pp_term st ee) t
190       in
191       rename f e a
192    | B.Bind (a, B.Void, t)   ->
193       let f a = 
194          let ee = B.push e B.empty a B.Void in
195          P.fprintf och "[%a].%a" (name C.err) a (pp_term st ee) t
196       in
197       rename f e a
198
199 let pp_lenv st och e =
200    let pp_entry f e c a b x = f x (* match b with
201       | B.Abst (a, w) -> 
202          let f a = P.fprintf och "%a : %a\n" (name C.err) a (pp_term st e) w; f a in
203          rename f x a
204       | B.Abbr (a, v) -> 
205          let f a = P.fprintf och "%a = %a\n" (name C.err) a (pp_term st e) v; f a in
206          rename f c a
207       | B.Void a      -> 
208          let f a = P.fprintf och "%a\n" (name C.err) a; f a in
209          rename f c a
210 *)   in
211    let e = B.empty in
212    if e = B.empty then P.fprintf och "%s\n" "not shown" else
213    B.fold_right ignore pp_entry e B.empty
214
215 let specs = {
216    L.pp_term = pp_term; L.pp_lenv = pp_lenv
217 }
218
219 (* term xml printing ********************************************************)
220
221 let export_term st =
222    BD.crg_of_brg (XD.export_term st)