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