]> matita.cs.unibo.it Git - helm.git/blob - matitaB/components/ng_kernel/nCicPp.ml
80eb372aeec1dace6d1622ebf9b83e16fcf81e38
[helm.git] / matitaB / 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 r2s status pp_fix_name r = 
19   try
20     match r with
21     | R.Ref (u,R.Ind (_,i,_)) -> 
22         (match NCicEnvironment.get_checked_obj status u with
23         | _,_,_,_, C.Inductive (_,_,itl,_) ->
24             let _,name,_,_ = List.nth itl i in name
25         | _ -> assert false)
26     | R.Ref (u,R.Con (i,j,_)) -> 
27         (match NCicEnvironment.get_checked_obj status u with
28         | _,_,_,_, C.Inductive (_,_,itl,_) ->
29             let _,_,_,cl = List.nth itl i in
30             let _,name,_ = List.nth cl (j-1) in name
31         | _ -> assert false)
32     | R.Ref (u,(R.Decl | R.Def _)) -> 
33         (match NCicEnvironment.get_checked_obj status u with
34         | _,_,_,_, C.Constant (_,name,_,_,_) -> name
35         | _ -> assert false)
36     | R.Ref (u,(R.Fix (i,_,_)|R.CoFix i)) ->
37         (match NCicEnvironment.get_checked_obj status u with
38         | _,_,_,_, C.Fixpoint (_,fl,_) -> 
39             if pp_fix_name then
40               let _,name,_,_,_ = List.nth fl i in name
41             else 
42               NUri.name_of_uri u (*^"("^ string_of_int i ^ ")"*)
43         | _ -> assert false)
44   with 
45   | NCicEnvironment.ObjectNotFound _ 
46   | Failure "nth" 
47   | Invalid_argument "List.nth" -> R.string_of_reference r
48 ;;
49
50 let string_of_implicit_annotation = function
51   | `Closed -> "▪"
52   | `Type -> ""
53   | `Hole -> "□"
54   | `Tagged s -> "[\"" ^ s ^ "\"]"
55   | `Term -> "Term"
56   | `Typeof x -> "Ty("^string_of_int x^")"
57   | `Vector -> "…"
58 ;;
59
60 let ppterm status ~formatter:f ~context ~subst ~metasenv:_ ?(inside_fix=false) t =
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 status 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 status 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 status 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 [] -> 
133       F.fprintf f "BAD APPLICATION: empty list"
134   | C.Appl [x] ->
135       F.fprintf f "BAD APPLICATION: just the head: ";
136       aux ctx x
137   | C.Appl (C.Appl _ as x::_) -> 
138       F.fprintf f "BAD APPLICATION: appl of appl with head: ";
139       aux ctx x
140   | C.Appl (C.Meta (n,lc) :: args) when List.mem_assoc n subst -> 
141       let _,_,t,_ = List.assoc n subst in
142       let hd = NCicSubstitution.subst_meta status lc t in
143       aux ctx 
144         (NCicReduction.head_beta_reduce (status :> NCicEnvironment.status) ~upto:(List.length args)
145           (match hd with
146           | NCic.Appl l -> NCic.Appl (l@args)
147           | _ -> NCic.Appl (hd :: args)))
148   | C.Appl l -> 
149       F.fprintf f "@[<hov 2>";
150       if not toplevel then F.fprintf f "(";
151       aux ctx (List.hd l);
152       List.iter (fun x -> F.fprintf f "@;";aux ctx x) (List.tl l);
153       if not toplevel then F.fprintf f ")";
154       F.fprintf f "@]"
155   | C.Implicit annot -> 
156       F.fprintf f "?%s" (string_of_implicit_annotation annot)
157   | C.Meta (n,lc) when List.mem_assoc n subst -> 
158        let _,_,t,_ = List.assoc n subst in
159        aux ctx (NCicSubstitution.subst_meta status lc t)
160   | C.Meta (n,(shift,C.Irl len)) -> 
161        F.fprintf f "?%d(%d,%d)" n shift len
162   | C.Meta (n,(shift,C.Ctx l)) -> 
163        F.fprintf f "?%d(%d,[" n shift;
164        if List.length l > 0 then
165          begin 
166            aux ctx (NCicSubstitution.lift status shift (List.hd l));
167            List.iter (fun x -> F.fprintf f ",";aux ctx x) 
168             (List.map (NCicSubstitution.lift status shift) (List.tl l));
169          end;
170        F.fprintf f "])"
171   | C.Sort s -> NCicEnvironment.ppsort f s
172  in 
173   aux ~toplevel:true (List.map fst context) t
174 ;;
175
176 let on_buffer f t = 
177  try
178   let buff = Buffer.create 100 in
179   let formatter = F.formatter_of_buffer buff in
180   f ~formatter:formatter t;
181   F.fprintf formatter "@?";
182   Buffer.contents buff
183  with Failure m ->
184   "[[Unprintable: " ^ m ^ "]]"
185 ;;
186
187 let ppterm ~formatter ~context ~subst ~metasenv ?(margin=80) ?inside_fix t = 
188   F.set_margin margin;
189   ppterm ~formatter ~context ~subst ~metasenv ?inside_fix t
190 ;;
191
192 let rec ppcontext status ~formatter ?(sep="; ") ~subst ~metasenv = function
193   | [] -> ()
194   | (name, NCic.Decl t) :: tl -> 
195       ppcontext status ~formatter ~sep ~subst ~metasenv tl;
196       F.fprintf formatter "%s: " name;
197       ppterm status ~formatter ~subst ~metasenv ~context:tl t;
198       F.fprintf formatter "%s@;" sep
199   | (name, NCic.Def (bo,ty)) :: tl->
200       ppcontext status ~formatter ~sep ~subst ~metasenv tl;
201       F.fprintf formatter "%s: " name;
202       ppterm status ~formatter ~subst ~metasenv ~context:tl ty;
203       F.fprintf formatter " := ";
204       ppterm status ~formatter ~subst ~metasenv ~context:tl bo;
205       F.fprintf formatter "%s@;" sep
206 ;;
207 let ppcontext status ~formatter ?sep ~subst ~metasenv c =
208   F.fprintf formatter "@[<hov>";
209   ppcontext status ~formatter ?sep ~subst ~metasenv c;
210   F.fprintf formatter "@]";
211 ;;
212
213 let ppmetaattrs =
214  function
215     [] -> ""
216   | attrs ->
217    "(" ^
218     String.concat ","
219      (List.map
220        (function
221          | `IsTerm -> "term"
222          | `IsType -> "type"
223          | `IsSort -> "sort"
224          | `Name n -> "name=" ^ n
225          | `InScope -> "in_scope"
226          | `OutScope n -> "out_scope:" ^ string_of_int n
227        ) attrs) ^
228     ")"
229 ;;
230
231 let rec ppmetasenv status ~formatter ~subst metasenv = function
232   | [] -> ()
233   | (i,(attrs, ctx, ty)) :: tl ->
234       F.fprintf formatter "@[<hov 2>";
235       ppcontext status ~formatter ~sep:"; " ~subst ~metasenv ctx;
236       F.fprintf formatter "@;⊢@;?%d%s :@;" i (ppmetaattrs attrs);
237       ppterm status ~formatter ~metasenv ~subst ~context:ctx ty;
238       F.fprintf formatter "@]@\n";
239       ppmetasenv status ~formatter ~subst metasenv tl
240 ;;
241
242 let ppmetasenv status ~formatter ~subst metasenv =
243  ppmetasenv status ~formatter ~subst metasenv metasenv
244 ;;
245
246 let rec ppsubst status ~formatter ~subst ~metasenv = function
247   | [] -> ()
248   | (i,(attrs, ctx, t, ty)) :: tl ->
249       ppcontext status ~formatter ~sep:"; " ~subst ~metasenv ctx;
250       F.fprintf formatter " ⊢ ?%d%s := " i (ppmetaattrs attrs);
251       ppterm status ~formatter ~metasenv ~subst ~context:ctx t;
252       F.fprintf formatter " : ";
253       ppterm status ~formatter ~metasenv ~subst ~context:ctx ty;
254       F.fprintf formatter "\n";
255       ppsubst status ~formatter ~subst ~metasenv tl
256 ;;
257
258 let ppsubst status ~formatter ~metasenv ?(use_subst=true) subst =
259  let ssubst = if use_subst then subst else [] in
260   ppsubst status ~formatter ~metasenv ~subst:ssubst subst
261 ;;
262
263 let string_of_generated = function
264   | `Generated -> "Generated"
265   | `Provided -> "Provided"
266 ;;
267
268 let string_of_flavour = function
269   | `Axiom -> "axiom"
270   | `Definition -> "definition"
271   | `Fact -> "fact"
272   | `Lemma -> "lemma"
273   | `Theorem -> "theorem"
274   | `Corollary -> "corollary"
275   | `Example -> "example"
276 ;;
277         
278 let string_of_pragma = function
279   | `Coercion _arity -> "Coercion _"
280   | `Elim _sort -> "Elim _"
281   | `Projection -> "Projection"
282   | `InversionPrinciple -> "InversionPrinciple"
283   | `Variant -> "Variant"
284   | `Local -> "Local"
285   | `Regular -> "Regular"
286 ;;
287
288 let string_of_fattrs (g,f,p) = 
289   String.concat ","
290    [ string_of_generated g; string_of_flavour f; string_of_pragma p ]
291 ;;
292
293 let ppobj status ~formatter (u,_,metasenv, subst, o) = 
294   F.fprintf formatter "metasenv:\n";
295   ppmetasenv status ~formatter ~subst metasenv;
296   F.fprintf formatter "\n";
297   F.fprintf formatter "subst:\n";
298   (*ppsubst subst ~formatter ~metasenv;*) F.fprintf formatter "...";
299   F.fprintf formatter "\n";
300   match o with 
301   | NCic.Fixpoint (b, fl, attrs) -> 
302       F.fprintf formatter "{%s}@\n@[<hov 0>" (NUri.string_of_uri u);
303       F.fprintf formatter "@[<hov 2>%s"(if b then "let rec " else "let corec ");
304       HExtlib.list_iter_sep
305        ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
306        (fun (_,name,n,ty,bo) ->
307         F.fprintf formatter "%s on %d :@;" name n;
308         ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
309         F.fprintf formatter "@]@;@[<hov 2>:=@;";
310         ppterm status ~formatter ~metasenv ~subst ~context:[] ~inside_fix:true bo;
311         F.fprintf formatter "@]") fl;
312       F.fprintf formatter "@; %s" (string_of_fattrs attrs);
313       F.fprintf formatter "@]"
314   | NCic.Inductive (b, leftno,tyl, _) -> 
315       F.fprintf formatter "{%s} with %d fixed params@\n@[<hov 0>@[<hov 2>%s"
316        (NUri.string_of_uri u) leftno
317        (if b then "inductive " else "coinductive ");
318       HExtlib.list_iter_sep
319         ~sep:(fun () -> F.fprintf formatter "@\n@[<hov 2>and ")
320        (fun (_,name,ty,cl) ->
321           F.fprintf formatter "%s:@;" name;
322           ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
323           F.fprintf formatter "@]@;@[<hov 3>:=@;";
324           HExtlib.list_iter_sep ~sep:(fun () -> F.fprintf formatter "@;")
325            (fun (_,name,ty) ->
326              F.fprintf formatter "| %s: " name;
327              ppterm status ~formatter ~metasenv ~subst ~context:[] ty;)
328            cl;
329           F.fprintf formatter "@]"
330         ) tyl ;
331         F.fprintf formatter "@]"
332   | NCic.Constant (_,name,None,ty, _) -> 
333      F.fprintf formatter "{%s}@\n@[<hov 2>axiom %s :@;" (NUri.string_of_uri u) name;
334      ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
335      F.fprintf formatter "@]@\n"
336   | NCic.Constant (_,name,Some bo,ty, _) ->
337      F.fprintf formatter "{%s}@\n@[<hov 0>@[<hov 2>definition %s :@;" (NUri.string_of_uri u) name;
338      ppterm status ~formatter ~metasenv ~subst ~context:[] ty;
339      F.fprintf formatter "@]@;@[<hov 2>:=@;";
340      ppterm status ~formatter ~metasenv ~subst ~context:[] bo;
341      F.fprintf formatter "@]@\n@]"
342 ;;
343
344 let ppterm status ~context ~subst ~metasenv ?margin ?inside_fix t = 
345  on_buffer (ppterm status ~context ~subst ~metasenv ?margin ?inside_fix) t
346 ;;
347
348 let ppcontext status ?sep ~subst ~metasenv ctx =
349  on_buffer (ppcontext status ?sep ~subst ~metasenv) ctx
350 ;;
351
352 let ppmetasenv status ~subst metasenv =
353  on_buffer (ppmetasenv status ~subst) metasenv
354 ;;
355
356 let ppsubst status ~metasenv ?use_subst subst =
357  on_buffer (ppsubst status ~metasenv ?use_subst) subst
358 ;;
359
360 let ppobj status obj = on_buffer (ppobj status) obj;;
361
362 class type cstatus =
363   object
364     inherit NCicEnvironment.status
365     inherit NCic.cstatus
366   end
367
368 class status (uid : string option) =
369  object(self)
370   inherit NCicEnvironment.status uid
371   (* this method is meant to be overridden in ApplyTransformation *)
372   method user = uid
373   method ppterm = ppterm self
374   method ppcontext = ppcontext self
375   method ppmetasenv = ppmetasenv self
376   method ppsubst = ppsubst self
377   method ppobj = ppobj self
378  end