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