]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/tacticAst2Box.ml
New argument (the hypothesis name) for cut.
[helm.git] / helm / ocaml / cic_transformations / tacticAst2Box.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (**************************************************************************)
27 (*                                                                        *)
28 (*                           PROJECT HELM                                 *)
29 (*                                                                        *)
30 (*                Andrea Asperti <asperti@cs.unibo.it>                    *)
31 (*                             18/2/2004                                  *)
32 (*                                                                        *)
33 (**************************************************************************)
34
35
36 open Ast2pres
37 open TacticAst
38
39 let rec count_tactic current_size tac =
40   if current_size > maxsize then current_size 
41   else match tac with 
42     | Absurd (_, term) -> countterm (current_size + 6) term
43     | Apply (_, term) -> countterm (current_size + 6) term
44     | Auto _ -> current_size + 4
45     | Assumption _ -> current_size + 10
46     | Compare (_, term) -> countterm (current_size + 7) term
47     | Constructor (_, n) -> current_size + 12
48     | Contradiction _ -> current_size + 13
49     | Cut (_, ident, term) ->
50        let id_size =
51         match ident with
52            None -> 0
53          | Some id -> String.length id + 4
54        in
55         countterm (current_size + 4 + id_size) term
56     | DecideEquality _ -> current_size + 15
57     | Decompose (_, term) ->
58        countterm (current_size + 11) term
59     | Discriminate (_, term) -> countterm (current_size + 12) term
60     | Elim (_, term, using) ->
61         let size1 = countterm (current_size + 5) term in
62           (match using with 
63            None -> size1
64              | Some term -> countterm (size1 + 7) term)
65     | ElimType (_, term) -> countterm (current_size + 10) term
66     | Exact (_, term) -> countterm (current_size + 6) term
67     | Exists _ -> current_size + 6
68     | Fold (_, kind, term) ->
69         countterm (current_size + 5) term
70     | Fourier _ -> current_size + 7
71     | Goal (_, n) -> current_size + 4 + int_of_float (ceil (log10 (float n)))
72     | Injection (_, term) ->
73        countterm (current_size + 10) term
74     | Intros (_, num, idents) ->
75         List.fold_left 
76           (fun size s -> size + (String.length s))
77           (current_size + 7) idents
78     | Left _ -> current_size + 4
79     | LetIn (_, term, ident) ->
80         countterm (current_size + 5 + String.length ident) term
81     | Reflexivity _ -> current_size + 11
82     | Replace (_, t1, t2) -> 
83         let size1 = countterm (current_size + 14) t1 in (* replace, with *)
84           countterm size1 t2    
85     | Right _ -> current_size + 5
86     | Ring _ -> current_size + 4
87     | Split _ -> current_size + 5
88     | Symmetry _ -> current_size + 8
89     | Transitivity (_, term) -> 
90         countterm (current_size + 13) term
91 ;;
92
93 let is_big_tac tac =
94   let n = (count_tactic 0 tac) in
95 (*   prerr_endline ("Lunghezza: " ^ (string_of_int n)); *)
96   n > maxsize
97 ;;
98
99 (* prova *)
100 let rec small_tactic2box tac =
101   Box.Text([], TacticAstPp.pp_tactic tac)
102
103 let string_of_kind = function
104   | `Reduce -> "reduce"
105   | `Simpl -> "simplify"
106   | `Whd -> "whd"
107   | `Normalize -> "normalize"
108
109 let dummy_tbl = Hashtbl.create 0
110
111 let ast2astBox ast = Ast2pres.ast2astBox (ast, dummy_tbl)
112
113 let pretty_append l ast =
114   if is_big ast then
115     [Box.H([],l);
116      Box.H([],[Box.skip; ast2astBox ast])]
117   else
118     [Box.H([],l@[Box.smallskip; ast2astBox ast])]
119
120 let rec tactic2box tac =
121   if (is_big_tac tac) then
122     big_tactic2box tac
123   else 
124     small_tactic2box tac
125
126 and big_tactic2box = function
127   | Absurd (_, term) ->
128       Box.V([],[Box.Text([],"absurd");
129                 ast2astBox term])
130   | Apply (_, term) -> 
131       Box.V([],[Box.Text([],"apply");
132                 ast2astBox term])
133   | Assumption _ -> Box.Text([],"assumption")
134   | Auto _ -> Box.Text([],"auto")
135   | Compare (_, term) ->
136       Box.V([],[Box.Text([],"compare");
137                 Box.indent(ast2astBox term)])
138   | Constructor (_,n) -> Box.Text ([],"constructor " ^ string_of_int n)
139   | Contradiction _ -> Box.Text([],"contradiction")
140   | DecideEquality _ -> Box.Text([],"decide equality")
141   | Decompose (_, term) ->
142       Box.V([],[Box.Text([],"decompose");
143                 Box.indent(ast2astBox term)])
144   | Discriminate (_, term) -> 
145       Box.V([],pretty_append [Box.Text([],"discriminate")] term)
146   | Elim (_, term, using) ->
147       let using =
148         (match using with 
149              None -> []
150            | Some term -> 
151                (pretty_append
152                   [Box.Text([],"using")]
153                   term)) in
154       Box.V([],
155             (pretty_append
156                [Box.Text([],"elim")]
157                term)@using)
158   | ElimType (_, term) -> 
159       Box.V([],[Box.Text([],"elim type");
160                 Box.indent(ast2astBox term)])
161   | Exact (_, term) -> 
162       Box.V([],[Box.Text([],"exact");
163                 Box.indent(ast2astBox term)])
164   | Exists _ -> Box.Text([],"exists")
165   | Fold (_, kind, term) ->
166       Box.V([],[Box.H([],[Box.Text([],"fold");
167                           Box.smallskip;
168                           Box.Text([],string_of_kind kind)]);
169                 Box.indent(ast2astBox term)])
170   | Fourier _ -> Box.Text([],"fourier")
171   | Goal (_, n) -> Box.Text([],"goal " ^ string_of_int n)
172   | Intros (_, num, idents) ->
173       let num =
174         (match num with 
175              None -> [] 
176            | Some num -> [Box.Text([],string_of_int num)]) in
177       let idents =
178         List.map (fun x -> Box.Text([],x)) idents in
179       Box.V([],[Box.Text([],"decompose");
180                 Box.H([],[Box.smallskip;
181                           Box.V([],idents)])])
182   | Left _ -> Box.Text([],"left")
183   | LetIn (_, term, ident) ->
184       Box.V([],[Box.H([],[Box.Text([],"let");
185                           Box.smallskip;
186                           Box.Text([],ident);
187                           Box.smallskip;
188                           Box.Text([],"=")]);
189                 Box.indent (ast2astBox term)])
190   | Reflexivity _ -> Box.Text([],"reflexivity")
191   | Replace (_, t1, t2) -> 
192       Box.V([],
193             (pretty_append 
194                [Box.Text([],"replace")]
195                t1)@
196             (pretty_append 
197                [Box.Text([],"with")]
198                t2))
199   | Right _ -> Box.Text([],"right")
200   | Ring _ ->  Box.Text([],"ring")
201   | Split _ -> Box.Text([],"split")
202   | Symmetry _ -> Box.Text([],"symmetry")
203   | Transitivity (_, term) -> 
204       Box.V([],[Box.Text([],"transitivity");
205                 Box.indent (ast2astBox term)])
206 ;;
207
208 open TacticAst
209
210 let rec tactical2box = function
211   | Tactic (_, tac) -> tactic2box tac
212 (*
213   | Command cmd -> (* TODO dummy implementation *)
214       Box.Text([], TacticAstPp.pp_tactical (Command cmd))
215 *)
216
217   | Fail _ -> Box.Text([],"fail")
218   | Do (_, count, tac) -> 
219       Box.V([],[Box.Text([],"do " ^ string_of_int count);
220                 Box.indent (tactical2box tac)])
221   | IdTac _ -> Box.Text([],"id")
222   | Repeat (_, tac) -> 
223       Box.V([],[Box.Text([],"repeat");
224                 Box.indent (tactical2box tac)])
225   | Seq (_, tacs) -> 
226       Box.V([],tacticals2box tacs)
227   | Then (_, tac, tacs) -> 
228       Box.V([],[tactical2box tac;
229                 Box.H([],[Box.skip;
230                           Box.Text([],"[");
231                           Box.V([],tacticals2box tacs);
232                           Box.Text([],"]");])])
233   | Tries (_, tacs) -> 
234       Box.V([],[Box.Text([],"try");
235                 Box.H([],[Box.skip;
236                           Box.Text([],"[");
237                           Box.V([],tacticals2box tacs);
238                           Box.Text([],"]");])])
239   | Try (_, tac) -> 
240       Box.V([],[Box.Text([],"try");
241                 Box.indent (tactical2box tac)])
242
243 and tacticals2box tacs =
244   List.map 
245     (function tac -> Box.H([],[tactical2box tac; Box.Text([],";")])) tacs
246 ;;
247
248 let tacticalPp tac =
249   String.concat "\n" 
250     (BoxPp.to_string CicAstPp.pp_term (tactical2box tac));;
251
252