]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/metaOutput.ml
Additional contribs.
[helm.git] / helm / software / lambda-delta / toplevel / metaOutput.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 U = NUri
15 module C = Cps
16 module L = Log
17 module Y = Entity
18 module M = Meta
19
20 type counters = {
21    eabsts: int;
22    eabbrs: int;
23    pabsts: int;    
24    tsorts: int;
25    tlrefs: int;
26    tgrefs: int;
27    pappls: int;
28    tappls: int;
29    tabsts: int;
30    uris  : U.uri list;
31    nodes : int;
32    xnodes: int
33 }
34
35 let initial_counters = {
36    eabsts = 0; eabbrs = 0; pabsts = 0; pappls = 0;
37    tsorts = 0; tlrefs = 0; tgrefs = 0; tappls = 0; tabsts = 0;
38    uris = []; nodes = 0; xnodes = 0
39 }
40
41 let rec count_term f c = function
42    | M.Sort _          -> 
43       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
44    | M.LRef _          -> 
45       f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
46    | M.GRef (_, u, ts) -> 
47       let c = {c with tgrefs = succ c.tgrefs} in
48       let c = {c with pappls = c.pappls + List.length ts} in
49       let c = {c with nodes = c.nodes + List.length ts} in
50       let c =    
51          if Cps.list_mem ~eq:U.eq u c.uris
52          then {c with nodes = succ c.nodes}
53          else {c with xnodes = succ c.xnodes}
54       in
55       Cps.list_fold_left f count_term c ts
56    | M.Appl (v, t)     -> 
57       let c = {c with tappls = succ c.tappls; nodes = succ c.nodes} in
58       let f c = count_term f c t in
59       count_term f c v
60    | M.Abst (_, w, t)  -> 
61       let c = {c with tabsts = succ c.tabsts; nodes = succ c.nodes} in
62       let f c = count_term f c t in
63       count_term f c w
64
65 let count_par f c (_, w) = count_term f c w
66
67 let count_xterm f c = function
68    | None   -> f c
69    | Some v -> count_term f c v
70
71 let count_entity f c = function
72    | _, u, Y.Abst (pars, w, xv) ->
73       let c = {c with eabsts = succ c.eabsts} in
74       let c = {c with pabsts = c.pabsts + List.length pars} in
75       let c = {c with uris = u :: c.uris; nodes = succ c.nodes + List.length pars} in
76       let f c = count_xterm f c xv in      
77       let f c = count_term f c w in
78       Cps.list_fold_left f count_par c pars   
79    | _, _, Y.Abbr (pars, w, xv) ->
80       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
81       let c = {c with pabsts = c.pabsts + List.length pars} in
82       let c = {c with nodes = c.nodes + List.length pars} in
83       let f c = count_xterm f c xv in
84       let f c = count_term f c w in
85       Cps.list_fold_left f count_par c pars
86    | _, _, Y.Void               -> assert false
87
88 let print_counters f c =
89    let terms = c.tsorts + c.tlrefs + c.tgrefs + c.tappls + c.tabsts in
90    let pars = c.pabsts + c.pappls in
91    let entries = c.eabsts + c.eabbrs in
92    let nodes = c.nodes + c.xnodes in
93    L.warn (P.sprintf "  Intermediate representation summary");
94    L.warn (P.sprintf "    Total entries:            %7u" entries);
95    L.warn (P.sprintf "      Declaration items:      %7u" c.eabsts);
96    L.warn (P.sprintf "      Definition items:       %7u" c.eabbrs);
97    L.warn (P.sprintf "    Total parameter items:    %7u" pars);
98    L.warn (P.sprintf "      Application items:      %7u" c.pappls);
99    L.warn (P.sprintf "      Abstraction items:      %7u" c.pabsts);
100    L.warn (P.sprintf "    Total term items:         %7u" terms);
101    L.warn (P.sprintf "      Sort items:             %7u" c.tsorts);
102    L.warn (P.sprintf "      Local reference items:  %7u" c.tlrefs);
103    L.warn (P.sprintf "      Global reference items: %7u" c.tgrefs);
104    L.warn (P.sprintf "      Application items:      %7u" c.tappls);
105    L.warn (P.sprintf "      Abstraction items:      %7u" c.tabsts);
106    L.warn (P.sprintf "    Global Int. Complexity:   %7u" c.nodes);
107    L.warn (P.sprintf "      + Abbreviation nodes:   %7u" nodes);
108    f ()
109
110 let string_of_sort = function
111    | true -> "Type"
112    | false -> "Prop"
113
114 let pp_transparent frm a =
115    let err () = F.fprintf frm "%s" "=" in
116    let f () = F.fprintf frm "%s" "~" in
117    Y.priv err f a
118
119 let pp_list pp opend sep closed frm l =
120    let rec aux frm = function
121       | []       -> ()
122       | [hd]     -> pp frm hd
123       | hd :: tl -> F.fprintf frm "%a%s%a" pp hd sep aux tl 
124    in
125    if l = [] then () else F.fprintf frm "%s%a%s" opend aux l closed
126
127 let pp_rev_list pp opend sep closed frm l =
128    pp_list pp opend sep closed frm (List.rev l)
129
130 let rec pp_args frm args = pp_list pp_term "(" "," ")" frm args
131
132 and pp_term frm = function
133    | M.Sort s            -> 
134       F.fprintf frm "@[*%s@]" (string_of_sort s)
135    | M.LRef (l, i)       ->
136       F.fprintf frm "@[%u@@#%u@]" l i
137    | M.GRef (l, uri, ts) ->
138       F.fprintf frm "@[%u@@$%s%a@]" l (U.string_of_uri uri) pp_args ts
139    | M.Appl (v, t)       ->
140       F.fprintf frm "@[(%a).%a@]" pp_term v pp_term t
141    | M.Abst (id, w, t)   ->
142       F.fprintf frm "@[[%s:%a].%a@]" id pp_term w pp_term t
143
144 let pp_par frm (id, w) =
145    F.fprintf frm "%s:%a" id pp_term w
146
147 let pp_pars = pp_rev_list pp_par "[" "," "]"
148
149 let pp_body a frm = function
150    | None   -> ()
151    | Some t -> F.fprintf frm "%a%a" pp_transparent a pp_term t
152
153 let pp_entity frm = function
154    | a, uri, Y.Abst (pars, u, body)
155    | a, uri, Y.Abbr (pars, u, body) ->
156       F.fprintf frm "@[%u@@%s%a%a:%a@]@\n%!" 
157          (Y.mark C.err C.start a) (U.string_of_uri uri) 
158          pp_pars pars (pp_body a) body pp_term u
159    | _, _, Y.Void                   -> assert false
160
161 let pp_entity f frm entity =
162    pp_entity frm entity; f ()