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"
266 | `Implied -> "Implied"
269 let string_of_flavour = function
271 | `Definition -> "definition"
274 | `Theorem -> "theorem"
275 | `Corollary -> "corollary"
276 | `Example -> "example"
279 let string_of_pragma = function
280 | `Coercion _arity -> "Coercion _"
281 | `Elim _sort -> "Elim _"
282 | `Projection -> "Projection"
283 | `InversionPrinciple -> "InversionPrinciple"
284 | `DiscriminationPrinciple -> "DiscriminationPrinciple"
285 | `Variant -> "Variant"
287 | `Regular -> "Regular"
290 let string_of_fattrs (g,f,p) =
292 [ string_of_generated g; string_of_flavour f; string_of_pragma p ]
295 let ppobj status ~formatter (u,_,metasenv, subst, o) =
296 F.fprintf formatter "metasenv:\n";
297 ppmetasenv status ~formatter ~subst metasenv;
298 F.fprintf formatter "\n";
299 F.fprintf formatter "subst:\n";
300 (*ppsubst subst ~formatter ~metasenv;*) F.fprintf formatter "...";
301 F.fprintf formatter "\n";
303 | NCic.Fixpoint (b, fl, attrs) ->
304 F.fprintf formatter "{%s}@\n@[<hov 0>" (NUri.string_of_uri u);
305 F.fprintf formatter "@[<hov 2>%s"(if b then "let rec " else "let corec ");
306 HExtlib.list_iter_sep
307 ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
308 (fun (_,name,n,ty,bo) ->
309 F.fprintf formatter "%s on %d :@;" name n;
310 ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
311 F.fprintf formatter "@]@;@[<hov 2>:=@;";
312 ppterm status ~formatter ~metasenv ~subst ~context:[] ~inside_fix:true bo;
313 F.fprintf formatter "@]") fl;
314 F.fprintf formatter "@; %s" (string_of_fattrs attrs);
315 F.fprintf formatter "@]"
316 | NCic.Inductive (b, leftno,tyl, _) ->
317 F.fprintf formatter "{%s} with %d fixed params@\n@[<hov 0>@[<hov 2>%s"
318 (NUri.string_of_uri u) leftno
319 (if b then "inductive " else "coinductive ");
320 HExtlib.list_iter_sep
321 ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
322 (fun (_,name,ty,cl) ->
323 F.fprintf formatter "%s:@;" name;
324 ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
325 F.fprintf formatter "@]@;@[<hov 3>:=@;";
326 HExtlib.list_iter_sep ~sep:(fun () -> F.fprintf formatter "@;")
328 F.fprintf formatter "| %s: " name;
329 ppterm status ~formatter ~metasenv ~subst ~context:[] ty;)
331 F.fprintf formatter "@]"
333 F.fprintf formatter "@]"
334 | NCic.Constant (_,name,None,ty, _) ->
335 F.fprintf formatter "{%s}@\n@[<hov 2>axiom %s :@;" (NUri.string_of_uri u) name;
336 ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
337 F.fprintf formatter "@]@\n"
338 | NCic.Constant (_,name,Some bo,ty, _) ->
339 F.fprintf formatter "{%s}@\n@[<hov 0>@[<hov 2>definition %s :@;" (NUri.string_of_uri u) name;
340 ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
341 F.fprintf formatter "@]@;@[<hov 2>:=@;";
342 ppterm status ~formatter ~metasenv ~subst ~context:[] bo;
343 F.fprintf formatter "@]@\n@]"
346 let ppterm status ~context ~subst ~metasenv ?margin ?inside_fix t =
347 on_buffer (ppterm status ~context ~subst ~metasenv ?margin ?inside_fix) t
350 let ppcontext status ?sep ~subst ~metasenv ctx =
351 on_buffer (ppcontext status ?sep ~subst ~metasenv) ctx
354 let ppmetasenv status ~subst metasenv =
355 on_buffer (ppmetasenv status ~subst) metasenv
358 let ppsubst status ~metasenv ?use_subst subst =
359 on_buffer (ppsubst status ~metasenv ?use_subst) subst
362 let ppobj status obj = on_buffer (ppobj status) obj;;
366 (* this method is meant to be overridden in ApplyTransformation *)
367 method ppterm = ppterm self
368 method ppcontext = ppcontext self
369 method ppmetasenv = ppmetasenv self
370 method ppsubst = ppsubst self
371 method ppobj = ppobj self