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