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 r2s pp_fix_name r =
20 | R.Ref (u,R.Ind (_,i,_)) ->
21 (match NCicLibrary.get_obj u with
22 | _,_,_,_, C.Inductive (_,_,itl,_) ->
23 let _,name,_,_ = List.nth itl i in name
25 | R.Ref (u,R.Con (i,j,_)) ->
26 (match NCicLibrary.get_obj u with
27 | _,_,_,_, C.Inductive (_,_,itl,_) ->
28 let _,_,_,cl = List.nth itl i in
29 let _,name,_ = List.nth cl (j-1) in name
31 | R.Ref (u,(R.Decl | R.Def _)) ->
32 (match NCicLibrary.get_obj u with
33 | _,_,_,_, C.Constant (_,name,_,_,_) -> name
35 | R.Ref (u,(R.Fix (i,_,_)|R.CoFix i)) ->
36 (match NCicLibrary.get_obj u with
37 | _,_,_,_, C.Fixpoint (_,fl,_) ->
39 let _,name,_,_,_ = List.nth fl i in name
41 NUri.name_of_uri u ^"("^ string_of_int i ^ ")"
43 with NCicLibrary.ObjectNotFound _ -> R.string_of_reference r
46 let ppterm ~context ~subst ~metasenv:_ ?(inside_fix=false) t =
47 let buff = Buffer.create 100 in
48 let f = Format.formatter_of_buffer buff in
49 let module F = Format in
50 let rec aux ?(toplevel=false) ctx = function
53 let name = List.nth ctx (m-1) in
54 F.fprintf f "%s" (if name = "_" then "__"^string_of_int m else name)
55 with Failure _ -> F.fprintf f " -%d" (m - List.length ctx))
56 | C.Const r -> F.fprintf f "%s" (r2s inside_fix r)
58 if not toplevel then F.fprintf f "(";
59 F.fprintf f "@[<hov 1>";
61 | C.Prod ("_",_,_) -> aux ~toplevel:false ctx s
62 | _ -> aux ~toplevel:true ctx s);
64 aux ~toplevel:true ("_"::ctx) t;
66 if not toplevel then F.fprintf f ")";
67 | C.Prod (name,s,t) ->
68 if not toplevel then F.fprintf f "(";
69 F.fprintf f "@[<hov 1>";
70 F.fprintf f "@[<hov 2>∀%s:@;" name;
71 aux ~toplevel:true ctx s;
73 aux ~toplevel:true (name::ctx) t;
75 if not toplevel then F.fprintf f ")";
76 | C.Lambda (name,s,t) ->
77 if not toplevel then F.fprintf f "(";
78 F.fprintf f "@[<hov 1>";
79 F.fprintf f "λ%s:" name;
80 aux ~toplevel:true ctx s;
82 aux ~toplevel:true (name::ctx) t;
84 if not toplevel then F.fprintf f ")";
85 | C.LetIn (name,ty,t,b) ->
86 if not toplevel then F.fprintf f "(";
87 F.fprintf f "@[<hov 1>";
88 F.fprintf f "let %s:@;" name;
89 aux ~toplevel:true ctx ty;
91 aux ~toplevel:true ctx t;
93 (aux ~toplevel:true (name::ctx) b);
95 if not toplevel then F.fprintf f ")";
96 | C.Match (r,oty,t,pl) ->
97 F.fprintf f "@[<hov>match ";
98 aux ~toplevel:true ctx t;
99 F.fprintf f "@;return ";
100 aux ~toplevel:true ctx oty;
101 F.fprintf f "@; @[<v>[ ";
104 F.fprintf f "@[<hov 2>%s ⇒@;" (r2s inside_fix (R.mk_constructor 1 r));
105 aux ~toplevel:true ctx (List.hd pl);
107 ignore(List.fold_left
109 F.fprintf f "@;| @[<hov 2>%s ⇒@;" (r2s inside_fix (R.mk_constructor i r));
110 aux ~toplevel:true ctx t;
115 F.fprintf f "]@] @]";
117 F.fprintf f "@[<hov 2>";
118 if not toplevel then F.fprintf f "(";
120 List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
121 if not toplevel then F.fprintf f ")";
123 | C.Implicit _ -> F.fprintf f "?"
124 | C.Meta (n,lc) when List.mem_assoc n subst ->
125 let _,_,t,_ = List.assoc n subst in
126 aux ctx (NCicSubstitution.subst_meta lc t)
127 | C.Meta (n,(shift,C.Irl len)) ->
128 F.fprintf f "?%d(%d,%d)" n shift len
129 | C.Meta (n,(shift,C.Ctx l)) ->
130 F.fprintf f "?%d(%d,[" n shift;
131 if List.length l > 0 then
134 List.iter (fun x -> F.fprintf f ",";aux ctx x) (List.tl l);
137 | C.Sort C.Prop -> F.fprintf f "Prop"
138 | C.Sort (C.Type []) -> F.fprintf f "IllFormedUniverse"
139 | C.Sort (C.Type [false, u]) -> F.fprintf f "%s" (NUri.name_of_uri u)
140 | C.Sort (C.Type [true, u]) -> F.fprintf f "S(%s)" (NUri.name_of_uri u)
141 | C.Sort (C.Type l) ->
143 aux ctx (C.Sort (C.Type [List.hd l]));
144 List.iter (fun x -> F.fprintf f ",";aux ctx (C.Sort (C.Type [x])))
148 aux ~toplevel:true (List.map fst context) t;
154 | (u,_,metasenv,subst,NCic.Fixpoint (b, fl, _)) ->
155 "{"^NUri.string_of_uri u^"}\n"^
156 (if b then "let rec " else "let corec ") ^
157 String.concat "\nand "
158 (List.map (fun (_,name,n,ty,bo) ->
159 name^ " on " ^ string_of_int n ^ " : " ^
160 ppterm ~metasenv ~subst ~context:[] ty ^ " :=\n"^
161 ppterm ~metasenv ~subst ~context:[] ~inside_fix:true bo) fl)
162 | (u,_,metasenv,subst,NCic.Inductive (b, leftno,tyl, _)) ->
163 "{"^NUri.string_of_uri u^"} with "^string_of_int leftno^" fixed params\n"^
164 (if b then "inductive " else "coinductive ")^
165 String.concat "\nand "
166 (List.map (fun (_,name,ty,cl) ->
167 name^": "^ppterm ~metasenv ~subst ~context:[] ty^ " :=\n"^
169 (List.map (fun (_,name,ty) ->
170 " | "^name^": "^ppterm ~metasenv ~subst ~context:[] ty)
172 | (u,_,metasenv,subst,NCic.Constant (_,name,None,ty, _)) ->
173 "{"^NUri.string_of_uri u^"}\n"^
174 "axiom " ^ name ^ " : " ^
175 ppterm ~metasenv ~subst ~context:[] ty ^ "\n"
176 | (u,_,metasenv,subst,NCic.Constant (_,name,Some bo,ty, _)) ->
177 "{"^NUri.string_of_uri u^"}\n"^
178 "definition " ^ name ^ " : " ^
179 ppterm ~metasenv ~subst ~context:[] ty ^ " := \n"^
180 ppterm ~metasenv ~subst ~context:[] bo ^ "\n"
183 let rec ppcontext ~subst ~metasenv = function
185 | (name, NCic.Decl t) :: tl ->
186 ppcontext ~subst ~metasenv tl ^
187 name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl t ^ "\n"
188 | (name, NCic.Def (bo,ty)) :: tl->
189 ppcontext ~subst ~metasenv tl ^
190 name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl ty ^
191 " := " ^ ppterm ~subst ~metasenv ~context:tl bo ^ "\n"