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_______________________________________________________________ *)
17 let head_beta_reduce = ref (fun ~upto:_ _ -> assert false);;
18 let set_head_beta_reduce = (:=) head_beta_reduce;;
20 let r2s pp_fix_name r =
23 | R.Ref (u,R.Ind (_,i,_)) ->
24 (match NCicLibrary.get_obj u with
25 | _,_,_,_, C.Inductive (_,_,itl,_) ->
26 let _,name,_,_ = List.nth itl i in name
28 | R.Ref (u,R.Con (i,j,_)) ->
29 (match NCicLibrary.get_obj u with
30 | _,_,_,_, C.Inductive (_,_,itl,_) ->
31 let _,_,_,cl = List.nth itl i in
32 let _,name,_ = List.nth cl (j-1) in name
34 | R.Ref (u,(R.Decl | R.Def _)) ->
35 (match NCicLibrary.get_obj u with
36 | _,_,_,_, C.Constant (_,name,_,_,_) -> name
38 | R.Ref (u,(R.Fix (i,_,_)|R.CoFix i)) ->
39 (match NCicLibrary.get_obj u with
40 | _,_,_,_, C.Fixpoint (_,fl,_) ->
42 let _,name,_,_,_ = List.nth fl i in name
44 NUri.name_of_uri u ^"("^ string_of_int i ^ ")"
46 with NCicLibrary.ObjectNotFound _ -> R.string_of_reference r
49 let string_of_implicit_annotation = function
54 | `Typeof x -> "Ty("^string_of_int x^")"
57 let ppterm ~context ~subst ~metasenv:_ ?(inside_fix=false) t =
58 let buff = Buffer.create 100 in
59 let f = Format.formatter_of_buffer buff in
60 let module F = Format in
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 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 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 inside_fix (R.mk_constructor i r)
125 with R.IllFormedReference _ -> "#ERROR#");
126 aux ~toplevel:true ctx t;
131 F.fprintf f "]@] @]";
132 | C.Appl [] | C.Appl [_] | C.Appl (C.Appl _::_) ->
133 F.fprintf f "BAD APPLICATION"
134 | C.Appl (C.Meta (n,lc) :: args) when List.mem_assoc n subst ->
135 let _,_,t,_ = List.assoc n subst in
136 let hd = NCicSubstitution.subst_meta lc t in
138 (!head_beta_reduce ~upto:(List.length args)
140 | NCic.Appl l -> NCic.Appl (l@args)
141 | _ -> NCic.Appl (hd :: args)))
143 F.fprintf f "@[<hov 2>";
144 if not toplevel then F.fprintf f "(";
146 List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
147 if not toplevel then F.fprintf f ")";
149 | C.Implicit annot ->
150 F.fprintf f "?%s" (string_of_implicit_annotation annot)
151 | C.Meta (n,lc) when List.mem_assoc n subst ->
152 let _,_,t,_ = List.assoc n subst in
153 aux ctx (NCicSubstitution.subst_meta lc t)
154 | C.Meta (n,(shift,C.Irl len)) ->
155 F.fprintf f "?%d(%d,%d)" n shift len
156 | C.Meta (n,(shift,C.Ctx l)) ->
157 F.fprintf f "?%d(%d,[" n shift;
158 if List.length l > 0 then
160 aux ctx (NCicSubstitution.lift shift (List.hd l));
161 List.iter (fun x -> F.fprintf f ",";aux ctx x)
162 (List.map (NCicSubstitution.lift shift) (List.tl l));
165 | C.Sort C.Prop -> F.fprintf f "Prop"
166 | C.Sort (C.Type []) -> F.fprintf f "Type0"
167 | C.Sort (C.Type [false, u]) -> F.fprintf f "%s" (NUri.name_of_uri u)
168 | C.Sort (C.Type [true, u]) -> F.fprintf f "S(%s)" (NUri.name_of_uri u)
169 | C.Sort (C.Type l) ->
171 aux ctx (C.Sort (C.Type [List.hd l]));
172 List.iter (fun x -> F.fprintf f ",";aux ctx (C.Sort (C.Type [x])))
176 aux ~toplevel:true (List.map fst context) t;
181 let ppterm ~context ~subst ~metasenv ?(margin=80) ?inside_fix t =
182 Format.set_margin margin;
183 ppterm ~context ~subst ~metasenv ?inside_fix t
186 let rec ppcontext ?(sep="\n") ~subst ~metasenv = function
188 | (name, NCic.Decl t) :: tl ->
189 ppcontext ~sep ~subst ~metasenv tl ^
190 name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl t ^ sep
191 | (name, NCic.Def (bo,ty)) :: tl->
192 ppcontext ~sep ~subst ~metasenv tl ^
193 name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl ty ^
194 " := " ^ ppterm ~subst ~metasenv ~context:tl bo ^ sep
197 let rec ppmetasenv ~subst metasenv = function
199 | (i,(name, ctx, ty)) :: tl ->
200 let name = match name with Some n -> "("^n^")" | _ -> "" in
201 ppcontext ~sep:"; " ~subst ~metasenv ctx ^
202 " ⊢ ?"^string_of_int i^name^" : " ^
203 ppterm ~metasenv ~subst ~context:ctx ty ^ "\n" ^
204 ppmetasenv ~subst metasenv tl
207 let ppmetasenv ~subst metasenv = ppmetasenv ~subst metasenv metasenv;;
209 let rec ppsubst ~subst ~metasenv = function
211 | (i,(name, ctx, t, ty)) :: tl ->
213 let name = match name with Some n -> "("^n^")" | _ -> "" in
214 ppcontext ~sep:"; " ~subst ~metasenv ctx ^
215 " ⊢ ?"^string_of_int i^name^" := " ^
216 ppterm ~metasenv ~subst ~context:ctx t ^ " : " ^
217 ppterm ~metasenv ~subst ~context:ctx ty ^ "\n" ^
218 ppsubst ~subst ~metasenv tl
221 let ppsubst ~metasenv subst = ppsubst ~metasenv ~subst subst;;
224 let ppobj (u,_,metasenv, subst, o) =
225 "metasenv:\n" ^ ppmetasenv ~subst metasenv ^ "\n" ^
226 "subst:\n" ^ ppsubst subst ~metasenv ^ "\n" ^
228 | NCic.Fixpoint (b, fl, _) ->
229 "{"^NUri.string_of_uri u^"}\n"^
230 (if b then "let rec " else "let corec ") ^
231 String.concat "\nand "
232 (List.map (fun (_,name,n,ty,bo) ->
233 name^ " on " ^ string_of_int n ^ " : " ^
234 ppterm ~metasenv ~subst ~context:[] ty ^ " :=\n"^
235 ppterm ~metasenv ~subst ~context:[] ~inside_fix:true bo) fl)
236 | NCic.Inductive (b, leftno,tyl, _) ->
237 "{"^NUri.string_of_uri u^"} with "^string_of_int leftno^" fixed params\n"^
238 (if b then "inductive " else "coinductive ")^
239 String.concat "\nand "
240 (List.map (fun (_,name,ty,cl) ->
241 name^": "^ppterm ~metasenv ~subst ~context:[] ty^ " :=\n"^
243 (List.map (fun (_,name,ty) ->
244 " | "^name^": "^ppterm ~metasenv ~subst ~context:[] ty)
246 | NCic.Constant (_,name,None,ty, _) ->
247 "{"^NUri.string_of_uri u^"}\n"^
248 "axiom " ^ name ^ " : " ^
249 ppterm ~metasenv ~subst ~context:[] ty ^ "\n"
250 | NCic.Constant (_,name,Some bo,ty, _) ->
251 "{"^NUri.string_of_uri u^"}\n"^
252 "definition " ^ name ^ " : " ^
253 ppterm ~metasenv ~subst ~context:[] ty ^ " := \n"^
254 ppterm ~metasenv ~subst ~context:[] bo ^ "\n"
257 let _ = NCicSubstitution.set_ppterm (ppterm ~margin:80);;