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