Section CENV.
Context {cs: compspecs}.
Definition field_at (sh: Share.t) (t: type) (gfs: list gfield) (v: reptype (nested_field_type t gfs)) (p: val): mpred :=
!! (field_compatible t gfs p) &&
at_offset (data_at_rec sh (nested_field_type t gfs) v) (nested_field_offset t gfs) p.
Arguments field_at sh t gfs v p : simpl never.
Definition field_at_ (sh: Share.t) (t: type) (gfs: list gfield) (p: val): mpred :=
field_at sh t gfs (default_val (nested_field_type t gfs)) p.
Arguments field_at_ sh t gfs p : simpl never.
Definition data_at (sh: Share.t) (t: type) (v: reptype t) := field_at sh t nil v.
Definition data_at_ (sh: Share.t) (t: type) := field_at_ sh t nil.
Definition nested_reptype_structlist t gfs (m: members) :=
compact_prod (map (fun it => reptype (nested_field_type t (StructField (fst it) :: gfs))) m).
Definition nested_reptype_unionlist t gfs (m: members) :=
compact_sum (map (fun it => reptype (nested_field_type t (UnionField (fst it) :: gfs))) m).
(************************************************  Unfold and split lemmas  ************************************************)
Ltac solve_ptr_derives :=
repeat rewrite isptr_offset_val;
apply derives_refl.
Ltac solve_nptr p A :=
let H := fresh "H" in
match A with
| (?B * ?C) % logic =>
try solve [assert (~ isptr p -> B = FF) as H by solve_nptr p B;
intro; rewrite H by auto ; apply FF_sepcon];
try solve [assert (~ isptr p -> C = FF) as H by solve_nptr p C;
intro; rewrite H by auto; apply sepcon_FF]
| (?B && ?C) % logic =>
try solve [assert (~ isptr p -> B = FF) as H by solve_nptr p B;
intro; rewrite H by auto ; apply FF_andp];
try solve [assert (~ isptr p -> C = FF) as H by solve_nptr p C;
intro; rewrite H by auto; apply andp_FF]
| _ => apply (proj1 (not_ptr_FF A p)); solve_ptr p A
end
with solve_ptr p A :=
let p0 := fresh "p" in
match A with
| (_ * _) % logic => apply (proj2 (not_ptr_FF A p)); solve_nptr p A
| (_ && _) % logic => apply (proj2 (not_ptr_FF A p)); solve_nptr p A
| (!! _ /\ _)%logic => destruct A as [_ A]; solve_ptr p A
| (!! field_compatible _ _ ?q) => apply (derives_trans _ _ _ (prop_derives _ _ (field_compatible_isptr _ _ _))); solve_ptr_derives
| (!! field_compatible0 _ _ ?q) => apply (derives_trans _ _ _ (prop_derives _ _ (field_compatible0_isptr _ _ _))); solve_ptr_derives
| (memory_block _ _ ?q) => apply (derives_trans _ _ _ (memory_block_local_facts _ _ _)); solve_ptr_derives
| (withspacer _ _ _ ?P p) => apply withspacer_preserve_local_facts;
intro p0; solve_ptr p0 (P p0)
| (at_offset ?P _ ?q) => apply (derives_trans _ (!! isptr q));
[apply at_offset_preserve_local_facts; intro p0; solve_ptr p0 (P p0) |
solve_ptr_derives]
| (field_at _ _ _ _ p) => apply field_at_isptr'
end.
Ltac destruct_ptr p :=
let b := fresh "b" in
let ofs := fresh "OFS" in
match goal with
| |- (@eq mpred) ?A ?B =>
let H := fresh "H" in
let H0 := fresh "H" in
assert (~ isptr p -> A = FF) as H by solve_nptr p A;
assert (~ isptr p -> B = FF) as H0 by solve_nptr p B;
destruct p as [| | | | | b ofs]; try (rewrite H, H0 by (simpl; congruence); reflexivity);
clear H H0;
inv_int ofs
| |- (?A |-- _) =>
let H := fresh "H" in
assert (~ isptr p -> A = FF) as H by solve_nptr p A;
destruct p as [| | | | | b ofs]; try (rewrite H by (simpl; congruence); apply FF_left);
clear H;
inv_int ofs
end.

Lemma array_at_len_1: forall sh t gfs i v v' p,
JMeq v v' ->
array_at sh t gfs i (i + 1) (v :: nil) p = field_at sh t (ArraySubsc i :: gfs) v' p.