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