1 (* Copyright (C) 2004, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
29 module UM = UriManager
31 exception Meta_not_found of int
32 exception Subst_not_found of int
34 let lookup_meta index metasenv =
36 List.find (fun (index', _, _) -> index = index') metasenv
37 with Not_found -> raise (Meta_not_found index)
39 let lookup_subst n subst =
42 with Not_found -> raise (Subst_not_found n)
44 let exists_meta index = List.exists (fun (index', _, _) -> (index = index'))
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
50 let clean_up_local_context subst metasenv n l =
53 let (cc,_,_) = lookup_subst n subst in cc
54 with Subst_not_found _ ->
56 let (_,cc,_) = lookup_meta n metasenv in cc
57 with Meta_not_found _ -> assert false) in
72 C.Rel m when m > k -> false
76 (fun i t -> i && (match t with None -> true | Some t -> is_closed k t)
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
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)
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
97 let len = List.length fl in
98 let k_plus_len = k + len in
100 (fun (_,_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
103 let len = List.length fl in
104 let k_plus_len = k + len in
106 (fun (_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
112 let rec is_meta_closed =
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) ->
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)
138 not (is_meta_closed ty) || not (is_meta_closed bo))
143 not (is_meta_closed ty) || not (is_meta_closed bo))
147 let xpointer_RE = Str.regexp "\\([^#]+\\)#xpointer(\\(.*\\))"
148 let slash_RE = Str.regexp "/"
150 let term_of_uri uri =
151 let s = UriManager.string_of_uri uri in
153 (if UriManager.uri_is_con uri then
155 else if UriManager.uri_is_var uri then
157 else if not (Str.string_match xpointer_RE s 0) then
158 raise (UriManager.IllFormedUri s)
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] ->
166 (baseuri, int_of_string tyno - 1, int_of_string consno, [])
171 | Not_found -> raise (UriManager.IllFormedUri s)
173 let uri_of_term = function
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)
183 | _ -> raise (Invalid_argument "uri_of_term")
189 (fun term acc -> C.Prod (C.Anonymous, term, acc))
190 terms (C.Sort (C.Type (CicUniv.fresh ())))
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
198 let rec strip_prods n = function
200 | C.Prod (_, _, tgt) when n > 0 -> strip_prods (n-1) tgt
201 | _ -> failwith "not enough prods"
203 let params_of_obj = function
204 | C.Constant (_, _, _, params, _)
205 | C.Variable (_, _, _, params, _)
206 | C.CurrentProof (_, _, _, _, params, _)
207 | C.InductiveDefinition (_, params, _, _) ->
210 let attributes_of_obj = function
211 | C.Constant (_, _, _, _, attributes)
212 | C.Variable (_, _, _, _, attributes)
213 | C.CurrentProof (_, _, _, _, _, attributes)
214 | C.InductiveDefinition (_, _, _, attributes) ->
217 let is_generated obj = List.exists ((=) `Generated) (attributes_of_obj obj)
219 let arity_of_composed_coercion obj =
220 let attrs = attributes_of_obj obj in
222 let tag=List.find (function `Class (`Coercion _) -> true|_->false) attrs in
224 | `Class (`Coercion n) -> n
229 let projections_of_record obj uri =
230 let attrs = attributes_of_obj obj in
232 let tag=List.find (function `Class (`Record _) -> true|_->false) attrs in
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
243 let rec mk_rels howmany from =
246 | _ -> (C.Rel (howmany + from)) :: (mk_rels (howmany-1) from)
257 | C.ALambda (id,_,_,_)
258 | C.ALetIn (id,_,_,_,_)
261 | C.AMutInd (id,_,_,_)
262 | C.AMutConstruct (id,_,_,_,_)
263 | C.AMutCase (id,_,_,_,_,_)
265 | C.ACoFix (id,_,_) -> id
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
272 | (C.Rel _) as t -> t
273 | C.Var (uri,exp_named_subst) ->
274 let uri' = recons uri in
275 let exp_named_subst' =
277 (function (uri,t) ->(recons uri,rehash_term t))
280 C.Var (uri',exp_named_subst')
286 | Some t -> Some (rehash_term t)
290 | C.Sort (C.Type u) ->
291 CicUniv.assert_univ u;
292 C.Sort (C.Type (CicUniv.recons_univ u))
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' =
305 (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
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' =
312 (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
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' =
319 (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
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)
328 (fun (name, i, ty, bo) ->
329 (name, i, rehash_term ty, rehash_term bo))
336 (fun (name, ty, bo) -> (name, rehash_term ty, rehash_term bo))
339 C.CoFix (i, liftedfl)
342 let module C = Cic in
343 let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
345 C.Constant (name,bo,ty,params,attrs) ->
349 | Some bo -> Some (rehash_term bo)
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) ->
357 (function (i,hyps,ty) ->
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,
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) ->
376 | Some bo -> Some (rehash_term bo)
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
384 List.map (function (name, inductive, ty, constructors) ->
389 (function (name, ty) -> name, rehash_term ty)
393 C.InductiveDefinition (tl', params', paramsno, attrs)
395 let rec metas_of_term = function
396 | C.Meta (i, c) -> [i,c]
399 | C.MutInd (_, _, ens)
400 | C.MutConstruct (_, _, _, ens) ->
401 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
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))
413 (List.map (fun (s, i, t1, t2) ->
414 (metas_of_term t1) @ (metas_of_term t2)) il)
417 (List.map (fun (s, t1, t2) ->
418 (metas_of_term t1) @ (metas_of_term t2)) il)
422 module MetaOT = struct
423 type t = int * C.term option list
424 let compare = Pervasives.compare
427 module S = Set.Make(MetaOT)
429 let rec metas_of_term_set = function
430 | C.Meta (i, c) -> S.singleton (i,c)
433 | C.MutInd (_, _, ens)
434 | C.MutConstruct (_, _, _, ens) ->
436 (fun s (_,t) -> S.union s (metas_of_term_set 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))
446 (fun s t -> S.union s (metas_of_term_set t))
448 | C.MutCase (uri, i, s, t, l) ->
450 (S.union (metas_of_term_set s) (metas_of_term_set t))
452 (fun s t -> S.union s (metas_of_term_set t))
456 (fun s (_,_,t1,t2) ->
457 S.union s (S.union (metas_of_term_set t1) (metas_of_term_set t2))))
462 S.union s (S.union (metas_of_term_set t1) (metas_of_term_set t2))))
467 let metas_of_term_set t =
468 let s = metas_of_term_set t in
472 (* syntactic_equality up to the *)
473 (* distinction between fake dependent products *)
474 (* and non-dependent products, alfa-conversion *)
475 let alpha_equivalence =
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') ->
487 | C.Lambda (_,s,t), C.Lambda (_,s',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' ->
494 (fun b t1 t2 -> b && aux t1 t2) true l l'
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' &&
512 (fun b t1 t2 -> b && aux t1 t2) true pl pl'
514 Invalid_argument _ -> false)
515 | C.Fix (i,fl), C.Fix (i',fl') ->
519 (fun b (_,i,ty,bo) (_,i',ty',bo') ->
520 b && i = i' && aux ty ty' && aux bo bo'
523 Invalid_argument _ -> false)
524 | C.CoFix (i,fl), C.CoFix (i',fl') ->
528 (fun b (_,ty,bo) (_,ty',bo') ->
529 b && aux ty ty' && aux bo bo'
532 Invalid_argument _ -> false)
533 | C.Meta (i, subst), C.Meta (i', subst') ->
537 (fun b xt xt' -> match xt,xt' with
538 | Some t, Some t' -> b && aux t t'
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
549 | _,_ -> false (* we already know that t != t' *)
550 and aux_exp_named_subst exp_named_subst1 exp_named_subst2 =
553 (fun b (uri1,t1) (uri2,t2) ->
554 b && UriManager.eq uri1 uri2 && aux t1 t2
555 ) true exp_named_subst1 exp_named_subst2
557 Invalid_argument _ -> false
562 let rec sober_term c g = function
568 | C.MutConstruct (_, _, _, xnss)
569 | C.MutInd (_, _, xnss) -> sober_xnss c g xnss
570 | C.Meta (_, xss) -> sober_xss c g xss
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
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
586 let map g (_, t) = sober_term c g t in
591 | Some t -> sober_term c g t
595 let map g (_, _, t, v) = sober_term c (sober_term c g t) v in
598 let map g (_, t, v) = sober_term c (sober_term c g t) v in
601 sober_term c (fun b -> b) t true
603 (* raw cic prettyprinter ****************************************************)
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
611 out so; aux l; out sc
613 let abst s w = Some (s, C.Decl w)
615 let abbr s v w = Some (s, C.Def (v, w))
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"
623 let pp_name out = function
625 | C.Anonymous -> out "_"
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))
633 let pp_implict out = function
635 | Some `Closed -> out "?[Closed]"
636 | Some `Type -> out "?[Type]"
637 | Some `Hole -> out "?[Hole]"
640 out (Printf.sprintf "%s<%s>" (UM.name_of_uri a) (UM.string_of_uri a))
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
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
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);
656 | C.MutConstruct (a, m, n, xss) ->
657 pp_uri out a; out (Printf.sprintf "/%u/%u" m n);
660 out "type "; pp_term out e c w; out " contains "; pp_term out e c v
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
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;
685 xiter out "let rec " " and " " in " map fs; pp_rel out c' (succ i)
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
690 out s; pp_term out e c' v;
691 out " of "; pp_term out e c w;
693 xiter out "let corec " " and " " in " map fs; pp_rel out c' (succ i)
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
700 out (Printf.sprintf "%u" i)
702 let pp_attrs out attrs =
706 xiter out "[" "; " "] " map attrs
708 let pp_pars out pars =
709 xiter out " (" ", " ")\n" (pp_uri out) pars
711 let pp_point out point =
712 if point then out "ind " else out "coind "
714 let pp_constructor out (s, w) =
715 out s; out " of "; pp_term out [] [] w
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
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) ->