]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicPp.ml
Preparing for 0.5.9 release.
[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 module F = Format
17
18 let head_beta_reduce = ref (fun ~upto:_ _ -> assert false);;
19 let set_head_beta_reduce = (:=) head_beta_reduce;;
20
21 let get_obj = ref (fun _ -> assert false);;
22 let set_get_obj f = get_obj := f;;
23
24 let r2s pp_fix_name r = 
25   try
26     match r with
27     | R.Ref (u,R.Ind (_,i,_)) -> 
28         (match !get_obj u with
29         | _,_,_,_, C.Inductive (_,_,itl,_) ->
30             let _,name,_,_ = List.nth itl i in name
31         | _ -> assert false)
32     | R.Ref (u,R.Con (i,j,_)) -> 
33         (match !get_obj u with
34         | _,_,_,_, C.Inductive (_,_,itl,_) ->
35             let _,_,_,cl = List.nth itl i in
36             let _,name,_ = List.nth cl (j-1) in name
37         | _ -> assert false)
38     | R.Ref (u,(R.Decl | R.Def _)) -> 
39         (match !get_obj u with
40         | _,_,_,_, C.Constant (_,name,_,_,_) -> name
41         | _ -> assert false)
42     | R.Ref (u,(R.Fix (i,_,_)|R.CoFix i)) ->
43         (match !get_obj u with
44         | _,_,_,_, C.Fixpoint (_,fl,_) -> 
45             if pp_fix_name then
46               let _,name,_,_,_ = List.nth fl i in name
47             else 
48               NUri.name_of_uri u (*^"("^ string_of_int i ^ ")"*)
49         | _ -> assert false)
50   with 
51   | NCicEnvironment.ObjectNotFound _ 
52   | Failure "nth" 
53   | Invalid_argument "List.nth" -> R.string_of_reference r
54 ;;
55
56 let string_of_implicit_annotation = function
57   | `Closed -> "▪"
58   | `Type -> ""
59   | `Hole -> "□"
60   | `Tagged s -> "[\"" ^ s ^ "\"]"
61   | `Term -> "Term"
62   | `Typeof x -> "Ty("^string_of_int x^")"
63   | `Vector -> "…"
64 ;;
65
66 let ppterm ~formatter:f ~context ~subst ~metasenv:_ ?(inside_fix=false) t =
67  let rec aux ?(toplevel=false) ctx = function
68   | C.Rel m ->
69         (try 
70            let name = List.nth ctx (m-1) in
71            F.fprintf f "%s" (if name = "_" then "__"^string_of_int m else name)
72         with Failure "nth" | Invalid_argument "List.nth" -> 
73             F.fprintf f " -%d" (m - List.length ctx))
74   | C.Const r -> F.fprintf f "%s" (r2s inside_fix r)
75   | C.Prod ("_",s,t) -> 
76       if not toplevel then F.fprintf f "(";
77       F.fprintf f "@[<hov 1>";
78       (match s with 
79       | C.Prod ("_",_,_) -> aux ~toplevel:false ctx s 
80       | _ -> aux ~toplevel:true ctx s);
81       F.fprintf f "@;→ ";
82       aux ~toplevel:true ("_"::ctx) t;
83       F.fprintf f "@]";
84       if not toplevel then F.fprintf f ")";
85   | C.Prod (name,s,t) -> 
86       if not toplevel then F.fprintf f "(";
87       F.fprintf f "@[<hov 1>";
88       F.fprintf f "@[<hov 2>∀%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.Lambda (name,s,t) -> 
95       if not toplevel then F.fprintf f "(";
96       F.fprintf f "@[<hov 1>";
97       F.fprintf f "λ%s:" name;
98       aux ~toplevel:true ctx s; 
99       F.fprintf f ".@;";
100       aux ~toplevel:true (name::ctx) t;
101       F.fprintf f "@]";
102       if not toplevel then F.fprintf f ")";
103   | C.LetIn (name,ty,t,b) -> 
104       if not toplevel then F.fprintf f "(";
105       F.fprintf f "@[<hov 1>";
106       F.fprintf f "let %s:@;" name;
107       aux ~toplevel:true ctx ty;
108       F.fprintf f "@;≝ ";
109       aux ~toplevel:true ctx t;
110       F.fprintf f "@;in@;";
111       (aux ~toplevel:true (name::ctx) b);
112       F.fprintf f "@]";
113       if not toplevel then F.fprintf f ")";
114   | C.Match (r,oty,t,pl) ->
115       F.fprintf f "@[<hov>match ";
116       aux ~toplevel:true ctx t;
117       F.fprintf f "@;return ";
118       aux ~toplevel:true ctx oty;
119       F.fprintf f "@; @[<v>[ ";
120       if pl <> [] then
121         begin
122           F.fprintf f "@[<hov 2>%s ⇒@;" 
123             (try r2s inside_fix (R.mk_constructor 1 r)
124              with R.IllFormedReference _ -> "#ERROR#");
125           aux ~toplevel:true ctx (List.hd pl);
126           F.fprintf f "@]";
127           ignore(List.fold_left 
128             (fun i t -> 
129              F.fprintf f "@;| @[<hov 2>%s ⇒@;" 
130                (try r2s inside_fix (R.mk_constructor i r)
131                 with R.IllFormedReference _ -> "#ERROR#");
132              aux ~toplevel:true ctx t; 
133              F.fprintf f "@]";
134              i+1)
135             2 (List.tl pl));
136         end;
137      F.fprintf f "]@] @]";
138   | C.Appl [] -> 
139       F.fprintf f "BAD APPLICATION: empty list"
140   | C.Appl [x] ->
141       F.fprintf f "BAD APPLICATION: just the head: ";
142       aux ctx x
143   | C.Appl (C.Appl _ as x::_) -> 
144       F.fprintf f "BAD APPLICATION: appl of appl with head: ";
145       aux ctx x
146   | C.Appl (C.Meta (n,lc) :: args) when List.mem_assoc n subst -> 
147       let _,_,t,_ = List.assoc n subst in
148       let hd = NCicSubstitution.subst_meta lc t in
149       aux ctx 
150         (!head_beta_reduce ~upto:(List.length args)
151           (match hd with
152           | NCic.Appl l -> NCic.Appl (l@args)
153           | _ -> NCic.Appl (hd :: args)))
154   | C.Appl l -> 
155       F.fprintf f "@[<hov 2>";
156       if not toplevel then F.fprintf f "(";
157       aux ctx (List.hd l);
158       List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
159       if not toplevel then F.fprintf f ")";
160       F.fprintf f "@]"
161   | C.Implicit annot -> 
162       F.fprintf f "?%s" (string_of_implicit_annotation annot)
163   | C.Meta (n,lc) when List.mem_assoc n subst -> 
164        let _,_,t,_ = List.assoc n subst in
165        aux ctx (NCicSubstitution.subst_meta lc t)
166   | C.Meta (n,(shift,C.Irl len)) -> 
167        F.fprintf f "?%d(%d,%d)" n shift len
168   | C.Meta (n,(shift,C.Ctx l)) -> 
169        F.fprintf f "?%d(%d,[" n shift;
170        if List.length l > 0 then
171          begin 
172            aux ctx (NCicSubstitution.lift shift (List.hd l));
173            List.iter (fun x -> F.fprintf f ",";aux ctx x) 
174             (List.map (NCicSubstitution.lift shift) (List.tl l));
175          end;
176        F.fprintf f "])"
177   | C.Sort s -> NCicEnvironment.ppsort f s
178  in 
179   aux ~toplevel:true (List.map fst context) t
180 ;;
181
182 let on_buffer f t = 
183  try
184   let buff = Buffer.create 100 in
185   let formatter = F.formatter_of_buffer buff in
186   f ~formatter:formatter t;
187   F.fprintf formatter "@?";
188   Buffer.contents buff
189  with Failure m ->
190   "[[Unprintable: " ^ m ^ "]]"
191 ;;
192
193 let ppterm ~formatter ~context ~subst ~metasenv ?(margin=80) ?inside_fix t = 
194   F.set_margin margin;
195   ppterm ~formatter ~context ~subst ~metasenv ?inside_fix t
196 ;;
197
198 let rec ppcontext ~formatter ?(sep="; ") ~subst ~metasenv = function
199   | [] -> ()
200   | (name, NCic.Decl t) :: tl -> 
201       ppcontext ~formatter ~sep ~subst ~metasenv tl;
202       F.fprintf formatter "%s: " name;
203       ppterm ~formatter ~subst ~metasenv ~context:tl t;
204       F.fprintf formatter "%s@;" sep
205   | (name, NCic.Def (bo,ty)) :: tl->
206       ppcontext ~formatter ~sep ~subst ~metasenv tl;
207       F.fprintf formatter "%s: " name;
208       ppterm ~formatter ~subst ~metasenv ~context:tl ty;
209       F.fprintf formatter " := ";
210       ppterm ~formatter ~subst ~metasenv ~context:tl bo;
211       F.fprintf formatter "%s@;" sep
212 ;;
213 let ppcontext ~formatter ?sep ~subst ~metasenv c =
214   F.fprintf formatter "@[<hov>";
215   ppcontext ~formatter ?sep ~subst ~metasenv c;
216   F.fprintf formatter "@]";
217 ;;
218
219 let ppmetaattrs =
220  function
221     [] -> ""
222   | attrs ->
223    "(" ^
224     String.concat ","
225      (List.map
226        (function
227          | `IsTerm -> "term"
228          | `IsType -> "type"
229          | `IsSort -> "sort"
230          | `Name n -> "name=" ^ n
231          | `InScope -> "in_scope"
232          | `OutScope n -> "out_scope:" ^ string_of_int n
233        ) attrs) ^
234     ")"
235 ;;
236
237 let rec ppmetasenv ~formatter ~subst metasenv = function
238   | [] -> ()
239   | (i,(attrs, ctx, ty)) :: tl ->
240       F.fprintf formatter "@[<hov 2>";
241       ppcontext ~formatter ~sep:"; " ~subst ~metasenv ctx;
242       F.fprintf formatter "@;⊢@;?%d%s :@;" i (ppmetaattrs attrs);
243       ppterm ~formatter ~metasenv ~subst ~context:ctx ty;
244       F.fprintf formatter "@]@\n";
245       ppmetasenv ~formatter ~subst metasenv tl
246 ;;
247
248 let ppmetasenv ~formatter ~subst metasenv =
249  ppmetasenv ~formatter ~subst metasenv metasenv
250 ;;
251
252 let rec ppsubst ~formatter ~subst ~metasenv = function
253   | [] -> ()
254   | (i,(attrs, ctx, t, ty)) :: tl ->
255       ppcontext ~formatter ~sep:"; " ~subst ~metasenv ctx;
256       F.fprintf formatter " ⊢ ?%d%s := " i (ppmetaattrs attrs);
257       ppterm ~formatter ~metasenv ~subst ~context:ctx t;
258       F.fprintf formatter " : ";
259       ppterm ~formatter ~metasenv ~subst ~context:ctx ty;
260       F.fprintf formatter "\n";
261       ppsubst ~formatter ~subst ~metasenv tl
262 ;;
263
264 let ppsubst ~formatter ~metasenv ?(use_subst=true) subst =
265  let ssubst = if use_subst then subst else [] in
266   ppsubst ~formatter ~metasenv ~subst:ssubst subst
267 ;;
268
269 let string_of_generated = function
270   | `Generated -> "Generated"
271   | `Provided -> "Provided"
272 ;;
273
274 let string_of_flavour = function
275   | `Definition -> "Definition"
276   | `Fact -> "Fact"
277   | `Lemma -> "Lemma"
278   | `Theorem -> "Theorem"
279   | `Corollary -> "Corollary"
280   | `Example -> "Example"
281 ;;
282         
283 let string_of_pragma = function
284   | `Coercion _arity -> "Coercion _"
285   | `Elim _sort -> "Elim _"
286   | `Projection -> "Projection"
287   | `InversionPrinciple -> "InversionPrinciple"
288   | `Variant -> "Variant"
289   | `Local -> "Local"
290   | `Regular -> "Regular"
291 ;;
292
293 let string_of_fattrs (g,f,p) = 
294   String.concat ","
295    [ string_of_generated g; string_of_flavour f; string_of_pragma p ]
296 ;;
297
298 let ppobj ~formatter (u,_,metasenv, subst, o) = 
299   F.fprintf formatter "metasenv:\n";
300   ppmetasenv ~formatter ~subst metasenv;
301   F.fprintf formatter "\n";
302   F.fprintf formatter "subst:\n";
303   (*ppsubst subst ~formatter ~metasenv;*) F.fprintf formatter "...";
304   F.fprintf formatter "\n";
305   match o with 
306   | NCic.Fixpoint (b, fl, attrs) -> 
307       F.fprintf formatter "{%s}@\n@[<hov 0>" (NUri.string_of_uri u);
308       F.fprintf formatter "@[<hov 2>%s"(if b then "let rec " else "let corec ");
309       HExtlib.list_iter_sep
310        ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
311        (fun (_,name,n,ty,bo) ->
312         F.fprintf formatter "%s on %d :@;" name n;
313         ppterm ~formatter ~metasenv ~subst ~context:[] ty;
314         F.fprintf formatter "@]@;@[<hov 2>:=@;";
315         ppterm ~formatter ~metasenv ~subst ~context:[] ~inside_fix:true bo;
316         F.fprintf formatter "@]") fl;
317       F.fprintf formatter "@; %s" (string_of_fattrs attrs);
318       F.fprintf formatter "@]"
319   | NCic.Inductive (b, leftno,tyl, _) -> 
320       F.fprintf formatter "{%s} with %d fixed params@\n@[<hov 0>@[<hov 2>%s"
321        (NUri.string_of_uri u) leftno
322        (if b then "inductive " else "coinductive ");
323       HExtlib.list_iter_sep
324         ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
325        (fun (_,name,ty,cl) ->
326           F.fprintf formatter "%s:@;" name;
327           ppterm ~formatter ~metasenv ~subst ~context:[] ty;
328           F.fprintf formatter "@]@;@[<hov 3>:=@;";
329           HExtlib.list_iter_sep ~sep:(fun () -> F.fprintf formatter "@;")
330            (fun (_,name,ty) ->
331              F.fprintf formatter "| %s: " name;
332              ppterm ~formatter ~metasenv ~subst ~context:[] ty;)
333            cl;
334           F.fprintf formatter "@]"
335         ) tyl ;
336         F.fprintf formatter "@]"
337   | NCic.Constant (_,name,None,ty, _) -> 
338      F.fprintf formatter "{%s}@\n@[<hov 2>axiom %s :@;" (NUri.string_of_uri u) name;
339      ppterm ~formatter ~metasenv ~subst ~context:[] ty;
340      F.fprintf formatter "@]@\n"
341   | NCic.Constant (_,name,Some bo,ty, _) ->
342      F.fprintf formatter "{%s}@\n@[<hov 0>@[<hov 2>definition %s :@;" (NUri.string_of_uri u) name;
343      ppterm ~formatter ~metasenv ~subst ~context:[] ty;
344      F.fprintf formatter "@]@;@[<hov 2>:=@;";
345      ppterm ~formatter ~metasenv ~subst ~context:[] bo;
346      F.fprintf formatter "@]@\n@]"
347 ;;
348
349 let ppterm ~context ~subst ~metasenv ?margin ?inside_fix t = 
350  on_buffer (ppterm ~context ~subst ~metasenv ?margin ?inside_fix) t
351 ;;
352
353 let ppcontext ?sep ~subst ~metasenv ctx =
354  on_buffer (ppcontext ?sep ~subst ~metasenv) ctx
355 ;;
356
357 let ppmetasenv ~subst metasenv = on_buffer (ppmetasenv ~subst) metasenv;;
358
359 let ppsubst ~metasenv ?use_subst subst =
360  on_buffer (ppsubst ~metasenv ?use_subst) subst
361 ;;
362
363 let ppobj obj = on_buffer ppobj obj;;
364
365 let _ = NCicSubstitution.set_ppterm (ppterm ~margin:80);;