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