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