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