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