]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/cic/cicUtil.ml
ng_disambiguation ng_kernel ng_refiner disambiguation: svn:ignore fixed
[helm.git] / helm / software / components / cic / cicUtil.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 module C  = Cic
29 module UM = UriManager
30
31 exception Meta_not_found of int
32 exception Subst_not_found of int
33
34 let lookup_meta index metasenv =
35   try
36     List.find (fun (index', _, _) -> index = index') metasenv
37   with Not_found -> raise (Meta_not_found index)
38
39 let lookup_subst n subst =
40   try
41     List.assoc n subst
42   with Not_found -> raise (Subst_not_found n)
43
44 let exists_meta index = List.exists (fun (index', _, _) -> (index = index'))
45
46 (* clean_up_meta take a substitution, a metasenv a meta_inex and a local
47 context l and clean up l with respect to the hidden hipothesis in the 
48 canonical context *)
49
50 let clean_up_local_context subst metasenv n l =
51   let cc =
52     (try
53        let (cc,_,_) = lookup_subst n subst in cc
54      with Subst_not_found _ ->
55        try
56          let (_,cc,_) = lookup_meta n metasenv in cc
57        with Meta_not_found _ -> assert false) in
58   (try 
59      List.map2
60        (fun t1 t2 ->
61           match t1,t2 with 
62               None , _ -> None
63             | _ , t -> t) cc l
64    with 
65        Invalid_argument _ -> 
66          assert false)
67
68 let is_closed =
69  let module C = Cic in
70  let rec is_closed k =
71   function
72       C.Rel m when m > k -> false
73     | C.Rel m -> true
74     | C.Meta (_,l) ->
75        List.fold_left
76         (fun i t -> i && (match t with None -> true | Some t -> is_closed k t)
77         ) true l
78     | C.Sort _ -> true
79     | C.Implicit _ -> assert false
80     | C.Cast (te,ty) -> is_closed k te && is_closed k ty
81     | C.Prod (name,so,dest) -> is_closed k so && is_closed (k+1) dest
82     | C.Lambda (_,so,dest) -> is_closed k so && is_closed (k+1) dest
83     | C.LetIn (_,so,ty,dest) ->
84        is_closed k so && is_closed k ty && is_closed (k+1) dest
85     | C.Appl l ->
86        List.fold_right (fun x i -> i && is_closed k x) l true
87     | C.Var (_,exp_named_subst)
88     | C.Const (_,exp_named_subst)
89     | C.MutInd (_,_,exp_named_subst)
90     | C.MutConstruct (_,_,_,exp_named_subst) ->
91        List.fold_right (fun (_,x) i -> i && is_closed k x)
92         exp_named_subst true
93     | C.MutCase (_,_,out,te,pl) ->
94        is_closed k out && is_closed k te &&
95         List.fold_right (fun x i -> i && is_closed k x) pl true
96     | C.Fix (_,fl) ->
97        let len = List.length fl in
98         let k_plus_len = k + len in
99          List.fold_right
100           (fun (_,_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
101           ) fl true
102     | C.CoFix (_,fl) ->
103        let len = List.length fl in
104         let k_plus_len = k + len in
105          List.fold_right
106           (fun (_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
107           ) fl true
108 in 
109  is_closed 0
110 ;;
111
112 let rec is_meta_closed =
113   function
114       C.Rel _ -> true
115     | C.Meta _ -> false
116     | C.Sort _ -> true
117     | C.Implicit _ -> assert false
118     | C.Cast (te,ty) -> is_meta_closed te && is_meta_closed ty
119     | C.Prod (name,so,dest) -> is_meta_closed so && is_meta_closed dest
120     | C.Lambda (_,so,dest) -> is_meta_closed so && is_meta_closed dest
121     | C.LetIn (_,so,ty,dest) ->
122        is_meta_closed so &&
123        is_meta_closed ty &&
124        is_meta_closed dest
125     | C.Appl l ->
126        not (List.exists (fun x -> not (is_meta_closed x)) l)
127     | C.Var (_,exp_named_subst)
128     | C.Const (_,exp_named_subst)
129     | C.MutInd (_,_,exp_named_subst)
130     | C.MutConstruct (_,_,_,exp_named_subst) ->
131        not (List.exists (fun (_,x) -> not (is_meta_closed x)) exp_named_subst)
132     | C.MutCase (_,_,out,te,pl) ->
133        is_meta_closed out && is_meta_closed te &&
134         not (List.exists (fun x -> not (is_meta_closed x)) pl)
135     | C.Fix (_,fl) ->
136         not (List.exists 
137               (fun (_,_,ty,bo) -> 
138                   not (is_meta_closed ty) || not (is_meta_closed bo)) 
139               fl)
140     | C.CoFix (_,fl) ->
141         not (List.exists 
142               (fun (_,ty,bo) -> 
143                   not (is_meta_closed ty) || not (is_meta_closed bo)) 
144               fl)
145 ;;
146
147 let xpointer_RE = Str.regexp "\\([^#]+\\)#xpointer(\\(.*\\))"
148 let slash_RE = Str.regexp "/"
149
150 let term_of_uri uri =
151   let s = UriManager.string_of_uri uri in
152   try
153     (if UriManager.uri_is_con uri then
154       C.Const (uri, [])
155     else if UriManager.uri_is_var uri then
156       C.Var (uri, [])
157     else if not (Str.string_match xpointer_RE s 0) then
158       raise (UriManager.IllFormedUri s)
159     else
160       let (baseuri,xpointer) = (Str.matched_group 1 s, Str.matched_group 2 s) in
161       let baseuri = UriManager.uri_of_string baseuri in
162       (match Str.split slash_RE xpointer with
163       | [_; tyno] -> C.MutInd (baseuri, int_of_string tyno - 1, [])
164       | [_; tyno; consno] ->
165           C.MutConstruct
166             (baseuri, int_of_string tyno - 1, int_of_string consno, [])
167       | _ -> raise Exit))
168   with
169   | Exit
170   | Failure _
171   | Not_found -> raise (UriManager.IllFormedUri s)
172
173 let uri_of_term = function
174   | C.Const (uri, _)
175   | C.Var (uri, _) -> uri
176   | C.MutInd (baseuri, tyno, _) ->
177      UriManager.uri_of_string
178       (Printf.sprintf "%s#xpointer(1/%d)" (UriManager.string_of_uri baseuri) (tyno+1))
179   | C.MutConstruct (baseuri, tyno, consno, _) ->
180      UriManager.uri_of_string
181       (Printf.sprintf "%s#xpointer(1/%d/%d)" (UriManager.string_of_uri baseuri)
182         (tyno + 1) consno)
183   | _ -> raise (Invalid_argument "uri_of_term")
184
185
186 (*
187 let pack terms =
188   List.fold_right
189     (fun term acc -> C.Prod (C.Anonymous, term, acc))
190     terms (C.Sort (C.Type (CicUniv.fresh ())))
191
192 let rec unpack = function
193   | C.Prod (C.Anonymous, term, C.Sort (C.Type _)) -> [term]
194   | C.Prod (C.Anonymous, term, tgt) -> term :: unpack tgt
195   | _ -> assert false
196 *)
197
198 let rec strip_prods n = function
199   | t when n = 0 -> t
200   | C.Prod (_, _, tgt) when n > 0 -> strip_prods (n-1) tgt
201   | _ -> failwith "not enough prods"
202
203 let params_of_obj = function
204   | C.Constant (_, _, _, params, _)
205   | C.Variable (_, _, _, params, _)
206   | C.CurrentProof (_, _, _, _, params, _)
207   | C.InductiveDefinition (_, params, _, _) ->
208       params
209
210 let attributes_of_obj = function
211   | C.Constant (_, _, _, _, attributes)
212   | C.Variable (_, _, _, _, attributes)
213   | C.CurrentProof (_, _, _, _, _, attributes)
214   | C.InductiveDefinition (_, _, _, attributes) ->
215       attributes
216
217 let is_generated obj = List.exists ((=) `Generated) (attributes_of_obj obj)
218
219 let arity_of_composed_coercion obj =
220   let attrs = attributes_of_obj obj in
221   try
222     let tag=List.find (function `Class (`Coercion _) -> true|_->false) attrs in
223     match tag with
224     |  `Class (`Coercion n) -> n
225     | _-> assert false 
226   with Not_found -> 0
227 ;;
228
229 let projections_of_record obj uri =
230   let attrs = attributes_of_obj obj in
231   try
232     let tag=List.find (function `Class (`Record _) -> true|_->false) attrs in
233     match tag with
234     |  `Class (`Record l) -> 
235          List.map (fun (name,_,_) ->
236            let buri = UriManager.buri_of_uri uri in
237            let puri = UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con") in
238            puri) l
239     | _-> assert false 
240   with Not_found -> []
241 ;;
242       
243 let rec mk_rels howmany from =
244   match howmany with 
245   | 0 -> []
246   | _ -> (C.Rel (howmany + from)) :: (mk_rels (howmany-1) from)
247
248 let id_of_annterm =
249   function
250   | C.ARel (id,_,_,_)
251   | C.AVar (id,_,_)
252   | C.AMeta (id,_,_)
253   | C.ASort (id,_)
254   | C.AImplicit (id,_)
255   | C.ACast (id,_,_)
256   | C.AProd (id,_,_,_)
257   | C.ALambda (id,_,_,_)
258   | C.ALetIn (id,_,_,_,_)
259   | C.AAppl (id,_)
260   | C.AConst (id,_,_)
261   | C.AMutInd (id,_,_,_)
262   | C.AMutConstruct (id,_,_,_,_)
263   | C.AMutCase (id,_,_,_,_,_)
264   | C.AFix (id,_,_)
265   | C.ACoFix (id,_,_) -> id
266
267
268 let rec rehash_term =
269   let module C = Cic in
270   let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
271   function
272    | (C.Rel _) as t -> t
273    | C.Var (uri,exp_named_subst) ->
274       let uri' = recons uri in
275       let exp_named_subst' =
276        List.map
277         (function (uri,t) ->(recons uri,rehash_term t)) 
278          exp_named_subst
279       in
280        C.Var (uri',exp_named_subst')
281    | C.Meta (i,l) ->
282       let l' =
283        List.map
284         (function
285             None -> None
286           | Some t -> Some (rehash_term t)
287         ) l
288       in
289        C.Meta(i,l')
290    | C.Sort (C.Type u) -> 
291        CicUniv.assert_univ u;
292        C.Sort (C.Type (CicUniv.recons_univ u))
293    | C.Sort _ as t -> t
294    | C.Implicit _ as t -> t
295    | C.Cast (te,ty) -> C.Cast (rehash_term te, rehash_term ty)
296    | C.Prod (n,s,t) -> C.Prod (n, rehash_term s, rehash_term t)
297    | C.Lambda (n,s,t) -> C.Lambda (n, rehash_term s, rehash_term t)
298    | C.LetIn (n,s,ty,t) ->
299       C.LetIn (n, rehash_term s, rehash_term ty, rehash_term t)
300    | C.Appl l -> C.Appl (List.map rehash_term l)
301    | C.Const (uri,exp_named_subst) ->
302       let uri' = recons uri in
303       let exp_named_subst' = 
304        List.map
305         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
306       in
307        C.Const (uri',exp_named_subst')
308    | C.MutInd (uri,tyno,exp_named_subst) ->
309       let uri' = recons uri in
310       let exp_named_subst' = 
311        List.map
312         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
313       in
314        C.MutInd (uri',tyno,exp_named_subst')
315    | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
316       let uri' = recons uri in
317       let exp_named_subst' = 
318        List.map
319         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
320       in
321        C.MutConstruct (uri',tyno,consno,exp_named_subst')
322    | C.MutCase (uri,i,outty,t,pl) ->
323       C.MutCase (recons uri, i, rehash_term outty, rehash_term t,
324        List.map rehash_term pl)
325    | C.Fix (i, fl) ->
326       let liftedfl =
327        List.map
328         (fun (name, i, ty, bo) ->
329           (name, i, rehash_term ty, rehash_term bo))
330          fl
331       in
332        C.Fix (i, liftedfl)
333    | C.CoFix (i, fl) ->
334       let liftedfl =
335        List.map
336         (fun (name, ty, bo) -> (name, rehash_term ty, rehash_term bo))
337          fl
338       in
339        C.CoFix (i, liftedfl)
340
341 let rehash_obj =
342  let module C = Cic in
343  let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
344  function 
345    C.Constant (name,bo,ty,params,attrs) ->
346      let bo' =
347        match bo with
348          None -> None
349        | Some bo -> Some (rehash_term bo)
350      in
351      let ty' = rehash_term ty in
352      let params' = List.map recons params in
353      C.Constant (name, bo', ty', params',attrs)
354  | C.CurrentProof (name,conjs,bo,ty,params,attrs) ->
355      let conjs' =
356        List.map
357          (function (i,hyps,ty) ->
358            (i,
359            List.map (function
360                None -> None
361              | Some (name,C.Decl t) ->
362                  Some (name,C.Decl (rehash_term t))
363              | Some (name,C.Def (bo,ty)) ->
364                  Some (name,C.Def (rehash_term bo, rehash_term ty))) hyps,
365            rehash_term ty))
366          conjs
367      in
368      let bo' = rehash_term bo in
369      let ty' = rehash_term ty in
370      let params' = List.map recons params in
371      C.CurrentProof (name, conjs', bo', ty', params',attrs)
372  | C.Variable (name,bo,ty,params,attrs) ->
373      let bo' =
374        match bo with
375          None -> None
376        | Some bo -> Some (rehash_term bo)
377      in
378      let ty' = rehash_term ty in
379      let params' = List.map recons params in
380      C.Variable (name, bo', ty', params',attrs)
381  | C.InductiveDefinition (tl,params,paramsno,attrs) ->
382      let params' = List.map recons params in
383      let tl' =
384        List.map (function (name, inductive, ty, constructors) ->
385          name,
386          inductive,
387          rehash_term ty,
388          (List.map
389            (function (name, ty) -> name, rehash_term ty)
390            constructors))
391          tl
392      in
393      C.InductiveDefinition (tl', params', paramsno, attrs)
394
395 let rec metas_of_term = function
396   | C.Meta (i, c) -> [i,c]
397   | C.Var (_, ens) 
398   | C.Const (_, ens) 
399   | C.MutInd (_, _, ens) 
400   | C.MutConstruct (_, _, _, ens) ->
401       List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
402   | C.Cast (s, t)
403   | C.Prod (_, s, t)
404   | C.Lambda (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
405   | C.LetIn (_, s, ty, t) ->
406      (metas_of_term s) @ (metas_of_term ty) @ (metas_of_term t)
407   | C.Appl l -> List.flatten (List.map metas_of_term l)
408   | C.MutCase (uri, i, s, t, l) ->
409       (metas_of_term s) @ (metas_of_term t) @
410         (List.flatten (List.map metas_of_term l))
411   | C.Fix (i, il) ->
412       List.flatten
413         (List.map (fun (s, i, t1, t2) ->
414                      (metas_of_term t1) @ (metas_of_term t2)) il)
415   | C.CoFix (i, il) ->
416       List.flatten
417         (List.map (fun (s, t1, t2) ->
418                      (metas_of_term t1) @ (metas_of_term t2)) il)
419   | _ -> []
420 ;;      
421
422 module MetaOT = struct
423   type t = int * C.term option list
424   let compare = Pervasives.compare
425 end
426
427 module S = Set.Make(MetaOT)
428
429 let rec metas_of_term_set = function
430   | C.Meta (i, c) -> S.singleton (i,c)
431   | C.Var (_, ens) 
432   | C.Const (_, ens) 
433   | C.MutInd (_, _, ens) 
434   | C.MutConstruct (_, _, _, ens) ->
435       List.fold_left 
436         (fun s (_,t) -> S.union s (metas_of_term_set t)) 
437         S.empty ens
438   | C.Cast (s, t)
439   | C.Prod (_, s, t)
440   | C.Lambda (_, s, t) -> S.union (metas_of_term_set s) (metas_of_term_set t)
441   | C.LetIn (_, s, ty, t) ->
442      S.union (metas_of_term_set s)
443       (S.union (metas_of_term_set ty) (metas_of_term_set t))
444   | C.Appl l -> 
445       List.fold_left 
446         (fun s t -> S.union s (metas_of_term_set t)) 
447         S.empty l
448   | C.MutCase (uri, i, s, t, l) ->
449       S.union 
450         (S.union (metas_of_term_set s)  (metas_of_term_set t))
451         (List.fold_left 
452           (fun s t -> S.union s (metas_of_term_set t)) 
453           S.empty l)
454   | C.Fix (_, il) ->
455       (List.fold_left 
456         (fun s (_,_,t1,t2) -> 
457           S.union s (S.union (metas_of_term_set t1) (metas_of_term_set t2))))
458         S.empty il
459   | C.CoFix (i, il) ->
460       (List.fold_left 
461         (fun s (_,t1,t2) -> 
462           S.union s (S.union (metas_of_term_set t1) (metas_of_term_set t2))))
463         S.empty il
464   | _ -> S.empty
465 ;;      
466
467 let metas_of_term_set t = 
468   let s = metas_of_term_set t in
469   S.elements s
470 ;;
471
472 (* syntactic_equality up to the                 *)
473 (* distinction between fake dependent products  *)
474 (* and non-dependent products, alfa-conversion  *)
475 let alpha_equivalence =
476   let rec aux t t' =
477    if t = t' then true
478    else
479     match t,t' with
480        C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2) ->
481         UriManager.eq uri1 uri2 &&
482          aux_exp_named_subst exp_named_subst1 exp_named_subst2
483      | C.Cast (te,ty), C.Cast (te',ty') ->
484         aux te te' && aux ty ty'
485      | C.Prod (_,s,t), C.Prod (_,s',t') ->
486         aux s s' && aux t t'
487      | C.Lambda (_,s,t), C.Lambda (_,s',t') ->
488         aux s s' && aux t t'
489      | C.LetIn (_,s,ty,t), C.LetIn(_,s',ty',t') ->
490         aux s s' && aux ty ty' && aux t t'
491      | C.Appl l, C.Appl l' when List.length l = List.length l' ->
492         (try
493           List.fold_left2
494            (fun b t1 t2 -> b && aux t1 t2) true l l'
495          with
496           Invalid_argument _ -> false)
497      | C.Const (uri,exp_named_subst1), C.Const (uri',exp_named_subst2) ->
498         UriManager.eq uri uri' &&
499          aux_exp_named_subst exp_named_subst1 exp_named_subst2
500      | C.MutInd (uri,i,exp_named_subst1), C.MutInd (uri',i',exp_named_subst2) ->
501         UriManager.eq uri uri' && i = i' &&
502          aux_exp_named_subst exp_named_subst1 exp_named_subst2
503      | C.MutConstruct (uri,i,j,exp_named_subst1),
504        C.MutConstruct (uri',i',j',exp_named_subst2) ->
505         UriManager.eq uri uri' && i = i' && j = j' &&
506          aux_exp_named_subst exp_named_subst1 exp_named_subst2
507      | C.MutCase (sp,i,outt,t,pl), C.MutCase (sp',i',outt',t',pl') ->
508         UriManager.eq sp sp' && i = i' &&
509          aux outt outt' && aux t t' &&
510           (try
511             List.fold_left2
512              (fun b t1 t2 -> b && aux t1 t2) true pl pl'
513            with
514             Invalid_argument _ -> false)
515      | C.Fix (i,fl), C.Fix (i',fl') ->
516         i = i' &&
517         (try
518           List.fold_left2
519            (fun b (_,i,ty,bo) (_,i',ty',bo') ->
520              b && i = i' && aux ty ty' && aux bo bo'
521            ) true fl fl'
522          with
523           Invalid_argument _ -> false)
524      | C.CoFix (i,fl), C.CoFix (i',fl') ->
525         i = i' &&
526         (try
527           List.fold_left2
528            (fun b (_,ty,bo) (_,ty',bo') ->
529              b && aux ty ty' && aux bo bo'
530            ) true fl fl'
531          with
532           Invalid_argument _ -> false)
533      | C.Meta (i, subst), C.Meta (i', subst') ->
534         i = i' &&
535         (try
536           List.fold_left2
537            (fun b xt xt' -> match xt,xt' with
538              | Some t, Some t' -> b && aux t t'
539              | _               -> b
540            ) true subst subst'
541          with
542           Invalid_argument _ -> false)
543      | C.Appl [t], t' | t, C.Appl [t'] -> assert false
544 (* FG: are we _really_ sure of these?      
545      | C.Sort (C.Type u), C.Sort (C.Type u') -> u = u' 
546      | C.Implicit a, C.Implicit a' -> a = a'
547    we insert an unused variable below to genarate a warning at compile time
548 *)     
549      | _,_ -> false (* we already know that t != t' *)
550   and aux_exp_named_subst exp_named_subst1 exp_named_subst2 =
551    try
552      List.fold_left2
553       (fun b (uri1,t1) (uri2,t2) ->
554         b && UriManager.eq uri1 uri2 && aux t1 t2
555       ) true exp_named_subst1 exp_named_subst2
556     with
557      Invalid_argument _ -> false
558   in
559    aux
560
561 let is_sober c t =
562    let rec sober_term c g = function
563       | C.Rel _ 
564       | C.Sort _  
565       | C.Implicit _                    -> g      
566       | C.Const (_, xnss) 
567       | C.Var (_, xnss) 
568       | C.MutConstruct (_, _, _, xnss)
569       | C.MutInd (_, _, xnss)           -> sober_xnss c g xnss
570       | C.Meta (_, xss)                 -> sober_xss c g xss
571       | C.Lambda (_, v, t)
572       | C.Prod (_, v, t)
573       | C.Cast (t, v)                   ->
574          sober_term c (sober_term c g t) v
575       | C.LetIn (_, v, ty, t)           ->
576          sober_term c (sober_term c (sober_term c g t) ty) v
577       | C.Appl []                       
578       | C.Appl [_]                      -> fun b -> false
579       | C.Appl ts                       -> sober_terms c g ts
580       | C.MutCase (_, _, t, v, ts)      -> 
581          sober_terms c (sober_term c (sober_term c g t) v) ts
582       | C.Fix (_, ifs)                  -> sober_ifs c g ifs
583       | C.CoFix (_, cifs)               -> sober_cifs c g cifs
584    and sober_terms c g = List.fold_left (sober_term c) g
585    and sober_xnss c g =
586       let map g (_, t) = sober_term c g t in
587       List.fold_left map g
588    and sober_xss c g =
589       let map g = function 
590          | None   -> g
591          | Some t -> sober_term c g t
592       in
593       List.fold_left map g
594    and sober_ifs c g =
595       let map g (_, _, t, v) = sober_term c (sober_term c g t) v in
596       List.fold_left map g
597    and sober_cifs c g =
598       let map g (_, t, v) = sober_term c (sober_term c g t) v in
599       List.fold_left map g
600    in 
601    sober_term c (fun b -> b) t true
602
603 (* raw cic prettyprinter ****************************************************)
604
605 let xiter out so ss sc map l =
606    let rec aux = function
607       | hd :: tl when tl <> [] -> map hd; out ss; aux tl
608       | hd :: tl               -> map hd; aux tl
609       | []                     -> ()
610    in
611    out so; aux l; out sc
612
613 let abst s w = Some (s, C.Decl w)
614
615 let abbr s v w = Some (s, C.Def (v, w))
616
617 let pp_sort out = function
618    | C.Type _  -> out "*Type"
619    | C.Prop    -> out "*Prop"
620    | C.CProp _ -> out "*CProp"
621    | C.Set     -> out "*Set"
622
623 let pp_name out = function
624    | C.Name s    -> out s
625    | C.Anonymous -> out "_"
626
627 let pp_rel out c i =
628    try match List.nth c (pred i) with
629       | None           -> out (Printf.sprintf "%u[?]" i)
630       | Some (s, _)    -> out (Printf.sprintf "%u[" i); pp_name out s; out "]"
631    with Failure "nth" -> out (Printf.sprintf "%u[%i]" i (List.length c - i))
632
633 let pp_implict out = function
634    | None         -> out "?"
635    | Some `Closed -> out "?[Closed]" 
636    | Some `Type   -> out "?[Type]"
637    | Some `Hole   -> out "?[Hole]"
638
639 let pp_uri out a =
640    out (Printf.sprintf "%s<%s>" (UM.name_of_uri a) (UM.string_of_uri a)) 
641
642 let rec pp_term out e c = function
643    | C.Sort h                      -> pp_sort out h
644    | C.Rel i                       -> pp_rel out c i
645    | C.Implicit x                  -> pp_implict out x
646    | C.Meta (i, iss)               ->
647       let map = function None   -> out "_" | Some v -> pp_term out e c v in
648       out (Printf.sprintf "?%u" i); xiter out "[" "; " "]" map iss
649    | C.Var (a, xss)              ->
650       pp_uri out a; pp_xss out e c xss
651    | C.Const (a, xss)              ->
652       pp_uri out a; pp_xss out e c xss
653    | C.MutInd (a, m, xss)          ->
654       pp_uri out a; out (Printf.sprintf "/%u" m);
655       pp_xss out e c xss
656    | C.MutConstruct (a, m, n, xss) ->
657       pp_uri out a; out (Printf.sprintf "/%u/%u" m n);
658       pp_xss out e c xss
659    | C.Cast (v, w)                 ->
660       out "type "; pp_term out e c w; out " contains "; pp_term out e c v
661    | C.Appl vs                     ->
662       xiter out "(" " @ " ")" (pp_term out e c) vs
663    | C.MutCase (a, m, w, v, vs)    ->
664       out "match "; pp_term out e c v;
665       out " of "; pp_uri out a; out (Printf.sprintf "/%u" m);
666       out " to "; pp_term out e c w;
667       xiter out " cases " " | " "" (pp_term out e c) vs
668    | C.Prod (s, w, t)             ->
669       out "forall "; pp_name out s; out " of "; pp_term out e c w;
670       out " in "; pp_term out e (abst s w :: c) t
671    | C.Lambda (s, w, t)            ->
672       out "fun "; pp_name out s; out " of "; pp_term out e c w;
673       out " in "; pp_term out e (abst s w :: c) t
674    | C.LetIn (s, v, w, t)          ->
675       out "let "; pp_name out s; 
676       out " def "; pp_term out e c v; out " of "; pp_term out e c w;
677       out " in "; pp_term out e (abbr s v w :: c) t
678    | C.Fix (i, fs)                 ->
679       let map c (s, _, w, v) = abbr (C.Name s) v w :: c in
680       let c' = List.fold_left map c fs in
681       let map (s, i, w, v) =
682          out (Printf.sprintf "%s[%u] def " s i); pp_term out e c' v; 
683          out " of "; pp_term out e c w;
684       in
685       xiter out "let rec " " and " " in " map fs; pp_rel out c' (succ i)
686    | C.CoFix (i, fs)                 ->
687       let map c (s, w, v) = abbr (C.Name s) v w :: c in
688       let c' = List.fold_left map c fs in
689       let map (s, w, v) =
690          out s; pp_term out e c' v; 
691          out " of "; pp_term out e c w;
692       in
693       xiter out "let corec " " and " " in " map fs; pp_rel out c' (succ i)
694
695 and pp_xss out e c xss = 
696    let map (a, v) = pp_uri out a; out " <- "; pp_term out e c v in
697    xiter out "[" "; " "]" map xss 
698
699 let pp_int out i =
700    out (Printf.sprintf "%u" i)
701
702 let pp_attrs out attrs = 
703    let map = function
704       | _ -> ()
705    in
706    xiter out "[" "; " "] " map attrs 
707    
708 let pp_pars out pars = 
709    xiter out " (" ", " ")\n" (pp_uri out) pars 
710
711 let pp_point out point =
712    if point then out "ind " else out "coind "
713
714 let pp_constructor out (s, w) =
715    out s; out " of "; pp_term out [] [] w
716
717 let pp_definition out (s, point, w, ts) =
718    out "let "; pp_point out point; out s; out " of "; pp_term out [] [] w;  
719    xiter out "\ndef " "\nor " "" (pp_constructor out) ts
720
721 let pp_obj out = function
722    | C.Constant (s, None, u, pars, attrs)           ->
723       out "fun "; pp_attrs out attrs; out s; pp_pars out pars;
724       out " of "; pp_term out [] [] u
725    | C.Constant (s, Some t, u, pars, attrs)         ->
726       out "let "; pp_attrs out attrs; out s; pp_pars out pars;
727       out " def "; pp_term out [] [] t; out " of "; pp_term out [] [] u
728    | C.Variable (s, None, u, pars, attrs)           ->
729       out "local fun "; pp_attrs out attrs; out s; pp_pars out pars;
730       out " of "; pp_term out [] [] u
731    | C.Variable (s, Some t, u, pars, attrs)         ->
732       out "local let "; pp_attrs out attrs; out s; pp_pars out pars;
733       out " def "; pp_term out [] [] t; out " of "; pp_term out [] [] u
734    | C.InductiveDefinition (us, pars, lpsno, attrs) ->
735       out "Inductive "; pp_attrs out attrs; pp_int out lpsno; pp_pars out pars;
736       xiter out "" "\n" "" (pp_definition out) us
737    | C.CurrentProof (s, e, t, u, pars, attrs)       ->
738       out "Current Proof" 
739