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