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