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 ^ ")"
47 | NCicLibrary.ObjectNotFound _
49 | Invalid_argument "List.nth" -> R.string_of_reference r
53 let string_of_implicit_annotation = function
58 | `Typeof x -> "Ty("^string_of_int x^")"
61 let ppterm ~context ~subst ~metasenv:_ ?(inside_fix=false) t =
62 let buff = Buffer.create 100 in
63 let f = Format.formatter_of_buffer buff in
64 let module F = Format in
65 let rec aux ?(toplevel=false) ctx = function
68 let name = List.nth ctx (m-1) in
69 F.fprintf f "%s" (if name = "_" then "__"^string_of_int m else name)
70 with Failure "nth" | Invalid_argument "List.nth" ->
71 F.fprintf f " -%d" (m - List.length ctx))
72 | C.Const r -> F.fprintf f "%s" (r2s inside_fix r)
74 if not toplevel then F.fprintf f "(";
75 F.fprintf f "@[<hov 1>";
77 | C.Prod ("_",_,_) -> aux ~toplevel:false ctx s
78 | _ -> aux ~toplevel:true ctx s);
80 aux ~toplevel:true ("_"::ctx) t;
82 if not toplevel then F.fprintf f ")";
83 | C.Prod (name,s,t) ->
84 if not toplevel then F.fprintf f "(";
85 F.fprintf f "@[<hov 1>";
86 F.fprintf f "@[<hov 2>∀%s:@;" name;
87 aux ~toplevel:true ctx s;
89 aux ~toplevel:true (name::ctx) t;
91 if not toplevel then F.fprintf f ")";
92 | C.Lambda (name,s,t) ->
93 if not toplevel then F.fprintf f "(";
94 F.fprintf f "@[<hov 1>";
95 F.fprintf f "λ%s:" name;
96 aux ~toplevel:true ctx s;
98 aux ~toplevel:true (name::ctx) t;
100 if not toplevel then F.fprintf f ")";
101 | C.LetIn (name,ty,t,b) ->
102 if not toplevel then F.fprintf f "(";
103 F.fprintf f "@[<hov 1>";
104 F.fprintf f "let %s:@;" name;
105 aux ~toplevel:true ctx ty;
107 aux ~toplevel:true ctx t;
108 F.fprintf f "@;in@;";
109 (aux ~toplevel:true (name::ctx) b);
111 if not toplevel then F.fprintf f ")";
112 | C.Match (r,oty,t,pl) ->
113 F.fprintf f "@[<hov>match ";
114 aux ~toplevel:true ctx t;
115 F.fprintf f "@;return ";
116 aux ~toplevel:true ctx oty;
117 F.fprintf f "@; @[<v>[ ";
120 F.fprintf f "@[<hov 2>%s ⇒@;"
121 (try r2s inside_fix (R.mk_constructor 1 r)
122 with R.IllFormedReference _ -> "#ERROR#");
123 aux ~toplevel:true ctx (List.hd pl);
125 ignore(List.fold_left
127 F.fprintf f "@;| @[<hov 2>%s ⇒@;"
128 (try r2s inside_fix (R.mk_constructor i r)
129 with R.IllFormedReference _ -> "#ERROR#");
130 aux ~toplevel:true ctx t;
135 F.fprintf f "]@] @]";
136 | C.Appl [] | C.Appl [_] | C.Appl (C.Appl _::_) ->
137 F.fprintf f "BAD APPLICATION"
138 | C.Appl (C.Meta (n,lc) :: args) when List.mem_assoc n subst ->
139 let _,_,t,_ = List.assoc n subst in
140 let hd = NCicSubstitution.subst_meta lc t in
142 (!head_beta_reduce ~upto:(List.length args)
144 | NCic.Appl l -> NCic.Appl (l@args)
145 | _ -> NCic.Appl (hd :: args)))
147 F.fprintf f "@[<hov 2>";
148 if not toplevel then F.fprintf f "(";
150 List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
151 if not toplevel then F.fprintf f ")";
153 | C.Implicit annot ->
154 F.fprintf f "?%s" (string_of_implicit_annotation annot)
155 | C.Meta (n,lc) when List.mem_assoc n subst ->
156 let _,_,t,_ = List.assoc n subst in
157 aux ctx (NCicSubstitution.subst_meta lc t)
158 | C.Meta (n,(shift,C.Irl len)) ->
159 F.fprintf f "?%d(%d,%d)" n shift len
160 | C.Meta (n,(shift,C.Ctx l)) ->
161 F.fprintf f "?%d(%d,[" n shift;
162 if List.length l > 0 then
164 aux ctx (NCicSubstitution.lift shift (List.hd l));
165 List.iter (fun x -> F.fprintf f ",";aux ctx x)
166 (List.map (NCicSubstitution.lift shift) (List.tl l));
169 | C.Sort C.Prop -> F.fprintf f "Prop"
170 | C.Sort (C.Type []) -> F.fprintf f "Type0"
171 | C.Sort (C.Type [false, u]) -> F.fprintf f "%s" (NUri.name_of_uri u)
172 | C.Sort (C.Type [true, u]) -> F.fprintf f "S(%s)" (NUri.name_of_uri u)
173 | C.Sort (C.Type l) ->
175 aux ctx (C.Sort (C.Type [List.hd l]));
176 List.iter (fun x -> F.fprintf f ",";aux ctx (C.Sort (C.Type [x])))
180 aux ~toplevel:true (List.map fst context) t;
185 let ppterm ~context ~subst ~metasenv ?(margin=80) ?inside_fix t =
186 Format.set_margin margin;
187 ppterm ~context ~subst ~metasenv ?inside_fix t
190 let rec ppcontext ?(sep="\n") ~subst ~metasenv = function
192 | (name, NCic.Decl t) :: tl ->
193 ppcontext ~sep ~subst ~metasenv tl ^
194 name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl t ^ sep
195 | (name, NCic.Def (bo,ty)) :: tl->
196 ppcontext ~sep ~subst ~metasenv tl ^
197 name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl ty ^
198 " := " ^ ppterm ~subst ~metasenv ~context:tl bo ^ sep
201 let rec ppmetasenv ~subst metasenv = function
203 | (i,(name, ctx, ty)) :: tl ->
204 let name = match name with Some n -> "("^n^")" | _ -> "" in
205 ppcontext ~sep:"; " ~subst ~metasenv ctx ^
206 " ⊢ ?"^string_of_int i^name^" : " ^
207 ppterm ~metasenv ~subst ~context:ctx ty ^ "\n" ^
208 ppmetasenv ~subst metasenv tl
211 let ppmetasenv ~subst metasenv = ppmetasenv ~subst metasenv metasenv;;
213 let rec ppsubst ~subst ~metasenv = function
215 | (i,(name, ctx, t, ty)) :: tl ->
217 let name = match name with Some n -> "("^n^")" | _ -> "" in
218 ppcontext ~sep:"; " ~subst ~metasenv ctx ^
219 " ⊢ ?"^string_of_int i^name^" := " ^
220 ppterm ~metasenv ~subst ~context:ctx t ^ " : " ^
221 ppterm ~metasenv ~subst ~context:ctx ty ^ "\n" ^
222 ppsubst ~subst ~metasenv tl
225 let ppsubst ~metasenv subst = ppsubst ~metasenv ~subst subst;;
228 let ppobj (u,_,metasenv, subst, o) =
229 "metasenv:\n" ^ ppmetasenv ~subst metasenv ^ "\n" ^
230 "subst:\n" ^ ppsubst subst ~metasenv ^ "\n" ^
232 | NCic.Fixpoint (b, fl, _) ->
233 "{"^NUri.string_of_uri u^"}\n"^
234 (if b then "let rec " else "let corec ") ^
235 String.concat "\nand "
236 (List.map (fun (_,name,n,ty,bo) ->
237 name^ " on " ^ string_of_int n ^ " : " ^
238 ppterm ~metasenv ~subst ~context:[] ty ^ " :=\n"^
239 ppterm ~metasenv ~subst ~context:[] ~inside_fix:true bo) fl)
240 | NCic.Inductive (b, leftno,tyl, _) ->
241 "{"^NUri.string_of_uri u^"} with "^string_of_int leftno^" fixed params\n"^
242 (if b then "inductive " else "coinductive ")^
243 String.concat "\nand "
244 (List.map (fun (_,name,ty,cl) ->
245 name^": "^ppterm ~metasenv ~subst ~context:[] ty^ " :=\n"^
247 (List.map (fun (_,name,ty) ->
248 " | "^name^": "^ppterm ~metasenv ~subst ~context:[] ty)
250 | NCic.Constant (_,name,None,ty, _) ->
251 "{"^NUri.string_of_uri u^"}\n"^
252 "axiom " ^ name ^ " : " ^
253 ppterm ~metasenv ~subst ~context:[] ty ^ "\n"
254 | NCic.Constant (_,name,Some bo,ty, _) ->
255 "{"^NUri.string_of_uri u^"}\n"^
256 "definition " ^ name ^ " : " ^
257 ppterm ~metasenv ~subst ~context:[] ty ^ " := \n"^
258 ppterm ~metasenv ~subst ~context:[] bo ^ "\n"
261 let _ = NCicSubstitution.set_ppterm (ppterm ~margin:80);;