]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite/grafiteAstPp.ml
1. matitaEngine splitted into disambiguation (now in grafite_parser) and
[helm.git] / helm / ocaml / 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 open Printf
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_term_ast term = CicNotationPp.pp_term term
35 let pp_term_cic term = CicPp.ppterm term
36
37 let pp_idents idents = "[" ^ String.concat "; " idents ^ "]"
38
39 let pp_terms_ast terms = String.concat ", " (List.map pp_term_ast terms)
40
41 let pp_reduction_kind = function
42   | `Normalize -> "normalize"
43   | `Reduce -> "reduce"
44   | `Simpl -> "simplify"
45   | `Unfold (Some t) -> "unfold " ^ pp_term_ast t
46   | `Unfold None -> "unfold"
47   | `Whd -> "whd"
48  
49   
50 let pp_pattern (t, hyp, goal) = 
51   let pp_hyp_pattern l =
52     String.concat "; "
53       (List.map (fun (name, p) -> sprintf "%s : %s" name (pp_term_ast p)) l) in
54   let pp_t t =
55    match t with
56       None -> ""
57     | Some t -> pp_term_ast t
58   in
59    pp_t t ^ " in " ^ pp_hyp_pattern hyp ^ " \\vdash " ^ pp_term_ast goal
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 rec pp_tactic = function
68   | Absurd (_, term) -> "absurd" ^ pp_term_ast term
69   | Apply (_, term) -> "apply " ^ pp_term_ast term
70   | Auto _ -> "auto"
71   | Assumption _ -> "assumption"
72   | Change (_, where, with_what) ->
73       sprintf "change %s with %s" (pp_pattern where) (pp_term_ast with_what)
74   | Clear (_,id) -> sprintf "clear %s" id
75   | ClearBody (_,id) -> sprintf "clearbody %s" id
76   | Compare (_,term) -> "compare " ^ pp_term_ast term
77   | Constructor (_,n) -> "constructor " ^ string_of_int n
78   | Contradiction _ -> "contradiction"
79   | Cut (_, ident, term) ->
80      "cut " ^ pp_term_ast term ^
81       (match ident with None -> "" | Some id -> " as " ^ id)
82   | DecideEquality _ -> "decide equality"
83   | Decompose (_, [], what, names) ->
84       sprintf "decompose %s%s" what (pp_intros_specs (None, names)) 
85   | Decompose (_, types, what, names) ->
86       let to_ident = function
87          | Ident id -> id
88          | Type _   -> assert false 
89       in
90       let types = List.rev_map to_ident types in
91       sprintf "decompose %s %s%s" (pp_idents types) what (pp_intros_specs (None, names)) 
92   | Discriminate (_, term) -> "discriminate " ^ pp_term_ast term
93   | Elim (_, term, using, num, idents) ->
94       sprintf "elim " ^ pp_term_ast term ^
95       (match using with None -> "" | Some term -> " using " ^ pp_term_ast term)
96       ^ pp_intros_specs (num, idents) 
97   | ElimType (_, term, using, num, idents) ->
98       sprintf "elim type " ^ pp_term_ast term ^
99       (match using with None -> "" | Some term -> " using " ^ pp_term_ast term)
100       ^ pp_intros_specs (num, idents)
101   | Exact (_, term) -> "exact " ^ pp_term_ast term
102   | Exists _ -> "exists"
103   | Fold (_, kind, term, pattern) ->
104       sprintf "fold %s %s %s" (pp_reduction_kind kind)
105        (pp_term_ast term) (pp_pattern pattern)
106   | FwdSimpl (_, hyp, idents) -> 
107       sprintf "fwd %s%s" hyp 
108         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
109   | Generalize (_, pattern, ident) ->
110      sprintf "generalize %s%s" (pp_pattern pattern)
111       (match ident with None -> "" | Some id -> " as " ^ id)
112   | Goal (_, n) -> "goal " ^ string_of_int n
113   | Fail _ -> "fail"
114   | Fourier _ -> "fourier"
115   | IdTac _ -> "id"
116   | Injection (_, term) -> "injection " ^ pp_term_ast term
117   | Intros (_, None, []) -> "intro"
118   | Intros (_, num, idents) ->
119       sprintf "intros%s%s"
120         (match num with None -> "" | Some num -> " " ^ string_of_int num)
121         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
122   | LApply (_, level_opt, terms, term, ident_opt) -> 
123       sprintf "lapply %s%s%s%s" 
124         (match level_opt with None -> "" | Some i -> " depth = " ^ string_of_int i ^ " ")  
125         (pp_term_ast term) 
126         (match terms with [] -> "" | _ -> " to " ^ pp_terms_ast terms)
127         (match ident_opt with None -> "" | Some ident -> " using " ^ ident)
128   | Left _ -> "left"
129   | LetIn (_, term, ident) -> sprintf "let %s in %s" (pp_term_ast term) ident
130   | Reduce (_, kind, pat) ->
131       sprintf "%s %s" (pp_reduction_kind kind) (pp_pattern pat)
132   | Reflexivity _ -> "reflexivity"
133   | Replace (_, pattern, t) ->
134       sprintf "replace %s with %s" (pp_pattern pattern) (pp_term_ast t)
135   | Rewrite (_, pos, t, pattern) -> 
136       sprintf "rewrite %s %s %s" 
137         (if pos = `LeftToRight then ">" else "<")
138         (pp_term_ast t)
139         (pp_pattern pattern)
140   | Right _ -> "right"
141   | Ring _ -> "ring"
142   | Split _ -> "split"
143   | Symmetry _ -> "symmetry"
144   | Transitivity (_, term) -> "transitivity " ^ pp_term_ast term
145
146 let pp_search_kind = function
147   | `Locate -> "locate"
148   | `Hint -> "hint"
149   | `Match -> "match"
150   | `Elim -> "elim"
151   | `Instance -> "instance"
152
153 let pp_macro pp_term = function 
154   (* Whelp *)
155   | WInstance (_, term) -> "whelp instance " ^ pp_term term
156   | WHint (_, t) -> "whelp hint " ^ pp_term t
157   | WLocate (_, s) -> "whelp locate " ^ s
158   | WElim (_, t) -> "whelp elim " ^ pp_term t
159   | WMatch (_, term) -> "whelp match " ^ pp_term term
160   (* real macros *)
161 (*   | Abort _ -> "Abort" *)
162   | Check (_, term) -> sprintf "Check %s" (pp_term term)
163   | Hint _ -> "hint"
164 (*   | Redo (_, None) -> "Redo"
165   | Redo (_, Some n) -> sprintf "Redo %d" n *)
166   | Search_pat (_, kind, pat) ->
167       sprintf "search %s \"%s\"" (pp_search_kind kind) pat
168   | Search_term (_, kind, term) ->
169       sprintf "search %s %s" (pp_search_kind kind) (pp_term term)
170 (*   | Undo (_, None) -> "Undo"
171   | Undo (_, Some n) -> sprintf "Undo %d" n *)
172   | Print (_, name) -> sprintf "Print \"%s\"" name
173   | Quit _ -> "Quit"
174
175 let pp_macro_ast = pp_macro pp_term_ast
176 let pp_macro_cic = pp_macro pp_term_cic
177
178 let pp_alias = function
179   | Ident_alias (id, uri) -> sprintf "alias id \"%s\" = \"%s\"" id uri
180   | Symbol_alias (symb, instance, desc) ->
181       sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
182         symb instance desc
183   | Number_alias (instance,desc) ->
184       sprintf "alias num (instance %d) = \"%s\"" instance desc
185   
186 let pp_argument_pattern = function
187   | CicNotationPt.IdentArg (eta_depth, name) ->
188       let eta_buf = Buffer.create 5 in
189       for i = 1 to eta_depth do
190         Buffer.add_string eta_buf "\\eta."
191       done;
192       sprintf "%s%s" (Buffer.contents eta_buf) name
193
194 let pp_l1_pattern = CicNotationPp.pp_term
195 let pp_l2_pattern = CicNotationPp.pp_term
196
197 let pp_associativity = function
198   | Gramext.LeftA -> "left associative"
199   | Gramext.RightA -> "right associative"
200   | Gramext.NonA -> "non associative"
201
202 let pp_precedence i = sprintf "with precedence %d" i
203
204 let pp_dir_opt = function
205   | None -> ""
206   | Some `LeftToRight -> "> "
207   | Some `RightToLeft -> "< "
208
209 let pp_metadata =
210   function
211   | Dependency buri -> sprintf "dependency %s" buri
212   | Baseuri buri -> sprintf "baseuri %s" buri
213
214 let pp_command = function
215   | Include (_,path) -> "include " ^ path
216   | Qed _ -> "qed"
217   | Drop _ -> "drop"
218   | Set (_, name, value) -> sprintf "set \"%s\" \"%s\"" name value
219   | Coercion (_,term, _do_composites) ->
220      sprintf "coercion %s" (pp_term_ast term)
221   | Alias (_,s) -> pp_alias s
222   | Obj (_,obj) -> CicNotationPp.pp_obj obj
223   | Default (_,what,uris) ->
224      sprintf "default \"%s\" %s" what
225       (String.concat " " (List.map UriManager.string_of_uri uris))
226   | Interpretation (_, dsc, (symbol, arg_patterns), cic_appl_pattern) ->
227       sprintf "interpretation \"%s\" '%s %s = %s"
228         dsc symbol
229         (String.concat " " (List.map pp_argument_pattern arg_patterns))
230         (CicNotationPp.pp_cic_appl_pattern cic_appl_pattern)
231   | Notation (_, dir_opt, l1_pattern, assoc, prec, l2_pattern) ->
232       sprintf "notation %s\"%s\" %s %s for %s"
233         (pp_dir_opt dir_opt)
234         (pp_l1_pattern l1_pattern)
235         (pp_associativity assoc)
236         (pp_precedence prec)
237         (pp_l2_pattern l2_pattern)
238   | Metadata (_, m) -> sprintf "metadata %s" (pp_metadata m)
239   | Render _
240   | Dump _ -> assert false  (* ZACK: debugging *)
241
242 let rec pp_tactical = function
243   | Tactic (_, tac) -> pp_tactic tac
244   | Do (_, count, tac) -> sprintf "do %d %s" count (pp_tactical tac)
245   | Repeat (_, tac) -> "repeat " ^ pp_tactical tac
246   | Seq (_, tacs) -> pp_tacticals ~sep:"; " tacs
247   | Then (_, tac, tacs) ->
248       sprintf "%s; [%s]" (pp_tactical tac) (pp_tacticals ~sep:" | " tacs)
249   | First (_, tacs) -> sprintf "tries [%s]" (pp_tacticals ~sep:" | " tacs)
250   | Try (_, tac) -> "try " ^ pp_tactical tac
251   | Solve (_, tac) -> sprintf "solve [%s]" (pp_tacticals ~sep:" | " tac)
252
253   | Dot _ -> "."
254   | Semicolon _ -> ";"
255   | Branch _ -> "["
256   | Shift _ -> "|"
257   | Pos (_, i) -> sprintf "%d:" i
258   | Merge _ -> "]"
259   | Focus (_, goals) ->
260       sprintf "focus %s" (String.concat " " (List.map string_of_int goals))
261   | Unfocus _ -> "unfocus"
262   | Skip _ -> "skip"
263
264 and pp_tacticals ~sep tacs = String.concat sep (List.map pp_tactical tacs)
265
266 let pp_tactical tac = pp_tactical tac
267 let pp_tactic tac = pp_tactic tac 
268 let pp_command tac = pp_command tac
269
270 let pp_executable = function
271   | Macro (_,x) -> pp_macro_ast x
272   | Tactical (_, tac, Some punct) -> pp_tactical tac ^ pp_tactical punct
273   | Tactical (_, tac, None) -> pp_tactical tac
274   | Command (_,x) -> pp_command x
275                       
276 let pp_comment = function
277   | Note (_,str) -> sprintf "(* %s *)" str
278   | Code (_,code) -> sprintf "(** %s. **)" (pp_executable code)
279
280 let pp_statement = function
281   | Executable (_, ex) -> pp_executable ex
282   | Comment (_, c) -> pp_comment c
283
284 let pp_cic_command = function
285   | Include (_,path) -> "include " ^ path
286   | Qed _ -> "qed"
287   | Drop _ -> "drop"
288   | Coercion (_,term,_add_composites) ->
289      sprintf "coercion %s" (CicPp.ppterm term)
290   | Set _
291   | Alias _
292   | Default _
293   | Render _
294   | Dump _
295   | Interpretation _
296   | Metadata _
297   | Notation _
298   | Obj _ -> assert false (* not implemented *)
299
300 let pp_dependency = function
301   | IncludeDep str -> "include \"" ^ str ^ "\""
302   | BaseuriDep str -> "set \"baseuri\" \"" ^ str ^ "\""
303   | UriDep uri -> "uri \"" ^ UriManager.string_of_uri uri ^ "\""
304