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