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