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