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