]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/grafiteAstPp.ml
- synced notation pretty printing with parsing syntax
[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 (_, term) ->
82       sprintf "decompose %s" (pp_term_ast term)
83   | Discriminate (_, term) -> "discriminate " ^ pp_term_ast term
84   | Elim (_, term, using, num, idents) ->
85       sprintf "elim " ^ pp_term_ast term ^
86       (match using with None -> "" | Some term -> " using " ^ pp_term_ast term)
87       ^ pp_intros_specs (num, idents) 
88   | ElimType (_, term, using, num, idents) ->
89       sprintf "elim type " ^ pp_term_ast term ^
90       (match using with None -> "" | Some term -> " using " ^ pp_term_ast term)
91       ^ pp_intros_specs (num, idents)
92   | Exact (_, term) -> "exact " ^ pp_term_ast term
93   | Exists _ -> "exists"
94   | Fold (_, kind, term, pattern) ->
95       sprintf "fold %s %s %s" (pp_reduction_kind kind)
96        (pp_term_ast term) (pp_pattern pattern)
97   | FwdSimpl (_, hyp, idents) -> 
98       sprintf "fwd %s%s" hyp 
99         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
100   | Generalize (_, pattern, ident) ->
101      sprintf "generalize %s%s" (pp_pattern pattern)
102       (match ident with None -> "" | Some id -> " as " ^ id)
103   | Goal (_, n) -> "goal " ^ string_of_int n
104   | Fail _ -> "fail"
105   | Fourier _ -> "fourier"
106   | IdTac _ -> "id"
107   | Injection (_, term) -> "injection " ^ pp_term_ast term
108   | Intros (_, None, []) -> "intro"
109   | Intros (_, num, idents) ->
110       sprintf "intros%s%s"
111         (match num with None -> "" | Some num -> " " ^ string_of_int num)
112         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
113   | LApply (_, level_opt, terms, term, ident_opt) -> 
114       sprintf "lapply %s%s%s%s" 
115         (match level_opt with None -> "" | Some i -> " depth = " ^ string_of_int i ^ " ")  
116         (pp_term_ast term) 
117         (match terms with [] -> "" | _ -> " to " ^ pp_terms_ast terms)
118         (match ident_opt with None -> "" | Some ident -> " using " ^ ident)
119   | Left _ -> "left"
120   | LetIn (_, term, ident) -> sprintf "let %s in %s" (pp_term_ast term) ident
121   | Reduce (_, kind, pat) ->
122       sprintf "%s %s" (pp_reduction_kind kind) (pp_pattern pat)
123   | Reflexivity _ -> "reflexivity"
124   | Replace (_, pattern, t) ->
125       sprintf "replace %s with %s" (pp_pattern pattern) (pp_term_ast t)
126   | Rewrite (_, pos, t, pattern) -> 
127       sprintf "rewrite %s %s %s" 
128         (if pos = `LeftToRight then ">" else "<")
129         (pp_term_ast t)
130         (pp_pattern pattern)
131   | Right _ -> "right"
132   | Ring _ -> "ring"
133   | Split _ -> "split"
134   | Symmetry _ -> "symmetry"
135   | Transitivity (_, term) -> "transitivity " ^ pp_term_ast term
136
137 let pp_flavour = function
138   | `Definition -> "Definition"
139   | `Fact -> "Fact"
140   | `Goal -> "Goal"
141   | `Lemma -> "Lemma"
142   | `Remark -> "Remark"
143   | `Theorem -> "Theorem"
144   | `Variant -> "Variant"
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_params = function
187   | [] -> ""
188   | params ->
189       " " ^
190       String.concat " "
191         (List.map
192           (fun (name, typ) -> sprintf "(%s:%s)" name (pp_term_ast typ))
193           params)
194       
195 let pp_fields fields =
196   (if fields <> [] then "\n" else "") ^ 
197   String.concat ";\n" 
198     (List.map (fun (name,ty) -> " " ^ name ^ ": " ^ pp_term_ast ty) fields)
199         
200 let pp_obj = function
201  | Inductive (params, types) ->
202     let pp_constructors constructors =
203       String.concat "\n"
204         (List.map (fun (name, typ) -> sprintf "| %s: %s" name (pp_term_ast typ))
205           constructors)
206     in
207     let pp_type (name, _, typ, constructors) =
208       sprintf "\nwith %s: %s \\def\n%s" name (pp_term_ast typ)
209         (pp_constructors constructors)
210     in
211     (match types with
212     | [] -> assert false
213     | (name, inductive, typ, constructors) :: tl ->
214         let fst_typ_pp =
215           sprintf "%sinductive %s%s: %s \\def\n%s"
216             (if inductive then "" else "co") name (pp_params params)
217             (pp_term_ast typ) (pp_constructors constructors)
218         in
219         fst_typ_pp ^ String.concat "" (List.map pp_type tl))
220  | Theorem (flavour, name, typ, body) ->
221     sprintf "%s %s: %s %s"
222       (pp_flavour flavour)
223       name
224       (pp_term_ast typ)
225       (match body with
226       | None -> ""
227       | Some body -> "\\def " ^ pp_term_ast body)
228  | Record (params,name,ty,fields) ->
229     "record " ^ name ^ " " ^ pp_params params ^ " \\def {" ^
230     pp_fields fields ^ "}"
231
232 let pp_argument_pattern = function
233   | CicNotationPt.IdentArg (eta_depth, name) ->
234       let eta_buf = Buffer.create 5 in
235       for i = 1 to eta_depth do
236         Buffer.add_string eta_buf "\\eta."
237       done;
238       sprintf "%s%s" (Buffer.contents eta_buf) name
239
240 let rec pp_cic_appl_pattern = function
241   | CicNotationPt.UriPattern uri -> UriManager.string_of_uri uri
242   | CicNotationPt.VarPattern name -> name
243   | CicNotationPt.ImplicitPattern -> "_"
244   | CicNotationPt.ApplPattern aps ->
245       sprintf "(%s)" (String.concat " " (List.map pp_cic_appl_pattern aps))
246
247 let pp_l1_pattern = CicNotationPp.pp_term
248 let pp_l2_pattern = CicNotationPp.pp_term
249
250 let pp_associativity = function
251   | Some Gramext.LeftA -> "left associative "
252   | Some Gramext.RightA -> "right associative "
253   | Some Gramext.NonA -> "non associative "
254   | None -> ""
255
256 let pp_precedence = function
257   | Some i -> sprintf "with precedence %d " i
258   | None -> ""
259
260 let pp_command = function
261   | Include (_,path) -> "include " ^ path
262   | Qed _ -> "qed"
263   | Drop _ -> "drop"
264   | Set (_, name, value) -> sprintf "set \"%s\" \"%s\"" name value
265   | Coercion (_,term) -> sprintf "coercion %s" (pp_term_ast term)
266   | Alias (_,s) -> pp_alias s
267   | Obj (_,obj) -> pp_obj obj
268   | Default (_,what,uris) ->
269      sprintf "default \"%s\" %s" what
270       (String.concat " " (List.map UriManager.string_of_uri uris))
271   | Interpretation (_, dsc, (symbol, arg_patterns), cic_appl_pattern) ->
272       sprintf "interpretation \"%s\" '%s %s = %s"
273         dsc symbol
274         (String.concat " " (List.map pp_argument_pattern arg_patterns))
275         (pp_cic_appl_pattern cic_appl_pattern)
276   | Notation (_, l1_pattern, assoc, prec, l2_pattern) ->
277       sprintf "notation \"%s\" %s%sfor %s"
278         (pp_l1_pattern l1_pattern)
279         (pp_associativity assoc)
280         (pp_precedence prec)
281         (pp_l2_pattern l2_pattern)
282   | Render _
283   | Dump _ -> assert false  (* ZACK: debugging *)
284
285 let rec pp_tactical = function
286   | Tactic (_, tac) -> pp_tactic tac
287   | Do (_, count, tac) -> sprintf "do %d %s" count (pp_tactical tac)
288   | Repeat (_, tac) -> "repeat " ^ pp_tactical tac
289   | Seq (_, tacs) -> pp_tacticals ~sep:"; " tacs
290   | Then (_, tac, tacs) ->
291       sprintf "%s; [%s]" (pp_tactical tac) (pp_tacticals ~sep:" | " tacs)
292   | First (_, tacs) -> sprintf "tries [%s]" (pp_tacticals ~sep:" | " tacs)
293   | Try (_, tac) -> "try " ^ pp_tactical tac
294   | Solve (_, tac) -> sprintf "solve [%s]" (pp_tacticals ~sep:" | " tac)
295
296 and pp_tacticals ~sep tacs =
297   String.concat sep (List.map pp_tactical tacs)
298
299 let pp_tactical tac = pp_tactical tac ^ tactical_terminator
300 let pp_tactic tac = pp_tactic tac ^ tactic_terminator
301 let pp_command tac = pp_command tac ^ command_terminator
302
303 let pp_executable = function
304   | Macro (_,x) -> pp_macro_ast x
305   | Tactical (_,x) -> pp_tactical x
306   | Command (_,x) -> pp_command x
307                       
308 let pp_comment = function
309   | Note (_,str) -> sprintf "(* %s *)" str
310   | Code (_,code) -> sprintf "(** %s. **)" (pp_executable code)
311
312 let pp_statement = function
313   | Executable (_, ex) -> pp_executable ex
314   | Comment (_, c) -> pp_comment c
315
316 let pp_cic_command = function
317   | Include (_,path) -> "include " ^ path
318   | Qed _ -> "qed"
319   | Drop _ -> "drop"
320   | Coercion (_,term) -> sprintf "coercion %s" (CicPp.ppterm term)
321   | Set _
322   | Alias _
323   | Default _
324   | Render _
325   | Dump _
326   | Interpretation _
327   | Notation _
328   | Obj _ -> assert false (* not implemented *)
329