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
61 | `Typeof x -> "Ty("^string_of_int x^")"
65 let ppterm ~formatter:f ~context ~subst ~metasenv:_ ?(inside_fix=false) t =
66 let rec aux ?(toplevel=false) ctx = function
69 let name = List.nth ctx (m-1) in
70 F.fprintf f "%s" (if name = "_" then "__"^string_of_int m else name)
71 with Failure "nth" | Invalid_argument "List.nth" ->
72 F.fprintf f " -%d" (m - List.length ctx))
73 | C.Const r -> F.fprintf f "%s" (r2s inside_fix r)
75 if not toplevel then F.fprintf f "(";
76 F.fprintf f "@[<hov 1>";
78 | C.Prod ("_",_,_) -> aux ~toplevel:false ctx s
79 | _ -> aux ~toplevel:true ctx s);
81 aux ~toplevel:true ("_"::ctx) t;
83 if not toplevel then F.fprintf f ")";
84 | C.Prod (name,s,t) ->
85 if not toplevel then F.fprintf f "(";
86 F.fprintf f "@[<hov 1>";
87 F.fprintf f "@[<hov 2>∀%s:@;" name;
88 aux ~toplevel:true ctx s;
90 aux ~toplevel:true (name::ctx) t;
92 if not toplevel then F.fprintf f ")";
93 | C.Lambda (name,s,t) ->
94 if not toplevel then F.fprintf f "(";
95 F.fprintf f "@[<hov 1>";
96 F.fprintf f "λ%s:" name;
97 aux ~toplevel:true ctx s;
99 aux ~toplevel:true (name::ctx) t;
101 if not toplevel then F.fprintf f ")";
102 | C.LetIn (name,ty,t,b) ->
103 if not toplevel then F.fprintf f "(";
104 F.fprintf f "@[<hov 1>";
105 F.fprintf f "let %s:@;" name;
106 aux ~toplevel:true ctx ty;
108 aux ~toplevel:true ctx t;
109 F.fprintf f "@;in@;";
110 (aux ~toplevel:true (name::ctx) b);
112 if not toplevel then F.fprintf f ")";
113 | C.Match (r,oty,t,pl) ->
114 F.fprintf f "@[<hov>match ";
115 aux ~toplevel:true ctx t;
116 F.fprintf f "@;return ";
117 aux ~toplevel:true ctx oty;
118 F.fprintf f "@; @[<v>[ ";
121 F.fprintf f "@[<hov 2>%s ⇒@;"
122 (try r2s inside_fix (R.mk_constructor 1 r)
123 with R.IllFormedReference _ -> "#ERROR#");
124 aux ~toplevel:true ctx (List.hd pl);
126 ignore(List.fold_left
128 F.fprintf f "@;| @[<hov 2>%s ⇒@;"
129 (try r2s inside_fix (R.mk_constructor i r)
130 with R.IllFormedReference _ -> "#ERROR#");
131 aux ~toplevel:true ctx t;
136 F.fprintf f "]@] @]";
138 F.fprintf f "BAD APPLICATION: empty list"
140 F.fprintf f "BAD APPLICATION: just the head: ";
142 | C.Appl (C.Appl _ as x::_) ->
143 F.fprintf f "BAD APPLICATION: appl of appl with head: ";
145 | C.Appl (C.Meta (n,lc) :: args) when List.mem_assoc n subst ->
146 let _,_,t,_ = List.assoc n subst in
147 let hd = NCicSubstitution.subst_meta lc t in
149 (!head_beta_reduce ~upto:(List.length args)
151 | NCic.Appl l -> NCic.Appl (l@args)
152 | _ -> NCic.Appl (hd :: args)))
154 F.fprintf f "@[<hov 2>";
155 if not toplevel then F.fprintf f "(";
157 List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
158 if not toplevel then F.fprintf f ")";
160 | C.Implicit annot ->
161 F.fprintf f "?%s" (string_of_implicit_annotation annot)
162 | C.Meta (n,lc) when List.mem_assoc n subst ->
163 let _,_,t,_ = List.assoc n subst in
164 aux ctx (NCicSubstitution.subst_meta lc t)
165 | C.Meta (n,(shift,C.Irl len)) ->
166 F.fprintf f "?%d(%d,%d)" n shift len
167 | C.Meta (n,(shift,C.Ctx l)) ->
168 F.fprintf f "?%d(%d,[" n shift;
169 if List.length l > 0 then
171 aux ctx (NCicSubstitution.lift shift (List.hd l));
172 List.iter (fun x -> F.fprintf f ",";aux ctx x)
173 (List.map (NCicSubstitution.lift shift) (List.tl l));
176 | C.Sort C.Prop -> F.fprintf f "Prop"
177 | C.Sort (C.Type []) -> F.fprintf f "Type0"
178 | C.Sort (C.Type [false, u]) -> F.fprintf f "%s" (NUri.name_of_uri u)
179 | C.Sort (C.Type [true, u]) -> F.fprintf f "S(%s)" (NUri.name_of_uri u)
180 | C.Sort (C.Type l) ->
182 aux ctx (C.Sort (C.Type [List.hd l]));
183 List.iter (fun x -> F.fprintf f ",";aux ctx (C.Sort (C.Type [x])))
187 aux ~toplevel:true (List.map fst context) t
191 let buff = Buffer.create 100 in
192 let formatter = F.formatter_of_buffer buff in
193 f ~formatter:formatter t;
194 F.fprintf formatter "@?";
198 let ppterm ~formatter ~context ~subst ~metasenv ?(margin=80) ?inside_fix t =
200 ppterm ~formatter ~context ~subst ~metasenv ?inside_fix t
203 let rec ppcontext ~formatter ?(sep="\n") ~subst ~metasenv = function
205 | (name, NCic.Decl t) :: tl ->
206 ppcontext ~formatter ~sep ~subst ~metasenv tl;
207 F.fprintf formatter "%s: " name;
208 ppterm ~formatter ~subst ~metasenv ~context:tl t;
209 F.fprintf formatter "%s" sep
210 | (name, NCic.Def (bo,ty)) :: tl->
211 ppcontext ~formatter ~sep ~subst ~metasenv tl;
212 F.fprintf formatter "%s: " name;
213 ppterm ~formatter ~subst ~metasenv ~context:tl ty;
214 F.fprintf formatter " := ";
215 ppterm ~formatter ~subst ~metasenv ~context:tl bo;
216 F.fprintf formatter "%s" sep
219 let rec ppmetasenv ~formatter ~subst metasenv = function
221 | (i,(name, ctx, ty)) :: tl ->
222 F.fprintf formatter "@[<hov 2>";
223 let name = match name with Some n -> "("^n^")" | _ -> "" in
224 ppcontext ~formatter ~sep:"; " ~subst ~metasenv ctx;
225 F.fprintf formatter "@;⊢@;?%d%s :@;" i name;
226 ppterm ~formatter ~metasenv ~subst ~context:ctx ty;
227 F.fprintf formatter "@]@\n";
228 ppmetasenv ~formatter ~subst metasenv tl
231 let ppmetasenv ~formatter ~subst metasenv =
232 ppmetasenv ~formatter ~subst metasenv metasenv
235 let rec ppsubst ~formatter ~subst ~metasenv = function
237 | (i,(name, ctx, t, ty)) :: tl ->
238 let name = match name with Some n -> "("^n^")" | _ -> "" in
239 ppcontext ~formatter ~sep:"; " ~subst ~metasenv ctx;
240 F.fprintf formatter " ⊢ ?%d%s := " i name;
241 ppterm ~formatter ~metasenv ~subst ~context:ctx t;
242 F.fprintf formatter " : ";
243 ppterm ~formatter ~metasenv ~subst ~context:ctx ty;
244 F.fprintf formatter "\n";
245 ppsubst ~formatter ~subst ~metasenv tl
248 let ppsubst ~formatter ~metasenv subst =
249 ppsubst ~formatter ~metasenv ~subst subst
252 let ppobj ~formatter (u,_,metasenv, subst, o) =
253 F.fprintf formatter "metasenv:\n";
254 ppmetasenv ~formatter ~subst metasenv;
255 F.fprintf formatter "\n";
256 F.fprintf formatter "subst:\n";
257 (*ppsubst subst ~formatter ~metasenv;*) F.fprintf formatter "...";
258 F.fprintf formatter "\n";
260 | NCic.Fixpoint (b, fl, _) ->
261 F.fprintf formatter "{%s}@\n@[<hov 0>" (NUri.string_of_uri u);
262 F.fprintf formatter "@[<hov 2>%s"(if b then "let rec " else "let corec ");
263 HExtlib.list_iter_sep
264 ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
265 (fun (_,name,n,ty,bo) ->
266 F.fprintf formatter "%s on %d :@;" name n;
267 ppterm ~formatter ~metasenv ~subst ~context:[] ty;
268 F.fprintf formatter "@]@;@[<hov 2>:=@;";
269 ppterm ~formatter ~metasenv ~subst ~context:[] ~inside_fix:true bo;
270 F.fprintf formatter "@]") fl;
271 F.fprintf formatter "@]"
272 | NCic.Inductive (b, leftno,tyl, _) ->
273 F.fprintf formatter "{%s} with %d fixed params@\n@[<hov 0>@[<hov 2>%s"
274 (NUri.string_of_uri u) leftno
275 (if b then "inductive " else "coinductive ");
276 HExtlib.list_iter_sep
277 ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
278 (fun (_,name,ty,cl) ->
279 F.fprintf formatter "%s:@;" name;
280 ppterm ~formatter ~metasenv ~subst ~context:[] ty;
281 F.fprintf formatter "@]@;@[<hov 3>:=@;";
282 HExtlib.list_iter_sep ~sep:(fun () -> F.fprintf formatter "@;")
284 F.fprintf formatter "| %s: " name;
285 ppterm ~formatter ~metasenv ~subst ~context:[] ty;)
287 F.fprintf formatter "@]"
289 F.fprintf formatter "@]"
290 | NCic.Constant (_,name,None,ty, _) ->
291 F.fprintf formatter "{%s}@\n@[<hov 2>axiom %s :@;" (NUri.string_of_uri u) name;
292 ppterm ~formatter ~metasenv ~subst ~context:[] ty;
293 F.fprintf formatter "@]@\n"
294 | NCic.Constant (_,name,Some bo,ty, _) ->
295 F.fprintf formatter "{%s}@\n@[<hov 0>@[<hov 2>definition %s :@;" (NUri.string_of_uri u) name;
296 ppterm ~formatter ~metasenv ~subst ~context:[] ty;
297 F.fprintf formatter "@]@;@[<hov 2>:=@;";
298 ppterm ~formatter ~metasenv ~subst ~context:[] bo;
299 F.fprintf formatter "@]@\n@]"
302 let ppterm ~context ~subst ~metasenv ?margin ?inside_fix t =
303 on_buffer (ppterm ~context ~subst ~metasenv ?margin ?inside_fix) t
306 let ppcontext ?sep ~subst ~metasenv ctx =
307 on_buffer (ppcontext ?sep ~subst ~metasenv) ctx
310 let ppmetasenv ~subst metasenv = on_buffer (ppmetasenv ~subst) metasenv;;
312 let ppsubst ~metasenv subst = on_buffer (ppsubst ~metasenv) subst;;
314 let ppobj obj = on_buffer ppobj obj;;
316 let _ = NCicSubstitution.set_ppterm (ppterm ~margin:80);;