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