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