]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicPp.ml
better pp
[helm.git] / helm / software / components / ng_kernel / nCicPp.ml
1 let ppterm = 
2   ref (fun ~context ~subst ~metasenv ?inside_fix t -> "Please, set a pp callback")
3 ;;
4
5 let set_ppterm f = ppterm := f;;
6
7 let ppterm ~context ~subst ~metasenv ?inside_fix t = 
8   !ppterm ~context ~subst ~metasenv ?inside_fix t
9 ;;
10
11 module C = NCic
12 module R = NReference
13
14 let r2s pp_fix_name r = 
15   try
16     match r with
17     | R.Ref (_,u,R.Ind i) -> 
18         (match snd(NCicEnvironment.get_obj u) with
19         | _,_,_,_, C.Inductive (_,_,itl,_) ->
20             let _,name,_,_ = List.nth itl i in name
21         | _ -> assert false)
22     | R.Ref (_,u,R.Con (i,j)) -> 
23         (match snd(NCicEnvironment.get_obj u) with
24         | _,_,_,_, C.Inductive (_,_,itl,_) ->
25             let _,_,_,cl = List.nth itl i in
26             let _,name,_ = List.nth cl (j-1) in name
27         | _ -> assert false)
28     | R.Ref (_,u,(R.Decl | R.Def )) -> 
29         (match snd(NCicEnvironment.get_obj u) with
30         | _,_,_,_, C.Constant (_,name,_,_,_) -> name
31         | _ -> assert false)
32     | R.Ref (_,u,(R.Fix (i,_)|R.CoFix i)) ->
33         (match snd(NCicEnvironment.get_obj u) with
34         | _,_,_,_, C.Fixpoint (_,fl,_) -> 
35             if pp_fix_name then
36               let _,name,_,_,_ = List.nth fl i in name
37             else 
38               let name = NUri.string_of_uri u in 
39               let name = Filename.basename name in
40               let name = Filename.chop_extension name in
41               name ^"("^ string_of_int i ^ ")"
42         | _ -> assert false)
43   with exn -> R.string_of_reference r
44 ;;
45
46 let trivial_pp_term ~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 ctx = function
51    | C.Rel m ->
52        (try F.fprintf f "%s" (List.nth ctx (m-1))
53        with Failure _ -> F.fprintf f " -%d" (m - List.length context))
54    | C.Const r -> F.fprintf f "%s" (r2s inside_fix r)
55    | C.Prod ("_",s,t) -> 
56        F.fprintf f "@[<hov 1>";
57        aux ctx s; 
58        F.fprintf f "@;→ ";
59        aux ("_"::ctx) t;
60        F.fprintf f "@]";
61    | C.Prod (name,s,t) -> 
62        F.fprintf f "@[<hov 1>";
63        F.fprintf f "@[<hov 2>∀%s:@;" name;
64        aux ctx s; 
65        F.fprintf f "@].@;";
66        aux (name::ctx) t;
67        F.fprintf f "@]";
68    | C.Lambda (name,s,t) -> 
69        F.fprintf f "@[<hov 1>";
70        F.fprintf f "λ%s:" name;
71        aux ctx s; 
72        F.fprintf f ".@;";
73        aux (name::ctx) t;
74        F.fprintf f "@]";
75    | C.LetIn (name,ty,t,b) -> 
76        F.fprintf f "let %s:" name;
77        aux ctx ty;
78        F.fprintf f "≝@;";
79        aux ctx t;
80        F.fprintf f "in@;";
81        (aux (name::ctx) b)
82    | C.Match (r,oty,t,pl) ->
83        F.fprintf f "@[<hov>match ";
84        aux ctx t;
85        F.fprintf f "@;return ";
86        aux ctx oty;
87        F.fprintf f "@; @[<v>[ ";
88        if pl <> [] then
89          begin
90            F.fprintf f "@[<hov 2>%s ⇒@;" (r2s inside_fix (R.mk_constructor 1 r));
91            aux ctx (List.hd pl);
92            F.fprintf f "@]";
93            ignore(List.fold_left 
94              (fun i t -> 
95               F.fprintf f "@;| @[<hov 2>%s ⇒@;" (r2s inside_fix (R.mk_constructor i r));
96               aux ctx t; 
97               F.fprintf f "@]";
98               i+1)
99              2 (List.tl pl));
100          end;
101       F.fprintf f "]@] @]";
102    | C.Appl l -> 
103        F.fprintf f "@[<h 2>(";
104        aux ctx (List.hd l);
105        List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
106        F.fprintf f ")@]"
107    | C.Implicit _ -> F.fprintf f "?"
108    | C.Meta (n,_) -> F.fprintf f "?%d" n
109    | C.Sort C.Prop -> F.fprintf f "Prop"
110    | C.Sort C.CProp -> F.fprintf f "CProp"
111    | C.Sort (C.Type n) -> F.fprintf f "Type%d" n
112   in 
113   aux (List.map fst context) t;
114   F.fprintf f "@?";
115   Buffer.contents buff
116 ;;
117
118 let ppobj = function
119   | (u,_,metasenv,subst,NCic.Fixpoint (b, fl, _)) -> 
120       "{"^NUri.string_of_uri u^"}\n"^
121       "let rec "^
122        String.concat "\nand " (fst
123         (List.fold_right (fun (_,name,n,ty,bo) (l,i) ->
124           ((name^
125           " on " ^ string_of_int n ^ " : " ^ 
126           ppterm ~metasenv ~subst ~context:[] ty ^ " :=\n"^
127           ppterm ~metasenv ~subst ~context:[] ~inside_fix:true bo) :: l),i-1) fl 
128            ([],List.length fl)))
129   | (u,_,_,_,NCic.Inductive (b, _,tyl, _)) -> "inductive"
130   | (u,_,metasenv,subst,NCic.Constant (_,name,None,ty, _)) -> 
131         "axiom " ^ name ^ " : " ^ 
132           ppterm ~metasenv ~subst ~context:[] ty ^ "\n"
133   | (u,_,metasenv,subst,NCic.Constant (_,name,Some bo,ty, _)) ->
134         "definition " ^ name ^ " : " ^ 
135           ppterm ~metasenv ~subst ~context:[] ty ^ " := \n"^
136           ppterm ~metasenv ~subst ~context:[] bo ^ "\n"
137 ;;