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/
26 (* $Id: cicUtil.ml 10153 2009-07-28 15:17:51Z sacerdot $ *)
28 exception Meta_not_found of int
29 exception Subst_not_found of int
31 (* syntactic_equality up to the *)
32 (* distinction between fake dependent products *)
33 (* and non-dependent products, alfa-conversion *)
34 let alpha_equivalence status =
39 | NCic.Prod (_,s,t), NCic.Prod (_,s',t') ->
41 | NCic.Lambda (_,s,t), NCic.Lambda (_,s',t') ->
43 | NCic.LetIn (_,s,ty,t), NCic.LetIn(_,s',ty',t') ->
44 aux s s' && aux ty ty' && aux t t'
45 | NCic.Appl l, NCic.Appl l' when List.length l = List.length l' ->
48 (fun b t1 t2 -> b && aux t1 t2) true l l'
50 Invalid_argument _ -> false)
51 | NCic.Const _, NCic.Const _ -> t == t'
52 | NCic.Match (it,outt,t,pl), NCic.Match (it',outt',t',pl') ->
54 aux outt outt' && aux t t' &&
57 (fun b t1 t2 -> b && aux t1 t2) true pl pl'
59 Invalid_argument _ -> false)
60 | NCic.Meta (n1,(s1, NCic.Irl _)), NCic.Meta (n2,(s2, NCic.Irl _))
61 when n1 = n2 && s1 = s2 -> true
62 | NCic.Meta (i, (s,lc)), NCic.Meta (i', (s',lc')) ->
63 let lc = NCicUtils.expand_local_context lc in
64 let lc' = NCicUtils.expand_local_context lc' in
68 (fun b xt xt' -> b && aux (NCicSubstitution.lift status s xt) (NCicSubstitution.lift status s' xt'))
71 Invalid_argument _ -> false)
72 | NCic.Appl [_], _ | _, NCic.Appl [_] -> assert false
73 | NCic.Sort s, NCic.Sort s' -> s == s'
74 | _,_ -> false (* we already know that t != t' *)
79 exception WhatAndWithWhatDoNotHaveTheSameLength;;
81 let replace_lifting status ~equality ~context ~what ~with_what ~where =
82 let find_image ctx what t =
83 let rec find_image_aux =
85 [],[] -> raise Not_found
86 | what::tl1,with_what::tl2 ->
87 if equality ctx what t then with_what else find_image_aux (tl1,tl2)
88 | _,_ -> raise WhatAndWithWhatDoNotHaveTheSameLength
90 find_image_aux (what,with_what)
92 let add_ctx ctx n s = (n, NCic.Decl s)::ctx in
93 let add_ctx1 ctx n s ty = (n, NCic.Def (s,ty))::ctx in
94 let rec substaux k ctx what t =
96 NCicSubstitution.lift status (k-1) (find_image ctx what t)
100 | NCic.Meta (i, (shift,l)) ->
101 let l = NCicUtils.expand_local_context l in
103 List.map (fun t -> substaux k ctx what (NCicSubstitution.lift status shift t)) l
105 NCic.Meta(i,NCicMetaSubst.pack_lc (0, NCic.Ctx l'))
106 | NCic.Sort _ as t -> t
107 | NCic.Implicit _ as t -> t
108 | NCic.Prod (n,s,t) ->
110 (n, substaux k ctx what s, substaux (k + 1) (add_ctx ctx n s) (List.map (NCicSubstitution.lift status 1) what) t)
111 | NCic.Lambda (n,s,t) ->
113 (n, substaux k ctx what s, substaux (k + 1) (add_ctx ctx n s) (List.map (NCicSubstitution.lift status 1) what) t)
114 | NCic.LetIn (n,s,ty,t) ->
116 (n, substaux k ctx what s, substaux k ctx what ty, substaux (k + 1) (add_ctx1 ctx n s ty) (List.map (NCicSubstitution.lift status 1) what) t)
117 | NCic.Appl (he::tl) ->
118 (* Invariant: no Appl applied to another Appl *)
119 let tl' = List.map (substaux k ctx what) tl in
121 match substaux k ctx what he with
122 NCic.Appl l -> NCic.Appl (l@tl')
123 | _ as he' -> NCic.Appl (he'::tl')
125 | NCic.Appl _ -> assert false
126 | NCic.Const _ as t -> t
127 | NCic.Match (it,outt,t,pl) ->
128 NCic.Match (it,substaux k ctx what outt, substaux k ctx what t,
129 List.map (substaux k ctx what) pl)
132 prerr_endline "*** replace lifting -- what:";
133 List.iter (fun x -> prerr_endline (NCicPp.ppterm ~metasenv:[] ~subst:[] ~context x)) what;
134 prerr_endline "\n*** replace lifting -- with what:";
135 List.iter (fun x -> prerr_endline (NCicPp.ppterm ~metasenv:[] ~subst:[] ~context x)) with_what;
136 prerr_endline "\n*** replace lifting -- where:";
137 prerr_endline (NCicPp.ppterm ~metasenv:[] ~subst:[] ~context where);
140 substaux 1 context what where