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 head_beta_reduce = ref (fun ~upto:_ _ -> assert false);;
19 let set_head_beta_reduce = (:=) head_beta_reduce;;
21 let get_obj = ref (fun _ -> assert false);;
22 let set_get_obj f = get_obj := f;;
24 let r2s pp_fix_name r =
27 | R.Ref (u,R.Ind (_,i,_)) ->
28 (match !get_obj u with
29 | _,_,_,_, C.Inductive (_,_,itl,_) ->
30 let _,name,_,_ = List.nth itl i in name
32 | R.Ref (u,R.Con (i,j,_)) ->
33 (match !get_obj u with
34 | _,_,_,_, C.Inductive (_,_,itl,_) ->
35 let _,_,_,cl = List.nth itl i in
36 let _,name,_ = List.nth cl (j-1) in name
38 | R.Ref (u,(R.Decl | R.Def _)) ->
39 (match !get_obj u with
40 | _,_,_,_, C.Constant (_,name,_,_,_) -> name
42 | R.Ref (u,(R.Fix (i,_,_)|R.CoFix i)) ->
43 (match !get_obj u with
44 | _,_,_,_, C.Fixpoint (_,fl,_) ->
46 let _,name,_,_,_ = List.nth fl i in name
48 NUri.name_of_uri u (*^"("^ string_of_int i ^ ")"*)
51 | NCicEnvironment.ObjectNotFound _
53 | Invalid_argument "List.nth" -> R.string_of_reference r
56 let string_of_implicit_annotation = function
60 | `Tagged s -> "[\"" ^ s ^ "\"]"
62 | `Typeof x -> "Ty("^string_of_int x^")"
66 let ppterm ~formatter:f ~context ~subst ~metasenv:_ ?(inside_fix=false) t =
67 let rec aux ?(toplevel=false) ctx = function
70 let name = List.nth ctx (m-1) in
71 F.fprintf f "%s" (if name = "_" then "__"^string_of_int m else name)
72 with Failure "nth" | Invalid_argument "List.nth" ->
73 F.fprintf f " -%d" (m - List.length ctx))
74 | C.Const r -> F.fprintf f "%s" (r2s inside_fix r)
76 if not toplevel then F.fprintf f "(";
77 F.fprintf f "@[<hov 1>";
79 | C.Prod ("_",_,_) -> aux ~toplevel:false ctx s
80 | _ -> aux ~toplevel:true ctx s);
82 aux ~toplevel:true ("_"::ctx) t;
84 if not toplevel then F.fprintf f ")";
85 | C.Prod (name,s,t) ->
86 if not toplevel then F.fprintf f "(";
87 F.fprintf f "@[<hov 1>";
88 F.fprintf f "@[<hov 2>∀%s:@;" name;
89 aux ~toplevel:true ctx s;
91 aux ~toplevel:true (name::ctx) t;
93 if not toplevel then F.fprintf f ")";
94 | C.Lambda (name,s,t) ->
95 if not toplevel then F.fprintf f "(";
96 F.fprintf f "@[<hov 1>";
97 F.fprintf f "λ%s:" name;
98 aux ~toplevel:true ctx s;
100 aux ~toplevel:true (name::ctx) t;
102 if not toplevel then F.fprintf f ")";
103 | C.LetIn (name,ty,t,b) ->
104 if not toplevel then F.fprintf f "(";
105 F.fprintf f "@[<hov 1>";
106 F.fprintf f "let %s:@;" name;
107 aux ~toplevel:true ctx ty;
109 aux ~toplevel:true ctx t;
110 F.fprintf f "@;in@;";
111 (aux ~toplevel:true (name::ctx) b);
113 if not toplevel then F.fprintf f ")";
114 | C.Match (r,oty,t,pl) ->
115 F.fprintf f "@[<hov>match ";
116 aux ~toplevel:true ctx t;
117 F.fprintf f "@;return ";
118 aux ~toplevel:true ctx oty;
119 F.fprintf f "@; @[<v>[ ";
122 F.fprintf f "@[<hov 2>%s ⇒@;"
123 (try r2s inside_fix (R.mk_constructor 1 r)
124 with R.IllFormedReference _ -> "#ERROR#");
125 aux ~toplevel:true ctx (List.hd pl);
127 ignore(List.fold_left
129 F.fprintf f "@;| @[<hov 2>%s ⇒@;"
130 (try r2s inside_fix (R.mk_constructor i r)
131 with R.IllFormedReference _ -> "#ERROR#");
132 aux ~toplevel:true ctx t;
137 F.fprintf f "]@] @]";
139 F.fprintf f "BAD APPLICATION: empty list"
141 F.fprintf f "BAD APPLICATION: just the head: ";
143 | C.Appl (C.Appl _ as x::_) ->
144 F.fprintf f "BAD APPLICATION: appl of appl with head: ";
146 | C.Appl (C.Meta (n,lc) :: args) when List.mem_assoc n subst ->
147 let _,_,t,_ = List.assoc n subst in
148 let hd = NCicSubstitution.subst_meta lc t in
150 (!head_beta_reduce ~upto:(List.length args)
152 | NCic.Appl l -> NCic.Appl (l@args)
153 | _ -> NCic.Appl (hd :: args)))
155 F.fprintf f "@[<hov 2>";
156 if not toplevel then F.fprintf f "(";
158 List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
159 if not toplevel then F.fprintf f ")";
161 | C.Implicit annot ->
162 F.fprintf f "?%s" (string_of_implicit_annotation annot)
163 | C.Meta (n,lc) when List.mem_assoc n subst ->
164 let _,_,t,_ = List.assoc n subst in
165 aux ctx (NCicSubstitution.subst_meta lc t)
166 | C.Meta (n,(shift,C.Irl len)) ->
167 F.fprintf f "?%d(%d,%d)" n shift len
168 | C.Meta (n,(shift,C.Ctx l)) ->
169 F.fprintf f "?%d(%d,[" n shift;
170 if List.length l > 0 then
172 aux ctx (NCicSubstitution.lift shift (List.hd l));
173 List.iter (fun x -> F.fprintf f ",";aux ctx x)
174 (List.map (NCicSubstitution.lift shift) (List.tl l));
177 | C.Sort s -> NCicEnvironment.ppsort f s
179 aux ~toplevel:true (List.map fst context) t
183 let buff = Buffer.create 100 in
184 let formatter = F.formatter_of_buffer buff in
185 f ~formatter:formatter t;
186 F.fprintf formatter "@?";
190 let ppterm ~formatter ~context ~subst ~metasenv ?(margin=80) ?inside_fix t =
192 ppterm ~formatter ~context ~subst ~metasenv ?inside_fix t
195 let rec ppcontext ~formatter ?(sep="\n") ~subst ~metasenv = function
197 | (name, NCic.Decl t) :: tl ->
198 ppcontext ~formatter ~sep ~subst ~metasenv tl;
199 F.fprintf formatter "%s: " name;
200 ppterm ~formatter ~subst ~metasenv ~context:tl t;
201 F.fprintf formatter "%s" sep
202 | (name, NCic.Def (bo,ty)) :: tl->
203 ppcontext ~formatter ~sep ~subst ~metasenv tl;
204 F.fprintf formatter "%s: " name;
205 ppterm ~formatter ~subst ~metasenv ~context:tl ty;
206 F.fprintf formatter " := ";
207 ppterm ~formatter ~subst ~metasenv ~context:tl bo;
208 F.fprintf formatter "%s" sep
220 | `Name n -> "name=" ^ n
221 | `InScope -> "in_scope"
222 | `OutScope n -> "out_scope:" ^ string_of_int n
227 let rec ppmetasenv ~formatter ~subst metasenv = function
229 | (i,(attrs, ctx, ty)) :: tl ->
230 F.fprintf formatter "@[<hov 2>";
231 ppcontext ~formatter ~sep:"; " ~subst ~metasenv ctx;
232 F.fprintf formatter "@;⊢@;?%d%s :@;" i (ppmetaattrs attrs);
233 ppterm ~formatter ~metasenv ~subst ~context:ctx ty;
234 F.fprintf formatter "@]@\n";
235 ppmetasenv ~formatter ~subst metasenv tl
238 let ppmetasenv ~formatter ~subst metasenv =
239 ppmetasenv ~formatter ~subst metasenv metasenv
242 let rec ppsubst ~formatter ~subst ~metasenv = function
244 | (i,(attrs, ctx, t, ty)) :: tl ->
245 ppcontext ~formatter ~sep:"; " ~subst ~metasenv ctx;
246 F.fprintf formatter " ⊢ ?%d%s := " i (ppmetaattrs attrs);
247 ppterm ~formatter ~metasenv ~subst ~context:ctx t;
248 F.fprintf formatter " : ";
249 ppterm ~formatter ~metasenv ~subst ~context:ctx ty;
250 F.fprintf formatter "\n";
251 ppsubst ~formatter ~subst ~metasenv tl
254 let ppsubst ~formatter ~metasenv subst =
255 ppsubst ~formatter ~metasenv ~subst subst
258 let string_of_generated = function
259 | `Generated -> "Generated"
260 | `Provided -> "Provided"
263 let string_of_flavour = function
264 | `Definition -> "Definition"
267 | `Theorem -> "Theorem"
268 | `Corollary -> "Corollary"
269 | `Example -> "Example"
272 let string_of_pragma = function
273 | `Coercion _arity -> "Coercion _"
274 | `Elim _sort -> "Elim _"
275 | `Projection -> "Projection"
276 | `InversionPrinciple -> "InversionPrinciple"
277 | `Variant -> "Variant"
279 | `Regular -> "Regular"
282 let string_of_fattrs (g,f,p) =
284 [ string_of_generated g; string_of_flavour f; string_of_pragma p ]
287 let ppobj ~formatter (u,_,metasenv, subst, o) =
288 F.fprintf formatter "metasenv:\n";
289 ppmetasenv ~formatter ~subst metasenv;
290 F.fprintf formatter "\n";
291 F.fprintf formatter "subst:\n";
292 (*ppsubst subst ~formatter ~metasenv;*) F.fprintf formatter "...";
293 F.fprintf formatter "\n";
295 | NCic.Fixpoint (b, fl, attrs) ->
296 F.fprintf formatter "{%s}@\n@[<hov 0>" (NUri.string_of_uri u);
297 F.fprintf formatter "@[<hov 2>%s"(if b then "let rec " else "let corec ");
298 HExtlib.list_iter_sep
299 ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
300 (fun (_,name,n,ty,bo) ->
301 F.fprintf formatter "%s on %d :@;" name n;
302 ppterm ~formatter ~metasenv ~subst ~context:[] ty;
303 F.fprintf formatter "@]@;@[<hov 2>:=@;";
304 ppterm ~formatter ~metasenv ~subst ~context:[] ~inside_fix:true bo;
305 F.fprintf formatter "@]") fl;
306 F.fprintf formatter "@; %s" (string_of_fattrs attrs);
307 F.fprintf formatter "@]"
308 | NCic.Inductive (b, leftno,tyl, _) ->
309 F.fprintf formatter "{%s} with %d fixed params@\n@[<hov 0>@[<hov 2>%s"
310 (NUri.string_of_uri u) leftno
311 (if b then "inductive " else "coinductive ");
312 HExtlib.list_iter_sep
313 ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
314 (fun (_,name,ty,cl) ->
315 F.fprintf formatter "%s:@;" name;
316 ppterm ~formatter ~metasenv ~subst ~context:[] ty;
317 F.fprintf formatter "@]@;@[<hov 3>:=@;";
318 HExtlib.list_iter_sep ~sep:(fun () -> F.fprintf formatter "@;")
320 F.fprintf formatter "| %s: " name;
321 ppterm ~formatter ~metasenv ~subst ~context:[] ty;)
323 F.fprintf formatter "@]"
325 F.fprintf formatter "@]"
326 | NCic.Constant (_,name,None,ty, _) ->
327 F.fprintf formatter "{%s}@\n@[<hov 2>axiom %s :@;" (NUri.string_of_uri u) name;
328 ppterm ~formatter ~metasenv ~subst ~context:[] ty;
329 F.fprintf formatter "@]@\n"
330 | NCic.Constant (_,name,Some bo,ty, _) ->
331 F.fprintf formatter "{%s}@\n@[<hov 0>@[<hov 2>definition %s :@;" (NUri.string_of_uri u) name;
332 ppterm ~formatter ~metasenv ~subst ~context:[] ty;
333 F.fprintf formatter "@]@;@[<hov 2>:=@;";
334 ppterm ~formatter ~metasenv ~subst ~context:[] bo;
335 F.fprintf formatter "@]@\n@]"
338 let ppterm ~context ~subst ~metasenv ?margin ?inside_fix t =
339 on_buffer (ppterm ~context ~subst ~metasenv ?margin ?inside_fix) t
342 let ppcontext ?sep ~subst ~metasenv ctx =
343 on_buffer (ppcontext ?sep ~subst ~metasenv) ctx
346 let ppmetasenv ~subst metasenv = on_buffer (ppmetasenv ~subst) metasenv;;
348 let ppsubst ~metasenv subst = on_buffer (ppsubst ~metasenv) subst;;
350 let ppobj obj = on_buffer ppobj obj;;
352 let _ = NCicSubstitution.set_ppterm (ppterm ~margin:80);;