]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicPp.ml
83422cd8f238ee1f207afb8931f57bcb2d7b16c4
[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 string_of_implicit_annotation = function
47   | `Closed -> "▪"
48   | `Type -> ""
49   | `Hole -> "□"
50   | `Term -> "Term"
51   | `Typeof x -> "Ty("^string_of_int x^")"
52 ;;
53
54 let ppterm ~context ~subst ~metasenv:_ ?(inside_fix=false) t = 
55   let buff = Buffer.create 100 in
56   let f = Format.formatter_of_buffer buff in
57   let module F = Format in
58   let rec aux ?(toplevel=false) ctx = function
59    | C.Rel m ->
60          (try 
61             let name = List.nth ctx (m-1) in
62             F.fprintf f "%s" (if name = "_" then "__"^string_of_int m else name)
63          with Failure "nth" | Invalid_argument "List.nth" -> 
64              F.fprintf f " -%d" (m - List.length ctx))
65    | C.Const r -> F.fprintf f "%s" (r2s inside_fix r)
66    | C.Prod ("_",s,t) -> 
67        if not toplevel then F.fprintf f "(";
68        F.fprintf f "@[<hov 1>";
69        (match s with 
70        | C.Prod ("_",_,_) -> aux ~toplevel:false ctx s 
71        | _ -> aux ~toplevel:true ctx s);
72        F.fprintf f "@;→ ";
73        aux ~toplevel:true ("_"::ctx) t;
74        F.fprintf f "@]";
75        if not toplevel then F.fprintf f ")";
76    | C.Prod (name,s,t) -> 
77        if not toplevel then F.fprintf f "(";
78        F.fprintf f "@[<hov 1>";
79        F.fprintf f "@[<hov 2>∀%s:@;" name;
80        aux ~toplevel:true ctx s; 
81        F.fprintf f "@].@;";
82        aux ~toplevel:true (name::ctx) t;
83        F.fprintf f "@]";
84        if not toplevel then F.fprintf f ")";
85    | C.Lambda (name,s,t) -> 
86        if not toplevel then F.fprintf f "(";
87        F.fprintf f "@[<hov 1>";
88        F.fprintf f "λ%s:" name;
89        aux ~toplevel:true ctx s; 
90        F.fprintf f ".@;";
91        aux ~toplevel:true (name::ctx) t;
92        F.fprintf f "@]";
93        if not toplevel then F.fprintf f ")";
94    | C.LetIn (name,ty,t,b) -> 
95        if not toplevel then F.fprintf f "(";
96        F.fprintf f "@[<hov 1>";
97        F.fprintf f "let %s:@;" name;
98        aux ~toplevel:true ctx ty;
99        F.fprintf f "@;≝ ";
100        aux ~toplevel:true ctx t;
101        F.fprintf f "@;in@;";
102        (aux ~toplevel:true (name::ctx) b);
103        F.fprintf f "@]";
104        if not toplevel then F.fprintf f ")";
105    | C.Match (r,oty,t,pl) ->
106        F.fprintf f "@[<hov>match ";
107        aux ~toplevel:true ctx t;
108        F.fprintf f "@;return ";
109        aux ~toplevel:true ctx oty;
110        F.fprintf f "@; @[<v>[ ";
111        if pl <> [] then
112          begin
113            F.fprintf f "@[<hov 2>%s ⇒@;" 
114              (try r2s inside_fix (R.mk_constructor 1 r)
115               with R.IllFormedReference _ -> "#ERROR#");
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 ⇒@;" 
121                 (try r2s inside_fix (R.mk_constructor i r)
122                  with R.IllFormedReference _ -> "#ERROR#");
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 [] | C.Appl [_] | C.Appl (C.Appl _::_) -> 
130        F.fprintf f "BAD APPLICATION"
131    | C.Appl l -> 
132        F.fprintf f "@[<hov 2>";
133        if not toplevel then F.fprintf f "(";
134        aux ctx (List.hd l);
135        List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
136        if not toplevel then F.fprintf f ")";
137        F.fprintf f "@]"
138    | C.Implicit annot -> 
139        F.fprintf f "?%s" (string_of_implicit_annotation annot)
140    | C.Meta (n,lc) when List.mem_assoc n subst -> 
141         let _,_,t,_ = List.assoc n subst in
142         aux ctx (NCicSubstitution.subst_meta lc t)
143    | C.Meta (n,(shift,C.Irl len)) -> 
144         F.fprintf f "?%d(%d,%d)" n shift len
145    | C.Meta (n,(shift,C.Ctx l)) -> 
146         F.fprintf f "?%d(%d,[" n shift;
147         if List.length l > 0 then
148           begin 
149             aux ctx (NCicSubstitution.lift shift (List.hd l));
150             List.iter (fun x -> F.fprintf f ",";aux ctx x) 
151              (List.map (NCicSubstitution.lift shift) (List.tl l));
152           end;
153         F.fprintf f "])"
154    | C.Sort C.Prop -> F.fprintf f "Prop"
155    | C.Sort (C.Type []) -> F.fprintf f "Type0"
156    | C.Sort (C.Type [false, u]) -> F.fprintf f "%s" (NUri.name_of_uri u)
157    | C.Sort (C.Type [true, u]) -> F.fprintf f "S(%s)" (NUri.name_of_uri u)
158    | C.Sort (C.Type l) -> 
159        F.fprintf f "Max(";
160        aux ctx (C.Sort (C.Type [List.hd l]));
161        List.iter (fun x -> F.fprintf f ",";aux ctx (C.Sort (C.Type [x])))
162         (List.tl l);
163        F.fprintf f ")"
164   in 
165   aux ~toplevel:true (List.map fst context) t;
166   F.fprintf f "@?";
167   Buffer.contents buff
168 ;;
169
170 let ppterm ~context ~subst ~metasenv ?(margin=80) ?inside_fix t = 
171   Format.set_margin margin;
172   ppterm ~context ~subst ~metasenv ?inside_fix t
173 ;;
174
175
176 let ppobj = function
177   | (u,_,metasenv,subst,NCic.Fixpoint (b, fl, _)) -> 
178       "{"^NUri.string_of_uri u^"}\n"^
179       (if b then "let rec " else "let corec ") ^
180        String.concat "\nand " 
181         (List.map (fun (_,name,n,ty,bo) ->
182           name^ " on " ^ string_of_int n ^ " : " ^ 
183           ppterm ~metasenv ~subst ~context:[] ty ^ " :=\n"^
184           ppterm ~metasenv ~subst ~context:[] ~inside_fix:true bo) fl)
185   | (u,_,metasenv,subst,NCic.Inductive (b, leftno,tyl, _)) -> 
186       "{"^NUri.string_of_uri u^"} with "^string_of_int leftno^" fixed params\n"^
187       (if b then "inductive " else "coinductive ")^
188       String.concat "\nand "
189         (List.map (fun (_,name,ty,cl) ->
190           name^": "^ppterm ~metasenv ~subst ~context:[] ty^ " :=\n"^
191           String.concat "\n"
192           (List.map (fun (_,name,ty) ->
193            "  | "^name^": "^ppterm ~metasenv ~subst ~context:[] ty)
194            cl)) tyl) ^ "."
195   | (u,_,metasenv,subst,NCic.Constant (_,name,None,ty, _)) -> 
196       "{"^NUri.string_of_uri u^"}\n"^
197         "axiom " ^ name ^ " : " ^ 
198           ppterm ~metasenv ~subst ~context:[] ty ^ "\n"
199   | (u,_,metasenv,subst,NCic.Constant (_,name,Some bo,ty, _)) ->
200       "{"^NUri.string_of_uri u^"}\n"^
201         "definition " ^ name ^ " : " ^ 
202           ppterm ~metasenv ~subst ~context:[] ty ^ " := \n"^
203           ppterm ~metasenv ~subst ~context:[] bo ^ "\n"
204 ;;
205
206 let rec ppcontext ?(sep="\n") ~subst ~metasenv = function
207   | [] -> ""
208   | (name, NCic.Decl t) :: tl -> 
209       ppcontext ~sep ~subst ~metasenv tl ^
210       name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl t ^ sep
211   | (name, NCic.Def (bo,ty)) :: tl->
212       ppcontext ~sep ~subst ~metasenv tl ^
213       name ^ ": " ^ ppterm ~subst ~metasenv ~context:tl ty ^ 
214       " := " ^ ppterm ~subst ~metasenv ~context:tl bo ^ sep
215 ;;
216
217 let rec ppmetasenv ~subst metasenv = function
218   | [] -> ""
219   | (i,(name, ctx, ty)) :: tl ->
220       let name = match name with Some n -> "(\""^n^"\")" | _ -> "" in
221       ppcontext ~sep:"; " ~subst ~metasenv ctx ^
222       " ⊢ ?"^string_of_int i^name^" : " ^ 
223       ppterm ~metasenv ~subst ~context:ctx ty ^ "\n" ^
224       ppmetasenv ~subst metasenv tl
225 ;;
226
227 let ppmetasenv ~subst metasenv = ppmetasenv ~subst metasenv metasenv;;
228
229 let rec ppsubst ~subst ~metasenv = function
230   | [] -> ""
231   | (i,(name, ctx, t, ty)) :: tl ->
232       
233       let name = match name with Some n -> "("^n^")" | _ -> "" in
234       ppcontext ~sep:"; " ~subst ~metasenv ctx ^
235       " ⊢ ?"^string_of_int i^name^" := " ^ 
236       ppterm ~metasenv ~subst ~context:ctx t ^ " : " ^
237       ppterm ~metasenv ~subst ~context:ctx ty ^ "\n" ^
238       ppsubst ~subst ~metasenv tl
239 ;;
240
241 let ppsubst ~metasenv subst = ppsubst ~metasenv ~subst subst;;
242
243 let _ = NCicSubstitution.set_ppterm (ppterm ~margin:80);;
244