]> matita.cs.unibo.it Git - helm.git/blob - components/grafite/grafiteAstPp.ml
Demodulate used to be a reduction_kind and it used to take a ~pattern.
[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   let what_text =
48     match what with
49     | None -> ""
50     | Some t -> sprintf "in match (%s) " (lazy_term_pp t) in
51   let hyp_text =
52     String.concat " "
53       (List.map (fun (name, p) -> sprintf "%s:(%s)" name (term_pp p)) hyp) in
54   let goal_text =
55     match goal with
56     | None -> ""
57     | Some t -> sprintf "\\vdash (%s)" (term_pp t) in
58   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   | Absurd (_, term) -> "absurd" ^ term_pp term
77   | Apply (_, term) -> "apply " ^ term_pp term
78   | ApplyS (_, term) -> "applyS " ^ term_pp term
79   | Auto (_,params) -> "auto " ^ 
80       String.concat " " 
81         (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)
82   | Assumption _ -> "assumption"
83   | Change (_, where, with_what) ->
84       sprintf "change %s with %s" (pp_tactic_pattern where) (lazy_term_pp with_what)
85   | Clear (_,ids) -> sprintf "clear %s" (pp_idents ids)
86   | ClearBody (_,id) -> sprintf "clearbody %s" id
87   | Constructor (_,n) -> "constructor " ^ string_of_int n
88   | Contradiction _ -> "contradiction"
89   | Cut (_, ident, term) ->
90      "cut " ^ term_pp term ^
91       (match ident with None -> "" | Some id -> " as " ^ id)
92   | Decompose (_, [], what, names) ->
93       sprintf "decompose %s%s" (opt_string_pp what) (pp_intros_specs (None, names)) 
94   | Decompose (_, types, what, names) ->
95       let to_ident = function
96          | Ident id -> id
97          | Type _   -> assert false 
98       in
99       let types = List.rev_map to_ident types in
100       sprintf "decompose %s %s%s" (pp_idents types) (opt_string_pp what) (pp_intros_specs (None, names)) 
101   | Demodulate _ -> "demodulate"
102   | Discriminate (_, term) -> "discriminate " ^ term_pp term
103   | Elim (_, term, using, num, idents) ->
104       sprintf "elim " ^ term_pp term ^
105       (match using with None -> "" | Some term -> " using " ^ term_pp term)
106       ^ pp_intros_specs (num, idents) 
107   | ElimType (_, term, using, num, idents) ->
108       sprintf "elim type " ^ term_pp term ^
109       (match using with None -> "" | Some term -> " using " ^ term_pp term)
110       ^ pp_intros_specs (num, idents)
111   | Exact (_, term) -> "exact " ^ term_pp term
112   | Exists _ -> "exists"
113   | Fold (_, kind, term, pattern) ->
114       sprintf "fold %s %s %s" (pp_reduction_kind kind)
115        (lazy_term_pp term) (pp_tactic_pattern pattern)
116   | FwdSimpl (_, hyp, idents) -> 
117       sprintf "fwd %s%s" hyp 
118         (match idents with [] -> "" | idents -> " as " ^ pp_idents idents)
119   | Generalize (_, pattern, ident) ->
120      sprintf "generalize %s%s" (pp_tactic_pattern pattern)
121       (match ident with None -> "" | Some id -> " as " ^ id)
122   | Goal (_, n) -> "goal " ^ string_of_int n
123   | Fail _ -> "fail"
124   | Fourier _ -> "fourier"
125   | IdTac _ -> "id"
126   | Injection (_, term) -> "injection " ^ term_pp term
127   | Intros (_, None, []) -> "intros"
128   | Inversion (_, term) -> "inversion " ^ term_pp term
129   | Intros (_, num, idents) ->
130       sprintf "intros%s%s"
131         (match num with None -> "" | Some num -> " " ^ string_of_int num)
132         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
133   | LApply (_, linear, level_opt, terms, term, ident_opt) -> 
134       sprintf "lapply %s%s%s%s%s" 
135         (if linear then " linear " else "")
136         (match level_opt with None -> "" | Some i -> " depth = " ^ string_of_int i ^ " ")  
137         (term_pp term) 
138         (match terms with [] -> "" | _ -> " to " ^ terms_pp ~term_pp terms)
139         (match ident_opt with None -> "" | Some ident -> " as " ^ ident)
140   | Left _ -> "left"
141   | LetIn (_, term, ident) -> sprintf "let %s in %s" (term_pp term) ident
142   | Reduce (_, kind, pat) ->
143       sprintf "%s %s" (pp_reduction_kind kind) (pp_tactic_pattern pat)
144   | Reflexivity _ -> "reflexivity"
145   | Replace (_, pattern, t) ->
146       sprintf "replace %s with %s" (pp_tactic_pattern pattern) (lazy_term_pp t)
147   | Rewrite (_, pos, t, pattern) -> 
148       sprintf "rewrite %s %s %s" 
149         (if pos = `LeftToRight then ">" else "<")
150         (term_pp t)
151         (pp_tactic_pattern pattern)
152   | Right _ -> "right"
153   | Ring _ -> "ring"
154   | Split _ -> "split"
155   | Symmetry _ -> "symmetry"
156   | Transitivity (_, term) -> "transitivity " ^ term_pp term
157
158 let pp_search_kind = function
159   | `Locate -> "locate"
160   | `Hint -> "hint"
161   | `Match -> "match"
162   | `Elim -> "elim"
163   | `Instance -> "instance"
164
165 let pp_arg ~term_pp arg = 
166   let s = term_pp arg in
167    if s = "" || (s.[0] = '(' && s.[String.length s - 1] = ')') then
168      (* _nice_ heuristic *)
169      s
170    else
171      "(" ^ s ^ ")"
172   
173 let pp_macro ~term_pp = 
174   let term_pp = pp_arg ~term_pp in
175   function 
176   (* Whelp *)
177   | WInstance (_, term) -> "whelp instance " ^ term_pp term
178   | WHint (_, t) -> "whelp hint " ^ term_pp t
179   | WLocate (_, s) -> "whelp locate \"" ^ s ^ "\""
180   | WElim (_, t) -> "whelp elim " ^ term_pp t
181   | WMatch (_, term) -> "whelp match " ^ term_pp term
182   (* real macros *)
183   | Check (_, term) -> sprintf "Check %s" (term_pp term)
184   | Hint _ -> "hint"
185   | Search_pat (_, kind, pat) ->
186       sprintf "search %s \"%s\"" (pp_search_kind kind) pat
187   | Search_term (_, kind, term) ->
188       sprintf "search %s %s" (pp_search_kind kind) (term_pp term)
189   | Print (_, name) -> sprintf "Print \"%s\"" name
190   | Quit _ -> "Quit"
191
192 let pp_associativity = function
193   | Gramext.LeftA -> "left associative"
194   | Gramext.RightA -> "right associative"
195   | Gramext.NonA -> "non associative"
196
197 let pp_precedence i = sprintf "with precedence %d" i
198
199 let pp_dir_opt = function
200   | None -> ""
201   | Some `LeftToRight -> "> "
202   | Some `RightToLeft -> "< "
203
204 let pp_default what uris = 
205   sprintf "default \"%s\" %s" what
206     (String.concat " " (List.map UriManager.string_of_uri uris))
207
208 let pp_coercion uri do_composites =
209    sprintf "coercion %s (* %s *)" (UriManager.string_of_uri uri)
210      (if do_composites then "compounds" else "no compounds")
211     
212 let pp_command ~obj_pp = function
213   | Include (_,path) -> "include \"" ^ path ^ "\""
214   | Qed _ -> "qed"
215   | Drop _ -> "drop"
216   | Set (_, name, value) -> sprintf "set \"%s\" \"%s\"" name value
217   | Coercion (_, uri, do_composites) -> pp_coercion uri do_composites
218   | Obj (_,obj) -> obj_pp obj
219   | Default (_,what,uris) ->
220       pp_default what uris
221
222 let rec pp_tactical ~term_pp ~lazy_term_pp =
223   let pp_tactic = pp_tactic ~lazy_term_pp ~term_pp in
224   let pp_tacticals = pp_tacticals ~lazy_term_pp ~term_pp in
225   function
226   | Tactic (_, tac) -> pp_tactic tac
227   | Do (_, count, tac) ->
228       sprintf "do %d %s" count (pp_tactical ~term_pp ~lazy_term_pp tac)
229   | Repeat (_, tac) -> "repeat " ^ pp_tactical ~term_pp ~lazy_term_pp tac
230   | Seq (_, tacs) -> pp_tacticals ~sep:"; " tacs
231   | Then (_, tac, tacs) ->
232       sprintf "%s; [%s]" (pp_tactical ~term_pp ~lazy_term_pp tac)
233         (pp_tacticals ~sep:" | " tacs)
234   | First (_, tacs) -> sprintf "tries [%s]" (pp_tacticals ~sep:" | " tacs)
235   | Try (_, tac) -> "try " ^ pp_tactical ~term_pp ~lazy_term_pp tac
236   | Solve (_, tac) -> sprintf "solve [%s]" (pp_tacticals ~sep:" | " tac)
237
238   | Dot _ -> "."
239   | Semicolon _ -> ";"
240   | Branch _ -> "["
241   | Shift _ -> "|"
242   | Pos (_, i) -> sprintf "%s:" (String.concat "," (List.map string_of_int i))
243   | Wildcard _ -> "*:"
244   | Merge _ -> "]"
245   | Focus (_, goals) ->
246       sprintf "focus %s" (String.concat " " (List.map string_of_int goals))
247   | Unfocus _ -> "unfocus"
248   | Skip _ -> "skip"
249
250 and pp_tacticals ~term_pp ~lazy_term_pp ~sep tacs =
251   String.concat sep (List.map (pp_tactical~lazy_term_pp ~term_pp) tacs)
252
253 let pp_executable ~term_pp ~lazy_term_pp ~obj_pp =
254   function
255   | Macro (_, macro) -> pp_macro ~term_pp macro ^ "."
256   | Tactical (_, tac, Some punct) ->
257       pp_tactical ~lazy_term_pp ~term_pp tac
258       ^ pp_tactical ~lazy_term_pp ~term_pp punct
259   | Tactical (_, tac, None) -> pp_tactical ~lazy_term_pp ~term_pp tac
260   | Command (_, cmd) -> pp_command ~obj_pp cmd ^ "."
261                       
262 let pp_comment ~term_pp ~lazy_term_pp ~obj_pp =
263   function
264   | Note (_,str) -> sprintf "(* %s *)" str
265   | Code (_,code) ->
266       sprintf "(** %s. **)" (pp_executable ~term_pp ~lazy_term_pp ~obj_pp code)
267
268 let pp_statement ~term_pp ~lazy_term_pp ~obj_pp =
269   function
270   | Executable (_, ex) -> pp_executable ~lazy_term_pp ~term_pp ~obj_pp ex 
271   | Comment (_, c) -> pp_comment ~term_pp ~lazy_term_pp ~obj_pp c