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