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.

Lemma cs_preserve_env_consisent: forall (coeq: PTree.t bool),
coeq = cs_preserve_env ->
forall i co b,
(@cenv_cs cs_to) ! i = Some co ->
coeq ! i = Some b ->
b = cs_preserve_members coeq (co_members co).