]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/tacticAst2Box.ml
renamed Http_client to Http_user_agent to avoid clashes with Gerd's
[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       LocatedTactic (_, tac) -> count_tactic current_size tac
43     | Absurd term -> countterm (current_size + 6) term
44     | Apply term -> countterm (current_size + 6) term
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     | Injection ident -> current_size + 10 + (String.length ident)
72     | Intros (num, idents) ->
73         List.fold_left 
74           (fun size s -> size + (String.length s))
75           (current_size + 7) idents
76     | Left -> current_size + 4
77     | LetIn (term, ident) ->
78         countterm (current_size + 5 + String.length ident) term
79     | Reduce (_, _, _) -> assert false  (* TODO *)
80     | Reflexivity -> current_size + 11
81     | Replace (t1, t2) -> 
82         let size1 = countterm (current_size + 14) t1 in (* replace, with *)
83           countterm size1 t2    
84     | Replace_pattern (_, _) -> assert false  (* TODO *)
85     | Rewrite (_, _, _) -> assert false (* TODO *)
86     | Right -> current_size + 5
87     | Ring -> current_size + 4
88     | Split -> current_size + 5
89     | Symmetry -> current_size + 8
90     | Transitivity term -> 
91         countterm (current_size + 13) term
92 ;;
93
94 let is_big_tac tac =
95   let n = (count_tactic 0 tac) in
96 (*   prerr_endline ("Lunghezza: " ^ (string_of_int n)); *)
97   n > maxsize
98 ;;
99
100 (* prova *)
101 let rec small_tactic2box tac =
102   Box.Text([], TacticAstPp.pp_tactic tac)
103
104 let string_of_kind = function
105   | `Reduce -> "reduce"
106   | `Simpl -> "simpl"
107   | `Whd -> "whd"
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     LocatedTactic (loc, tac) -> 
128       big_tactic2box tac
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   | Change (t1, t2, where) ->
137       let where =
138         (match where with 
139              None -> []
140            | Some ident -> 
141                [Box.Text([],"in");
142                 Box.smallskip;
143                 Box.Text([],ident)]) in
144         Box.V([],
145               (pretty_append 
146                  [Box.Text([],"change")]
147                  t1)@
148               (pretty_append 
149                  [Box.Text([],"with")]
150                  t2)@where)
151   | Change_pattern (_, _, _) -> assert false  (* TODO *)
152   | Contradiction -> Box.Text([],"contradiction")
153   | Cut term -> 
154       Box.V([],[Box.Text([],"cut");
155                 Box.indent(ast2astBox term)])
156   | Decompose (ident, principles) ->
157       let principles =
158         List.map (fun x -> Box.Text([],x)) principles in
159       Box.V([],[Box.Text([],"decompose");
160                 Box.H([],[Box.Text([],"[");
161                           Box.V([],principles);
162                           Box.Text([],"]")]);
163                 Box.Text([],ident)])
164   | Discriminate ident -> 
165       Box.V([],[Box.Text([],"discriminate");
166                 Box.Text([],ident)])
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   | Injection ident -> 
193       Box.V([],[Box.Text([],"transitivity");
194                 Box.indent (Box.Text([],ident))])
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 (_, _, _) -> assert false  (* TODO *)
214   | Reflexivity -> Box.Text([],"reflexivity")
215   | Replace (t1, t2) -> 
216       Box.V([],
217             (pretty_append 
218                [Box.Text([],"replace")]
219                t1)@
220             (pretty_append 
221                [Box.Text([],"with")]
222                t2))
223   | Replace_pattern (_, _) -> assert false  (* TODO *)
224   | Rewrite (_, _, _) -> assert false (* TODO *)
225   | Right -> Box.Text([],"right")
226   | Ring ->  Box.Text([],"ring")
227   | Split -> Box.Text([],"split")
228   | Symmetry -> Box.Text([],"symmetry")
229   | Transitivity term -> 
230       Box.V([],[Box.Text([],"transitivity");
231                 Box.indent (ast2astBox term)])
232 ;;
233
234 open TacticAst
235
236 let rec tactical2box = function
237   | LocatedTactical (loc, tac) -> tactical2box tac
238
239   | Tactic tac -> tactic2box tac
240   | Command cmd -> (* TODO dummy implementation *)
241       Box.Text([], TacticAstPp.pp_tactical (Command cmd))
242
243   | Fail -> Box.Text([],"fail")
244   | Do (count, tac) -> 
245       Box.V([],[Box.Text([],"do " ^ string_of_int count);
246                 Box.indent (tactical2box tac)])
247   | IdTac -> Box.Text([],"id")
248   | Repeat tac -> 
249       Box.V([],[Box.Text([],"repeat");
250                 Box.indent (tactical2box tac)])
251   | Seq tacs -> 
252       Box.V([],tacticals2box tacs)
253   | Then (tac, tacs) -> 
254       Box.V([],[tactical2box tac;
255                 Box.H([],[Box.skip;
256                           Box.Text([],"[");
257                           Box.V([],tacticals2box tacs);
258                           Box.Text([],"]");])])
259   | Tries tacs -> 
260       Box.V([],[Box.Text([],"try");
261                 Box.H([],[Box.skip;
262                           Box.Text([],"[");
263                           Box.V([],tacticals2box tacs);
264                           Box.Text([],"]");])])
265   | Try tac -> 
266       Box.V([],[Box.Text([],"try");
267                 Box.indent (tactical2box tac)])
268
269 and tacticals2box tacs =
270   List.map 
271     (function tac -> Box.H([],[tactical2box tac; Box.Text([],";")])) tacs
272 ;;
273
274 let tacticalPp tac =
275   String.concat "\n" 
276     (BoxPp.to_string CicAstPp.pp_term (tactical2box tac));;
277
278