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/
31 let print_attributes = true
33 let pp_binder = function
46 let rec pp_term = function
47 | AttributedTerm (`Href _, term) when print_attributes ->
48 sprintf "#[%s]" (pp_term term)
49 | AttributedTerm (_, term) when print_attributes ->
50 sprintf "@[%s]" (pp_term term)
51 | AttributedTerm (_, term) -> pp_term term
54 sprintf "(%s)" (String.concat " " (List.map pp_term terms))
55 | Binder (`Forall, (Ident ("_", None), typ), body)
56 | Binder (`Pi, (Ident ("_", None), typ), body) ->
57 sprintf "(%s \\to %s)"
58 (match typ with None -> "?" | Some typ -> pp_term typ)
60 | Binder (kind, var, body) ->
61 sprintf "\\%s %s.%s" (pp_binder kind) (pp_capture_variable var)
63 | Case (term, indtype, typ, patterns) ->
64 sprintf "%smatch %s with %s"
65 (match typ with None -> "" | Some t -> sprintf "<%s>" (pp_term t))
66 (pp_term term) (pp_patterns patterns)
67 | LetIn (var, t1, t2) ->
68 sprintf "let %s = %s in %s" (pp_capture_variable var) (pp_term t1)
70 | LetRec (kind, definitions, term) ->
71 sprintf "let %s %s in %s"
72 (match kind with `Inductive -> "rec" | `CoInductive -> "corec")
73 (String.concat " and "
75 (fun (var, body, _) ->
76 sprintf "%s = %s" (pp_capture_variable var) (pp_term body))
79 | Ident (name, Some []) | Ident (name, None)
80 | Uri (name, Some []) | Uri (name, None) ->
82 | Ident (name, Some substs)
83 | Uri (name, Some substs) ->
84 sprintf "%s \\subst [%s]" name (pp_substs substs)
86 | Meta (index, substs) ->
87 sprintf "%d[%s]" index
89 (List.map (function None -> "_" | Some term -> pp_term term) substs))
92 | Sort `Prop -> "Prop"
93 | Sort `Type -> "Type"
94 | Sort `CProp -> "CProp"
95 | Symbol (name, _) -> name
99 | Literal l -> pp_literal l
100 | Layout l -> pp_layout l
101 | Magic m -> pp_magic m
102 | Variable v -> pp_variable v
104 and pp_subst (name, term) = sprintf "%s \\Assign %s" name (pp_term term)
105 and pp_substs substs = String.concat "; " (List.map pp_subst substs)
107 and pp_pattern ((head, vars), term) =
108 sprintf "%s \\Rightarrow %s"
112 sprintf "(%s %s)" head
113 (String.concat " " (List.map pp_capture_variable vars)))
116 and pp_patterns patterns =
117 sprintf "[%s]" (String.concat " | " (List.map pp_pattern patterns))
119 and pp_capture_variable = function
120 | term, None -> pp_term term
121 | term, Some typ -> "(" ^ pp_term term ^ ": " ^ pp_term typ ^ ")"
123 and pp_box_spec (kind, spacing, indent) =
124 let int_of_bool b = if b then 1 else 0 in
126 match kind with H -> "H" | V -> "V" | HV -> "HV" | HOV -> "HOV"
128 sprintf "%sBOX%d%d" kind_string (int_of_bool spacing) (int_of_bool indent)
130 and pp_layout = function
131 | Sub (t1, t2) -> sprintf "%s \\SUB %s" (pp_term t1) (pp_term t2)
132 | Sup (t1, t2) -> sprintf "%s \\SUP %s" (pp_term t1) (pp_term t2)
133 | Below (t1, t2) -> sprintf "%s \\BELOW %s" (pp_term t1) (pp_term t2)
134 | Above (t1, t2) -> sprintf "%s \\ABOVE %s" (pp_term t1) (pp_term t2)
135 | Over (t1, t2) -> sprintf "[%s \\OVER %s]" (pp_term t1) (pp_term t2)
136 | Atop (t1, t2) -> sprintf "[%s \\ATOP %s]" (pp_term t1) (pp_term t2)
137 | Frac (t1, t2) -> sprintf "\\FRAC %s %s" (pp_term t1) (pp_term t2)
138 | Sqrt t -> sprintf "\\SQRT %s" (pp_term t)
139 | Root (arg, index) ->
140 sprintf "\\ROOT %s \\OF %s" (pp_term index) (pp_term arg)
141 (* | Break -> "\\BREAK" *)
142 (* | Space -> "\\SPACE" *)
143 | Box (box_spec, terms) ->
144 sprintf "\\%s [%s]" (pp_box_spec box_spec)
145 (String.concat " " (List.map pp_term terms))
147 and pp_magic = function
148 | List0 (t, sep_opt) ->
149 sprintf "\\LIST0 %s%s" (pp_term t) (pp_sep_opt sep_opt)
150 | List1 (t, sep_opt) ->
151 sprintf "\\LIST1 %s%s" (pp_term t) (pp_sep_opt sep_opt)
152 | Opt t -> sprintf "\\OPT %s" (pp_term t)
153 | Fold (k, p_base, names, p_rec) ->
154 let acc = match names with acc :: _ -> acc | _ -> assert false in
155 sprintf "\\FOLD %s \\[%s\\] \\LAMBDA %s \\[%s\\]"
156 (pp_fold_kind k) (pp_term p_base) acc (pp_term p_rec)
157 | Default (p_some, p_none) ->
158 sprintf "\\DEFAULT \\[%s\\] \\[%s\\]" (pp_term p_some) (pp_term p_none)
160 and pp_fold_kind = function
164 and pp_sep_opt = function
166 | Some sep -> sprintf "\\SEP %s" (pp_literal sep)
168 and pp_variable = function
169 | NumVar s -> "\\NUM " ^ s
170 | IdentVar s -> "\\IDENT " ^ s
171 | TermVar s -> "\\TERM " ^ s
172 | Ascription (t, n) -> sprintf "[%s \\AS %s]" (pp_term t) n
173 | FreshVar n -> "\\FRESH " ^ n
175 let rec pp_value = function
176 | TermValue t -> sprintf "$%s$" (pp_term t)
177 | StringValue s -> sprintf "\"%s\"" s
179 | OptValue (Some v) -> "Some " ^ pp_value v
180 | OptValue None -> "None"
181 | ListValue l -> sprintf "[%s]" (String.concat "; " (List.map pp_value l))
183 let rec pp_value_type =
186 | StringType -> "String"
187 | NumType -> "Number"
188 | OptType t -> "Maybe " ^ pp_value_type t
189 | ListType l -> "List " ^ pp_value_type l
194 (fun (name, (ty, value)) ->
195 sprintf "%s : %s = %s" name (pp_value_type ty) (pp_value value))