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