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