2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
18 let r2s status pp_fix_name r =
21 | R.Ref (u,R.Ind (_,i,_)) ->
22 (match NCicEnvironment.get_checked_obj status u with
23 | _,_,_,_, C.Inductive (_,_,itl,_) ->
24 let _,name,_,_ = List.nth itl i in name
26 | R.Ref (u,R.Con (i,j,_)) ->
27 (match NCicEnvironment.get_checked_obj status u with
28 | _,_,_,_, C.Inductive (_,_,itl,_) ->
29 let _,_,_,cl = List.nth itl i in
30 let _,name,_ = List.nth cl (j-1) in name
32 | R.Ref (u,(R.Decl | R.Def _)) ->
33 (match NCicEnvironment.get_checked_obj status u with
34 | _,_,_,_, C.Constant (_,name,_,_,_) -> name
36 | R.Ref (u,(R.Fix (i,_,_)|R.CoFix i)) ->
37 (match NCicEnvironment.get_checked_obj status u with
38 | _,_,_,_, C.Fixpoint (_,fl,_) ->
40 let _,name,_,_,_ = List.nth fl i in name
42 NUri.name_of_uri u (*^"("^ string_of_int i ^ ")"*)
45 | NCicEnvironment.ObjectNotFound _
47 | Invalid_argument "List.nth" -> R.string_of_reference r
50 let string_of_implicit_annotation = function
54 | `Tagged s -> "[\"" ^ s ^ "\"]"
56 | `Typeof x -> "Ty("^string_of_int x^")"
60 let ppterm status ~formatter:f ~context ~subst ~metasenv:_ ?(inside_fix=false) t =
61 let rec aux ?(toplevel=false) ctx = function
64 let name = List.nth ctx (m-1) in
65 F.fprintf f "%s" (if name = "_" then "__"^string_of_int m else name)
66 with Failure "nth" | Invalid_argument "List.nth" ->
67 F.fprintf f " -%d" (m - List.length ctx))
68 | C.Const r -> F.fprintf f "%s" (r2s status inside_fix r)
70 if not toplevel then F.fprintf f "(";
71 F.fprintf f "@[<hov 1>";
73 | C.Prod ("_",_,_) -> aux ~toplevel:false ctx s
74 | _ -> aux ~toplevel:true ctx s);
76 aux ~toplevel:true ("_"::ctx) t;
78 if not toplevel then F.fprintf f ")";
79 | C.Prod (name,s,t) ->
80 if not toplevel then F.fprintf f "(";
81 F.fprintf f "@[<hov 1>";
82 F.fprintf f "@[<hov 2>∀%s:@;" name;
83 aux ~toplevel:true ctx s;
85 aux ~toplevel:true (name::ctx) t;
87 if not toplevel then F.fprintf f ")";
88 | C.Lambda (name,s,t) ->
89 if not toplevel then F.fprintf f "(";
90 F.fprintf f "@[<hov 1>";
91 F.fprintf f "λ%s:" name;
92 aux ~toplevel:true ctx s;
94 aux ~toplevel:true (name::ctx) t;
96 if not toplevel then F.fprintf f ")";
97 | C.LetIn (name,ty,t,b) ->
98 if not toplevel then F.fprintf f "(";
99 F.fprintf f "@[<hov 1>";
100 F.fprintf f "let %s:@;" name;
101 aux ~toplevel:true ctx ty;
103 aux ~toplevel:true ctx t;
104 F.fprintf f "@;in@;";
105 (aux ~toplevel:true (name::ctx) b);
107 if not toplevel then F.fprintf f ")";
108 | C.Match (r,oty,t,pl) ->
109 F.fprintf f "@[<hov>match ";
110 aux ~toplevel:true ctx t;
111 F.fprintf f "@;return ";
112 aux ~toplevel:true ctx oty;
113 F.fprintf f "@; @[<v>[ ";
116 F.fprintf f "@[<hov 2>%s ⇒@;"
117 (try r2s status inside_fix (R.mk_constructor 1 r)
118 with R.IllFormedReference _ -> "#ERROR#");
119 aux ~toplevel:true ctx (List.hd pl);
121 ignore(List.fold_left
123 F.fprintf f "@;| @[<hov 2>%s ⇒@;"
124 (try r2s status inside_fix (R.mk_constructor i r)
125 with R.IllFormedReference _ -> "#ERROR#");
126 aux ~toplevel:true ctx t;
131 F.fprintf f "]@] @]";
133 F.fprintf f "BAD APPLICATION: empty list"
135 F.fprintf f "BAD APPLICATION: just the head: ";
137 | C.Appl (C.Appl _ as x::_) ->
138 F.fprintf f "BAD APPLICATION: appl of appl with head: ";
140 | C.Appl (C.Meta (n,lc) :: args) when List.mem_assoc n subst ->
141 let _,_,t,_ = List.assoc n subst in
142 let hd = NCicSubstitution.subst_meta status lc t in
144 (NCicReduction.head_beta_reduce (status :> NCic.status) ~upto:(List.length args)
146 | NCic.Appl l -> NCic.Appl (l@args)
147 | _ -> NCic.Appl (hd :: args)))
149 F.fprintf f "@[<hov 2>";
150 if not toplevel then F.fprintf f "(";
152 List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
153 if not toplevel then F.fprintf f ")";
155 | C.Implicit annot ->
156 F.fprintf f "?%s" (string_of_implicit_annotation annot)
157 | C.Meta (n,lc) when List.mem_assoc n subst ->
158 let _,_,t,_ = List.assoc n subst in
159 aux ctx (NCicSubstitution.subst_meta status lc t)
160 | C.Meta (n,(shift,C.Irl len)) ->
161 F.fprintf f "?%d(%d,%d)" n shift len
162 | C.Meta (n,(shift,C.Ctx l)) ->
163 F.fprintf f "?%d(%d,[" n shift;
164 if List.length l > 0 then
166 aux ctx (NCicSubstitution.lift status shift (List.hd l));
167 List.iter (fun x -> F.fprintf f ",";aux ctx x)
168 (List.map (NCicSubstitution.lift status shift) (List.tl l));
171 | C.Sort s -> NCicEnvironment.ppsort f s
173 aux ~toplevel:true (List.map fst context) t
178 let buff = Buffer.create 100 in
179 let formatter = F.formatter_of_buffer buff in
180 f ~formatter:formatter t;
181 F.fprintf formatter "@?";
184 "[[Unprintable: " ^ m ^ "]]"
187 let ppterm ~formatter ~context ~subst ~metasenv ?(margin=80) ?inside_fix t =
189 ppterm ~formatter ~context ~subst ~metasenv ?inside_fix t
192 let rec ppcontext status ~formatter ?(sep="; ") ~subst ~metasenv = function
194 | (name, NCic.Decl t) :: tl ->
195 ppcontext status ~formatter ~sep ~subst ~metasenv tl;
196 F.fprintf formatter "%s: " name;
197 ppterm status ~formatter ~subst ~metasenv ~context:tl t;
198 F.fprintf formatter "%s@;" sep
199 | (name, NCic.Def (bo,ty)) :: tl->
200 ppcontext status ~formatter ~sep ~subst ~metasenv tl;
201 F.fprintf formatter "%s: " name;
202 ppterm status ~formatter ~subst ~metasenv ~context:tl ty;
203 F.fprintf formatter " := ";
204 ppterm status ~formatter ~subst ~metasenv ~context:tl bo;
205 F.fprintf formatter "%s@;" sep
207 let ppcontext status ~formatter ?sep ~subst ~metasenv c =
208 F.fprintf formatter "@[<hov>";
209 ppcontext status ~formatter ?sep ~subst ~metasenv c;
210 F.fprintf formatter "@]";
224 | `Name n -> "name=" ^ n
225 | `InScope -> "in_scope"
226 | `OutScope n -> "out_scope:" ^ string_of_int n
231 let rec ppmetasenv status ~formatter ~subst metasenv = function
233 | (i,(attrs, ctx, ty)) :: tl ->
234 F.fprintf formatter "@[<hov 2>";
235 ppcontext status ~formatter ~sep:"; " ~subst ~metasenv ctx;
236 F.fprintf formatter "@;⊢@;?%d%s :@;" i (ppmetaattrs attrs);
237 ppterm status ~formatter ~metasenv ~subst ~context:ctx ty;
238 F.fprintf formatter "@]@\n";
239 ppmetasenv status ~formatter ~subst metasenv tl
242 let ppmetasenv status ~formatter ~subst metasenv =
243 ppmetasenv status ~formatter ~subst metasenv metasenv
246 let rec ppsubst status ~formatter ~subst ~metasenv = function
248 | (i,(attrs, ctx, t, ty)) :: tl ->
249 ppcontext status ~formatter ~sep:"; " ~subst ~metasenv ctx;
250 F.fprintf formatter " ⊢ ?%d%s := " i (ppmetaattrs attrs);
251 ppterm status ~formatter ~metasenv ~subst ~context:ctx t;
252 F.fprintf formatter " : ";
253 ppterm status ~formatter ~metasenv ~subst ~context:ctx ty;
254 F.fprintf formatter "\n";
255 ppsubst status ~formatter ~subst ~metasenv tl
258 let ppsubst status ~formatter ~metasenv ?(use_subst=true) subst =
259 let ssubst = if use_subst then subst else [] in
260 ppsubst status ~formatter ~metasenv ~subst:ssubst subst
263 let string_of_generated = function
264 | `Generated -> "Generated"
265 | `Provided -> "Provided"
268 let string_of_flavour = function
270 | `Definition -> "definition"
273 | `Theorem -> "theorem"
274 | `Corollary -> "corollary"
275 | `Example -> "example"
278 let string_of_pragma = function
279 | `Coercion _arity -> "Coercion _"
280 | `Elim _sort -> "Elim _"
281 | `Projection -> "Projection"
282 | `InversionPrinciple -> "InversionPrinciple"
283 | `DiscriminationPrinciple -> "DiscriminationPrinciple"
284 | `Variant -> "Variant"
286 | `Regular -> "Regular"
289 let string_of_fattrs (g,f,p) =
291 [ string_of_generated g; string_of_flavour f; string_of_pragma p ]
294 let ppobj status ~formatter (u,_,metasenv, subst, o) =
295 F.fprintf formatter "metasenv:\n";
296 ppmetasenv status ~formatter ~subst metasenv;
297 F.fprintf formatter "\n";
298 F.fprintf formatter "subst:\n";
299 (*ppsubst subst ~formatter ~metasenv;*) F.fprintf formatter "...";
300 F.fprintf formatter "\n";
302 | NCic.Fixpoint (b, fl, attrs) ->
303 F.fprintf formatter "{%s}@\n@[<hov 0>" (NUri.string_of_uri u);
304 F.fprintf formatter "@[<hov 2>%s"(if b then "let rec " else "let corec ");
305 HExtlib.list_iter_sep
306 ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
307 (fun (_,name,n,ty,bo) ->
308 F.fprintf formatter "%s on %d :@;" name n;
309 ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
310 F.fprintf formatter "@]@;@[<hov 2>:=@;";
311 ppterm status ~formatter ~metasenv ~subst ~context:[] ~inside_fix:true bo;
312 F.fprintf formatter "@]") fl;
313 F.fprintf formatter "@; %s" (string_of_fattrs attrs);
314 F.fprintf formatter "@]"
315 | NCic.Inductive (b, leftno,tyl, _) ->
316 F.fprintf formatter "{%s} with %d fixed params@\n@[<hov 0>@[<hov 2>%s"
317 (NUri.string_of_uri u) leftno
318 (if b then "inductive " else "coinductive ");
319 HExtlib.list_iter_sep
320 ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
321 (fun (_,name,ty,cl) ->
322 F.fprintf formatter "%s:@;" name;
323 ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
324 F.fprintf formatter "@]@;@[<hov 3>:=@;";
325 HExtlib.list_iter_sep ~sep:(fun () -> F.fprintf formatter "@;")
327 F.fprintf formatter "| %s: " name;
328 ppterm status ~formatter ~metasenv ~subst ~context:[] ty;)
330 F.fprintf formatter "@]"
332 F.fprintf formatter "@]"
333 | NCic.Constant (_,name,None,ty, _) ->
334 F.fprintf formatter "{%s}@\n@[<hov 2>axiom %s :@;" (NUri.string_of_uri u) name;
335 ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
336 F.fprintf formatter "@]@\n"
337 | NCic.Constant (_,name,Some bo,ty, _) ->
338 F.fprintf formatter "{%s}@\n@[<hov 0>@[<hov 2>definition %s :@;" (NUri.string_of_uri u) name;
339 ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
340 F.fprintf formatter "@]@;@[<hov 2>:=@;";
341 ppterm status ~formatter ~metasenv ~subst ~context:[] bo;
342 F.fprintf formatter "@]@\n@]"
345 let ppterm status ~context ~subst ~metasenv ?margin ?inside_fix t =
346 on_buffer (ppterm status ~context ~subst ~metasenv ?margin ?inside_fix) t
349 let ppcontext status ?sep ~subst ~metasenv ctx =
350 on_buffer (ppcontext status ?sep ~subst ~metasenv) ctx
353 let ppmetasenv status ~subst metasenv =
354 on_buffer (ppmetasenv status ~subst) metasenv
357 let ppsubst status ~metasenv ?use_subst subst =
358 on_buffer (ppsubst status ~metasenv ?use_subst) subst
361 let ppobj status obj = on_buffer (ppobj status) obj;;
365 (* this method is meant to be overridden in ApplyTransformation *)
366 method ppterm = ppterm self
367 method ppcontext = ppcontext self
368 method ppmetasenv = ppmetasenv self
369 method ppsubst = ppsubst self
370 method ppobj = ppobj self