]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/grafiteAstPp.ml
New tactic unfold.
[helm.git] / helm / ocaml / cic_notation / 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_flavour = function
147   | `Definition -> "Definition"
148   | `Fact -> "Fact"
149   | `Goal -> "Goal"
150   | `Lemma -> "Lemma"
151   | `Remark -> "Remark"
152   | `Theorem -> "Theorem"
153   | `Variant -> "Variant"
154
155 let pp_search_kind = function
156   | `Locate -> "locate"
157   | `Hint -> "hint"
158   | `Match -> "match"
159   | `Elim -> "elim"
160   | `Instance -> "instance"
161
162 let pp_macro pp_term = function 
163   (* Whelp *)
164   | WInstance (_, term) -> "whelp instance " ^ pp_term term
165   | WHint (_, t) -> "whelp hint " ^ pp_term t
166   | WLocate (_, s) -> "whelp locate " ^ s
167   | WElim (_, t) -> "whelp elim " ^ pp_term t
168   | WMatch (_, term) -> "whelp match " ^ pp_term term
169   (* real macros *)
170 (*   | Abort _ -> "Abort" *)
171   | Check (_, term) -> sprintf "Check %s" (pp_term term)
172   | Hint _ -> "hint"
173 (*   | Redo (_, None) -> "Redo"
174   | Redo (_, Some n) -> sprintf "Redo %d" n *)
175   | Search_pat (_, kind, pat) ->
176       sprintf "search %s \"%s\"" (pp_search_kind kind) pat
177   | Search_term (_, kind, term) ->
178       sprintf "search %s %s" (pp_search_kind kind) (pp_term term)
179 (*   | Undo (_, None) -> "Undo"
180   | Undo (_, Some n) -> sprintf "Undo %d" n *)
181   | Print (_, name) -> sprintf "Print \"%s\"" name
182   | Quit _ -> "Quit"
183
184 let pp_macro_ast = pp_macro pp_term_ast
185 let pp_macro_cic = pp_macro pp_term_cic
186
187 let pp_alias = function
188   | Ident_alias (id, uri) -> sprintf "alias id \"%s\" = \"%s\"" id uri
189   | Symbol_alias (symb, instance, desc) ->
190       sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
191         symb instance desc
192   | Number_alias (instance,desc) ->
193       sprintf "alias num (instance %d) = \"%s\"" instance desc
194   
195 let pp_params = function
196   | [] -> ""
197   | params ->
198       " " ^
199       String.concat " "
200         (List.map
201           (fun (name, typ) -> sprintf "(%s:%s)" name (pp_term_ast typ))
202           params)
203       
204 let pp_fields fields =
205   (if fields <> [] then "\n" else "") ^ 
206   String.concat ";\n" 
207     (List.map (fun (name,ty) -> " " ^ name ^ ": " ^ pp_term_ast ty) fields)
208         
209 let pp_obj = function
210  | Inductive (params, types) ->
211     let pp_constructors constructors =
212       String.concat "\n"
213         (List.map (fun (name, typ) -> sprintf "| %s: %s" name (pp_term_ast typ))
214           constructors)
215     in
216     let pp_type (name, _, typ, constructors) =
217       sprintf "\nwith %s: %s \\def\n%s" name (pp_term_ast typ)
218         (pp_constructors constructors)
219     in
220     (match types with
221     | [] -> assert false
222     | (name, inductive, typ, constructors) :: tl ->
223         let fst_typ_pp =
224           sprintf "%sinductive %s%s: %s \\def\n%s"
225             (if inductive then "" else "co") name (pp_params params)
226             (pp_term_ast typ) (pp_constructors constructors)
227         in
228         fst_typ_pp ^ String.concat "" (List.map pp_type tl))
229  | Theorem (flavour, name, typ, body) ->
230     sprintf "%s %s: %s %s"
231       (pp_flavour flavour)
232       name
233       (pp_term_ast typ)
234       (match body with
235       | None -> ""
236       | Some body -> "\\def " ^ pp_term_ast body)
237  | Record (params,name,ty,fields) ->
238     "record " ^ name ^ " " ^ pp_params params ^ " \\def {" ^
239     pp_fields fields ^ "}"
240
241 let pp_argument_pattern = function
242   | CicNotationPt.IdentArg (eta_depth, name) ->
243       let eta_buf = Buffer.create 5 in
244       for i = 1 to eta_depth do
245         Buffer.add_string eta_buf "\\eta."
246       done;
247       sprintf "%s%s" (Buffer.contents eta_buf) name
248
249 let rec pp_cic_appl_pattern = function
250   | CicNotationPt.UriPattern uri -> UriManager.string_of_uri uri
251   | CicNotationPt.VarPattern name -> name
252   | CicNotationPt.ImplicitPattern -> "_"
253   | CicNotationPt.ApplPattern aps ->
254       sprintf "(%s)" (String.concat " " (List.map pp_cic_appl_pattern aps))
255
256 let pp_l1_pattern = CicNotationPp.pp_term
257 let pp_l2_pattern = CicNotationPp.pp_term
258
259 let pp_associativity = function
260   | Gramext.LeftA -> "left associative"
261   | Gramext.RightA -> "right associative"
262   | Gramext.NonA -> "non associative"
263
264 let pp_precedence i = sprintf "with precedence %d" i
265
266 let pp_dir_opt = function
267   | None -> ""
268   | Some `LeftToRight -> "> "
269   | Some `RightToLeft -> "< "
270
271 let pp_command = function
272   | Include (_,path) -> "include " ^ path
273   | Qed _ -> "qed"
274   | Drop _ -> "drop"
275   | Set (_, name, value) -> sprintf "set \"%s\" \"%s\"" name value
276   | Coercion (_,term) -> sprintf "coercion %s" (pp_term_ast term)
277   | Alias (_,s) -> pp_alias s
278   | Obj (_,obj) -> pp_obj obj
279   | Default (_,what,uris) ->
280      sprintf "default \"%s\" %s" what
281       (String.concat " " (List.map UriManager.string_of_uri uris))
282   | Interpretation (_, dsc, (symbol, arg_patterns), cic_appl_pattern) ->
283       sprintf "interpretation \"%s\" '%s %s = %s"
284         dsc symbol
285         (String.concat " " (List.map pp_argument_pattern arg_patterns))
286         (pp_cic_appl_pattern cic_appl_pattern)
287   | Notation (_, dir_opt, l1_pattern, assoc, prec, l2_pattern) ->
288       sprintf "notation %s\"%s\" %s %s for %s"
289         (pp_dir_opt dir_opt)
290         (pp_l1_pattern l1_pattern)
291         (pp_associativity assoc)
292         (pp_precedence prec)
293         (pp_l2_pattern l2_pattern)
294   | Render _
295   | Dump _ -> assert false  (* ZACK: debugging *)
296
297 let rec pp_tactical = function
298   | Tactic (_, tac) -> pp_tactic tac
299   | Do (_, count, tac) -> sprintf "do %d %s" count (pp_tactical tac)
300   | Repeat (_, tac) -> "repeat " ^ pp_tactical tac
301   | Seq (_, tacs) -> pp_tacticals ~sep:"; " tacs
302   | Then (_, tac, tacs) ->
303       sprintf "%s; [%s]" (pp_tactical tac) (pp_tacticals ~sep:" | " tacs)
304   | First (_, tacs) -> sprintf "tries [%s]" (pp_tacticals ~sep:" | " tacs)
305   | Try (_, tac) -> "try " ^ pp_tactical tac
306   | Solve (_, tac) -> sprintf "solve [%s]" (pp_tacticals ~sep:" | " tac)
307
308 and pp_tacticals ~sep tacs =
309   String.concat sep (List.map pp_tactical tacs)
310
311 let pp_tactical tac = pp_tactical tac ^ tactical_terminator
312 let pp_tactic tac = pp_tactic tac ^ tactic_terminator
313 let pp_command tac = pp_command tac ^ command_terminator
314
315 let pp_executable = function
316   | Macro (_,x) -> pp_macro_ast x
317   | Tactical (_,x) -> pp_tactical x
318   | Command (_,x) -> pp_command x
319                       
320 let pp_comment = function
321   | Note (_,str) -> sprintf "(* %s *)" str
322   | Code (_,code) -> sprintf "(** %s. **)" (pp_executable code)
323
324 let pp_statement = function
325   | Executable (_, ex) -> pp_executable ex
326   | Comment (_, c) -> pp_comment c
327
328 let pp_cic_command = function
329   | Include (_,path) -> "include " ^ path
330   | Qed _ -> "qed"
331   | Drop _ -> "drop"
332   | Coercion (_,term) -> sprintf "coercion %s" (CicPp.ppterm term)
333   | Set _
334   | Alias _
335   | Default _
336   | Render _
337   | Dump _
338   | Interpretation _
339   | Notation _
340   | Obj _ -> assert false (* not implemented *)
341