]> matita.cs.unibo.it Git - helm.git/blob - components/grafite/grafiteAstPp.ml
Much ado about nothing:
[helm.git] / 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 Printf
29
30 open GrafiteAst
31
32 let tactical_terminator = ""
33 let tactic_terminator = tactical_terminator
34 let command_terminator = tactical_terminator
35
36 let pp_idents idents = "(" ^ String.concat " " idents ^ ")"
37
38 let pp_reduction_kind ~term_pp = function
39   | `Normalize -> "normalize"
40   | `Reduce -> "reduce"
41   | `Simpl -> "simplify"
42   | `Unfold (Some t) -> "unfold " ^ term_pp t
43   | `Unfold None -> "unfold"
44   | `Whd -> "whd"
45  
46 let pp_tactic_pattern ~term_pp ~lazy_term_pp (what, hyp, goal) = 
47   if what = None && hyp = [] && goal = None then "" else 
48   let what_text =
49     match what with
50     | None -> ""
51     | Some t -> sprintf "in match (%s) " (lazy_term_pp t) in
52   let hyp_text =
53     String.concat " "
54       (List.map (fun (name, p) -> sprintf "%s:(%s)" name (term_pp p)) hyp) in
55   let goal_text =
56     match goal with
57     | None -> ""
58     | Some t -> sprintf "\\vdash (%s)" (term_pp t) in
59   sprintf "%sin %s%s" what_text hyp_text goal_text
60
61 let pp_intros_specs = function
62    | None, []         -> ""
63    | Some num, []     -> Printf.sprintf " names %i" num
64    | None, idents     -> Printf.sprintf " names %s" (pp_idents idents)
65    | Some num, idents -> Printf.sprintf " names %i %s" num (pp_idents idents)
66
67 let terms_pp ~term_pp terms = String.concat ", " (List.map term_pp terms)
68
69 let opt_string_pp = function
70    | None -> ""
71    | Some what -> what ^ " "
72
73 let rec pp_tactic ~term_pp ~lazy_term_pp =
74   let pp_reduction_kind = pp_reduction_kind ~term_pp in
75   let pp_tactic_pattern = pp_tactic_pattern ~lazy_term_pp ~term_pp in
76   function
77   (* Higher order tactics *)
78   | Do (_, count, tac) ->
79       sprintf "do %d %s" count (pp_tactic ~term_pp ~lazy_term_pp tac)
80   | Repeat (_, tac) -> "repeat " ^ pp_tactic ~term_pp ~lazy_term_pp tac
81   | Seq (_, tacs) -> pp_tactics ~term_pp ~lazy_term_pp ~sep:"; " tacs
82   | Then (_, tac, tacs) ->
83       sprintf "%s; [%s]" (pp_tactic ~term_pp ~lazy_term_pp tac)
84         (pp_tactics ~term_pp ~lazy_term_pp ~sep:" | " tacs)
85   | First (_, tacs) ->
86      sprintf "tries [%s]" (pp_tactics ~term_pp ~lazy_term_pp ~sep:" | " tacs)
87   | Try (_, tac) -> "try " ^ pp_tactic ~term_pp ~lazy_term_pp tac
88   | Solve (_, tac) ->
89      sprintf "solve [%s]" (pp_tactics ~term_pp ~lazy_term_pp ~sep:" | " tac)
90   | Progress (_, tac) -> "progress " ^ pp_tactic ~term_pp ~lazy_term_pp tac
91   (* First order tactics *)
92   | Absurd (_, term) -> "absurd" ^ term_pp term
93   | Apply (_, term) -> "apply " ^ term_pp term
94   | ApplyS (_, term, params) ->
95      "applyS " ^ term_pp term ^
96       String.concat " " 
97         (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)
98   | Auto (_,params) -> "auto " ^ 
99       String.concat " " 
100         (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)
101   | Assumption _ -> "assumption"
102   | Cases (_, term, idents) -> sprintf "cases " ^ term_pp term ^
103       pp_intros_specs (None, idents) 
104   | Change (_, where, with_what) ->
105       sprintf "change %s with %s" (pp_tactic_pattern where) (lazy_term_pp with_what)
106   | Clear (_,ids) -> sprintf "clear %s" (pp_idents ids)
107   | ClearBody (_,id) -> sprintf "clearbody %s" id
108   | Constructor (_,n) -> "constructor " ^ string_of_int n
109   | Contradiction _ -> "contradiction"
110   | Cut (_, ident, term) ->
111      "cut " ^ term_pp term ^
112       (match ident with None -> "" | Some id -> " as " ^ id)
113   | Decompose (_, names) ->
114       sprintf "decompose%s" (pp_intros_specs (None, names)) 
115   | Demodulate _ -> "demodulate"
116   | Destruct (_, term) -> "destruct " ^ term_pp term
117   | Elim (_, what, using, pattern, num, idents) ->
118       sprintf "elim %s%s %s%s" 
119       (term_pp what)
120       (match using with None -> "" | Some term -> " using " ^ term_pp term)
121       (pp_tactic_pattern pattern)
122       (pp_intros_specs (num, idents)) 
123   | ElimType (_, term, using, num, idents) ->
124       sprintf "elim type " ^ term_pp term ^
125       (match using with None -> "" | Some term -> " using " ^ term_pp term)
126       ^ pp_intros_specs (num, idents)
127   | Exact (_, term) -> "exact " ^ term_pp term
128   | Exists _ -> "exists"
129   | Fold (_, kind, term, pattern) ->
130       sprintf "fold %s %s %s" (pp_reduction_kind kind)
131        (lazy_term_pp term) (pp_tactic_pattern pattern)
132   | FwdSimpl (_, hyp, idents) -> 
133       sprintf "fwd %s%s" hyp 
134         (match idents with [] -> "" | idents -> " as " ^ pp_idents idents)
135   | Generalize (_, pattern, ident) ->
136      sprintf "generalize %s%s" (pp_tactic_pattern pattern)
137       (match ident with None -> "" | Some id -> " as " ^ id)
138   | Fail _ -> "fail"
139   | Fourier _ -> "fourier"
140   | IdTac _ -> "id"
141   | Intros (_, None, []) -> "intros"
142   | Inversion (_, term) -> "inversion " ^ term_pp term
143   | Intros (_, num, idents) ->
144       sprintf "intros%s%s"
145         (match num with None -> "" | Some num -> " " ^ string_of_int num)
146         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
147   | LApply (_, linear, level_opt, terms, term, ident_opt) -> 
148       sprintf "lapply %s%s%s%s%s" 
149         (if linear then " linear " else "")
150         (match level_opt with None -> "" | Some i -> " depth = " ^ string_of_int i ^ " ")  
151         (term_pp term) 
152         (match terms with [] -> "" | _ -> " to " ^ terms_pp ~term_pp terms)
153         (match ident_opt with None -> "" | Some ident -> " as " ^ ident)
154   | Left _ -> "left"
155   | LetIn (_, term, ident) -> 
156      sprintf "letin %s \\def %s" ident (term_pp term)
157   | Reduce (_, kind, pat) ->
158       sprintf "%s %s" (pp_reduction_kind kind) (pp_tactic_pattern pat)
159   | Reflexivity _ -> "reflexivity"
160   | Replace (_, pattern, t) ->
161       sprintf "replace %s with %s" (pp_tactic_pattern pattern) (lazy_term_pp t)
162   | Rewrite (_, pos, t, pattern, names) -> 
163       sprintf "rewrite %s %s %s%s" 
164         (if pos = `LeftToRight then ">" else "<")
165         (term_pp t)
166         (pp_tactic_pattern pattern)
167         (if names = [] then "" else " as " ^ pp_idents names)
168   | Right _ -> "right"
169   | Ring _ -> "ring"
170   | Split _ -> "split"
171   | Subst _ -> "subst"
172   | Symmetry _ -> "symmetry"
173   | Transitivity (_, term) -> "transitivity " ^ term_pp term
174   (* Tattiche Aggiunte *)
175   | Assume (_, ident , term) -> "assume" ^ ident ^ ":" ^ term_pp term 
176   | Suppose (_, term, ident,term1) -> "suppose" ^ term_pp term ^ "("  ^ ident ^ ")" ^ (match term1 with None -> " " | Some term1 -> term_pp term1)
177   | Bydone (_, term) -> "by" ^ (match term with None -> "_" | Some term -> term_pp term) ^ "done"
178   | By_term_we_proved (_, term, term1, ident, term2) -> "by" ^ (match term with None -> "_" | Some term -> term_pp term)  ^ "we proved" ^ term_pp term1 ^ (match ident with None -> "" | Some ident -> "(" ^ident^ ")") ^
179        (match term2 with  None -> " " | Some term2 -> term_pp term2)
180   | 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)
181   | We_proceed_by_cases_on (_, term, term1) -> "we proceed by cases on" ^ term_pp term ^ "to prove" ^ term_pp term1
182   | We_proceed_by_induction_on (_, term, term1) -> "we proceed by induction on" ^ term_pp term ^ "to prove" ^ term_pp term1
183   | Byinduction (_, term, ident) -> "by induction hypothesis we know" ^ term_pp term ^ "(" ^ ident ^ ")"
184   | Thesisbecomes (_, term) -> "the thesis becomes " ^ term_pp term
185   | ExistsElim (_, term0, ident, term, ident1, term1) -> "by " ^ (match term0 with None -> "_" | Some term -> term_pp term) ^ "let " ^ ident ^ ":" ^ term_pp term ^ "such that " ^ lazy_term_pp term1 ^ "(" ^ ident1 ^ ")"
186   | AndElim (_, term, ident1, term1, ident2, term2) -> "by " ^ term_pp term ^ "we have " ^ term_pp term1 ^ " (" ^ ident1 ^ ") " ^ "and " ^ term_pp term2 ^ " (" ^ ident2 ^ ")" 
187   | RewritingStep (_, term, term1, term2, cont) -> (match term with None -> " " | Some (None,term) -> "conclude " ^ term_pp term | Some (Some name,term) -> "obtain (" ^ name ^ ") " ^ term_pp term) ^ "=" ^ term_pp term1 ^ (match term2 with `Auto params -> "_" ^ String.concat " " (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)  | `Term term2 -> term_pp term2) ^ (if cont then " done" else "")
188   | Case (_, id, args) ->
189      "case" ^ id ^
190        String.concat " "
191         (List.map (function (id,term) -> "(" ^ id ^ ": " ^ term_pp term ^  ")")
192           args)
193
194 and pp_tactics ~term_pp ~lazy_term_pp ~sep tacs =
195   String.concat sep (List.map (pp_tactic ~lazy_term_pp ~term_pp) tacs)
196
197  let pp_search_kind = function
198   | `Locate -> "locate"
199   | `Hint -> "hint"
200   | `Match -> "match"
201   | `Elim -> "elim"
202   | `Instance -> "instance"
203
204 let pp_arg ~term_pp arg = 
205   let s = term_pp arg in
206    if s = "" || (s.[0] = '(' && s.[String.length s - 1] = ')') then
207      (* _nice_ heuristic *)
208      s
209    else
210      "(" ^ s ^ ")"
211   
212 let pp_macro ~term_pp = 
213   let term_pp = pp_arg ~term_pp in
214   let style_pp = function
215      | Declarative         -> ""
216      | Procedural None     -> "procedural "
217      | Procedural (Some i) -> sprintf "procedural %u " i
218   in
219   let prefix_pp prefix = 
220      if prefix = "" then "" else sprintf " \"%s\"" prefix
221   in
222   function 
223   (* Whelp *)
224   | WInstance (_, term) -> "whelp instance " ^ term_pp term
225   | WHint (_, t) -> "whelp hint " ^ term_pp t
226   | WLocate (_, s) -> "whelp locate \"" ^ s ^ "\""
227   | WElim (_, t) -> "whelp elim " ^ term_pp t
228   | WMatch (_, term) -> "whelp match " ^ term_pp term
229   (* real macros *)
230   | Check (_, term) -> sprintf "check %s" (term_pp term)
231   | Hint _ -> "hint"
232   | Inline (_, style, suri, prefix) ->  
233       sprintf "inline %s\"%s\"%s" (style_pp style) suri (prefix_pp prefix) 
234
235 let pp_associativity = function
236   | Gramext.LeftA -> "left associative"
237   | Gramext.RightA -> "right associative"
238   | Gramext.NonA -> "non associative"
239
240 let pp_precedence i = sprintf "with precedence %d" i
241
242 let pp_dir_opt = function
243   | None -> ""
244   | Some `LeftToRight -> "> "
245   | Some `RightToLeft -> "< "
246
247 let pp_default what uris = 
248   sprintf "default \"%s\" %s" what
249     (String.concat " " (List.map UriManager.string_of_uri uris))
250
251 let pp_coercion uri do_composites arity =
252    sprintf "coercion %s %d (* %s *)" (UriManager.string_of_uri uri) arity
253      (if do_composites then "compounds" else "no compounds")
254     
255 let pp_command ~term_pp ~obj_pp = function
256   | Index (_,_,uri) -> "Indexing " ^ UriManager.string_of_uri uri
257   | Coercion (_, uri, do_composites, i) -> pp_coercion uri do_composites i
258   | Default (_,what,uris) -> pp_default what uris
259   | Drop _ -> "drop"
260   | Include (_,path) -> "include \"" ^ path ^ "\""
261   | Obj (_,obj) -> obj_pp obj
262   | Qed _ -> "qed"
263   | Relation (_,id,a,aeq,refl,sym,trans) ->
264      "relation " ^ term_pp aeq ^ " on " ^ term_pp a ^
265      (match refl with
266          Some r -> " reflexivity proved by " ^ term_pp r
267        | None -> "") ^
268      (match sym with
269          Some r -> " symmetry proved by " ^ term_pp r
270        | None -> "") ^
271      (match trans with
272          Some r -> " transitivity proved by " ^ term_pp r
273        | None -> "")
274   | Print (_,s) -> "print " ^ s
275   | Set (_, name, value) -> sprintf "set \"%s\" \"%s\"" name value
276
277 let pp_punctuation_tactical ~term_pp ~lazy_term_pp =
278   function
279   | Dot _ -> "."
280   | Semicolon _ -> ";"
281   | Branch _ -> "["
282   | Shift _ -> "|"
283   | Pos (_, i) -> sprintf "%s:" (String.concat "," (List.map string_of_int i))
284   | Wildcard _ -> "*:"
285   | Merge _ -> "]"
286
287 let pp_non_punctuation_tactical ~term_pp ~lazy_term_pp =
288   function
289   | Focus (_, goals) ->
290       sprintf "focus %s" (String.concat " " (List.map string_of_int goals))
291   | Unfocus _ -> "unfocus"
292   | Skip _ -> "skip"
293
294 let pp_executable ~term_pp ~lazy_term_pp ~obj_pp =
295   function
296   | Macro (_, macro) -> pp_macro ~term_pp macro ^ "."
297   | Tactic (_, Some tac, punct) ->
298       pp_tactic ~lazy_term_pp ~term_pp tac
299       ^ pp_punctuation_tactical ~lazy_term_pp ~term_pp punct
300   | Tactic (_, None, punct) ->
301      pp_punctuation_tactical ~lazy_term_pp ~term_pp punct
302   | NonPunctuationTactical (_, tac, punct) ->
303      pp_non_punctuation_tactical ~lazy_term_pp ~term_pp tac
304      ^ pp_punctuation_tactical ~lazy_term_pp ~term_pp punct
305   | Command (_, cmd) -> pp_command ~term_pp ~obj_pp cmd ^ ".\n"
306                       
307 let pp_comment ~term_pp ~lazy_term_pp ~obj_pp =
308   function
309   | Note (_,"") -> sprintf "\n"
310   | Note (_,str) -> sprintf "(* %s *)\n" str
311   | Code (_,code) ->
312       sprintf "(** %s. **)\n" (pp_executable ~term_pp ~lazy_term_pp ~obj_pp code)
313
314 let pp_statement ~term_pp ~lazy_term_pp ~obj_pp =
315   function
316   | Executable (_, ex) -> pp_executable ~lazy_term_pp ~term_pp ~obj_pp ex 
317   | Comment (_, c) -> pp_comment ~term_pp ~lazy_term_pp ~obj_pp c