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 = false
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)
58 | Gramext.LeftA -> "L"
59 | Gramext.RightA -> "R"
61 let rec pp_term ?(pp_parens = true) t =
64 | Ast.AttributedTerm (`IdRef id, term) when debug_printing ->
65 sprintf "x(%s)[%s]" id (pp_term ~pp_parens:false term)
66 | Ast.AttributedTerm (`XmlAttrs attrs, term) when debug_printing ->
69 (List.map (fun (_, n, v) -> sprintf "%s=%s" n v) attrs))
70 (pp_term ~pp_parens:false term)
71 | Ast.AttributedTerm (`Level (prec, assoc), term) when debug_printing ->
72 sprintf "L(%d%s)[%s]" prec (pp_assoc assoc)
73 (pp_term ~pp_parens:false term)
74 | Ast.AttributedTerm (`Raw _, term) when debug_printing ->
75 sprintf "R[%s]" (pp_term ~pp_parens:false term)
76 | Ast.AttributedTerm (`Loc _, term) when debug_printing ->
77 sprintf "@[%s]" (pp_term ~pp_parens:false term)
79 | Ast.AttributedTerm (`Raw text, _) -> text
80 | Ast.AttributedTerm (_, term) -> pp_term ~pp_parens:false term
83 sprintf "%s" (String.concat " " (List.map pp_term terms))
84 | Ast.Binder (`Forall, (Ast.Ident ("_", None), typ), body)
85 | Ast.Binder (`Pi, (Ast.Ident ("_", None), typ), body) ->
87 (match typ with None -> "?" | Some typ -> pp_term typ)
89 | Ast.Binder (kind, var, body) ->
90 sprintf "\\%s %s.%s" (pp_binder kind) (pp_capture_variable var)
92 | Ast.Case (term, indtype, typ, patterns) ->
93 sprintf "%smatch %s%s with %s"
94 (match typ with None -> "" | Some t -> sprintf "[%s]" (pp_term t))
98 | Some (ty, href_opt) ->
100 (match debug_printing, href_opt with
102 sprintf "(i.e.%s)" (UriManager.string_of_uri uri)
104 (pp_patterns patterns)
105 | Ast.Cast (t1, t2) -> sprintf "(%s: %s)" (pp_term t1) (pp_term t2)
106 | Ast.LetIn (var, t1, t2) ->
107 sprintf "let %s = %s in %s" (pp_capture_variable var) (pp_term t1)
109 | Ast.LetRec (kind, definitions, term) ->
110 sprintf "let %s %s in %s"
111 (match kind with `Inductive -> "rec" | `CoInductive -> "corec")
112 (String.concat " and "
114 (fun (var, body, _) ->
115 sprintf "%s = %s" (pp_capture_variable var) (pp_term body))
118 | Ast.Ident (name, Some []) | Ast.Ident (name, None)
119 | Ast.Uri (name, Some []) | Ast.Uri (name, None) ->
121 | Ast.Ident (name, Some substs)
122 | Ast.Uri (name, Some substs) ->
123 sprintf "%s \\subst [%s]" name (pp_substs substs)
124 | Ast.Implicit -> "?"
125 | Ast.Meta (index, substs) ->
126 sprintf "%d[%s]" index
128 (List.map (function None -> "_" | Some t -> pp_term t) substs))
129 | Ast.Num (num, _) -> num
130 | Ast.Sort `Set -> "Set"
131 | Ast.Sort `Prop -> "Prop"
132 | Ast.Sort (`Type _) -> "Type"
133 | Ast.Sort `CProp -> "CProp"
134 | Ast.Symbol (name, _) -> "'" ^ name
136 | Ast.UserInput -> ""
138 | Ast.Literal l -> pp_literal l
139 | Ast.Layout l -> pp_layout l
140 | Ast.Magic m -> pp_magic m
141 | Ast.Variable v -> pp_variable v
143 if pp_parens then sprintf "(%s)" t_pp
146 and pp_subst (name, term) = sprintf "%s \\Assign %s" name (pp_term term)
147 and pp_substs substs = String.concat "; " (List.map pp_subst substs)
149 and pp_pattern ((head, href, vars), term) =
152 (match debug_printing, href with
153 | true, Some uri -> sprintf "(i.e.%s)" (UriManager.string_of_uri uri)
156 sprintf "%s \\Rightarrow %s"
160 sprintf "(%s %s)" head_pp
161 (String.concat " " (List.map pp_capture_variable vars)))
164 and pp_patterns patterns =
165 sprintf "[%s]" (String.concat " | " (List.map pp_pattern patterns))
167 and pp_capture_variable = function
168 | term, None -> pp_term term
169 | term, Some typ -> "(" ^ pp_term term ^ ": " ^ pp_term typ ^ ")"
171 and pp_box_spec (kind, spacing, indent) =
172 let int_of_bool b = if b then 1 else 0 in
175 Ast.H -> "H" | Ast.V -> "V" | Ast.HV -> "HV" | Ast.HOV -> "HOV"
177 sprintf "%sBOX%d%d" kind_string (int_of_bool spacing) (int_of_bool indent)
179 and pp_layout = function
180 | Ast.Sub (t1, t2) -> sprintf "%s \\SUB %s" (pp_term t1) (pp_term t2)
181 | Ast.Sup (t1, t2) -> sprintf "%s \\SUP %s" (pp_term t1) (pp_term t2)
182 | Ast.Below (t1, t2) -> sprintf "%s \\BELOW %s" (pp_term t1) (pp_term t2)
183 | Ast.Above (t1, t2) -> sprintf "%s \\ABOVE %s" (pp_term t1) (pp_term t2)
184 | Ast.Over (t1, t2) -> sprintf "[%s \\OVER %s]" (pp_term t1) (pp_term t2)
185 | Ast.Atop (t1, t2) -> sprintf "[%s \\ATOP %s]" (pp_term t1) (pp_term t2)
186 | Ast.Frac (t1, t2) -> sprintf "\\FRAC %s %s" (pp_term t1) (pp_term t2)
187 | Ast.Sqrt t -> sprintf "\\SQRT %s" (pp_term t)
188 | Ast.Root (arg, index) ->
189 sprintf "\\ROOT %s \\OF %s" (pp_term index) (pp_term arg)
190 | Ast.Break -> "\\BREAK"
191 (* | Space -> "\\SPACE" *)
192 | Ast.Box (box_spec, terms) ->
193 sprintf "\\%s [%s]" (pp_box_spec box_spec)
194 (String.concat " " (List.map pp_term terms))
196 sprintf "\\GROUP [%s]" (String.concat " " (List.map pp_term terms))
198 and pp_magic = function
199 | Ast.List0 (t, sep_opt) ->
200 sprintf "list0 %s%s" (pp_term t) (pp_sep_opt sep_opt)
201 | Ast.List1 (t, sep_opt) ->
202 sprintf "list1 %s%s" (pp_term t) (pp_sep_opt sep_opt)
203 | Ast.Opt t -> sprintf "opt %s" (pp_term t)
204 | Ast.Fold (kind, p_base, names, p_rec) ->
205 let acc = match names with acc :: _ -> acc | _ -> assert false in
206 sprintf "fold %s %s rec %s %s"
207 (pp_fold_kind kind) (pp_term p_base) acc (pp_term p_rec)
208 | Ast.Default (p_some, p_none) ->
209 sprintf "default %s %s" (pp_term p_some) (pp_term p_none)
210 | Ast.If (p_test, p_true, p_false) ->
211 sprintf "if %s then %s else %s"
212 (pp_term p_test) (pp_term p_true) (pp_term p_false)
215 and pp_fold_kind = function
219 and pp_sep_opt = function
221 | Some sep -> sprintf " sep %s" (pp_literal sep)
223 and pp_variable = function
224 | Ast.NumVar s -> "number " ^ s
225 | Ast.IdentVar s -> "ident " ^ s
226 | Ast.TermVar s -> "term " ^ s
227 | Ast.Ascription (t, n) -> assert false
228 | Ast.FreshVar n -> "fresh " ^ n
230 let pp_term t = pp_term ~pp_parens:false t
232 let rec pp_value = function
233 | Env.TermValue t -> sprintf "$%s$" (pp_term t)
234 | Env.StringValue s -> sprintf "\"%s\"" s
235 | Env.NumValue n -> n
236 | Env.OptValue (Some v) -> "Some " ^ pp_value v
237 | Env.OptValue None -> "None"
238 | Env.ListValue l -> sprintf "[%s]" (String.concat "; " (List.map pp_value l))
240 let rec pp_value_type =
242 | Env.TermType -> "Term"
243 | Env.StringType -> "String"
244 | Env.NumType -> "Number"
245 | Env.OptType t -> "Maybe " ^ pp_value_type t
246 | Env.ListType l -> "List " ^ pp_value_type l
251 (fun (name, (ty, value)) ->
252 sprintf "%s : %s = %s" name (pp_value_type ty) (pp_value value))