]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicPp.ml
height of constants properly handled
[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 let ppterm = 
15   ref (fun ~context ~subst ~metasenv ?inside_fix t -> "Please, set a pp callback")
16 ;;
17
18 let set_ppterm f = ppterm := f;;
19
20 let ppterm ~context ~subst ~metasenv ?inside_fix t = 
21   !ppterm ~context ~subst ~metasenv ?inside_fix t
22 ;;
23
24 module C = NCic
25 module R = NReference
26
27 let r2s pp_fix_name r = 
28   try
29     match r with
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
34         | _ -> assert false)
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
40         | _ -> assert false)
41     | R.Ref (u,(R.Decl | R.Def _)) -> 
42         (match NCicLibrary.get_obj u with
43         | _,_,_,_, C.Constant (_,name,_,_,_) -> name
44         | _ -> assert false)
45     | R.Ref (u,(R.Fix (i,_,_)|R.CoFix i)) ->
46         (match NCicLibrary.get_obj u with
47         | _,_,_,_, C.Fixpoint (_,fl,_) -> 
48             if pp_fix_name then
49               let _,name,_,_,_ = List.nth fl i in name
50             else 
51               let name = NUri.string_of_uri u in 
52               let name = Filename.basename name in
53               let name = Filename.chop_extension name in
54               name ^"("^ string_of_int i ^ ")"
55         | _ -> assert false)
56   with exn -> R.string_of_reference r
57 ;;
58
59 let trivial_pp_term ~context ~subst ~metasenv ?(inside_fix=false) t = 
60   let buff = Buffer.create 100 in
61   let f = Format.formatter_of_buffer buff in
62   let module F = Format in
63   let rec aux ?(toplevel=false) ctx = function
64    | C.Rel m ->
65          (try 
66             let name = List.nth ctx (m-1) in
67             F.fprintf f "%s" (if name = "_" then "__"^string_of_int m else name)
68          with Failure _ -> F.fprintf f " -%d" (m - List.length ctx))
69    | C.Const r -> F.fprintf f "%s" (r2s inside_fix r)
70    | C.Prod ("_",s,t) -> 
71        if not toplevel then F.fprintf f "(";
72        F.fprintf f "@[<hov 1>";
73        (match s with 
74        | C.Prod ("_",_,_) -> aux ~toplevel:false ctx s 
75        | _ -> aux ~toplevel:true ctx s);
76        F.fprintf f "@;→ ";
77        aux ~toplevel:true ("_"::ctx) t;
78        F.fprintf f "@]";
79        if not toplevel then F.fprintf f ")";
80    | C.Prod (name,s,t) -> 
81        if not toplevel then F.fprintf f "(";
82        F.fprintf f "@[<hov 1>";
83        F.fprintf f "@[<hov 2>∀%s:@;" name;
84        aux ~toplevel:true ctx s; 
85        F.fprintf f "@].@;";
86        aux ~toplevel:true (name::ctx) t;
87        F.fprintf f "@]";
88        if not toplevel then F.fprintf f ")";
89    | C.Lambda (name,s,t) -> 
90        if not toplevel then F.fprintf f "(";
91        F.fprintf f "@[<hov 1>";
92        F.fprintf f "λ%s:" name;
93        aux ~toplevel:true ctx s; 
94        F.fprintf f ".@;";
95        aux ~toplevel:true (name::ctx) t;
96        F.fprintf f "@]";
97        if not toplevel then F.fprintf f ")";
98    | C.LetIn (name,ty,t,b) -> 
99        if not toplevel then F.fprintf f "(";
100        F.fprintf f "@[<hov 1>";
101        F.fprintf f "let %s:@;" name;
102        aux ~toplevel:true ctx ty;
103        F.fprintf f "@;≝ ";
104        aux ~toplevel:true ctx t;
105        F.fprintf f "@;in@;";
106        (aux ~toplevel:true (name::ctx) b);
107        F.fprintf f "@]";
108        if not toplevel then F.fprintf f ")";
109    | C.Match (r,oty,t,pl) ->
110        F.fprintf f "@[<hov>match ";
111        aux ~toplevel:true ctx t;
112        F.fprintf f "@;return ";
113        aux ~toplevel:true ctx oty;
114        F.fprintf f "@; @[<v>[ ";
115        if pl <> [] then
116          begin
117            F.fprintf f "@[<hov 2>%s ⇒@;" (r2s inside_fix (R.mk_constructor 1 r));
118            aux ~toplevel:true ctx (List.hd pl);
119            F.fprintf f "@]";
120            ignore(List.fold_left 
121              (fun i t -> 
122               F.fprintf f "@;| @[<hov 2>%s ⇒@;" (r2s inside_fix (R.mk_constructor i r));
123               aux ~toplevel:true ctx t; 
124               F.fprintf f "@]";
125               i+1)
126              2 (List.tl pl));
127          end;
128       F.fprintf f "]@] @]";
129    | C.Appl l -> 
130        F.fprintf f "@[<hov 2>";
131        if not toplevel then F.fprintf f "(";
132        aux ctx (List.hd l);
133        List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
134        if not toplevel then F.fprintf f ")";
135        F.fprintf f "@]"
136    | C.Implicit _ -> F.fprintf f "?"
137    | C.Meta (n,_) -> F.fprintf f "?%d" n
138    | C.Sort C.Prop -> F.fprintf f "Prop"
139    | C.Sort C.CProp -> F.fprintf f "CProp"
140    | C.Sort (C.Type n) -> F.fprintf f "Type%d" n
141   in 
142   aux ~toplevel:true (List.map fst context) t;
143   F.fprintf f "@?";
144   Buffer.contents buff
145 ;;
146
147 let ppobj = function
148   | (u,_,metasenv,subst,NCic.Fixpoint (b, fl, _)) -> 
149       "{"^NUri.string_of_uri u^"}\n"^
150       (if b then "let rec " else "let corec ") ^
151        String.concat "\nand " 
152         (List.map (fun (_,name,n,ty,bo) ->
153           name^ " on " ^ string_of_int n ^ " : " ^ 
154           ppterm ~metasenv ~subst ~context:[] ty ^ " :=\n"^
155           ppterm ~metasenv ~subst ~context:[] ~inside_fix:true bo) fl)
156   | (u,_,metasenv,subst,NCic.Inductive (b, leftno,tyl, _)) -> 
157       "{"^NUri.string_of_uri u^"} with "^string_of_int leftno^" fixed params\n"^
158       "inductive "^
159       String.concat "\nand "
160         (List.map (fun (_,name,ty,cl) ->
161           name^": "^ppterm ~metasenv ~subst ~context:[] ty^ " :=\n"^
162           String.concat "\n"
163           (List.map (fun (_,name,ty) ->
164            "  | "^name^": "^ppterm ~metasenv ~subst ~context:[] ty)
165            cl)) tyl) ^ "."
166   | (u,_,metasenv,subst,NCic.Constant (_,name,None,ty, _)) -> 
167       "{"^NUri.string_of_uri u^"}\n"^
168         "axiom " ^ name ^ " : " ^ 
169           ppterm ~metasenv ~subst ~context:[] ty ^ "\n"
170   | (u,_,metasenv,subst,NCic.Constant (_,name,Some bo,ty, _)) ->
171       "{"^NUri.string_of_uri u^"}\n"^
172         "definition " ^ name ^ " : " ^ 
173           ppterm ~metasenv ~subst ~context:[] ty ^ " := \n"^
174           ppterm ~metasenv ~subst ~context:[] bo ^ "\n"
175 ;;
176
177 let rec ppcontext ~subst ~metasenv = function
178   | [] -> ""
179   | (name, NCic.Decl t) :: tl -> 
180       ppcontext ~subst ~metasenv tl ^
181       name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl t ^ "\n"
182   | (name, NCic.Def (bo,ty)) :: tl->
183       ppcontext ~subst ~metasenv tl ^
184       name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl ty ^ 
185       " := " ^ ppterm ~subst ~metasenv ~context:tl bo ^ "\n"
186 ;;