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 *)
49 sprintf "symbol(%s)" (NotationUtil.string_of_literal t)
51 sprintf "keyword(%s)" (NotationUtil.string_of_literal t)
52 | `Number _ as t -> sprintf "number(%s)" (NotationUtil.string_of_literal t))
53 else NotationUtil.string_of_literal
57 (* `None -> "`None" *)
64 | `IdRef id -> sprintf "x(%s)" id
68 (List.map (fun (_, n, v) -> sprintf "%s=%s" n v) attrs))
69 | `Level (prec) -> sprintf "L(%d)" prec
71 | `Loc l -> let x,y = HExtlib.loc_of_floc l in
72 "@" ^ (string_of_int x) ^ "," ^ (string_of_int y)
73 | `ChildPos p -> sprintf "P(%s)" (pp_pos p)
75 let pp_capture_variable pp_term =
77 | term, None -> pp_term (* ~pp_parens:false *) term
78 | term, Some typ -> "(" ^ pp_term (* ~pp_parens:false *) term ^ ": " ^ pp_term typ ^ ")"
80 let rec pp_term (status : #NCic.status) ?(pp_parens = true) t =
81 let debug_printing = true in
82 let pp_ident = function
83 | Ast.Ident (id,`Ambiguous) -> "A:" ^ id
84 | Ast.Ident (id,`Rel) -> "R:" ^ id
85 | Ast.Ident (id,`Uri u) -> ("U:(" ^ id ^ "," ^ u ^ ")")
88 let pp_term = pp_term status in
91 | Ast.AttributedTerm (attr, term) when debug_printing ->
92 sprintf "%s[%s]" (pp_attribute attr) (pp_term ~pp_parens:false term)
93 | Ast.AttributedTerm (`Raw text, t) -> "RAW:(" ^ (pp_term t) ^ ")"
94 | Ast.AttributedTerm (_, term) -> pp_term ~pp_parens:false term
96 sprintf "%s" (String.concat " " (List.map pp_term terms))
97 | Ast.Binder (`Forall, (Ast.Ident ("_", `Ambiguous), typ), body)
98 | Ast.Binder (`Forall, (Ast.Ident ("_", `Rel), typ), body)
99 | Ast.Binder (`Pi, (Ast.Ident ("_", `Ambiguous), typ), body)
100 | Ast.Binder (`Pi, (Ast.Ident ("_", `Rel), typ), body) ->
102 (match typ with None -> "?" | Some typ -> pp_term typ)
103 (pp_term ~pp_parens:true body)
104 | Ast.Binder (kind, var, body) ->
106 * sprintf "\\%s[%s] %s.%s" (pp_binder kind) (pp_ident var) (pp_capture_variable pp_term var)
108 sprintf "\\%s %s.%s" (pp_binder kind) (pp_capture_variable pp_term var)
109 (pp_term ~pp_parens:true body)
110 | Ast.Case (term, indtype, typ, patterns) ->
111 sprintf "match %s%s%s with %s"
115 | Some (ty, href_opt) ->
116 sprintf " in %s%s" ty
117 (match (*debug_printing*) true, href_opt with
119 sprintf "(i.e.%s)" (NReference.string_of_reference uri)
121 (match typ with None -> "" | Some t -> sprintf " return %s" (pp_term t))
122 (pp_patterns status patterns)
123 | Ast.Cast (t1, t2) -> sprintf "(%s: %s)" (pp_term ~pp_parens:true t1) (pp_term ~pp_parens:true t2)
124 | Ast.LetIn ((var,t2), t1, t3) ->
125 (* let t2 = match t2 with None -> Ast.Implicit | Some t -> t in *)
126 sprintf "let %s \\def %s in %s" (pp_term var)
127 (* (pp_term ~pp_parens:true t2) *)
128 (pp_term ~pp_parens:true t1)
129 (pp_term ~pp_parens:true t3)
130 | Ast.LetRec (kind, definitions, term) ->
131 let rec get_guard i = function
132 | [] -> (*assert false*) Ast.Implicit `JustOne
133 | [term, _] when i = 1 -> term
134 | _ :: tl -> get_guard (pred i) tl
136 let map (params, (id,typ), body, i) =
139 None -> Ast.Implicit `JustOne
142 sprintf "%s %s on %s: %s \\def %s"
143 (pp_term ~pp_parens:false term)
144 (String.concat " " (List.map (pp_capture_variable pp_term) params))
145 (pp_term ~pp_parens:false (get_guard i params))
146 (pp_term typ) (pp_term body)
148 sprintf "let %s %s in %s"
149 (match kind with `Inductive -> "rec" | `CoInductive -> "corec")
150 (String.concat " and " (List.map map definitions))
152 | Ast.Ident (name, _ ) -> pp_ident t
153 | Ast.NRef nref -> NReference.string_of_reference nref
154 | Ast.NCic cic -> status#ppterm ~metasenv:[] ~context:[] ~subst:[] cic
155 | Ast.Implicit `Vector -> "…"
156 | Ast.Implicit `JustOne -> "?"
157 | Ast.Implicit (`Tagged name) -> "?"^name
158 | Ast.Meta (index, substs) ->
159 sprintf "%d[%s]" index
161 (List.map (function None -> "?" | Some t -> pp_term t) substs))
162 | Ast.Num (num, _) -> num
163 | Ast.Sort `Set -> "Set"
164 | Ast.Sort `Prop -> "Prop"
165 | Ast.Sort (`NType s)-> "Type[" ^ s ^ "]"
166 | Ast.Sort (`NCProp s)-> "CProp[" ^ s ^ "]"
167 | Ast.Symbol (name, None) -> "'" ^ name
168 | Ast.Symbol (name, Some (_,desc)) -> "'" ^ name ^ ":" ^ desc
170 | Ast.UserInput -> "%"
172 | Ast.Literal l -> "literal:(" ^ (pp_literal l) ^ ")"
173 | Ast.Layout l -> "layout:(" ^ (pp_layout status l) ^ ")"
174 | Ast.Magic m -> "magic:(" ^ (pp_magic status m) ^ ")"
175 | Ast.Variable v -> "variable:" ^ (pp_variable v)
177 match pp_parens, t with
179 | true, Ast.Implicit _
180 | true, Ast.UserInput
182 | true, Ast.Ident _ -> t_pp
183 | _ -> sprintf "(%s)" t_pp
185 and pp_pattern status =
187 Ast.Pattern (head, href, vars), term ->
190 (match (*debug_printing*)true, href with
191 | true, Some uri -> sprintf "(i.e.%s)" (NReference.string_of_reference uri)
192 | true, None -> "(i.e.ambiguous)"
195 sprintf "%s \\Rightarrow %s"
199 sprintf "(%s %s)" head_pp
200 (String.concat " " (List.map (pp_capture_variable (pp_term status)) vars)))
201 (pp_term status term)
202 | Ast.Wildcard, term ->
203 sprintf "_ \\Rightarrow %s" (pp_term status term)
205 and pp_patterns status patterns =
206 sprintf "[%s]" (String.concat " | " (List.map (pp_pattern status) patterns))
208 and pp_box_spec (kind, spacing, indent) =
209 let int_of_bool b = if b then 1 else 0 in
212 Ast.H -> "H" | Ast.V -> "V" | Ast.HV -> "HV" | Ast.HOV -> "HOV"
214 sprintf "%sBOX%d%d" kind_string (int_of_bool spacing) (int_of_bool indent)
216 and pp_layout status =
217 let pp_term = pp_term status in
219 | Ast.Sub (t1, t2) -> sprintf "%s \\SUB %s" (pp_term t1) (pp_term t2)
220 | Ast.Sup (t1, t2) -> sprintf "%s \\SUP %s" (pp_term t1) (pp_term t2)
221 | Ast.Below (t1, t2) -> sprintf "%s \\BELOW %s" (pp_term t1) (pp_term t2)
222 | Ast.Above (t1, t2) -> sprintf "%s \\ABOVE %s" (pp_term t1) (pp_term t2)
223 | Ast.Over (t1, t2) -> sprintf "[%s \\OVER %s]" (pp_term t1) (pp_term t2)
224 | Ast.Atop (t1, t2) -> sprintf "[%s \\ATOP %s]" (pp_term t1) (pp_term t2)
225 | Ast.Frac (t1, t2) -> sprintf "\\FRAC %s %s" (pp_term t1) (pp_term t2)
226 | Ast.InfRule (t1, t2, t3) -> sprintf "\\INFRULE %s %s %s" (pp_term t1)
227 (pp_term t2) (pp_term t3)
228 | Ast.Maction l -> sprintf "\\MACTION (%s)"
229 (String.concat "," (List.map pp_term l))
230 | Ast.Sqrt t -> sprintf "\\SQRT %s" (pp_term t)
231 | Ast.Root (arg, index) ->
232 sprintf "\\ROOT %s \\OF %s" (pp_term index) (pp_term arg)
233 | Ast.Break -> "\\BREAK"
234 (* | Space -> "\\SPACE" *)
235 | Ast.Box (box_spec, terms) ->
236 sprintf "\\%s [%s]" (pp_box_spec box_spec)
237 (String.concat " " (List.map pp_term terms))
239 sprintf "\\GROUP [%s]" (String.concat " " (List.map pp_term terms))
240 | Ast.Mstyle (l,terms) ->
241 sprintf "\\MSTYLE %s [%s]"
242 (String.concat " " (List.map (fun (k,v) -> k^"="^v) l))
243 (String.concat " " (List.map pp_term terms))
244 | Ast.Mpadded (l,terms) ->
245 sprintf "\\MSTYLE %s [%s]"
246 (String.concat " " (List.map (fun (k,v) -> k^"="^v) l))
247 (String.concat " " (List.map pp_term terms))
249 and pp_magic status =
250 let pp_term = pp_term status in
252 | Ast.List0 (t, sep_opt) ->
253 sprintf "list0 %s%s" (pp_term t) (pp_sep_opt sep_opt)
254 | Ast.List1 (t, sep_opt) ->
255 sprintf "list1 %s%s" (pp_term t) (pp_sep_opt sep_opt)
256 | Ast.Opt t -> sprintf "opt %s" (pp_term t)
257 | Ast.Fold (kind, p_base, names, p_rec) ->
258 let acc = match names with acc :: _ -> acc | _ -> assert false in
259 sprintf "fold %s %s rec %s %s"
260 (pp_fold_kind kind) (pp_term p_base) acc (pp_term p_rec)
261 | Ast.Default (p_some, p_none) ->
262 sprintf "default %s %s" (pp_term p_some) (pp_term p_none)
263 | Ast.If (p_test, p_true, p_false) ->
264 sprintf "if %s then %s else %s"
265 (pp_term p_test) (pp_term p_true) (pp_term p_false)
268 and pp_fold_kind = function
272 and pp_sep_opt = function
274 | Some sep -> sprintf " sep %s" (pp_literal sep)
276 and pp_variable = function
277 | Ast.NumVar s -> "number " ^ s
278 | Ast.IdentVar s -> "ident " ^ s
279 | Ast.TermVar (s,Ast.Self _) -> s
280 | Ast.TermVar (s,Ast.Level l) -> "term " ^string_of_int l
281 | Ast.Ascription (t, n) -> assert false
282 | Ast.FreshVar n -> "fresh " ^ n
284 let _pp_term = ref (pp_term ~pp_parens:false)
285 let pp_term status t = !_pp_term (status :> NCic.status) t
286 let set_pp_term f = _pp_term := f
288 let pp_params pp_term = function
290 | params -> " " ^ String.concat " " (List.map (pp_capture_variable pp_term) params)
292 let pp_fields pp_term fields =
293 (if fields <> [] then "\n" else "") ^
296 (fun (name,ty,coercion,arity) ->
299 (":" ^ (if arity > 0 then string_of_int arity else "") ^ "> ")
304 let pp_obj pp_term = function
305 | Ast.Inductive (params, types) ->
306 let pp_constructors constructors =
308 (List.map (fun (name, typ) -> sprintf "| %s: %s" name (pp_term typ))
311 let pp_type (name, _, typ, constructors) =
312 sprintf "\nwith %s: %s \\def\n%s" name (pp_term typ)
313 (pp_constructors constructors)
317 | (name, inductive, typ, constructors) :: tl ->
319 sprintf "%sinductive %s%s: %s \\def\n%s"
320 (if inductive then "" else "co") name (pp_params pp_term params)
321 (pp_term typ) (pp_constructors constructors)
323 fst_typ_pp ^ String.concat "" (List.map pp_type tl))
324 | Ast.Theorem (flavour, name, typ, body,_) ->
325 sprintf "%s %s:\n %s\n%s"
326 (NCicPp.string_of_flavour flavour)
331 | Some body -> "\\def\n " ^ pp_term body)
332 | Ast.Record (params,name,ty,fields) ->
333 "record " ^ name ^ " " ^ pp_params pp_term params ^ ": " ^ pp_term ty ^
334 " \\def {" ^ pp_fields pp_term fields ^ "\n}"
336 let rec pp_value (status: #NCic.status) = function
337 | Env.TermValue t -> sprintf "$%s$" (pp_term status t)
338 | Env.StringValue (Env.Ident s) -> sprintf "\"%s\"" s
339 | Env.StringValue (Env.Var s) -> sprintf "\"${ident %s}\"" s
340 | Env.NumValue n -> n
341 | Env.OptValue (Some v) -> "Some " ^ pp_value status v
342 | Env.OptValue None -> "None"
343 | Env.ListValue l -> sprintf "[%s]" (String.concat "; " (List.map (pp_value status) l))
344 | Env.DisambiguationValue _ -> sprintf "#"
346 let rec pp_value_type =
348 | Env.TermType l -> "Term "^string_of_int l
349 | Env.StringType -> "String"
350 | Env.NumType -> "Number"
351 | Env.OptType t -> "Maybe " ^ pp_value_type t
352 | Env.ListType l -> "List " ^ pp_value_type l
353 | Env.NoType -> "NoType"
355 let pp_env status env =
358 (fun (name, (ty, value)) ->
359 sprintf "%s : %s = %s" name (pp_value_type ty) (pp_value status value))
362 let rec pp_cic_appl_pattern = function
363 | Ast.NRefPattern nref -> NReference.string_of_reference nref
364 | Ast.VarPattern name -> name
365 | Ast.ImplicitPattern -> "?"
366 | Ast.ApplPattern aps ->
367 sprintf "(%s)" (String.concat " " (List.map pp_cic_appl_pattern aps))