]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/tacticAstPp.ml
52f7b1ab20099709575e37bcbd18eb61f2b96116
[helm.git] / helm / ocaml / cic_transformations / tacticAstPp.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 open Printf
27
28 open TacticAst
29
30 let tactical_terminator = "."
31 let tactic_terminator = tactical_terminator
32 let command_terminator = tactical_terminator
33 let tactical_separator = ";"
34
35 let pp_term_ast term = CicAstPp.pp_term term
36 let pp_term_cic term = CicPp.ppterm term
37
38 let pp_idents idents = "[" ^ String.concat "; " idents ^ "]"
39
40 let pp_reduction_kind = function
41   | `Reduce -> "reduce"
42   | `Simpl -> "simplify"
43   | `Whd -> "whd"
44   | `Normalize -> "normalize"
45  
46   
47 let pp_pattern (hyp, goal) = 
48   let pp_hyp_pattern l =
49     String.concat "; "
50       (List.map (fun (name, p) -> sprintf "%s : %s" name (pp_term_ast p)) l)
51   in
52   let pp_goal_pattern p = 
53     match p with 
54     | None -> ""
55     | Some p -> pp_term_ast p
56   in
57   let separator = 
58     if hyp <> [] then " \vdash " else " "
59   in
60   pp_hyp_pattern hyp ^ separator ^ pp_goal_pattern goal
61 let rec pp_tactic = function
62   | Absurd (_, term) -> "absurd" ^ pp_term_ast term
63   | Apply (_, term) -> "apply " ^ pp_term_ast term
64   | Auto _ -> "auto"
65   | Assumption _ -> "assumption"
66   | Change (_, t1, t2, where) ->
67       sprintf "change %s with %s%s" (pp_term_ast t1) (pp_term_ast t2)
68         (match where with None -> "" | Some ident -> "in " ^ ident)
69 (*   | Change_pattern (_, _, _, _) -> assert false  (* TODO *) *)
70   | Contradiction _ -> "contradiction"
71   | Cut (_, term) -> "cut " ^ pp_term_ast term
72   | Decompose (_, ident, principles) ->
73       sprintf "decompose %s %s" (pp_idents principles) ident
74   | Discriminate (_, term) -> "discriminate " ^ pp_term_ast term
75   | Elim (_, term, using) ->
76       sprintf "elim " ^ pp_term_ast term ^
77       (match using with None -> "" | Some term -> " using " ^ pp_term_ast term)
78   | ElimType (_, term) -> "elim type " ^ pp_term_ast term
79   | Exact (_, term) -> "exact " ^ pp_term_ast term
80   | Exists _ -> "exists"
81   | Fold (_, kind, term) ->
82       sprintf "fold %s %s" (pp_reduction_kind kind) (pp_term_ast term)
83   | Generalize (_, term, pattern) ->
84      sprintf "generalize %s %s" (pp_term_ast term) (pp_pattern pattern)
85   | Goal (_, n) -> "goal " ^ string_of_int n
86   | Fourier _ -> "fourier"
87   | Injection (_, ident) -> "injection " ^ ident
88   | Intros (_, None, []) -> "intro"
89   | Intros (_, num, idents) ->
90       sprintf "intros%s%s"
91         (match num with None -> "" | Some num -> " " ^ string_of_int num)
92         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
93   | Left _ -> "left"
94   | LetIn (_, term, ident) -> sprintf "let %s in %s" (pp_term_ast term) ident
95 (*   | Reduce (_, kind, None) *)
96 (*   | Reduce (_, kind, Some ([], `Goal)) -> pp_reduction_kind kind *)
97 (*   | Reduce (_, kind, Some ([], `Everywhere)) -> *)
98 (*       sprintf "%s in hyp" (pp_reduction_kind kind) *)
99 (*   | Reduce (_, kind, Some (terms, `Goal)) -> *)
100 (*       sprintf "%s %s" (pp_reduction_kind kind) *)
101 (*         (String.concat ", " (List.map pp_term_ast terms)) *)
102 (*   | Reduce (_, kind, Some (terms, `Everywhere)) -> *)
103 (*       sprintf "%s in hyp %s" (pp_reduction_kind kind) *)
104 (*         (String.concat ", " (List.map pp_term_ast terms)) *)
105   | Reduce (_, kind, pat) ->
106       sprintf "%s in %s" (pp_reduction_kind kind) (pp_pattern pat)
107   | Reflexivity _ -> "reflexivity"
108   | Replace (_, t1, t2) ->
109       sprintf "replace %s with %s" (pp_term_ast t1) (pp_term_ast t2)
110 (*   | Replace_pattern (_, _, _) -> assert false  (* TODO *) *)
111   | Rewrite (_, pos, t, pattern) -> 
112       sprintf "rewrite %s %s in %s" 
113         (if pos = `Left then "left" else "right") (pp_term_ast t)
114         (pp_pattern pattern)
115   | Right _ -> "right"
116   | Ring _ -> "ring"
117   | Split _ -> "split"
118   | Symmetry _ -> "symmetry"
119   | Transitivity (_, term) -> "transitivity " ^ pp_term_ast term
120   | FwdSimpl (_, ident) -> sprintf "fwd %s" ident
121
122 let pp_flavour = function
123   | `Definition -> "Definition"
124   | `Fact -> "Fact"
125   | `Goal -> "Goal"
126   | `Lemma -> "Lemma"
127   | `Remark -> "Remark"
128   | `Theorem -> "Theorem"
129
130 let pp_search_kind = function
131   | `Locate -> "locate"
132   | `Hint -> "hint"
133   | `Match -> "match"
134   | `Elim -> "elim"
135
136 let pp_macro pp_term = function 
137   (* Whelp *)
138   | WInstance (_, term) -> "whelp instance " ^ pp_term term
139   | WHint (_, t) -> "whelp hint " ^ pp_term t
140   | WLocate (_, s) -> "whelp locate " ^ s
141   | WElim (_, t) -> "whelp elim " ^ pp_term t
142   | WMatch (_, term) -> "whelp match " ^ pp_term term
143   (* real macros *)
144 (*   | Abort _ -> "Abort" *)
145   | Check (_, term) -> sprintf "Check %s" (pp_term term)
146   | Hint _ -> "hint"
147 (*   | Redo (_, None) -> "Redo"
148   | Redo (_, Some n) -> sprintf "Redo %d" n *)
149   | Search_pat (_, kind, pat) ->
150       sprintf "search %s \"%s\"" (pp_search_kind kind) pat
151   | Search_term (_, kind, term) ->
152       sprintf "search %s %s" (pp_search_kind kind) (pp_term term)
153 (*   | Undo (_, None) -> "Undo"
154   | Undo (_, Some n) -> sprintf "Undo %d" n *)
155   | Print (_, name) -> sprintf "Print \"%s\"" name
156   | Quit _ -> "Quit"
157
158 let pp_macro_ast = pp_macro pp_term_ast
159 let pp_macro_cic = pp_macro pp_term_cic
160
161 let pp_alias = function
162   | Ident_alias (id, uri) -> sprintf "alias id \"%s\" = \"%s\"" id uri
163   | Symbol_alias (symb, instance, desc) ->
164       sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
165         symb instance desc
166   | Number_alias (instance,desc) ->
167       sprintf "alias num (instance %d) = \"%s\"" instance desc
168   
169 let pp_params = function
170   | [] -> ""
171   | params ->
172       " " ^
173       String.concat " "
174         (List.map
175           (fun (name, typ) -> sprintf "(%s:%s)" name (pp_term_ast typ))
176           params)
177       
178 let pp_fields fields =
179   (if fields <> [] then "\n" else "") ^ 
180   String.concat ";\n" 
181     (List.map (fun (name,ty) -> " " ^ name ^ ": " ^ pp_term_ast ty) fields)
182         
183 let pp_obj = function
184  | Inductive (params, types) ->
185     let pp_constructors constructors =
186       String.concat "\n"
187         (List.map (fun (name, typ) -> sprintf "| %s: %s" name (pp_term_ast typ))
188           constructors)
189     in
190     let pp_type (name, _, typ, constructors) =
191       sprintf "\nwith %s: %s \\def\n%s" name (pp_term_ast typ)
192         (pp_constructors constructors)
193     in
194     (match types with
195     | [] -> assert false
196     | (name, inductive, typ, constructors) :: tl ->
197         let fst_typ_pp =
198           sprintf "%sinductive %s%s: %s \\def\n%s"
199             (if inductive then "" else "co") name (pp_params params)
200             (pp_term_ast typ) (pp_constructors constructors)
201         in
202         fst_typ_pp ^ String.concat "" (List.map pp_type tl))
203  | Theorem (flavour, name, typ, body) ->
204     sprintf "%s %s: %s %s"
205       (pp_flavour flavour)
206       name
207       (pp_term_ast typ)
208       (match body with
209       | None -> ""
210       | Some body -> "\\def " ^ pp_term_ast body)
211  | Record (params,name,ty,fields) ->
212     "record " ^ name ^ " " ^ pp_params params ^ " \\def {" ^
213     pp_fields fields ^ "}"
214
215
216 let pp_command = function
217   | Qed _ -> "qed"
218   | Set (_, name, value) -> sprintf "Set \"%s\" \"%s\"" name value
219   | Coercion (_,_) -> "Coercion IMPLEMENT ME!!!!!"
220   | Alias (_,s) -> pp_alias s
221   | Obj (_,obj) -> pp_obj obj
222
223 let rec pp_tactical = function
224   | Tactic (_, tac) -> pp_tactic tac
225
226   | Fail _ -> "fail"
227   | Do (_, count, tac) -> sprintf "do %d %s" count (pp_tactical tac)
228   | IdTac _ -> "id"
229   | Repeat (_, tac) -> "repeat " ^ pp_tactical tac
230   | Seq (_, tacs) -> pp_tacticals tacs
231   | Then (_, tac, tacs) ->
232       sprintf "%s [%s]" (pp_tactical tac) (pp_tacticals tacs)
233   | Tries (_, tacs) -> sprintf "tries [%s]" (pp_tacticals tacs)
234   | Try (_, tac) -> "try " ^ pp_tactical tac
235
236 and pp_tacticals tacs =
237   String.concat (tactical_separator ^ " ") (List.map pp_tactical tacs)
238
239 let pp_tactical tac = pp_tactical tac ^ tactical_terminator
240 let pp_tactic tac = pp_tactic tac ^ tactic_terminator
241 let pp_command tac = pp_command tac ^ command_terminator
242
243 let pp_executable = function
244   | Macro (_,x) -> pp_macro_ast x
245   | Tactical (_,x) -> pp_tactical x
246   | Command (_,x) -> pp_command x
247                       
248 let pp_comment = function
249   | Note (_,str) -> sprintf "(* %s *)" str
250   | Code (_,code) -> sprintf "(** %s. **)" (pp_executable code)
251
252 let pp_statement = function
253   | Executable (_, ex) -> pp_executable ex
254   | Comment (_, c) -> pp_comment c