]> matita.cs.unibo.it Git - helm.git/blob - components/grafite/grafiteAstPp.ml
elim tactic: it needs two arguments, a term as well as a pattern
[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   | `Normalize -> "normalize"
40   | `Reduce -> "reduce"
41   | `Simpl -> "simplify"
42   | `Unfold (Some t) -> "unfold " ^ term_pp t
43   | `Unfold None -> "unfold"
44   | `Whd -> "whd"
45  
46 let pp_tactic_pattern ~term_pp ~lazy_term_pp (what, hyp, goal) = 
47   if what = None && hyp = [] && goal = None then "" else 
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 opt_string_pp = function
70    | None -> ""
71    | Some what -> what ^ " "
72
73 let rec pp_tactic ~term_pp ~lazy_term_pp =
74   let pp_reduction_kind = pp_reduction_kind ~term_pp in
75   let pp_tactic_pattern = pp_tactic_pattern ~lazy_term_pp ~term_pp in
76   function
77   | Absurd (_, term) -> "absurd" ^ term_pp term
78   | Apply (_, term) -> "apply " ^ term_pp term
79   | ApplyS (_, term, params) ->
80      "applyS " ^ term_pp term ^
81       String.concat " " 
82         (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)
83   | Auto (_,params) -> "auto " ^ 
84       String.concat " " 
85         (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)
86   | Assumption _ -> "assumption"
87   | Cases (_, term, idents) -> sprintf "cases " ^ term_pp term ^
88       pp_intros_specs (None, idents) 
89   | Change (_, where, with_what) ->
90       sprintf "change %s with %s" (pp_tactic_pattern where) (lazy_term_pp with_what)
91   | Clear (_,ids) -> sprintf "clear %s" (pp_idents ids)
92   | ClearBody (_,id) -> sprintf "clearbody %s" id
93   | Constructor (_,n) -> "constructor " ^ string_of_int n
94   | Contradiction _ -> "contradiction"
95   | Cut (_, ident, term) ->
96      "cut " ^ term_pp term ^
97       (match ident with None -> "" | Some id -> " as " ^ id)
98   | Decompose (_, names) ->
99       sprintf "decompose%s" (pp_intros_specs (None, names)) 
100   | Demodulate _ -> "demodulate"
101   | Destruct (_, term) -> "destruct " ^ term_pp term
102   | Elim (_, what, using, pattern, num, idents) ->
103       sprintf "elim %s%s %s%s" 
104       (term_pp what)
105       (match using with None -> "" | Some term -> " using " ^ term_pp term)
106       (pp_tactic_pattern pattern)
107       (pp_intros_specs (num, idents)) 
108   | ElimType (_, term, using, num, idents) ->
109       sprintf "elim type " ^ term_pp term ^
110       (match using with None -> "" | Some term -> " using " ^ term_pp term)
111       ^ pp_intros_specs (num, idents)
112   | Exact (_, term) -> "exact " ^ term_pp term
113   | Exists _ -> "exists"
114   | Fold (_, kind, term, pattern) ->
115       sprintf "fold %s %s %s" (pp_reduction_kind kind)
116        (lazy_term_pp term) (pp_tactic_pattern pattern)
117   | FwdSimpl (_, hyp, idents) -> 
118       sprintf "fwd %s%s" hyp 
119         (match idents with [] -> "" | idents -> " as " ^ pp_idents idents)
120   | Generalize (_, pattern, ident) ->
121      sprintf "generalize %s%s" (pp_tactic_pattern pattern)
122       (match ident with None -> "" | Some id -> " as " ^ id)
123   | Goal (_, n) -> "goal " ^ string_of_int n
124   | Fail _ -> "fail"
125   | Fourier _ -> "fourier"
126   | IdTac _ -> "id"
127   | Intros (_, None, []) -> "intros"
128   | Inversion (_, term) -> "inversion " ^ term_pp term
129   | Intros (_, num, idents) ->
130       sprintf "intros%s%s"
131         (match num with None -> "" | Some num -> " " ^ string_of_int num)
132         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
133   | LApply (_, linear, level_opt, terms, term, ident_opt) -> 
134       sprintf "lapply %s%s%s%s%s" 
135         (if linear then " linear " else "")
136         (match level_opt with None -> "" | Some i -> " depth = " ^ string_of_int i ^ " ")  
137         (term_pp term) 
138         (match terms with [] -> "" | _ -> " to " ^ terms_pp ~term_pp terms)
139         (match ident_opt with None -> "" | Some ident -> " as " ^ ident)
140   | Left _ -> "left"
141   | LetIn (_, term, ident) -> 
142      sprintf "letin %s \\def %s" ident (term_pp term)
143   | Reduce (_, kind, pat) ->
144       sprintf "%s %s" (pp_reduction_kind kind) (pp_tactic_pattern pat)
145   | Reflexivity _ -> "reflexivity"
146   | Replace (_, pattern, t) ->
147       sprintf "replace %s with %s" (pp_tactic_pattern pattern) (lazy_term_pp t)
148   | Rewrite (_, pos, t, pattern, names) -> 
149       sprintf "rewrite %s %s %s%s" 
150         (if pos = `LeftToRight then ">" else "<")
151         (term_pp t)
152         (pp_tactic_pattern pattern)
153         (if names = [] then "" else " as " ^ pp_idents names)
154   | Right _ -> "right"
155   | Ring _ -> "ring"
156   | Split _ -> "split"
157   | Subst _ -> "subst"
158   | Symmetry _ -> "symmetry"
159   | Transitivity (_, term) -> "transitivity " ^ term_pp term
160   (* Tattiche Aggiunte *)
161   | Assume (_, ident , term) -> "assume" ^ ident ^ ":" ^ term_pp term 
162   | Suppose (_, term, ident,term1) -> "suppose" ^ term_pp term ^ "("  ^ ident ^ ")" ^ (match term1 with None -> " " | Some term1 -> term_pp term1)
163   | Bydone (_, term) -> "by" ^ (match term with None -> "_" | Some term -> term_pp term) ^ "done"
164   | By_term_we_proved (_, term, term1, ident, term2) -> "by" ^ (match term with None -> "_" | Some term -> term_pp term)  ^ "we proved" ^ term_pp term1 ^ (match ident with None -> "" | Some ident -> "(" ^ident^ ")") ^
165        (match term2 with  None -> " " | Some term2 -> term_pp term2)
166   | We_need_to_prove (_, term, ident, term1) -> "we need to prove" ^ term_pp term ^ (match ident with None -> "" | Some ident -> "(" ^ ident ^ ")") ^ (match term1 with None -> " " | Some term1 -> term_pp term1)
167   | We_proceed_by_cases_on (_, term, term1) -> "we proceed by cases on" ^ term_pp term ^ "to prove" ^ term_pp term1
168   | We_proceed_by_induction_on (_, term, term1) -> "we proceed by induction on" ^ term_pp term ^ "to prove" ^ term_pp term1
169   | Byinduction (_, term, ident) -> "by induction hypothesis we know" ^ term_pp term ^ "(" ^ ident ^ ")"
170   | Thesisbecomes (_, term) -> "the thesis becomes " ^ term_pp term
171   | ExistsElim (_, term0, ident, term, ident1, term1) -> "by " ^ (match term0 with None -> "_" | Some term -> term_pp term) ^ "let " ^ ident ^ ":" ^ term_pp term ^ "such that " ^ lazy_term_pp term1 ^ "(" ^ ident1 ^ ")"
172   | AndElim (_, term, ident1, term1, ident2, term2) -> "by " ^ term_pp term ^ "we have " ^ term_pp term1 ^ " (" ^ ident1 ^ ") " ^ "and " ^ term_pp term2 ^ " (" ^ ident2 ^ ")" 
173   | RewritingStep (_, term, term1, term2, cont) -> (match term with None -> " " | Some (None,term) -> "conclude " ^ term_pp term | Some (Some name,term) -> "obtain (" ^ name ^ ") " ^ term_pp term) ^ "=" ^ term_pp term1 ^ (match term2 with `Auto params -> "_" ^ String.concat " " (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)  | `Term term2 -> term_pp term2) ^ (if cont then " done" else "")
174   | Case (_, id, args) ->
175      "case" ^ id ^
176        String.concat " "
177         (List.map (function (id,term) -> "(" ^ id ^ ": " ^ term_pp term ^  ")")
178           args)
179
180  let pp_search_kind = function
181   | `Locate -> "locate"
182   | `Hint -> "hint"
183   | `Match -> "match"
184   | `Elim -> "elim"
185   | `Instance -> "instance"
186
187 let pp_arg ~term_pp arg = 
188   let s = term_pp arg in
189    if s = "" || (s.[0] = '(' && s.[String.length s - 1] = ')') then
190      (* _nice_ heuristic *)
191      s
192    else
193      "(" ^ s ^ ")"
194   
195 let pp_macro ~term_pp = 
196   let term_pp = pp_arg ~term_pp in
197   let style_pp = function
198      | Declarative         -> ""
199      | Procedural None     -> "procedural "
200      | Procedural (Some i) -> sprintf "procedural %u " i
201   in
202   let prefix_pp prefix = 
203      if prefix = "" then "" else sprintf " \"%s\"" prefix
204   in
205   function 
206   (* Whelp *)
207   | WInstance (_, term) -> "whelp instance " ^ term_pp term
208   | WHint (_, t) -> "whelp hint " ^ term_pp t
209   | WLocate (_, s) -> "whelp locate \"" ^ s ^ "\""
210   | WElim (_, t) -> "whelp elim " ^ term_pp t
211   | WMatch (_, term) -> "whelp match " ^ term_pp term
212   (* real macros *)
213   | Check (_, term) -> sprintf "check %s" (term_pp term)
214   | Hint _ -> "hint"
215   | Inline (_, style, suri, prefix) ->  
216       sprintf "inline %s\"%s\"%s" (style_pp style) suri (prefix_pp prefix) 
217
218 let pp_associativity = function
219   | Gramext.LeftA -> "left associative"
220   | Gramext.RightA -> "right associative"
221   | Gramext.NonA -> "non associative"
222
223 let pp_precedence i = sprintf "with precedence %d" i
224
225 let pp_dir_opt = function
226   | None -> ""
227   | Some `LeftToRight -> "> "
228   | Some `RightToLeft -> "< "
229
230 let pp_default what uris = 
231   sprintf "default \"%s\" %s" what
232     (String.concat " " (List.map UriManager.string_of_uri uris))
233
234 let pp_coercion uri do_composites arity =
235    sprintf "coercion %s %d (* %s *)" (UriManager.string_of_uri uri) arity
236      (if do_composites then "compounds" else "no compounds")
237     
238 let pp_command ~term_pp ~obj_pp = function
239   | Index (_,_,uri) -> "Indexing " ^ UriManager.string_of_uri uri
240   | Coercion (_, uri, do_composites, i) -> pp_coercion uri do_composites i
241   | Default (_,what,uris) -> pp_default what uris
242   | Drop _ -> "drop"
243   | Include (_,path) -> "include \"" ^ path ^ "\""
244   | Obj (_,obj) -> obj_pp obj
245   | Qed _ -> "qed"
246   | Relation (_,id,a,aeq,refl,sym,trans) ->
247      "relation " ^ term_pp aeq ^ " on " ^ term_pp a ^
248      (match refl with
249          Some r -> " reflexivity proved by " ^ term_pp r
250        | None -> "") ^
251      (match sym with
252          Some r -> " symmetry proved by " ^ term_pp r
253        | None -> "") ^
254      (match trans with
255          Some r -> " transitivity proved by " ^ term_pp r
256        | None -> "")
257   | Print (_,s) -> "print " ^ s
258   | Set (_, name, value) -> sprintf "set \"%s\" \"%s\"" name value
259
260 let rec pp_tactical ~term_pp ~lazy_term_pp =
261   let pp_tactic = pp_tactic ~lazy_term_pp ~term_pp in
262   let pp_tacticals = pp_tacticals ~lazy_term_pp ~term_pp in
263   function
264   | Tactic (_, tac) -> pp_tactic tac
265   | Do (_, count, tac) ->
266       sprintf "do %d %s" count (pp_tactical ~term_pp ~lazy_term_pp tac)
267   | Repeat (_, tac) -> "repeat " ^ pp_tactical ~term_pp ~lazy_term_pp tac
268   | Seq (_, tacs) -> pp_tacticals ~sep:"; " tacs
269   | Then (_, tac, tacs) ->
270       sprintf "%s; [%s]" (pp_tactical ~term_pp ~lazy_term_pp tac)
271         (pp_tacticals ~sep:" | " tacs)
272   | First (_, tacs) -> sprintf "tries [%s]" (pp_tacticals ~sep:" | " tacs)
273   | Try (_, tac) -> "try " ^ pp_tactical ~term_pp ~lazy_term_pp tac
274   | Solve (_, tac) -> sprintf "solve [%s]" (pp_tacticals ~sep:" | " tac)
275   | Progress (_, tac) -> "progress " ^ pp_tactical ~term_pp ~lazy_term_pp tac
276
277   | Dot _ -> "."
278   | Semicolon _ -> ";"
279   | Branch _ -> "["
280   | Shift _ -> "|"
281   | Pos (_, i) -> sprintf "%s:" (String.concat "," (List.map string_of_int i))
282   | Wildcard _ -> "*:"
283   | Merge _ -> "]"
284   | Focus (_, goals) ->
285       sprintf "focus %s" (String.concat " " (List.map string_of_int goals))
286   | Unfocus _ -> "unfocus"
287   | Skip _ -> "skip"
288
289 and pp_tacticals ~term_pp ~lazy_term_pp ~sep tacs =
290   String.concat sep (List.map (pp_tactical~lazy_term_pp ~term_pp) tacs)
291
292 let pp_executable ~term_pp ~lazy_term_pp ~obj_pp =
293   function
294   | Macro (_, macro) -> pp_macro ~term_pp macro ^ "."
295   | Tactical (_, tac, Some punct) ->
296       pp_tactical ~lazy_term_pp ~term_pp tac
297       ^ pp_tactical ~lazy_term_pp ~term_pp punct
298   | Tactical (_, tac, None) -> pp_tactical ~lazy_term_pp ~term_pp tac
299   | Command (_, cmd) -> pp_command ~term_pp ~obj_pp cmd ^ ".\n"
300                       
301 let pp_comment ~term_pp ~lazy_term_pp ~obj_pp =
302   function
303   | Note (_,"") -> sprintf "\n"
304   | Note (_,str) -> sprintf "(* %s *)\n" str
305   | Code (_,code) ->
306       sprintf "(** %s. **)\n" (pp_executable ~term_pp ~lazy_term_pp ~obj_pp code)
307
308 let pp_statement ~term_pp ~lazy_term_pp ~obj_pp =
309   function
310   | Executable (_, ex) -> pp_executable ~lazy_term_pp ~term_pp ~obj_pp ex 
311   | Comment (_, c) -> pp_comment ~term_pp ~lazy_term_pp ~obj_pp c