Section CBV.
Variables variable function constructor : Type.
Variable variable_eq_dec : forall (x1 x2 : variable), {x1=x2}+{x1<>x2}.
Variable function_eq_dec : forall (x1 x2 : function), {x1=x2}+{x1<>x2}.
Variable constructor_eq_dec : forall (x1 x2 : constructor), {x1=x2}+{x1<>x2}.
Notation value := (Syntax.value constructor).
Notation term := (Syntax.term variable function constructor).
Notation pattern := (Syntax.pattern variable constructor).
Notation rule := (Syntax.rule variable function constructor).
Notation term_beq := (term_beq variable_eq_dec function_eq_dec constructor_eq_dec).
Definition cache : Type := list (term * value).
Notation assoc_cache := (assoc term_beq).
Definition cache_beq (C C' : cache) : bool :=
list_beq _ (prod_beq _ _ term_beq (value_beq constructor_eq_dec)) C C'.
Inductive cbv : Type :=
| cbv_constr : list cbv -> cache -> term -> cache -> value -> cbv
| cbv_split : list cbv -> cbv -> cache -> term -> cache -> value -> cbv
| cbv_update : nat -> (variable -> value) -> cbv -> cache -> term -> cache -> value -> cbv
| cbv_read : cache -> term -> value -> cbv.
(* Smarter induction principle than the one automatically generated *)
(* Being a subtree of *)
Fixpoint InCBV p proof_tree : Prop :=
p = proof_tree \/
match proof_tree with
| cbv_constr lp _ _ _ _ => orl (map (InCBV p) lp)
| cbv_split lp p' _ _ _ _ => InCBV p p' \/ orl (map (InCBV p) lp)
| cbv_update _ _ p' _ _ _ _ => InCBV p p'
| cbv_read _ _ _ => False
end.
(* Reverse induction on proof trees *)
Definition rule_subst_of_cbv_update ( subst_default : variable -> value) (proof_tree : cbv) : nat * (variable -> value) :=
match proof_tree with
| cbv_update i s _ _ _ _ _ => (i, s)
| _ => (0, subst_default) (* impossible case *)
end.
Definition proj_left (proof_tree : cbv) : term :=
match proof_tree with
| cbv_constr _ _ t _ _ => t
| cbv_split _ _ _ t _ _ => t
| cbv_update _ _ _ _ t _ _ => t
| cbv_read _ t _ => t
end.
Definition proj_right (proof_tree : cbv) : value :=
match proof_tree with
| cbv_constr _ _ _ _ v => v
| cbv_split _ _ _ _ _ v => v
| cbv_update _ _ _ _ _ _ v => v
| cbv_read _ _ v => v
end.
Definition cache_left (proof_tree : cbv) : cache :=
match proof_tree with
| cbv_constr _ c _ _ _ => c
| cbv_split _ _ c _ _ _ => c
| cbv_update _ _ _ c _ _ _ => c
| cbv_read c _ _ => c
end.
Definition cache_right (proof_tree : cbv) : cache :=
match proof_tree with
| cbv_constr _ _ _ c _ => c
| cbv_split _ _ _ _ c _ => c
| cbv_update _ _ _ _ _ c _ => c
| cbv_read c _ _ => c
end.
Fixpoint cache_path (C C' : cache)(l : list cbv) : bool :=
match l with
| nil => cache_beq C C'
| p :: l' => cache_beq C (cache_left p) && cache_path (cache_right p) C' l'
end.

Lemma cache_path_transitivity c c' l: forall P:cache-> Prop,
cache_path c c' l = true ->
P c ->(forall p, In p l ->
P (cache_left p) ->
P (cache_right p)) -> P c'.