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