exception WrongUriToConstant;;
exception RelToHiddenHypothesis;;
+module C = Cic
+module S = CicSubstitution
+
let alpha_equivalence =
- let module C = Cic in
let rec aux t t' =
if t = t' then true
else
exception WhatAndWithWhatDoNotHaveTheSameLength;;
-(* "textual" replacement of several subterms with other ones *)
+(* Replaces "textually" in "where" every term in "what" with the corresponding
+ term in "with_what". The terms in "what" ARE NOT lifted when binders are
+ crossed. The terms in "with_what" ARE NOT lifted when binders are crossed.
+ Every free variable in "where" IS NOT lifted by nnn.
+*)
let replace ~equality ~what ~with_what ~where =
- let module C = Cic in
let find_image t =
let rec find_image_aux =
function
aux where
;;
-(* replaces in a term a term with another one. *)
-(* Lifting are performed as usual. *)
+(* Replaces in "where" every term in "what" with the corresponding
+ term in "with_what". The terms in "what" ARE lifted when binders are
+ crossed. The terms in "with_what" ARE lifted when binders are crossed.
+ Every free variable in "where" IS NOT lifted by nnn.
+ Thus "replace_lifting_csc 1 ~with_what:[Rel 1; ... ; Rel 1]" is the
+ inverse of subst up to the fact that free variables in "where" are NOT
+ lifted. *)
let replace_lifting ~equality ~what ~with_what ~where =
- let module C = Cic in
- let module S = CicSubstitution in
let find_image what t =
let rec find_image_aux =
function
substaux 1 what where
;;
-(* replaces in a term a list of terms with other ones. *)
-(* Lifting are performed as usual. *)
+(* Replaces in "where" every term in "what" with the corresponding
+ term in "with_what". The terms in "what" ARE NOT lifted when binders are
+ crossed. The terms in "with_what" ARE lifted when binders are crossed.
+ Every free variable in "where" IS lifted by nnn.
+ Thus "replace_lifting_csc 1 ~with_what:[Rel 1; ... ; Rel 1]" is the
+ inverse of subst up to the fact that "what" terms are NOT lifted. *)
let replace_lifting_csc nnn ~equality ~what ~with_what ~where =
- let module C = Cic in
- let module S = CicSubstitution in
let find_image t =
let rec find_image_aux =
function
substaux 1 where
;;
+(* This is like "replace_lifting_csc 1 ~with_what:[Rel 1; ... ; Rel 1]"
+ up to the fact that the index to start from can be specified *)
+let replace_with_rel_1_from ~equality ~what =
+ let rec find_image t = function
+ | [] -> false
+ | hd :: tl -> equality t hd || find_image t tl
+ in
+ let rec subst_term k t =
+ if find_image t what then C.Rel k else inspect_term k t
+ and inspect_term k = function
+ | C.Rel i -> if i < k then C.Rel i else C.Rel (succ i)
+ | C.Sort _ as t -> t
+ | C.Implicit _ as t -> t
+ | C.Var (uri, enss) ->
+ let enss = List.map (subst_ens k) enss in
+ C.Var (uri, enss)
+ | C.Const (uri ,enss) ->
+ let enss = List.map (subst_ens k) enss in
+ C.Const (uri, enss)
+ | C.MutInd (uri, tyno, enss) ->
+ let enss = List.map (subst_ens k) enss in
+ C.MutInd (uri, tyno, enss)
+ | C.MutConstruct (uri, tyno, consno, enss) ->
+ let enss = List.map (subst_ens k) enss in
+ C.MutConstruct (uri, tyno, consno, enss)
+ | C.Meta (i, mss) ->
+ let mss = List.map (subst_ms k) mss in
+ C.Meta(i, mss)
+ | C.Cast (t, v) -> C.Cast (subst_term k t, subst_term k v)
+ | C.Appl ts ->
+ let ts = List.map (subst_term k) ts in
+ C.Appl ts
+ | C.MutCase (uri, tyno, outty, t, cases) ->
+ let cases = List.map (subst_term k) cases in
+ C.MutCase (uri, tyno, subst_term k outty, subst_term k t, cases)
+ | C.Prod (n, v, t) ->
+ C.Prod (n, subst_term k v, subst_term (succ k) t)
+ | C.Lambda (n, v, t) ->
+ C.Lambda (n, subst_term k v, subst_term (succ k) t)
+ | C.LetIn (n, v, t) ->
+ C.LetIn (n, subst_term k v, subst_term (succ k) t)
+ | C.Fix (i, fixes) ->
+ let fixesno = List.length fixes in
+ let fixes = List.map (subst_fix fixesno k) fixes in
+ C.Fix (i, fixes)
+ | C.CoFix (i, cofixes) ->
+ let cofixesno = List.length cofixes in
+ let cofixes = List.map (subst_cofix cofixesno k) cofixes in
+ C.CoFix (i, cofixes)
+ and subst_ens k (uri, t) = uri, subst_term k t
+ and subst_ms k = function
+ | None -> None
+ | Some t -> Some (subst_term k t)
+ and subst_fix fixesno k (n, ind, ty, bo) =
+ n, ind, subst_term k ty, subst_term (k + fixesno) bo
+ and subst_cofix cofixesno k (n, ty, bo) =
+ n, subst_term k ty, subst_term (k + cofixesno) bo
+in
+subst_term
+
+
+
+
(* Takes a well-typed term and fully reduces it. *)
(*CSC: It does not perform reduction in a Case *)
let reduce context =
let rec reduceaux context l =
- let module C = Cic in
- let module S = CicSubstitution in
function
C.Rel n as t ->
(match List.nth context (n-1) with
if l = [] then res else C.Appl (res::l)
)
| C.Fix (i,fl) ->
- let tys =
- List.map (function (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) fl
+ let tys,_ =
+ List.fold_left
+ (fun (types,len) (n,_,ty,_) ->
+ (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
+ len+1)
+ ) ([],0) fl
in
let t' () =
let fl' =
| None -> if l = [] then t' () else C.Appl ((t' ())::l)
)
| C.CoFix (i,fl) ->
- let tys =
- List.map (function (name,ty,_) -> Some (C.Name name, C.Decl ty)) fl
+ let tys,_ =
+ List.fold_left
+ (fun (types,len) (n,ty,_) ->
+ (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
+ len+1)
+ ) ([],0) fl
in
let t' =
let fl' =
(*CSC: It does not perform simplification in a Case *)
let simpl context =
+ (* a simplified term is active if it can create a redex when used as an *)
+ (* actual parameter *)
+ let rec is_active =
+ function
+ C.Lambda _
+ | C.MutConstruct _
+ | C.Appl (C.MutConstruct _::_)
+ | C.CoFix _ -> true
+ | C.Cast (bo,_) -> is_active bo
+ | C.LetIn _ -> assert false
+ | _ -> false
+ in
(* reduceaux is equal to the reduceaux locally defined inside *)
(* reduce, but for the const case. *)
(**** Step 1 ****)
let rec reduceaux context l =
- let module C = Cic in
- let module S = CicSubstitution in
function
C.Rel n as t ->
(* we never perform delta expansion automatically *)
(let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
match o with
C.Constant (_,Some body,_,_,_) ->
- try_delta_expansion context l
- (C.Const (uri,exp_named_subst'))
- (CicSubstitution.subst_vars exp_named_subst' body)
+ if List.exists is_active l then
+ try_delta_expansion context l
+ (C.Const (uri,exp_named_subst'))
+ (CicSubstitution.subst_vars exp_named_subst' body)
+ else
+ let t' = C.Const (uri,exp_named_subst') in
+ if l = [] then t' else C.Appl (t'::l)
| C.Constant (_,None,_,_,_) ->
let t' = C.Const (uri,exp_named_subst') in
if l = [] then t' else C.Appl (t'::l)
reduceaux context tl' body'
| t -> t
in
- (match decofix (CicReduction.whd context term) with
+ (match decofix (reduceaux context [] term) (*(CicReduction.whd context term)*) with
C.MutConstruct (_,_,j,_) -> reduceaux context l (List.nth pl (j-1))
| C.Appl (C.MutConstruct (_,_,j,_) :: tl) ->
let (arity, r) =
if l = [] then res else C.Appl (res::l)
)
| C.Fix (i,fl) ->
- let tys =
- List.map (function (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) fl
+ let tys,_ =
+ List.fold_left
+ (fun (types,len) (n,_,ty,_) ->
+ (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
+ len+1)
+ ) ([],0) fl
in
let t' () =
let fl' =
| None -> if l = [] then t' () else C.Appl ((t' ())::l)
)
| C.CoFix (i,fl) ->
- let tys =
- List.map (function (name,ty,_) -> Some (C.Name name, C.Decl ty)) fl
+ let tys,_ =
+ List.fold_left
+ (fun (types,len) (n,ty,_) ->
+ (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
+ len+1)
+ ) ([],0) fl
in
let t' =
let fl' =
List.map (function uri,t -> uri,reduceaux context [] t)
(**** Step 2 ****)
and try_delta_expansion context l term body =
- let module C = Cic in
- let module S = CicSubstitution in
try
let res,constant_args =
let rec aux rev_constant_args l =
with
_ -> raise AlreadySimplified
in
- (match CicReduction.whd context recparam with
+ (match reduceaux context [] recparam (*CicReduction.whd context recparam*) with
C.MutConstruct _
| C.Appl ((C.MutConstruct _)::_) ->
let body' =
let simplified_term_to_fold =
reduceaux context [] delta_expanded_term_to_fold
in
- replace (=) [simplified_term_to_fold] [term_to_fold] res
+ replace_lifting (=) [simplified_term_to_fold] [term_to_fold] res
with
WrongShape ->
(**** Step 3.2 ****)