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