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