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