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