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/
28 module Ast = CicNotationPt
29 module Env = CicNotationEnv
31 (* when set to true debugging information, not in sync with input syntax, will
32 * be added to the output of pp_term.
33 * set to false if you need, for example, cut and paste from matitac output to
35 let debug_printing = true
37 let pp_binder = function
44 if debug_printing then
45 (function (* debugging version *)
46 | `Symbol s -> sprintf "symbol(%s)" s
47 | `Keyword s -> sprintf "keyword(%s)" s
48 | `Number s -> sprintf "number(%s)" s)
57 | Gramext.NonA -> "NonA"
58 | Gramext.LeftA -> "LeftA"
59 | Gramext.RightA -> "RightA"
63 (* `None -> "`None" *)
70 | `IdRef id -> sprintf "x(%s)" id
74 (List.map (fun (_, n, v) -> sprintf "%s=%s" n v) attrs))
75 | `Level (prec, assoc) -> sprintf "L(%d%s)" prec (pp_assoc assoc)
78 | `ChildPos p -> sprintf "P(%s)" (pp_pos p)
80 let rec pp_term ?(pp_parens = true) t =
83 | Ast.AttributedTerm (attr, term) when debug_printing ->
84 sprintf "%s[%s]" (pp_attribute attr) (pp_term ~pp_parens:false term)
85 | Ast.AttributedTerm (`Raw text, _) -> text
86 | Ast.AttributedTerm (_, term) -> pp_term ~pp_parens:false term
88 sprintf "%s" (String.concat " " (List.map pp_term terms))
89 | Ast.Binder (`Forall, (Ast.Ident ("_", None), typ), body)
90 | Ast.Binder (`Pi, (Ast.Ident ("_", None), typ), body) ->
92 (match typ with None -> "?" | Some typ -> pp_term typ)
94 | Ast.Binder (kind, var, body) ->
95 sprintf "\\%s %s.%s" (pp_binder kind) (pp_capture_variable var)
97 | Ast.Case (term, indtype, typ, patterns) ->
98 sprintf "%smatch %s%s with %s"
99 (match typ with None -> "" | Some t -> sprintf "[%s]" (pp_term t))
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 (pp_patterns patterns)
110 | Ast.Cast (t1, t2) -> sprintf "(%s: %s)" (pp_term t1) (pp_term t2)
111 | Ast.LetIn (var, t1, t2) ->
112 sprintf "let %s = %s in %s" (pp_capture_variable var) (pp_term t1)
114 | Ast.LetRec (kind, definitions, term) ->
115 sprintf "let %s %s in %s"
116 (match kind with `Inductive -> "rec" | `CoInductive -> "corec")
117 (String.concat " and "
119 (fun (var, body, _) ->
120 sprintf "%s = %s" (pp_capture_variable var) (pp_term body))
123 | Ast.Ident (name, Some []) | Ast.Ident (name, None)
124 | Ast.Uri (name, Some []) | Ast.Uri (name, None) ->
126 | Ast.Ident (name, Some substs)
127 | Ast.Uri (name, Some substs) ->
128 sprintf "%s \\subst [%s]" name (pp_substs substs)
129 | Ast.Implicit -> "?"
130 | Ast.Meta (index, substs) ->
131 sprintf "%d[%s]" index
133 (List.map (function None -> "_" | Some t -> pp_term t) substs))
134 | Ast.Num (num, _) -> num
135 | Ast.Sort `Set -> "Set"
136 | Ast.Sort `Prop -> "Prop"
137 | Ast.Sort (`Type _) -> "Type"
138 | Ast.Sort `CProp -> "CProp"
139 | Ast.Symbol (name, _) -> "'" ^ name
141 | Ast.UserInput -> ""
143 | Ast.Literal l -> pp_literal l
144 | Ast.Layout l -> pp_layout l
145 | Ast.Magic m -> pp_magic m
146 | Ast.Variable v -> pp_variable v
148 if pp_parens then sprintf "(%s)" t_pp
151 and pp_subst (name, term) = sprintf "%s \\Assign %s" name (pp_term term)
152 and pp_substs substs = String.concat "; " (List.map pp_subst substs)
154 and pp_pattern ((head, href, vars), term) =
157 (match debug_printing, href with
158 | true, Some uri -> sprintf "(i.e.%s)" (UriManager.string_of_uri uri)
161 sprintf "%s \\Rightarrow %s"
165 sprintf "(%s %s)" head_pp
166 (String.concat " " (List.map pp_capture_variable vars)))
169 and pp_patterns patterns =
170 sprintf "[%s]" (String.concat " | " (List.map pp_pattern patterns))
172 and pp_capture_variable = function
173 | term, None -> pp_term term
174 | term, Some typ -> "(" ^ pp_term term ^ ": " ^ pp_term typ ^ ")"
176 and pp_box_spec (kind, spacing, indent) =
177 let int_of_bool b = if b then 1 else 0 in
180 Ast.H -> "H" | Ast.V -> "V" | Ast.HV -> "HV" | Ast.HOV -> "HOV"
182 sprintf "%sBOX%d%d" kind_string (int_of_bool spacing) (int_of_bool indent)
184 and pp_layout = function
185 | Ast.Sub (t1, t2) -> sprintf "%s \\SUB %s" (pp_term t1) (pp_term t2)
186 | Ast.Sup (t1, t2) -> sprintf "%s \\SUP %s" (pp_term t1) (pp_term t2)
187 | Ast.Below (t1, t2) -> sprintf "%s \\BELOW %s" (pp_term t1) (pp_term t2)
188 | Ast.Above (t1, t2) -> sprintf "%s \\ABOVE %s" (pp_term t1) (pp_term t2)
189 | Ast.Over (t1, t2) -> sprintf "[%s \\OVER %s]" (pp_term t1) (pp_term t2)
190 | Ast.Atop (t1, t2) -> sprintf "[%s \\ATOP %s]" (pp_term t1) (pp_term t2)
191 | Ast.Frac (t1, t2) -> sprintf "\\FRAC %s %s" (pp_term t1) (pp_term t2)
192 | Ast.Sqrt t -> sprintf "\\SQRT %s" (pp_term t)
193 | Ast.Root (arg, index) ->
194 sprintf "\\ROOT %s \\OF %s" (pp_term index) (pp_term arg)
195 | Ast.Break -> "\\BREAK"
196 (* | Space -> "\\SPACE" *)
197 | Ast.Box (box_spec, terms) ->
198 sprintf "\\%s [%s]" (pp_box_spec box_spec)
199 (String.concat " " (List.map pp_term terms))
201 sprintf "\\GROUP [%s]" (String.concat " " (List.map pp_term terms))
203 and pp_magic = function
204 | Ast.List0 (t, sep_opt) ->
205 sprintf "list0 %s%s" (pp_term t) (pp_sep_opt sep_opt)
206 | Ast.List1 (t, sep_opt) ->
207 sprintf "list1 %s%s" (pp_term t) (pp_sep_opt sep_opt)
208 | Ast.Opt t -> sprintf "opt %s" (pp_term t)
209 | Ast.Fold (kind, p_base, names, p_rec) ->
210 let acc = match names with acc :: _ -> acc | _ -> assert false in
211 sprintf "fold %s %s rec %s %s"
212 (pp_fold_kind kind) (pp_term p_base) acc (pp_term p_rec)
213 | Ast.Default (p_some, p_none) ->
214 sprintf "default %s %s" (pp_term p_some) (pp_term p_none)
215 | Ast.If (p_test, p_true, p_false) ->
216 sprintf "if %s then %s else %s"
217 (pp_term p_test) (pp_term p_true) (pp_term p_false)
220 and pp_fold_kind = function
224 and pp_sep_opt = function
226 | Some sep -> sprintf " sep %s" (pp_literal sep)
228 and pp_variable = function
229 | Ast.NumVar s -> "number " ^ s
230 | Ast.IdentVar s -> "ident " ^ s
231 | Ast.TermVar s -> "term " ^ s
232 | Ast.Ascription (t, n) -> assert false
233 | Ast.FreshVar n -> "fresh " ^ n
235 let pp_term t = pp_term ~pp_parens:false t
237 let pp_params = function
243 (fun (name, typ) -> sprintf "(%s:%s)" name (pp_term typ))
246 let pp_flavour = function
247 | `Definition -> "Definition"
251 | `Remark -> "Remark"
252 | `Theorem -> "Theorem"
253 | `Variant -> "Variant"
255 let pp_fields fields =
256 (if fields <> [] then "\n" else "") ^
259 (fun (name,ty,coercion) ->
260 " " ^ name ^ if coercion then ":>" else ": " ^ pp_term ty) fields)
262 let pp_obj = function
263 | Ast.Inductive (params, types) ->
264 let pp_constructors constructors =
266 (List.map (fun (name, typ) -> sprintf "| %s: %s" name (pp_term typ))
269 let pp_type (name, _, typ, constructors) =
270 sprintf "\nwith %s: %s \\def\n%s" name (pp_term typ)
271 (pp_constructors constructors)
275 | (name, inductive, typ, constructors) :: tl ->
277 sprintf "%sinductive %s%s: %s \\def\n%s"
278 (if inductive then "" else "co") name (pp_params params)
279 (pp_term typ) (pp_constructors constructors)
281 fst_typ_pp ^ String.concat "" (List.map pp_type tl))
282 | Ast.Theorem (flavour, name, typ, body) ->
283 sprintf "%s %s: %s %s"
289 | Some body -> "\\def " ^ pp_term body)
290 | Ast.Record (params,name,ty,fields) ->
291 "record " ^ name ^ " " ^ pp_params params ^ " \\def {" ^
292 pp_fields fields ^ "}"
294 let rec pp_value = function
295 | Env.TermValue t -> sprintf "$%s$" (pp_term t)
296 | Env.StringValue s -> sprintf "\"%s\"" s
297 | Env.NumValue n -> n
298 | Env.OptValue (Some v) -> "Some " ^ pp_value v
299 | Env.OptValue None -> "None"
300 | Env.ListValue l -> sprintf "[%s]" (String.concat "; " (List.map pp_value l))
302 let rec pp_value_type =
304 | Env.TermType -> "Term"
305 | Env.StringType -> "String"
306 | Env.NumType -> "Number"
307 | Env.OptType t -> "Maybe " ^ pp_value_type t
308 | Env.ListType l -> "List " ^ pp_value_type l
313 (fun (name, (ty, value)) ->
314 sprintf "%s : %s = %s" name (pp_value_type ty) (pp_value value))
317 let rec pp_cic_appl_pattern = function
318 | Ast.UriPattern uri -> UriManager.string_of_uri uri
319 | Ast.VarPattern name -> name
320 | Ast.ImplicitPattern -> "_"
321 | Ast.ApplPattern aps ->
322 sprintf "(%s)" (String.concat " " (List.map pp_cic_appl_pattern aps))