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