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