]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicPp.ml
ppmetasenv & subst added
[helm.git] / helm / software / components / ng_kernel / nCicPp.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 (* $Id$ *)
13
14 module C = NCic
15 module R = NReference
16
17 let r2s pp_fix_name r = 
18   try
19     match r with
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
24         | _ -> assert false)
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
30         | _ -> assert false)
31     | R.Ref (u,(R.Decl | R.Def _)) -> 
32         (match NCicLibrary.get_obj u with
33         | _,_,_,_, C.Constant (_,name,_,_,_) -> name
34         | _ -> assert false)
35     | R.Ref (u,(R.Fix (i,_,_)|R.CoFix i)) ->
36         (match NCicLibrary.get_obj u with
37         | _,_,_,_, C.Fixpoint (_,fl,_) -> 
38             if pp_fix_name then
39               let _,name,_,_,_ = List.nth fl i in name
40             else 
41               NUri.name_of_uri u ^"("^ string_of_int i ^ ")"
42         | _ -> assert false)
43   with NCicLibrary.ObjectNotFound _ -> R.string_of_reference r
44 ;;
45
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
51    | C.Rel m ->
52          (try 
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 "nth" | Invalid_argument "List.nth" -> 
56              F.fprintf f " -%d" (m - List.length ctx))
57    | C.Const r -> F.fprintf f "%s" (r2s inside_fix r)
58    | C.Prod ("_",s,t) -> 
59        if not toplevel then F.fprintf f "(";
60        F.fprintf f "@[<hov 1>";
61        (match s with 
62        | C.Prod ("_",_,_) -> aux ~toplevel:false ctx s 
63        | _ -> aux ~toplevel:true ctx s);
64        F.fprintf f "@;→ ";
65        aux ~toplevel:true ("_"::ctx) t;
66        F.fprintf f "@]";
67        if not toplevel then F.fprintf f ")";
68    | C.Prod (name,s,t) -> 
69        if not toplevel then F.fprintf f "(";
70        F.fprintf f "@[<hov 1>";
71        F.fprintf f "@[<hov 2>∀%s:@;" name;
72        aux ~toplevel:true ctx s; 
73        F.fprintf f "@].@;";
74        aux ~toplevel:true (name::ctx) t;
75        F.fprintf f "@]";
76        if not toplevel then F.fprintf f ")";
77    | C.Lambda (name,s,t) -> 
78        if not toplevel then F.fprintf f "(";
79        F.fprintf f "@[<hov 1>";
80        F.fprintf f "λ%s:" name;
81        aux ~toplevel:true ctx s; 
82        F.fprintf f ".@;";
83        aux ~toplevel:true (name::ctx) t;
84        F.fprintf f "@]";
85        if not toplevel then F.fprintf f ")";
86    | C.LetIn (name,ty,t,b) -> 
87        if not toplevel then F.fprintf f "(";
88        F.fprintf f "@[<hov 1>";
89        F.fprintf f "let %s:@;" name;
90        aux ~toplevel:true ctx ty;
91        F.fprintf f "@;≝ ";
92        aux ~toplevel:true ctx t;
93        F.fprintf f "@;in@;";
94        (aux ~toplevel:true (name::ctx) b);
95        F.fprintf f "@]";
96        if not toplevel then F.fprintf f ")";
97    | C.Match (r,oty,t,pl) ->
98        F.fprintf f "@[<hov>match ";
99        aux ~toplevel:true ctx t;
100        F.fprintf f "@;return ";
101        aux ~toplevel:true ctx oty;
102        F.fprintf f "@; @[<v>[ ";
103        if pl <> [] then
104          begin
105            F.fprintf f "@[<hov 2>%s ⇒@;" (r2s inside_fix (R.mk_constructor 1 r));
106            aux ~toplevel:true ctx (List.hd pl);
107            F.fprintf f "@]";
108            ignore(List.fold_left 
109              (fun i t -> 
110               F.fprintf f "@;| @[<hov 2>%s ⇒@;" (r2s inside_fix (R.mk_constructor i r));
111               aux ~toplevel:true ctx t; 
112               F.fprintf f "@]";
113               i+1)
114              2 (List.tl pl));
115          end;
116       F.fprintf f "]@] @]";
117    | C.Appl l -> 
118        F.fprintf f "@[<hov 2>";
119        if not toplevel then F.fprintf f "(";
120        aux ctx (List.hd l);
121        List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
122        if not toplevel then F.fprintf f ")";
123        F.fprintf f "@]"
124    | C.Implicit _ -> F.fprintf f "?"
125    | C.Meta (n,lc) when List.mem_assoc n subst -> 
126         let _,_,t,_ = List.assoc n subst in
127         aux ctx (NCicSubstitution.subst_meta lc t)
128    | C.Meta (n,(shift,C.Irl len)) -> 
129         F.fprintf f "?%d(%d,%d)" n shift len
130    | C.Meta (n,(shift,C.Ctx l)) -> 
131         F.fprintf f "?%d(%d,[" n shift;
132         if List.length l > 0 then
133           begin 
134             aux ctx (NCicSubstitution.lift shift (List.hd l));
135             List.iter (fun x -> F.fprintf f ",";aux ctx x) 
136              (List.map (NCicSubstitution.lift shift) (List.tl l));
137           end;
138         F.fprintf f "])"
139    | C.Sort C.Prop -> F.fprintf f "Prop"
140    | C.Sort (C.Type []) -> F.fprintf f "IllFormedUniverse"
141    | C.Sort (C.Type [false, u]) -> F.fprintf f "%s" (NUri.name_of_uri u)
142    | C.Sort (C.Type [true, u]) -> F.fprintf f "S(%s)" (NUri.name_of_uri u)
143    | C.Sort (C.Type l) -> 
144        F.fprintf f "Max(";
145        aux ctx (C.Sort (C.Type [List.hd l]));
146        List.iter (fun x -> F.fprintf f ",";aux ctx (C.Sort (C.Type [x])))
147         (List.tl l);
148        F.fprintf f ")"
149   in 
150   aux ~toplevel:true (List.map fst context) t;
151   F.fprintf f "@?";
152   Buffer.contents buff
153 ;;
154
155 let ppobj = function
156   | (u,_,metasenv,subst,NCic.Fixpoint (b, fl, _)) -> 
157       "{"^NUri.string_of_uri u^"}\n"^
158       (if b then "let rec " else "let corec ") ^
159        String.concat "\nand " 
160         (List.map (fun (_,name,n,ty,bo) ->
161           name^ " on " ^ string_of_int n ^ " : " ^ 
162           ppterm ~metasenv ~subst ~context:[] ty ^ " :=\n"^
163           ppterm ~metasenv ~subst ~context:[] ~inside_fix:true bo) fl)
164   | (u,_,metasenv,subst,NCic.Inductive (b, leftno,tyl, _)) -> 
165       "{"^NUri.string_of_uri u^"} with "^string_of_int leftno^" fixed params\n"^
166       (if b then "inductive " else "coinductive ")^
167       String.concat "\nand "
168         (List.map (fun (_,name,ty,cl) ->
169           name^": "^ppterm ~metasenv ~subst ~context:[] ty^ " :=\n"^
170           String.concat "\n"
171           (List.map (fun (_,name,ty) ->
172            "  | "^name^": "^ppterm ~metasenv ~subst ~context:[] ty)
173            cl)) tyl) ^ "."
174   | (u,_,metasenv,subst,NCic.Constant (_,name,None,ty, _)) -> 
175       "{"^NUri.string_of_uri u^"}\n"^
176         "axiom " ^ name ^ " : " ^ 
177           ppterm ~metasenv ~subst ~context:[] ty ^ "\n"
178   | (u,_,metasenv,subst,NCic.Constant (_,name,Some bo,ty, _)) ->
179       "{"^NUri.string_of_uri u^"}\n"^
180         "definition " ^ name ^ " : " ^ 
181           ppterm ~metasenv ~subst ~context:[] ty ^ " := \n"^
182           ppterm ~metasenv ~subst ~context:[] bo ^ "\n"
183 ;;
184
185 let rec ppcontext ?(sep="\n") ~subst ~metasenv = function
186   | [] -> ""
187   | (name, NCic.Decl t) :: tl -> 
188       ppcontext ~sep ~subst ~metasenv tl ^
189       name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl t ^ sep
190   | (name, NCic.Def (bo,ty)) :: tl->
191       ppcontext ~sep ~subst ~metasenv tl ^
192       name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl ty ^ 
193       " := " ^ ppterm ~subst ~metasenv ~context:tl bo ^ sep
194 ;;
195
196 let rec ppmetasenv ~subst metasenv = function
197   | [] -> ""
198   | (i,(name, ctx, ty)) :: tl ->
199       let name = match name with Some n -> "("^n^")" | _ -> "" in
200       ppcontext ~sep:"; " ~subst ~metasenv ctx ^
201       " ⊢ ?"^string_of_int i^name^" : " ^ 
202       ppterm ~metasenv ~subst ~context:ctx ty ^ "\n" ^
203       ppmetasenv ~subst metasenv tl
204 ;;
205
206 let ppmetasenv ~subst metasenv = ppmetasenv ~subst metasenv metasenv;;
207
208 let rec ppsubst ~subst ~metasenv = function
209   | [] -> ""
210   | (i,(name, ctx, t, ty)) :: tl ->
211       
212       let name = match name with Some n -> "("^n^")" | _ -> "" in
213       ppcontext ~sep:"; " ~subst ~metasenv ctx ^
214       " ⊢ ?"^string_of_int i^name^" := " ^ 
215       ppterm ~metasenv ~subst ~context:ctx t ^ " : " ^
216       ppterm ~metasenv ~subst ~context:ctx ty ^ "\n" ^
217       ppsubst ~subst ~metasenv tl
218 ;;
219
220 let ppsubst ~metasenv subst = ppsubst ~metasenv ~subst subst;;
221
222 let _ = NCicSubstitution.set_ppterm ppterm;;
223