Section cs_preserve.
Context (cs_from cs_to: compspecs).
Definition test_aux (b: bool) (i: ident): bool :=
b && match (@cenv_cs cs_from) ! i, (@cenv_cs cs_to) ! i with
| Some co_from, Some co_to => eqb_list eqb_member (co_members co_from) (co_members co_to) && eqb_su (co_su co_from) (co_su co_to) && eqb_attr (co_attr co_from) (co_attr co_to)
| _, _ => false
end.
Fixpoint cs_preserve_type (coeq: PTree.t bool) (t: type): bool :=
match t with
| Tarray t0 _ _ => cs_preserve_type coeq t0
| Tstruct id _ => match coeq ! id with | Some b => test_aux b id | _ => false end
| Tunion id _ => match coeq ! id with | Some b => test_aux b id | _ => false end
| _ => true
end.
Fixpoint cs_preserve_members (coeq: PTree.t bool) (m: members): bool :=
match m with
| nil => true
| ((_, t) :: m) => andb (cs_preserve_type coeq t) (cs_preserve_members coeq m)
end.
Class change_composite_env: Type := {
coeq: PTree.t bool;
coeq_consistent:
forall i co b,
(@cenv_cs cs_to) ! i = Some co ->
coeq ! i = Some b ->
b = cs_preserve_members coeq (co_members co);
coeq_complete:
forall i,
(exists co, (@cenv_cs cs_to) ! i = Some co) <->
(exists b, coeq ! i = Some b)
}.
Definition cs_preserve_env: PTree.t bool :=
let l := composite_reorder.rebuild_composite_elements (@cenv_cs cs_to) in
fold_right (fun (ic: positive * composite) (T0: PTree.t bool) => let (i, co) := ic in let T := T0 in PTree.set i (cs_preserve_members T (co_members co)) T) (PTree.empty _) l.
End cs_preserve.
Ltac make_cs_preserve cs_from cs_to :=
let coeq0 := eval cbv in (cs_preserve_env cs_from cs_to) in
let Hcoeq0 := constr: (eq_refl: coeq0 = cs_preserve_env cs_from cs_to) in
let coeq0_consistent := constr: (cs_preserve_env_consisent cs_from cs_to coeq0 Hcoeq0) in
let coeq0_complete := constr: (cs_preserve_completeness cs_from cs_to coeq0 Hcoeq0) in
refine (  {| coeq := coeq0 ;
coeq_consistent := coeq0_consistent;
coeq_complete := coeq0_complete |}).

Lemma align_compatible_rec_field_change_composite {cs_from cs_to} {CCE: change_composite_env cs_from cs_to}: forall (m: members),
true = cs_preserve_members cs_from cs_to (coeq cs_from cs_to) m ->
Forall
(Basics.compose
(fun t : type =>
cs_preserve_type cs_from cs_to (coeq cs_from cs_to) t = true ->
forall ofs : Z, @align_compatible_rec (@cenv_cs cs_from) t ofs <-> @align_compatible_rec (@cenv_cs cs_to) t ofs) snd)
m ->
forall i t,
field_type i m = Errors.OK t ->
forall ofs : Z, @align_compatible_rec (@cenv_cs cs_from) t ofs <-> @align_compatible_rec (@cenv_cs cs_to) t ofs.