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 projections_of_record obj uri =
220 let attrs = attributes_of_obj obj in
222 let tag=List.find (function `Class (`Record _) -> true|_->false) attrs in
224 | `Class (`Record l) ->
225 List.map (fun (name,_,_) ->
226 let buri = UriManager.buri_of_uri uri in
227 let puri = UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con") in
233 let rec mk_rels howmany from =
236 | _ -> (C.Rel (howmany + from)) :: (mk_rels (howmany-1) from)
247 | C.ALambda (id,_,_,_)
248 | C.ALetIn (id,_,_,_,_)
251 | C.AMutInd (id,_,_,_)
252 | C.AMutConstruct (id,_,_,_,_)
253 | C.AMutCase (id,_,_,_,_,_)
255 | C.ACoFix (id,_,_) -> id
258 let rec rehash_term =
259 let module C = Cic in
260 let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
262 | (C.Rel _) as t -> t
263 | C.Var (uri,exp_named_subst) ->
264 let uri' = recons uri in
265 let exp_named_subst' =
267 (function (uri,t) ->(recons uri,rehash_term t))
270 C.Var (uri',exp_named_subst')
276 | Some t -> Some (rehash_term t)
280 | C.Sort (C.Type u) ->
281 CicUniv.assert_univ u;
282 C.Sort (C.Type (CicUniv.recons_univ u))
284 | C.Implicit _ as t -> t
285 | C.Cast (te,ty) -> C.Cast (rehash_term te, rehash_term ty)
286 | C.Prod (n,s,t) -> C.Prod (n, rehash_term s, rehash_term t)
287 | C.Lambda (n,s,t) -> C.Lambda (n, rehash_term s, rehash_term t)
288 | C.LetIn (n,s,ty,t) ->
289 C.LetIn (n, rehash_term s, rehash_term ty, rehash_term t)
290 | C.Appl l -> C.Appl (List.map rehash_term l)
291 | C.Const (uri,exp_named_subst) ->
292 let uri' = recons uri in
293 let exp_named_subst' =
295 (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
297 C.Const (uri',exp_named_subst')
298 | C.MutInd (uri,tyno,exp_named_subst) ->
299 let uri' = recons uri in
300 let exp_named_subst' =
302 (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
304 C.MutInd (uri',tyno,exp_named_subst')
305 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
306 let uri' = recons uri in
307 let exp_named_subst' =
309 (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
311 C.MutConstruct (uri',tyno,consno,exp_named_subst')
312 | C.MutCase (uri,i,outty,t,pl) ->
313 C.MutCase (recons uri, i, rehash_term outty, rehash_term t,
314 List.map rehash_term pl)
318 (fun (name, i, ty, bo) ->
319 (name, i, rehash_term ty, rehash_term bo))
326 (fun (name, ty, bo) -> (name, rehash_term ty, rehash_term bo))
329 C.CoFix (i, liftedfl)
332 let module C = Cic in
333 let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
335 C.Constant (name,bo,ty,params,attrs) ->
339 | Some bo -> Some (rehash_term bo)
341 let ty' = rehash_term ty in
342 let params' = List.map recons params in
343 C.Constant (name, bo', ty', params',attrs)
344 | C.CurrentProof (name,conjs,bo,ty,params,attrs) ->
347 (function (i,hyps,ty) ->
351 | Some (name,C.Decl t) ->
352 Some (name,C.Decl (rehash_term t))
353 | Some (name,C.Def (bo,ty)) ->
354 Some (name,C.Def (rehash_term bo, rehash_term ty))) hyps,
358 let bo' = rehash_term bo in
359 let ty' = rehash_term ty in
360 let params' = List.map recons params in
361 C.CurrentProof (name, conjs', bo', ty', params',attrs)
362 | C.Variable (name,bo,ty,params,attrs) ->
366 | Some bo -> Some (rehash_term bo)
368 let ty' = rehash_term ty in
369 let params' = List.map recons params in
370 C.Variable (name, bo', ty', params',attrs)
371 | C.InductiveDefinition (tl,params,paramsno,attrs) ->
372 let params' = List.map recons params in
374 List.map (function (name, inductive, ty, constructors) ->
379 (function (name, ty) -> name, rehash_term ty)
383 C.InductiveDefinition (tl', params', paramsno, attrs)
385 let rec metas_of_term = function
386 | C.Meta (i, c) -> [i,c]
389 | C.MutInd (_, _, ens)
390 | C.MutConstruct (_, _, _, ens) ->
391 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
394 | C.Lambda (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
395 | C.LetIn (_, s, ty, t) ->
396 (metas_of_term s) @ (metas_of_term ty) @ (metas_of_term t)
397 | C.Appl l -> List.flatten (List.map metas_of_term l)
398 | C.MutCase (uri, i, s, t, l) ->
399 (metas_of_term s) @ (metas_of_term t) @
400 (List.flatten (List.map metas_of_term l))
403 (List.map (fun (s, i, t1, t2) ->
404 (metas_of_term t1) @ (metas_of_term t2)) il)
407 (List.map (fun (s, t1, t2) ->
408 (metas_of_term t1) @ (metas_of_term t2)) il)
412 module MetaOT = struct
413 type t = int * C.term option list
414 let compare = Pervasives.compare
417 module S = Set.Make(MetaOT)
419 let rec metas_of_term_set = function
420 | C.Meta (i, c) -> S.singleton (i,c)
423 | C.MutInd (_, _, ens)
424 | C.MutConstruct (_, _, _, ens) ->
426 (fun s (_,t) -> S.union s (metas_of_term_set t))
430 | C.Lambda (_, s, t) -> S.union (metas_of_term_set s) (metas_of_term_set t)
431 | C.LetIn (_, s, ty, t) ->
432 S.union (metas_of_term_set s)
433 (S.union (metas_of_term_set ty) (metas_of_term_set t))
436 (fun s t -> S.union s (metas_of_term_set t))
438 | C.MutCase (uri, i, s, t, l) ->
440 (S.union (metas_of_term_set s) (metas_of_term_set t))
442 (fun s t -> S.union s (metas_of_term_set t))
446 (fun s (_,_,t1,t2) ->
447 S.union s (S.union (metas_of_term_set t1) (metas_of_term_set t2))))
452 S.union s (S.union (metas_of_term_set t1) (metas_of_term_set t2))))
457 let metas_of_term_set t =
458 let s = metas_of_term_set t in
462 (* syntactic_equality up to the *)
463 (* distinction between fake dependent products *)
464 (* and non-dependent products, alfa-conversion *)
465 let alpha_equivalence =
470 C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2) ->
471 UriManager.eq uri1 uri2 &&
472 aux_exp_named_subst exp_named_subst1 exp_named_subst2
473 | C.Cast (te,ty), C.Cast (te',ty') ->
474 aux te te' && aux ty ty'
475 | C.Prod (_,s,t), C.Prod (_,s',t') ->
477 | C.Lambda (_,s,t), C.Lambda (_,s',t') ->
479 | C.LetIn (_,s,ty,t), C.LetIn(_,s',ty',t') ->
480 aux s s' && aux ty ty' && aux t t'
481 | C.Appl l, C.Appl l' when List.length l = List.length l' ->
484 (fun b t1 t2 -> b && aux t1 t2) true l l'
486 Invalid_argument _ -> false)
487 | C.Const (uri,exp_named_subst1), C.Const (uri',exp_named_subst2) ->
488 UriManager.eq uri uri' &&
489 aux_exp_named_subst exp_named_subst1 exp_named_subst2
490 | C.MutInd (uri,i,exp_named_subst1), C.MutInd (uri',i',exp_named_subst2) ->
491 UriManager.eq uri uri' && i = i' &&
492 aux_exp_named_subst exp_named_subst1 exp_named_subst2
493 | C.MutConstruct (uri,i,j,exp_named_subst1),
494 C.MutConstruct (uri',i',j',exp_named_subst2) ->
495 UriManager.eq uri uri' && i = i' && j = j' &&
496 aux_exp_named_subst exp_named_subst1 exp_named_subst2
497 | C.MutCase (sp,i,outt,t,pl), C.MutCase (sp',i',outt',t',pl') ->
498 UriManager.eq sp sp' && i = i' &&
499 aux outt outt' && aux t t' &&
502 (fun b t1 t2 -> b && aux t1 t2) true pl pl'
504 Invalid_argument _ -> false)
505 | C.Fix (i,fl), C.Fix (i',fl') ->
509 (fun b (_,i,ty,bo) (_,i',ty',bo') ->
510 b && i = i' && aux ty ty' && aux bo bo'
513 Invalid_argument _ -> false)
514 | C.CoFix (i,fl), C.CoFix (i',fl') ->
518 (fun b (_,ty,bo) (_,ty',bo') ->
519 b && aux ty ty' && aux bo bo'
522 Invalid_argument _ -> false)
523 | C.Meta (i, subst), C.Meta (i', subst') ->
527 (fun b xt xt' -> match xt,xt' with
528 | Some t, Some t' -> b && aux t t'
532 Invalid_argument _ -> false)
533 | C.Appl [t], t' | t, C.Appl [t'] -> assert false
534 (* FG: are we _really_ sure of these?
535 | C.Sort (C.Type u), C.Sort (C.Type u') -> u = u'
536 | C.Implicit a, C.Implicit a' -> a = a'
537 we insert an unused variable below to genarate a warning at compile time
539 | _,_ -> false (* we already know that t != t' *)
540 and aux_exp_named_subst exp_named_subst1 exp_named_subst2 =
543 (fun b (uri1,t1) (uri2,t2) ->
544 b && UriManager.eq uri1 uri2 && aux t1 t2
545 ) true exp_named_subst1 exp_named_subst2
547 Invalid_argument _ -> false
552 let rec sober_term c g = function
554 if i <= 0 then fun b -> false else g
559 | C.MutConstruct (_, _, _, xnss)
560 | C.MutInd (_, _, xnss) -> sober_xnss c g xnss
561 | C.Meta (_, xss) -> sober_xss c g xss
565 sober_term c (sober_term c g t) v
566 | C.LetIn (_, v, ty, t) ->
567 sober_term c (sober_term c (sober_term c g t) ty) v
570 | C.Appl (C.Appl _ :: _) -> fun b -> false
571 | C.Appl ts -> sober_terms c g ts
572 | C.MutCase (_, _, t, v, ts) ->
573 sober_terms c (sober_term c (sober_term c g t) v) ts
574 | C.Fix (_, ifs) -> sober_ifs c g ifs
575 | C.CoFix (_, cifs) -> sober_cifs c g cifs
576 and sober_terms c g = List.fold_left (sober_term c) g
578 let map g (_, t) = sober_term c g t in
583 | Some t -> sober_term c g t
587 let map g (_, _, t, v) = sober_term c (sober_term c g t) v in
590 let map g (_, t, v) = sober_term c (sober_term c g t) v in
593 sober_term c (fun b -> b) t true
595 (* raw cic prettyprinter ****************************************************)
597 let xiter out so ss sc map l =
598 let rec aux = function
599 | hd :: tl when tl <> [] -> map hd; out ss; aux tl
600 | hd :: tl -> map hd; aux tl
603 out so; aux l; out sc
605 let abst s w = Some (s, C.Decl w)
607 let abbr s v w = Some (s, C.Def (v, w))
609 let pp_sort out = function
610 | C.Type _ -> out "*Type"
611 | C.Prop -> out "*Prop"
612 | C.CProp _ -> out "*CProp"
613 | C.Set -> out "*Set"
615 let pp_name out = function
617 | C.Anonymous -> out "_"
620 try match List.nth c (pred i) with
621 | None -> out (Printf.sprintf "%u[?]" i)
622 | Some (s, _) -> out (Printf.sprintf "%u[" i); pp_name out s; out "]"
623 with Failure "nth" -> out (Printf.sprintf "%u[%i]" i (List.length c - i))
625 let pp_implicit out = function
627 | Some `Closed -> out "?[Closed]"
628 | Some `Type -> out "?[Type]"
629 | Some `Hole -> out "?[Hole]"
630 | Some `Vector -> out "?[...]"
633 out (Printf.sprintf "%s<%s>" (UM.name_of_uri a) (UM.string_of_uri a))
635 let rec pp_term out e c = function
636 | C.Sort h -> pp_sort out h
637 | C.Rel i -> pp_rel out c i
638 | C.Implicit x -> pp_implicit out x
640 let map = function None -> out "_" | Some v -> pp_term out e c v in
641 out (Printf.sprintf "?%u" i); xiter out "[" "; " "]" map iss
643 pp_uri out a; pp_xss out e c xss
644 | C.Const (a, xss) ->
645 pp_uri out a; pp_xss out e c xss
646 | C.MutInd (a, m, xss) ->
647 pp_uri out a; out (Printf.sprintf "/%u" m);
649 | C.MutConstruct (a, m, n, xss) ->
650 pp_uri out a; out (Printf.sprintf "/%u/%u" m n);
653 out "type "; pp_term out e c w; out " contains "; pp_term out e c v
655 xiter out "(" " @ " ")" (pp_term out e c) vs
656 | C.MutCase (a, m, w, v, vs) ->
657 out "match "; pp_term out e c v;
658 out " of "; pp_uri out a; out (Printf.sprintf "/%u" m);
659 out " to "; pp_term out e c w;
660 xiter out " cases " " | " "" (pp_term out e c) vs
661 | C.Prod (s, w, t) ->
662 out "forall "; pp_name out s; out " of "; pp_term out e c w;
663 out " in "; pp_term out e (abst s w :: c) t
664 | C.Lambda (s, w, t) ->
665 out "fun "; pp_name out s; out " of "; pp_term out e c w;
666 out " in "; pp_term out e (abst s w :: c) t
667 | C.LetIn (s, v, w, t) ->
668 out "let "; pp_name out s;
669 out " def "; pp_term out e c v; out " of "; pp_term out e c w;
670 out " in "; pp_term out e (abbr s v w :: c) t
672 let map c (s, _, w, v) = abbr (C.Name s) v w :: c in
673 let c' = List.fold_left map c fs in
674 let map (s, i, w, v) =
675 out (Printf.sprintf "%s[%u] def " s i); pp_term out e c' v;
676 out " of "; pp_term out e c w;
678 xiter out "let rec " " and " " in " map fs; pp_rel out c' (succ i)
680 let map c (s, w, v) = abbr (C.Name s) v w :: c in
681 let c' = List.fold_left map c fs in
683 out s; pp_term out e c' v;
684 out " of "; pp_term out e c w;
686 xiter out "let corec " " and " " in " map fs; pp_rel out c' (succ i)
688 and pp_xss out e c xss =
689 let map (a, v) = pp_uri out a; out " <- "; pp_term out e c v in
690 xiter out "[" "; " "]" map xss
693 out (Printf.sprintf "%u" i)
695 let pp_attrs out attrs =
699 xiter out "[" "; " "] " map attrs
701 let pp_pars out pars =
702 xiter out " (" ", " ")\n" (pp_uri out) pars
704 let pp_point out point =
705 if point then out "ind " else out "coind "
707 let pp_constructor out (s, w) =
708 out s; out " of "; pp_term out [] [] w
710 let pp_definition out (s, point, w, ts) =
711 out "let "; pp_point out point; out s; out " of "; pp_term out [] [] w;
712 xiter out "\ndef " "\nor " "" (pp_constructor out) ts
714 let pp_obj out = function
715 | C.Constant (s, None, u, pars, attrs) ->
716 out "fun "; pp_attrs out attrs; out s; pp_pars out pars;
717 out " of "; pp_term out [] [] u
718 | C.Constant (s, Some t, u, pars, attrs) ->
719 out "let "; pp_attrs out attrs; out s; pp_pars out pars;
720 out " def "; pp_term out [] [] t; out " of "; pp_term out [] [] u
721 | C.Variable (s, None, u, pars, attrs) ->
722 out "local fun "; pp_attrs out attrs; out s; pp_pars out pars;
723 out " of "; pp_term out [] [] u
724 | C.Variable (s, Some t, u, pars, attrs) ->
725 out "local let "; pp_attrs out attrs; out s; pp_pars out pars;
726 out " def "; pp_term out [] [] t; out " of "; pp_term out [] [] u
727 | C.InductiveDefinition (us, pars, lpsno, attrs) ->
728 out "Inductive "; pp_attrs out attrs; pp_int out lpsno; pp_pars out pars;
729 xiter out "" "\n" "" (pp_definition out) us
730 | C.CurrentProof (s, e, t, u, pars, attrs) ->