(* Copyright (C) 2000, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://cs.unibo.it/helm/. *) exception CicReductionInternalError;; exception WrongUriToInductiveDefinition;; let fdebug = ref 1;; let debug t env s = let rec debug_aux t i = let module C = Cic in let module U = UriManager in CicPp.ppobj (C.Variable ("DEBUG", None, t, [], [])) ^ "\n" ^ i in if !fdebug = 0 then prerr_endline (s ^ "\n" ^ List.fold_right debug_aux (t::env) "") ;; exception Impossible of int;; exception ReferenceToConstant;; exception ReferenceToVariable;; exception ReferenceToCurrentProof;; exception ReferenceToInductiveDefinition;; exception RelToHiddenHypothesis;; (* takes a well-typed term *) let whd context = let rec whdaux l = let module C = Cic in let module S = CicSubstitution in function C.Rel n as t -> (match List.nth context (n-1) with Some (_, C.Decl _) -> if l = [] then t else C.Appl (t::l) | Some (_, C.Def (bo,_)) -> whdaux l (S.lift n bo) | None -> raise RelToHiddenHypothesis ) | C.Var (uri,exp_named_subst) as t -> let o,_ = CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri in (match o with C.Constant _ -> raise ReferenceToConstant | C.CurrentProof _ -> raise ReferenceToCurrentProof | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition | C.Variable (_,None,_,_,_) -> if l = [] then t else C.Appl (t::l) | C.Variable (_,Some body,_,_,_) -> whdaux l (CicSubstitution.subst_vars exp_named_subst body) ) | C.Meta _ as t -> if l = [] then t else C.Appl (t::l) | C.Sort _ as t -> t (* l should be empty *) | C.Implicit _ as t -> t | C.Cast (te,ty) -> whdaux l te (*CSC E' GIUSTO BUTTARE IL CAST? *) | C.Prod _ as t -> t (* l should be empty *) | C.Lambda (name,s,t) as t' -> (match l with [] -> t' | he::tl -> whdaux tl (S.subst he t) (* when name is Anonimous the substitution should be superfluous *) ) | C.LetIn (n,s,t) -> whdaux l (S.subst (whdaux [] s) t) | C.Appl (he::tl) -> whdaux (tl@l) he | C.Appl [] -> raise (Impossible 1) | C.Const (uri,exp_named_subst) as t -> let o,_ = CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri in (match o with C.Constant (_,Some body,_,_,_) -> whdaux l (CicSubstitution.subst_vars exp_named_subst body) | C.Constant _ -> if l = [] then t else C.Appl (t::l) | C.Variable _ -> raise ReferenceToVariable | C.CurrentProof (_,_,body,_,_,_) -> whdaux l (CicSubstitution.subst_vars exp_named_subst body) | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition ) | C.MutInd _ as t -> if l = [] then t else C.Appl (t::l) | C.MutConstruct _ as t -> if l = [] then t else C.Appl (t::l) | C.MutCase (mutind,i,_,term,pl) as t-> let decofix = function C.CoFix (i,fl) as t -> let (_,_,body) = List.nth fl i in let body' = let counter = ref (List.length fl) in List.fold_right (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl))) fl body in whdaux [] body' | C.Appl (C.CoFix (i,fl) :: tl) -> let (_,_,body) = List.nth fl i in let body' = let counter = ref (List.length fl) in List.fold_right (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl))) fl body in whdaux tl body' | t -> t in (match decofix (whdaux [] term) with C.MutConstruct (_,_,j,_) -> whdaux l (List.nth pl (j-1)) | C.Appl (C.MutConstruct (_,_,j,_) :: tl) -> let (arity, r) = let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph mutind in match o with C.InductiveDefinition (tl,ingredients,r,_) -> let (_,_,arity,_) = List.nth tl i in (arity,r) | _ -> raise WrongUriToInductiveDefinition in let ts = let rec eat_first = function (0,l) -> l | (n,he::tl) when n > 0 -> eat_first (n - 1, tl) | _ -> raise (Impossible 5) in eat_first (r,tl) in whdaux (ts@l) (List.nth pl (j-1)) | C.Cast _ | C.Implicit _ -> raise (Impossible 2) (* we don't trust our whd ;-) *) | _ -> if l = [] then t else C.Appl (t::l) ) | C.Fix (i,fl) as t -> let (_,recindex,_,body) = List.nth fl i in let recparam = try Some (List.nth l recindex) with _ -> None in (match recparam with Some recparam -> (match whdaux [] recparam with C.MutConstruct _ | C.Appl ((C.MutConstruct _)::_) -> let body' = let counter = ref (List.length fl) in List.fold_right (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl))) fl body in (* Possible optimization: substituting whd recparam in l *) whdaux l body' | _ -> if l = [] then t else C.Appl (t::l) ) | None -> if l = [] then t else C.Appl (t::l) ) | C.CoFix (i,fl) as t -> if l = [] then t else C.Appl (t::l) in (*CSC function t -> prerr_endline ("PRIMA WHD" ^ CicPp.ppterm t) ; flush stderr ; List.iter (function (Cic.Decl t) -> prerr_endline ("Context: " ^ CicPp.ppterm t) | (Cic.Def t) -> prerr_endline ("Context:= " ^ CicPp.ppterm t)) context ; flush stderr ; prerr_endline " (n1 = n2),ugraph | (C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2)) -> let b = U.eq uri1 uri2 in if b then (try List.fold_right2 (fun (uri1,x) (uri2,y) (b,ugraph) -> (* FIXME: lazy! *) let b',ugraph' = aux test_equality_only context x y ugraph in (U.eq uri1 uri2 && b' && b),ugraph' ) exp_named_subst1 exp_named_subst2 (true,ugraph) with Invalid_argument _ -> false,ugraph ) else false,ugraph | (C.Meta (n1,l1), C.Meta (n2,l2)) -> let b = n1 = n2 in if b then List.fold_left2 (fun (b,ugraph) t1 t2 -> if b then match t1,t2 with None,_ | _,None -> true,ugraph | Some t1',Some t2' -> aux test_equality_only context t1' t2' ugraph else false,ugraph ) (true,ugraph) l1 l2 else false,ugraph (* TASSI: CONSTRAINTS *) | (C.Sort (C.Type t1), C.Sort (C.Type t2)) when test_equality_only -> true,(CicUniv.add_eq t2 t1 ugraph) (* TASSI: CONSTRAINTS *) | (C.Sort (C.Type t1), C.Sort (C.Type t2)) -> true,(CicUniv.add_ge t2 t1 ugraph) (* TASSI: CONSTRAINTS *) | (C.Sort s1, C.Sort (C.Type _)) -> (not test_equality_only),ugraph (* TASSI: CONSTRAINTS *) | (C.Sort s1, C.Sort s2) -> (s1 = s2),ugraph | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) -> let b',ugraph' = aux true context s1 s2 ugraph in if b' then aux test_equality_only ((Some (name1, (C.Decl s1)))::context) t1 t2 ugraph' else false,ugraph | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) -> let b',ugraph' = aux test_equality_only context s1 s2 ugraph in if b' then aux test_equality_only ((Some (name1, (C.Decl s1)))::context) t1 t2 ugraph' else false,ugraph | (C.LetIn (name1,s1,t1), C.LetIn(_,s2,t2)) -> let b',ugraph' = aux test_equality_only context s1 s2 ugraph in if b' then aux test_equality_only ((Some (name1, (C.Def (s1,None))))::context) t1 t2 ugraph' else false,ugraph | (C.Appl l1, C.Appl l2) -> (try List.fold_right2 (fun x y (b,ugraph) -> if b then aux test_equality_only context x y ugraph else false,ugraph) l1 l2 (true,ugraph) with Invalid_argument _ -> false,ugraph ) | (C.Const (uri1,exp_named_subst1), C.Const (uri2,exp_named_subst2)) -> let b' = U.eq uri1 uri2 in if b' then (try List.fold_right2 (fun (uri1,x) (uri2,y) (b,ugraph) -> if b && U.eq uri1 uri2 then aux test_equality_only context x y ugraph else false,ugraph ) exp_named_subst1 exp_named_subst2 (true,ugraph) with Invalid_argument _ -> false,ugraph ) else false,ugraph | (C.MutInd (uri1,i1,exp_named_subst1), C.MutInd (uri2,i2,exp_named_subst2) ) -> let b' = U.eq uri1 uri2 && i1 = i2 in if b' then (try List.fold_right2 (fun (uri1,x) (uri2,y) (b,ugraph) -> if b && U.eq uri1 uri2 then aux test_equality_only context x y ugraph else false,ugraph ) exp_named_subst1 exp_named_subst2 (true,ugraph) with Invalid_argument _ -> false,ugraph ) else false,ugraph | (C.MutConstruct (uri1,i1,j1,exp_named_subst1), C.MutConstruct (uri2,i2,j2,exp_named_subst2) ) -> let b' = U.eq uri1 uri2 && i1 = i2 && j1 = j2 in if b' then (try List.fold_right2 (fun (uri1,x) (uri2,y) (b,ugraph) -> if b && U.eq uri1 uri2 then aux test_equality_only context x y ugraph else false,ugraph ) exp_named_subst1 exp_named_subst2 (true,ugraph) with Invalid_argument _ -> false,ugraph ) else false,ugraph | (C.MutCase (uri1,i1,outtype1,term1,pl1), C.MutCase (uri2,i2,outtype2,term2,pl2)) -> let b' = U.eq uri1 uri2 && i1 = i2 in if b' then let b'',ugraph''=aux test_equality_only context outtype1 outtype2 ugraph in if b'' then let b''',ugraph'''= aux test_equality_only context term1 term2 ugraph'' in List.fold_right2 (fun x y (b,ugraph) -> if b then aux test_equality_only context x y ugraph else false,ugraph) pl1 pl2 (true,ugraph''') else false,ugraph else false,ugraph | (C.Fix (i1,fl1), C.Fix (i2,fl2)) -> let tys = List.map (function (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1 in if i1 = i2 then List.fold_right2 (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) (b,ugraph) -> if b && recindex1 = recindex2 then let b',ugraph' = aux test_equality_only context ty1 ty2 ugraph in if b' then aux test_equality_only (tys@context) bo1 bo2 ugraph' else false,ugraph else false,ugraph) fl1 fl2 (true,ugraph) else false,ugraph | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) -> let tys = List.map (function (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1 in if i1 = i2 then List.fold_right2 (fun (_,ty1,bo1) (_,ty2,bo2) (b,ugraph) -> if b then let b',ugraph' = aux test_equality_only context ty1 ty2 ugraph in if b' then aux test_equality_only (tys@context) bo1 bo2 ugraph' else false,ugraph else false,ugraph) fl1 fl2 (true,ugraph) else false,ugraph | (C.Cast _, _) | (_, C.Cast _) | (C.Implicit _, _) | (_, C.Implicit _) -> assert false | (_,_) -> false,ugraph end in let b,ugraph' = aux2 test_equality_only t1 t2 ugraph in if b then b,ugraph' else begin debug t1 [t2] "PREWHD"; let t1' = whd context t1 in let t2' = whd context t2 in debug t1' [t2'] "POSTWHD"; aux2 test_equality_only t1' t2' ugraph end in aux false c t1 t2 ugraph ;;