]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/tacticAst2Box.ml
0eb2fbde130bb5a50a84e48d0fc79a94b5ae1fa1
[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 (_, term) -> countterm (current_size + 4) term
50     | DecideEquality _ -> current_size + 15
51     | Decompose (_, term) ->
52        countterm (current_size + 11) term
53     | Discriminate (_, term) -> countterm (current_size + 12) term
54     | Elim (_, term, using) ->
55         let size1 = countterm (current_size + 5) term in
56           (match using with 
57            None -> size1
58              | Some term -> countterm (size1 + 7) term)
59     | ElimType (_, term) -> countterm (current_size + 10) term
60     | Exact (_, term) -> countterm (current_size + 6) term
61     | Exists _ -> current_size + 6
62     | Fold (_, kind, term) ->
63         countterm (current_size + 5) term
64     | Fourier _ -> current_size + 7
65     | Goal (_, n) -> current_size + 4 + int_of_float (ceil (log10 (float n)))
66     | Injection (_, term) ->
67        countterm (current_size + 10) term
68     | Intros (_, num, idents) ->
69         List.fold_left 
70           (fun size s -> size + (String.length s))
71           (current_size + 7) idents
72     | Left _ -> current_size + 4
73     | LetIn (_, term, ident) ->
74         countterm (current_size + 5 + String.length ident) term
75     | Reflexivity _ -> current_size + 11
76     | Replace (_, t1, t2) -> 
77         let size1 = countterm (current_size + 14) t1 in (* replace, with *)
78           countterm size1 t2    
79     | Right _ -> current_size + 5
80     | Ring _ -> current_size + 4
81     | Split _ -> current_size + 5
82     | Symmetry _ -> current_size + 8
83     | Transitivity (_, term) -> 
84         countterm (current_size + 13) term
85 ;;
86
87 let is_big_tac tac =
88   let n = (count_tactic 0 tac) in
89 (*   prerr_endline ("Lunghezza: " ^ (string_of_int n)); *)
90   n > maxsize
91 ;;
92
93 (* prova *)
94 let rec small_tactic2box tac =
95   Box.Text([], TacticAstPp.pp_tactic tac)
96
97 let string_of_kind = function
98   | `Reduce -> "reduce"
99   | `Simpl -> "simplify"
100   | `Whd -> "whd"
101   | `Normalize -> "normalize"
102
103 let dummy_tbl = Hashtbl.create 0
104
105 let ast2astBox ast = Ast2pres.ast2astBox (ast, dummy_tbl)
106
107 let pretty_append l ast =
108   if is_big ast then
109     [Box.H([],l);
110      Box.H([],[Box.skip; ast2astBox ast])]
111   else
112     [Box.H([],l@[Box.smallskip; ast2astBox ast])]
113
114 let rec tactic2box tac =
115   if (is_big_tac tac) then
116     big_tactic2box tac
117   else 
118     small_tactic2box tac
119
120 and big_tactic2box = function
121   | Absurd (_, term) ->
122       Box.V([],[Box.Text([],"absurd");
123                 ast2astBox term])
124   | Apply (_, term) -> 
125       Box.V([],[Box.Text([],"apply");
126                 ast2astBox term])
127   | Assumption _ -> Box.Text([],"assumption")
128   | Auto _ -> Box.Text([],"auto")
129   | Compare (_, term) ->
130       Box.V([],[Box.Text([],"compare");
131                 Box.indent(ast2astBox term)])
132   | Constructor (_,n) -> Box.Text ([],"constructor " ^ string_of_int n)
133   | Contradiction _ -> Box.Text([],"contradiction")
134   | Cut (_, term) -> 
135       Box.V([],[Box.Text([],"cut");
136                 Box.indent(ast2astBox term)])
137   | DecideEquality _ -> Box.Text([],"decide equality")
138   | Decompose (_, term) ->
139       Box.V([],[Box.Text([],"decompose");
140                 Box.indent(ast2astBox term)])
141   | Discriminate (_, term) -> 
142       Box.V([],pretty_append [Box.Text([],"discriminate")] term)
143   | Elim (_, term, using) ->
144       let using =
145         (match using with 
146              None -> []
147            | Some term -> 
148                (pretty_append
149                   [Box.Text([],"using")]
150                   term)) in
151       Box.V([],
152             (pretty_append
153                [Box.Text([],"elim")]
154                term)@using)
155   | ElimType (_, term) -> 
156       Box.V([],[Box.Text([],"elim type");
157                 Box.indent(ast2astBox term)])
158   | Exact (_, term) -> 
159       Box.V([],[Box.Text([],"exact");
160                 Box.indent(ast2astBox term)])
161   | Exists _ -> Box.Text([],"exists")
162   | Fold (_, kind, term) ->
163       Box.V([],[Box.H([],[Box.Text([],"fold");
164                           Box.smallskip;
165                           Box.Text([],string_of_kind kind)]);
166                 Box.indent(ast2astBox term)])
167   | Fourier _ -> Box.Text([],"fourier")
168   | Goal (_, n) -> Box.Text([],"goal " ^ string_of_int n)
169   | Intros (_, num, idents) ->
170       let num =
171         (match num with 
172              None -> [] 
173            | Some num -> [Box.Text([],string_of_int num)]) in
174       let idents =
175         List.map (fun x -> Box.Text([],x)) idents in
176       Box.V([],[Box.Text([],"decompose");
177                 Box.H([],[Box.smallskip;
178                           Box.V([],idents)])])
179   | Left _ -> Box.Text([],"left")
180   | LetIn (_, term, ident) ->
181       Box.V([],[Box.H([],[Box.Text([],"let");
182                           Box.smallskip;
183                           Box.Text([],ident);
184                           Box.smallskip;
185                           Box.Text([],"=")]);
186                 Box.indent (ast2astBox term)])
187   | Reflexivity _ -> Box.Text([],"reflexivity")
188   | Replace (_, t1, t2) -> 
189       Box.V([],
190             (pretty_append 
191                [Box.Text([],"replace")]
192                t1)@
193             (pretty_append 
194                [Box.Text([],"with")]
195                t2))
196   | Right _ -> Box.Text([],"right")
197   | Ring _ ->  Box.Text([],"ring")
198   | Split _ -> Box.Text([],"split")
199   | Symmetry _ -> Box.Text([],"symmetry")
200   | Transitivity (_, term) -> 
201       Box.V([],[Box.Text([],"transitivity");
202                 Box.indent (ast2astBox term)])
203 ;;
204
205 open TacticAst
206
207 let rec tactical2box = function
208   | Tactic (_, tac) -> tactic2box tac
209 (*
210   | Command cmd -> (* TODO dummy implementation *)
211       Box.Text([], TacticAstPp.pp_tactical (Command cmd))
212 *)
213
214   | Fail _ -> Box.Text([],"fail")
215   | Do (_, count, tac) -> 
216       Box.V([],[Box.Text([],"do " ^ string_of_int count);
217                 Box.indent (tactical2box tac)])
218   | IdTac _ -> Box.Text([],"id")
219   | Repeat (_, tac) -> 
220       Box.V([],[Box.Text([],"repeat");
221                 Box.indent (tactical2box tac)])
222   | Seq (_, tacs) -> 
223       Box.V([],tacticals2box tacs)
224   | Then (_, tac, tacs) -> 
225       Box.V([],[tactical2box tac;
226                 Box.H([],[Box.skip;
227                           Box.Text([],"[");
228                           Box.V([],tacticals2box tacs);
229                           Box.Text([],"]");])])
230   | Tries (_, tacs) -> 
231       Box.V([],[Box.Text([],"try");
232                 Box.H([],[Box.skip;
233                           Box.Text([],"[");
234                           Box.V([],tacticals2box tacs);
235                           Box.Text([],"]");])])
236   | Try (_, tac) -> 
237       Box.V([],[Box.Text([],"try");
238                 Box.indent (tactical2box tac)])
239
240 and tacticals2box tacs =
241   List.map 
242     (function tac -> Box.H([],[tactical2box tac; Box.Text([],";")])) tacs
243 ;;
244
245 let tacticalPp tac =
246   String.concat "\n" 
247     (BoxPp.to_string CicAstPp.pp_term (tactical2box tac));;
248
249