1 (* Copyright (C) 2004-2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
30 module Ast = NotationPt
31 module Env = NotationEnv
33 (* when set to true debugging information, not in sync with input syntax, will
34 * be added to the output of pp_term.
35 * set to false if you need, for example, cut and paste from matitac output to
37 let debug_printing = false
39 let pp_binder = function
46 if debug_printing then
47 (function (* debugging version *)
48 | `Symbol s -> sprintf "symbol(%s)" s
49 | `Keyword s -> sprintf "keyword(%s)" s
50 | `Number s -> sprintf "number(%s)" s)
59 (* `None -> "`None" *)
66 | `IdRef id -> sprintf "x(%s)" id
70 (List.map (fun (_, n, v) -> sprintf "%s=%s" n v) attrs))
71 | `Level (prec) -> sprintf "L(%d)" prec
74 | `ChildPos p -> sprintf "P(%s)" (pp_pos p)
76 let pp_capture_variable pp_term =
78 | term, None -> pp_term (* ~pp_parens:false *) term
79 | term, Some typ -> "(" ^ pp_term (* ~pp_parens:false *) term ^ ": " ^ pp_term typ ^ ")"
81 let rec pp_term ?(pp_parens = true) t =
84 | Ast.AttributedTerm (attr, term) when debug_printing ->
85 sprintf "%s[%s]" (pp_attribute attr) (pp_term ~pp_parens:false term)
86 | Ast.AttributedTerm (`Raw text, _) -> text
87 | Ast.AttributedTerm (_, term) -> pp_term ~pp_parens:false term
89 sprintf "%s" (String.concat " " (List.map pp_term terms))
90 | Ast.Binder (`Forall, (Ast.Ident ("_", None), typ), body)
91 | Ast.Binder (`Pi, (Ast.Ident ("_", None), typ), body) ->
93 (match typ with None -> "?" | Some typ -> pp_term typ)
94 (pp_term ~pp_parens:true body)
95 | Ast.Binder (kind, var, body) ->
96 sprintf "\\%s %s.%s" (pp_binder kind) (pp_capture_variable pp_term var)
97 (pp_term ~pp_parens:true body)
98 | Ast.Case (term, indtype, typ, patterns) ->
99 sprintf "match %s%s%s with %s"
103 | Some (ty, href_opt) ->
104 sprintf " in %s%s" ty
105 (match debug_printing, href_opt with
107 sprintf "(i.e.%s)" (UriManager.string_of_uri uri)
109 (match typ with None -> "" | Some t -> sprintf " return %s" (pp_term t))
110 (pp_patterns patterns)
111 | Ast.Cast (t1, t2) -> sprintf "(%s: %s)" (pp_term ~pp_parens:true t1) (pp_term ~pp_parens:true t2)
112 | Ast.LetIn ((var,t2), t1, t3) ->
113 (* let t2 = match t2 with None -> Ast.Implicit | Some t -> t in *)
114 sprintf "let %s \\def %s in %s" (pp_term var)
115 (* (pp_term ~pp_parens:true t2) *)
116 (pp_term ~pp_parens:true t1)
117 (pp_term ~pp_parens:true t3)
118 | Ast.LetRec (kind, definitions, term) ->
119 let rec get_guard i = function
120 | [] -> (*assert false*) Ast.Implicit `JustOne
121 | [term, _] when i = 1 -> term
122 | _ :: tl -> get_guard (pred i) tl
124 let map (params, (id,typ), body, i) =
127 None -> Ast.Implicit `JustOne
130 sprintf "%s %s on %s: %s \\def %s"
131 (pp_term ~pp_parens:false term)
132 (String.concat " " (List.map (pp_capture_variable pp_term) params))
133 (pp_term ~pp_parens:false (get_guard i params))
134 (pp_term typ) (pp_term body)
136 sprintf "let %s %s in %s"
137 (match kind with `Inductive -> "rec" | `CoInductive -> "corec")
138 (String.concat " and " (List.map map definitions))
140 | Ast.Ident (name, Some []) | Ast.Ident (name, None)
141 | Ast.Uri (name, Some []) | Ast.Uri (name, None) -> name
142 | Ast.NRef nref -> NReference.string_of_reference nref
143 | Ast.NCic cic -> NCicPp.ppterm ~metasenv:[] ~context:[] ~subst:[] cic
144 | Ast.Ident (name, Some substs)
145 | Ast.Uri (name, Some substs) ->
146 sprintf "%s \\subst [%s]" name (pp_substs substs)
147 | Ast.Implicit `Vector -> "…"
148 | Ast.Implicit `JustOne -> "?"
149 | Ast.Implicit (`Tagged name) -> "?"^name
150 | Ast.Meta (index, substs) ->
151 sprintf "%d[%s]" index
153 (List.map (function None -> "?" | Some t -> pp_term t) substs))
154 | Ast.Num (num, _) -> num
155 | Ast.Sort `Set -> "Set"
156 | Ast.Sort `Prop -> "Prop"
157 | Ast.Sort (`Type _) -> "Type"
158 | Ast.Sort (`CProp _)-> "CProp"
159 | Ast.Sort (`NType s)-> "Type[" ^ s ^ "]"
160 | Ast.Sort (`NCProp s)-> "CProp[" ^ s ^ "]"
161 | Ast.Symbol (name, _) -> "'" ^ name
163 | Ast.UserInput -> "%"
165 | Ast.Literal l -> pp_literal l
166 | Ast.Layout l -> pp_layout l
167 | Ast.Magic m -> pp_magic m
168 | Ast.Variable v -> pp_variable v
170 match pp_parens, t with
172 | true, Ast.Implicit _
173 | true, Ast.UserInput
175 | true, Ast.Ident (_, Some [])
176 | true, Ast.Ident (_, None) -> t_pp
177 | _ -> sprintf "(%s)" t_pp
179 and pp_subst (name, term) = sprintf "%s \\Assign %s" name (pp_term term)
180 and pp_substs substs = String.concat "; " (List.map pp_subst substs)
184 Ast.Pattern (head, href, vars), term ->
187 (match debug_printing, href with
188 | true, Some uri -> sprintf "(i.e.%s)" (UriManager.string_of_uri uri)
191 sprintf "%s \\Rightarrow %s"
195 sprintf "(%s %s)" head_pp
196 (String.concat " " (List.map (pp_capture_variable pp_term) vars)))
198 | Ast.Wildcard, term ->
199 sprintf "_ \\Rightarrow %s" (pp_term term)
201 and pp_patterns patterns =
202 sprintf "[%s]" (String.concat " | " (List.map pp_pattern patterns))
204 and pp_box_spec (kind, spacing, indent) =
205 let int_of_bool b = if b then 1 else 0 in
208 Ast.H -> "H" | Ast.V -> "V" | Ast.HV -> "HV" | Ast.HOV -> "HOV"
210 sprintf "%sBOX%d%d" kind_string (int_of_bool spacing) (int_of_bool indent)
212 and pp_layout = function
213 | Ast.Sub (t1, t2) -> sprintf "%s \\SUB %s" (pp_term t1) (pp_term t2)
214 | Ast.Sup (t1, t2) -> sprintf "%s \\SUP %s" (pp_term t1) (pp_term t2)
215 | Ast.Below (t1, t2) -> sprintf "%s \\BELOW %s" (pp_term t1) (pp_term t2)
216 | Ast.Above (t1, t2) -> sprintf "%s \\ABOVE %s" (pp_term t1) (pp_term t2)
217 | Ast.Over (t1, t2) -> sprintf "[%s \\OVER %s]" (pp_term t1) (pp_term t2)
218 | Ast.Atop (t1, t2) -> sprintf "[%s \\ATOP %s]" (pp_term t1) (pp_term t2)
219 | Ast.Frac (t1, t2) -> sprintf "\\FRAC %s %s" (pp_term t1) (pp_term t2)
220 | Ast.InfRule (t1, t2, t3) -> sprintf "\\INFRULE %s %s %s" (pp_term t1)
221 (pp_term t2) (pp_term t3)
222 | Ast.Maction l -> sprintf "\\MACTION (%s)"
223 (String.concat "," (List.map pp_term l))
224 | Ast.Sqrt t -> sprintf "\\SQRT %s" (pp_term t)
225 | Ast.Root (arg, index) ->
226 sprintf "\\ROOT %s \\OF %s" (pp_term index) (pp_term arg)
227 | Ast.Break -> "\\BREAK"
228 (* | Space -> "\\SPACE" *)
229 | Ast.Box (box_spec, terms) ->
230 sprintf "\\%s [%s]" (pp_box_spec box_spec)
231 (String.concat " " (List.map pp_term terms))
233 sprintf "\\GROUP [%s]" (String.concat " " (List.map pp_term terms))
234 | Ast.Mstyle (l,terms) ->
235 sprintf "\\MSTYLE %s [%s]"
236 (String.concat " " (List.map (fun (k,v) -> k^"="^v) l))
237 (String.concat " " (List.map pp_term terms))
238 | Ast.Mpadded (l,terms) ->
239 sprintf "\\MSTYLE %s [%s]"
240 (String.concat " " (List.map (fun (k,v) -> k^"="^v) l))
241 (String.concat " " (List.map pp_term terms))
243 and pp_magic = function
244 | Ast.List0 (t, sep_opt) ->
245 sprintf "list0 %s%s" (pp_term t) (pp_sep_opt sep_opt)
246 | Ast.List1 (t, sep_opt) ->
247 sprintf "list1 %s%s" (pp_term t) (pp_sep_opt sep_opt)
248 | Ast.Opt t -> sprintf "opt %s" (pp_term t)
249 | Ast.Fold (kind, p_base, names, p_rec) ->
250 let acc = match names with acc :: _ -> acc | _ -> assert false in
251 sprintf "fold %s %s rec %s %s"
252 (pp_fold_kind kind) (pp_term p_base) acc (pp_term p_rec)
253 | Ast.Default (p_some, p_none) ->
254 sprintf "default %s %s" (pp_term p_some) (pp_term p_none)
255 | Ast.If (p_test, p_true, p_false) ->
256 sprintf "if %s then %s else %s"
257 (pp_term p_test) (pp_term p_true) (pp_term p_false)
260 and pp_fold_kind = function
264 and pp_sep_opt = function
266 | Some sep -> sprintf " sep %s" (pp_literal sep)
268 and pp_variable = function
269 | Ast.NumVar s -> "number " ^ s
270 | Ast.IdentVar s -> "ident " ^ s
271 | Ast.TermVar (s,Ast.Self _) -> s
272 | Ast.TermVar (s,Ast.Level l) -> "term " ^string_of_int l
273 | Ast.Ascription (t, n) -> assert false
274 | Ast.FreshVar n -> "fresh " ^ n
276 let _pp_term = ref (pp_term ~pp_parens:false)
277 let pp_term t = !_pp_term t
278 let set_pp_term f = _pp_term := f
280 let pp_params pp_term = function
282 | params -> " " ^ String.concat " " (List.map (pp_capture_variable pp_term) params)
284 let pp_flavour = function
285 | `Definition -> "definition"
286 | `MutualDefinition -> assert false
290 | `Remark -> "remark"
291 | `Theorem -> "theorem"
292 | `Variant -> "variant"
295 let pp_fields pp_term fields =
296 (if fields <> [] then "\n" else "") ^
299 (fun (name,ty,coercion,arity) ->
302 (":" ^ (if arity > 0 then string_of_int arity else "") ^ "> ")
307 let pp_obj pp_term = function
308 | Ast.Inductive (params, types) ->
309 let pp_constructors constructors =
311 (List.map (fun (name, typ) -> sprintf "| %s: %s" name (pp_term typ))
314 let pp_type (name, _, typ, constructors) =
315 sprintf "\nwith %s: %s \\def\n%s" name (pp_term typ)
316 (pp_constructors constructors)
320 | (name, inductive, typ, constructors) :: tl ->
322 sprintf "%sinductive %s%s: %s \\def\n%s"
323 (if inductive then "" else "co") name (pp_params pp_term params)
324 (pp_term typ) (pp_constructors constructors)
326 fst_typ_pp ^ String.concat "" (List.map pp_type tl))
327 | Ast.Theorem (`MutualDefinition, name, typ, body,_) ->
328 sprintf "<<pretty printing of mutual definitions not implemented yet>>"
329 | Ast.Theorem (flavour, name, typ, body,_) ->
330 sprintf "%s %s:\n %s\n%s"
336 | Some body -> "\\def\n " ^ pp_term body)
337 | Ast.Record (params,name,ty,fields) ->
338 "record " ^ name ^ " " ^ pp_params pp_term params ^ ": " ^ pp_term ty ^
339 " \\def {" ^ pp_fields pp_term fields ^ "\n}"
341 let rec pp_value = function
342 | Env.TermValue t -> sprintf "$%s$" (pp_term t)
343 | Env.StringValue s -> sprintf "\"%s\"" s
344 | Env.NumValue n -> n
345 | Env.OptValue (Some v) -> "Some " ^ pp_value v
346 | Env.OptValue None -> "None"
347 | Env.ListValue l -> sprintf "[%s]" (String.concat "; " (List.map pp_value l))
349 let rec pp_value_type =
351 | Env.TermType l -> "Term "^string_of_int l
352 | Env.StringType -> "String"
353 | Env.NumType -> "Number"
354 | Env.OptType t -> "Maybe " ^ pp_value_type t
355 | Env.ListType l -> "List " ^ pp_value_type l
360 (fun (name, (ty, value)) ->
361 sprintf "%s : %s = %s" name (pp_value_type ty) (pp_value value))
364 let rec pp_cic_appl_pattern = function
365 | Ast.UriPattern uri -> UriManager.string_of_uri uri
366 | Ast.NRefPattern nref -> NReference.string_of_reference nref
367 | Ast.VarPattern name -> name
368 | Ast.ImplicitPattern -> "?"
369 | Ast.ApplPattern aps ->
370 sprintf "(%s)" (String.concat " " (List.map pp_cic_appl_pattern aps))