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