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_______________________________________________________________ *)
15 ref (fun ~context ~subst ~metasenv ?inside_fix t -> "Please, set a pp callback")
18 let set_ppterm f = ppterm := f;;
20 let ppterm ~context ~subst ~metasenv ?inside_fix t =
21 !ppterm ~context ~subst ~metasenv ?inside_fix t
27 let r2s pp_fix_name r =
30 | R.Ref (u,R.Ind (_,i,_)) ->
31 (match NCicLibrary.get_obj u with
32 | _,_,_,_, C.Inductive (_,_,itl,_) ->
33 let _,name,_,_ = List.nth itl i in name
35 | R.Ref (u,R.Con (i,j,_)) ->
36 (match NCicLibrary.get_obj u with
37 | _,_,_,_, C.Inductive (_,_,itl,_) ->
38 let _,_,_,cl = List.nth itl i in
39 let _,name,_ = List.nth cl (j-1) in name
41 | R.Ref (u,(R.Decl | R.Def _)) ->
42 (match NCicLibrary.get_obj u with
43 | _,_,_,_, C.Constant (_,name,_,_,_) -> name
45 | R.Ref (u,(R.Fix (i,_,_)|R.CoFix i)) ->
46 (match NCicLibrary.get_obj u with
47 | _,_,_,_, C.Fixpoint (_,fl,_) ->
49 let _,name,_,_,_ = List.nth fl i in name
51 NUri.name_of_uri u ^"("^ string_of_int i ^ ")"
53 with exn -> R.string_of_reference r
56 let trivial_pp_term ~context ~subst ~metasenv ?(inside_fix=false) t =
57 let buff = Buffer.create 100 in
58 let f = Format.formatter_of_buffer buff in
59 let module F = Format in
60 let rec aux ?(toplevel=false) ctx = function
63 let name = List.nth ctx (m-1) in
64 F.fprintf f "%s" (if name = "_" then "__"^string_of_int m else name)
65 with Failure _ -> F.fprintf f " -%d" (m - List.length ctx))
66 | C.Const r -> F.fprintf f "%s" (r2s inside_fix r)
68 if not toplevel then F.fprintf f "(";
69 F.fprintf f "@[<hov 1>";
71 | C.Prod ("_",_,_) -> aux ~toplevel:false ctx s
72 | _ -> aux ~toplevel:true ctx s);
74 aux ~toplevel:true ("_"::ctx) t;
76 if not toplevel then F.fprintf f ")";
77 | C.Prod (name,s,t) ->
78 if not toplevel then F.fprintf f "(";
79 F.fprintf f "@[<hov 1>";
80 F.fprintf f "@[<hov 2>∀%s:@;" name;
81 aux ~toplevel:true ctx s;
83 aux ~toplevel:true (name::ctx) t;
85 if not toplevel then F.fprintf f ")";
86 | C.Lambda (name,s,t) ->
87 if not toplevel then F.fprintf f "(";
88 F.fprintf f "@[<hov 1>";
89 F.fprintf f "λ%s:" name;
90 aux ~toplevel:true ctx s;
92 aux ~toplevel:true (name::ctx) t;
94 if not toplevel then F.fprintf f ")";
95 | C.LetIn (name,ty,t,b) ->
96 if not toplevel then F.fprintf f "(";
97 F.fprintf f "@[<hov 1>";
98 F.fprintf f "let %s:@;" name;
99 aux ~toplevel:true ctx ty;
101 aux ~toplevel:true ctx t;
102 F.fprintf f "@;in@;";
103 (aux ~toplevel:true (name::ctx) b);
105 if not toplevel then F.fprintf f ")";
106 | C.Match (r,oty,t,pl) ->
107 F.fprintf f "@[<hov>match ";
108 aux ~toplevel:true ctx t;
109 F.fprintf f "@;return ";
110 aux ~toplevel:true ctx oty;
111 F.fprintf f "@; @[<v>[ ";
114 F.fprintf f "@[<hov 2>%s ⇒@;" (r2s inside_fix (R.mk_constructor 1 r));
115 aux ~toplevel:true ctx (List.hd pl);
117 ignore(List.fold_left
119 F.fprintf f "@;| @[<hov 2>%s ⇒@;" (r2s inside_fix (R.mk_constructor i r));
120 aux ~toplevel:true ctx t;
125 F.fprintf f "]@] @]";
127 F.fprintf f "@[<hov 2>";
128 if not toplevel then F.fprintf f "(";
130 List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
131 if not toplevel then F.fprintf f ")";
133 | C.Implicit _ -> F.fprintf f "?"
134 | C.Meta (n,_) -> F.fprintf f "?%d" n
135 | C.Sort C.Prop -> F.fprintf f "Prop"
136 | C.Sort (C.Type []) -> F.fprintf f "IllFormedUniverse"
137 | C.Sort (C.Type [false, u]) -> F.fprintf f "%s" (NUri.name_of_uri u)
138 | C.Sort (C.Type [true, u]) -> F.fprintf f "S(%s)" (NUri.name_of_uri u)
139 | C.Sort (C.Type l) ->
141 aux ctx (C.Sort (C.Type [List.hd l]));
142 List.iter (fun x -> F.fprintf f ",";aux ctx (C.Sort (C.Type [x])))
146 aux ~toplevel:true (List.map fst context) t;
152 | (u,_,metasenv,subst,NCic.Fixpoint (b, fl, _)) ->
153 "{"^NUri.string_of_uri u^"}\n"^
154 (if b then "let rec " else "let corec ") ^
155 String.concat "\nand "
156 (List.map (fun (_,name,n,ty,bo) ->
157 name^ " on " ^ string_of_int n ^ " : " ^
158 ppterm ~metasenv ~subst ~context:[] ty ^ " :=\n"^
159 ppterm ~metasenv ~subst ~context:[] ~inside_fix:true bo) fl)
160 | (u,_,metasenv,subst,NCic.Inductive (b, leftno,tyl, _)) ->
161 "{"^NUri.string_of_uri u^"} with "^string_of_int leftno^" fixed params\n"^
163 String.concat "\nand "
164 (List.map (fun (_,name,ty,cl) ->
165 name^": "^ppterm ~metasenv ~subst ~context:[] ty^ " :=\n"^
167 (List.map (fun (_,name,ty) ->
168 " | "^name^": "^ppterm ~metasenv ~subst ~context:[] ty)
170 | (u,_,metasenv,subst,NCic.Constant (_,name,None,ty, _)) ->
171 "{"^NUri.string_of_uri u^"}\n"^
172 "axiom " ^ name ^ " : " ^
173 ppterm ~metasenv ~subst ~context:[] ty ^ "\n"
174 | (u,_,metasenv,subst,NCic.Constant (_,name,Some bo,ty, _)) ->
175 "{"^NUri.string_of_uri u^"}\n"^
176 "definition " ^ name ^ " : " ^
177 ppterm ~metasenv ~subst ~context:[] ty ^ " := \n"^
178 ppterm ~metasenv ~subst ~context:[] bo ^ "\n"
181 let rec ppcontext ~subst ~metasenv = function
183 | (name, NCic.Decl t) :: tl ->
184 ppcontext ~subst ~metasenv tl ^
185 name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl t ^ "\n"
186 | (name, NCic.Def (bo,ty)) :: tl->
187 ppcontext ~subst ~metasenv tl ^
188 name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl ty ^
189 " := " ^ ppterm ~subst ~metasenv ~context:tl bo ^ "\n"