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