]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite/grafiteAstPp.ml
New tactic: inversion.
[helm.git] / helm / ocaml / grafite / grafiteAstPp.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 GrafiteAst
29
30 let tactical_terminator = ""
31 let tactic_terminator = tactical_terminator
32 let command_terminator = tactical_terminator
33
34 let pp_idents idents = "[" ^ String.concat "; " idents ^ "]"
35
36 let pp_reduction_kind ~term_pp = function
37   | `Normalize -> "normalize"
38   | `Reduce -> "reduce"
39   | `Simpl -> "simplify"
40   | `Unfold (Some t) -> "unfold " ^ term_pp t
41   | `Unfold None -> "unfold"
42   | `Whd -> "whd"
43  
44 let pp_tactic_pattern ~term_pp ~lazy_term_pp (what, hyp, goal) = 
45   let what_text =
46     match what with
47     | None -> ""
48     | Some t -> sprintf "in match (%s) " (lazy_term_pp t) in
49   let hyp_text =
50     String.concat " "
51       (List.map (fun (name, p) -> sprintf "%s:(%s)" name (term_pp p)) hyp) in
52   let goal_text =
53     match goal with
54     | None -> ""
55     | Some t -> sprintf "\\vdash (%s)" (term_pp t) in
56   sprintf "%sin %s%s" what_text hyp_text goal_text
57
58 let pp_intros_specs = function
59    | None, []         -> ""
60    | Some num, []     -> Printf.sprintf " names %i" num
61    | None, idents     -> Printf.sprintf " names %s" (pp_idents idents)
62    | Some num, idents -> Printf.sprintf " names %i %s" num (pp_idents idents)
63
64 let terms_pp ~term_pp terms = String.concat ", " (List.map term_pp terms)
65
66 let rec pp_tactic ~term_pp ~lazy_term_pp =
67   let pp_reduction_kind = pp_reduction_kind ~term_pp in
68   let pp_tactic_pattern = pp_tactic_pattern ~lazy_term_pp ~term_pp in
69   function
70   | Absurd (_, term) -> "absurd" ^ term_pp term
71   | Apply (_, term) -> "apply " ^ term_pp term
72   | Auto _ -> "auto"
73   | Assumption _ -> "assumption"
74   | Change (_, where, with_what) ->
75       sprintf "change %s with %s" (pp_tactic_pattern where) (lazy_term_pp with_what)
76   | Clear (_,id) -> sprintf "clear %s" id
77   | ClearBody (_,id) -> sprintf "clearbody %s" id
78   | Compare (_,term) -> "compare " ^ term_pp term
79   | Constructor (_,n) -> "constructor " ^ string_of_int n
80   | Contradiction _ -> "contradiction"
81   | Cut (_, ident, term) ->
82      "cut " ^ term_pp term ^
83       (match ident with None -> "" | Some id -> " as " ^ id)
84   | DecideEquality _ -> "decide equality"
85   | Decompose (_, [], what, names) ->
86       sprintf "decompose %s%s" what (pp_intros_specs (None, names)) 
87   | Decompose (_, types, what, names) ->
88       let to_ident = function
89          | Ident id -> id
90          | Type _   -> assert false 
91       in
92       let types = List.rev_map to_ident types in
93       sprintf "decompose %s %s%s" (pp_idents types) what (pp_intros_specs (None, names)) 
94   | Discriminate (_, term) -> "discriminate " ^ term_pp term
95   | Elim (_, term, using, num, idents) ->
96       sprintf "elim " ^ term_pp term ^
97       (match using with None -> "" | Some term -> " using " ^ term_pp term)
98       ^ pp_intros_specs (num, idents) 
99   | ElimType (_, term, using, num, idents) ->
100       sprintf "elim type " ^ term_pp term ^
101       (match using with None -> "" | Some term -> " using " ^ term_pp term)
102       ^ pp_intros_specs (num, idents)
103   | Exact (_, term) -> "exact " ^ term_pp term
104   | Exists _ -> "exists"
105   | Fold (_, kind, term, pattern) ->
106       sprintf "fold %s %s %s" (pp_reduction_kind kind)
107        (lazy_term_pp term) (pp_tactic_pattern pattern)
108   | FwdSimpl (_, hyp, idents) -> 
109       sprintf "fwd %s%s" hyp 
110         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
111   | Generalize (_, pattern, ident) ->
112      sprintf "generalize %s%s" (pp_tactic_pattern pattern)
113       (match ident with None -> "" | Some id -> " as " ^ id)
114   | Goal (_, n) -> "goal " ^ string_of_int n
115   | Fail _ -> "fail"
116   | Fourier _ -> "fourier"
117   | IdTac _ -> "id"
118   | Injection (_, term) -> "injection " ^ term_pp term
119   | Intros (_, None, []) -> "intro"
120   | Inversion (_, term) -> "inversion " ^ term_pp term
121   | Intros (_, num, idents) ->
122       sprintf "intros%s%s"
123         (match num with None -> "" | Some num -> " " ^ string_of_int num)
124         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
125   | LApply (_, level_opt, terms, term, ident_opt) -> 
126       sprintf "lapply %s%s%s%s" 
127         (match level_opt with None -> "" | Some i -> " depth = " ^ string_of_int i ^ " ")  
128         (term_pp term) 
129         (match terms with [] -> "" | _ -> " to " ^ terms_pp ~term_pp terms)
130         (match ident_opt with None -> "" | Some ident -> " using " ^ ident)
131   | Left _ -> "left"
132   | LetIn (_, term, ident) -> sprintf "let %s in %s" (term_pp term) ident
133   | Reduce (_, kind, pat) ->
134       sprintf "%s %s" (pp_reduction_kind kind) (pp_tactic_pattern pat)
135   | Reflexivity _ -> "reflexivity"
136   | Replace (_, pattern, t) ->
137       sprintf "replace %s with %s" (pp_tactic_pattern pattern) (lazy_term_pp t)
138   | Rewrite (_, pos, t, pattern) -> 
139       sprintf "rewrite %s %s %s" 
140         (if pos = `LeftToRight then ">" else "<")
141         (term_pp t)
142         (pp_tactic_pattern pattern)
143   | Right _ -> "right"
144   | Ring _ -> "ring"
145   | Split _ -> "split"
146   | Symmetry _ -> "symmetry"
147   | Transitivity (_, term) -> "transitivity " ^ term_pp term
148
149 let pp_search_kind = function
150   | `Locate -> "locate"
151   | `Hint -> "hint"
152   | `Match -> "match"
153   | `Elim -> "elim"
154   | `Instance -> "instance"
155
156 let pp_macro ~term_pp = function 
157   (* Whelp *)
158   | WInstance (_, term) -> "whelp instance " ^ term_pp term
159   | WHint (_, t) -> "whelp hint " ^ term_pp t
160   | WLocate (_, s) -> "whelp locate " ^ s
161   | WElim (_, t) -> "whelp elim " ^ term_pp t
162   | WMatch (_, term) -> "whelp match " ^ term_pp term
163   (* real macros *)
164   | Check (_, term) -> sprintf "Check %s" (term_pp term)
165   | Hint _ -> "hint"
166   | Search_pat (_, kind, pat) ->
167       sprintf "search %s \"%s\"" (pp_search_kind kind) pat
168   | Search_term (_, kind, term) ->
169       sprintf "search %s %s" (pp_search_kind kind) (term_pp term)
170   | Print (_, name) -> sprintf "Print \"%s\"" name
171   | Quit _ -> "Quit"
172
173 let pp_alias = function
174   | Ident_alias (id, uri) -> sprintf "alias id \"%s\" = \"%s\"" id uri
175   | Symbol_alias (symb, instance, desc) ->
176       sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
177         symb instance desc
178   | Number_alias (instance,desc) ->
179       sprintf "alias num (instance %d) = \"%s\"" instance desc
180   
181 let pp_argument_pattern = function
182   | CicNotationPt.IdentArg (eta_depth, name) ->
183       let eta_buf = Buffer.create 5 in
184       for i = 1 to eta_depth do
185         Buffer.add_string eta_buf "\\eta."
186       done;
187       sprintf "%s%s" (Buffer.contents eta_buf) name
188
189 let pp_l1_pattern = CicNotationPp.pp_term
190 let pp_l2_pattern = CicNotationPp.pp_term
191
192 let pp_associativity = function
193   | Gramext.LeftA -> "left associative"
194   | Gramext.RightA -> "right associative"
195   | Gramext.NonA -> "non associative"
196
197 let pp_precedence i = sprintf "with precedence %d" i
198
199 let pp_dir_opt = function
200   | None -> ""
201   | Some `LeftToRight -> "> "
202   | Some `RightToLeft -> "< "
203
204 let pp_interpretation dsc symbol arg_patterns cic_appl_pattern = 
205   sprintf "interpretation \"%s\" '%s %s = %s"
206     dsc symbol
207     (String.concat " " (List.map pp_argument_pattern arg_patterns))
208     (CicNotationPp.pp_cic_appl_pattern cic_appl_pattern)
209  
210 let pp_default what uris = 
211   sprintf "default \"%s\" %s" what
212     (String.concat " " (List.map UriManager.string_of_uri uris))
213
214 let pp_notation dir_opt l1_pattern assoc prec l2_pattern = 
215   sprintf "notation %s\"%s\" %s %s for %s"
216     (pp_dir_opt dir_opt)
217     (pp_l1_pattern l1_pattern)
218     (pp_associativity assoc)
219     (pp_precedence prec)
220     (pp_l2_pattern l2_pattern)
221     
222 let pp_coercion uri do_composites =
223    sprintf "coercion %s (* %s *)" (UriManager.string_of_uri uri)
224      (if do_composites then "compounds" else "no compounds")
225     
226 let pp_command ~obj_pp = function
227   | Include (_,path) -> "include " ^ path
228   | Qed _ -> "qed"
229   | Drop _ -> "drop"
230   | Set (_, name, value) -> sprintf "set \"%s\" \"%s\"" name value
231   | Coercion (_, uri, do_composites) -> pp_coercion uri do_composites
232   | Alias (_,s) -> pp_alias s
233   | Obj (_,obj) -> obj_pp obj
234   | Default (_,what,uris) ->
235       pp_default what uris
236   | Interpretation (_, dsc, (symbol, arg_patterns), cic_appl_pattern) ->
237       pp_interpretation dsc symbol arg_patterns cic_appl_pattern
238   | Notation (_, dir_opt, l1_pattern, assoc, prec, l2_pattern) ->
239       pp_notation dir_opt l1_pattern assoc prec l2_pattern
240   | Render _
241   | Dump _ -> assert false  (* ZACK: debugging *)
242
243 let rec pp_tactical ~term_pp ~lazy_term_pp =
244   let pp_tactic = pp_tactic ~lazy_term_pp ~term_pp in
245   let pp_tacticals = pp_tacticals ~lazy_term_pp ~term_pp in
246   function
247   | Tactic (_, tac) -> pp_tactic tac
248   | Do (_, count, tac) ->
249       sprintf "do %d %s" count (pp_tactical ~term_pp ~lazy_term_pp tac)
250   | Repeat (_, tac) -> "repeat " ^ pp_tactical ~term_pp ~lazy_term_pp tac
251   | Seq (_, tacs) -> pp_tacticals ~sep:"; " tacs
252   | Then (_, tac, tacs) ->
253       sprintf "%s; [%s]" (pp_tactical ~term_pp ~lazy_term_pp tac)
254         (pp_tacticals ~sep:" | " tacs)
255   | First (_, tacs) -> sprintf "tries [%s]" (pp_tacticals ~sep:" | " tacs)
256   | Try (_, tac) -> "try " ^ pp_tactical ~term_pp ~lazy_term_pp tac
257   | Solve (_, tac) -> sprintf "solve [%s]" (pp_tacticals ~sep:" | " tac)
258
259   | Dot _ -> "."
260   | Semicolon _ -> ";"
261   | Branch _ -> "["
262   | Shift _ -> "|"
263   | Pos (_, i) -> sprintf "%d:" i
264   | Merge _ -> "]"
265   | Focus (_, goals) ->
266       sprintf "focus %s" (String.concat " " (List.map string_of_int goals))
267   | Unfocus _ -> "unfocus"
268   | Skip _ -> "skip"
269
270 and pp_tacticals ~term_pp ~lazy_term_pp ~sep tacs =
271   String.concat sep (List.map (pp_tactical~lazy_term_pp ~term_pp) tacs)
272
273 let pp_executable ~term_pp ~lazy_term_pp ~obj_pp =
274   function
275   | Macro (_, macro) -> pp_macro ~term_pp macro
276   | Tactical (_, tac, Some punct) ->
277       pp_tactical ~lazy_term_pp ~term_pp tac
278       ^ pp_tactical ~lazy_term_pp ~term_pp punct
279   | Tactical (_, tac, None) -> pp_tactical ~lazy_term_pp ~term_pp tac
280   | Command (_, cmd) -> pp_command ~obj_pp cmd
281                       
282 let pp_comment ~term_pp ~lazy_term_pp ~obj_pp =
283   function
284   | Note (_,str) -> sprintf "(* %s *)" str
285   | Code (_,code) ->
286       sprintf "(** %s. **)" (pp_executable ~term_pp ~lazy_term_pp ~obj_pp code)
287
288 let pp_statement ~term_pp ~lazy_term_pp ~obj_pp =
289   function
290   | Executable (_, ex) -> pp_executable ~lazy_term_pp ~term_pp ~obj_pp ex
291   | Comment (_, c) -> pp_comment ~term_pp ~lazy_term_pp ~obj_pp c
292   
293 let pp_dependency = function
294   | IncludeDep str -> "include \"" ^ str ^ "\""
295   | BaseuriDep str -> "set \"baseuri\" \"" ^ str ^ "\""
296   | UriDep uri -> "uri \"" ^ UriManager.string_of_uri uri ^ "\""
297