Require Import VST.veric.juicy_base.
Require Import VST.veric.juicy_mem.
Require Import VST.veric.juicy_mem_lemmas.

Require Import VST.veric.extend_tc.
Require Import VST.veric.Clight_seplog.
Require Import VST.veric.Clight_assert_lemmas.
Require Import VST.veric.tycontext.
Require Import VST.veric.expr2.
Require Import VST.veric.expr_lemmas.

Require Export VST.veric.initial_world.

Local Open Scope pred.

Obligation Tactic := idtac.

(*
Definition initial_core' (ge: Genv.t fundef type) (G: funspecs) (n: nat) (loc: address) : resource :=
   if Z.eq_dec (snd loc) 0
   then match Genv.invert_symbol ge (fst loc) with
           | Some id =>
                  match find_id id G with
                  | Some (mk_funspec fsig cc A P Q _ _) =>
                           PURE (FUN fsig cc) (SomeP (SpecTT A) (fun ts => fmap _ (approx n) (approx n) (packPQ P Q ts)))
                  | None => NO Share.bot bot_unreadable
                  end
           | None => NO Share.bot bot_unreadable
          end
   else NO Share.bot bot_unreadable.*)
Notation initial_core' := (initial_core' function).

(* This version starts with an empty ghost. 
Program Definition initial_core (ge: Genv.t fundef type) (G: funspecs) (n: nat): rmap :=
  proj1_sig (make_rmap (initial_core' ge G n) nil n _ eq_refl).
Next Obligation.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
intros.
extensionality loc; unfold compose, initial_core'.
if_tac; [ | simpl; auto].
destruct (Genv.invert_symbol ge (fst loc)); [ | simpl; auto].
destruct (find_id i G); [ | simpl; auto].
destruct f.
unfold resource_fmap.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
f_equal.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
simpl.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
f_equal.
change R.approx with approx.
extensionality i0 ts b rho.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (approx (S n) (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x)) b rho))) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
rewrite fmap_app.
pattern (approx n) at 7 8 9.
rewrite <- approx_oo_approx.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
auto.
Qed.*)
Notation initial_core := (@initial_core function).

(*
Program Definition initial_core_ext {Z} (ora : Z) (ge: Genv.t fundef type) (G: funspecs) (n: nat): rmap :=
  proj1_sig (make_rmap (initial_core' ge G n) (Some (ext_ghost ora, NoneP) :: nil) n _ eq_refl).
Next Obligation.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
intros.
extensionality loc; unfold compose, initial_core'.
if_tac; [ | simpl; auto].
destruct (Genv.invert_symbol ge (fst loc)); [ | simpl; auto].
destruct (find_id i G); [ | simpl; auto].
destruct f.
unfold resource_fmap.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
f_equal.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
simpl.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
f_equal.
change R.approx with approx.
extensionality i0 ts b rho.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (approx (S n) (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x)) b rho))) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
rewrite fmap_app.
pattern (approx n) at 7 8 9.
rewrite <- approx_oo_approx.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
auto.
Qed.
*)
Notation initial_core_ext := (@initial_core_ext  function).

(*Definition prog_funct (p: program) := prog_funct' (prog_defs p).*)
Notation prog_funct := (@prog_funct function).

Inductive match_fdecs: list  (ident * fundef) -> funspecs -> Prop :=
| match_fdecs_nil: match_fdecs nil nil
| match_fdecs_cons: forall i fd fspec fs G,
                  type_of_fundef fd = type_of_funspec fspec ->
                  match_fdecs fs G ->
                  match_fdecs ((i,fd)::fs) ((i,fspec)::G)
(* EXPERIMENT
| match_fdecs_skip: forall ifd fs G,
                 match_fdecs fs G ->
                 match_fdecs (ifd::fs) G*)
.
(*
Fixpoint match_fdecs (fdecs: list (ident * fundef)) (G: funspecs) :=
 match fdecs, G with
 | _, nil => True
 | (i,fd)::f', (j,fspec)::G' =>
       i=j /\ type_of_fundef fd = type_of_funspec fspec /\ match_fdecs f' G'
        \/ match_fdecs f' G
 | nil, _::_ => False
 end.
*)
(*
Definition match_fdecs (fdecs: list (ident * fundef)) (G: funspecs) :=
 map (fun idf => (fst idf, Clight.type_of_fundef (snd idf))) fdecs =
 map (fun idf => (fst idf, type_of_funspec (snd idf))) G.
*)


Lemma match_fdecs_exists_Gfun:
  forall prog G i f,
    find_id i G = Some f ->
    match_fdecs (prog_funct prog) G ->
    exists fd,   In (i, Gfun fd) (prog_defs prog) /\
                     type_of_fundef fd = type_of_funspec f.
(* Goal: forall (prog : Ctypes.program function) (G : funspecs) (i : ident) (f : funspec) (_ : @eq (option funspec) (find_id i G) (@Some funspec f)) (_ : match_fdecs (@initial_world.prog_funct function prog) G), @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) (@prog_defs function prog)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
Proof. unfold prog_funct. unfold prog_defs_names.
(* Goal: forall (prog : Ctypes.program function) (G : funspecs) (i : ident) (f : funspec) (_ : @eq (option funspec) (find_id i G) (@Some funspec f)) (_ : match_fdecs (@prog_funct' (Ctypes.fundef function) type (@prog_defs function prog)) G), @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) (@prog_defs function prog)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
intros ? ? ? ?.
(* Goal: forall (_ : @eq (option funspec) (find_id i G) (@Some funspec f)) (_ : match_fdecs (@prog_funct' (Ctypes.fundef function) type (@prog_defs function prog)) G), @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) (@prog_defs function prog)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
forget (prog_defs prog) as dl.
(* Goal: forall (_ : @eq (option funspec) (find_id i G) (@Some funspec f)) (_ : match_fdecs (@prog_funct' (Ctypes.fundef function) type dl) G), @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
revert G; induction dl; simpl; intros.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gfun (Ctypes.fundef function) type f0)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
inv H0. inv H.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) a (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
destruct a as [i' [?|?]].
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gfun (Ctypes.fundef function) type f0)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
inv H0.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and False (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) a (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
simpl in H; if_tac in H. subst i'; inv H.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type f0)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gfun (Ctypes.fundef function) type f0)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
eauto.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gfun (Ctypes.fundef function) type f0)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
destruct (IHdl G0) as [fd [? ?]]; auto.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
exists fd; split; auto.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
destruct (IHdl G) as [fd [? ?]]; auto.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
exists fd; split; auto.
(* EXPERIMENT
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
destruct (IHdl G) as [fd [? ?]]; auto.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
exists fd; split; auto.
*)
Qed.

(*Specializations of stuff in initial_world:
(*Partial attempt at porting add_globales_hack*)
Lemma add_globals_hack_nil:
   forall gev prog_pub,
    gev = Genv.add_globals (Genv.empty_genv fundef type prog_pub) (rev nil) ->
   forall id, Genv.find_symbol gev id = None.
Proof. simpl; intros; subst.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  unfold Genv.find_symbol, Genv.empty_genv. simpl. apply PTree.gempty.
Qed.

Lemma add_globals_hack_single:
   forall v gev prog_pub,
    gev = Genv.add_globals (Genv.empty_genv fundef type prog_pub) (cons v nil) ->
   forall id b, (Genv.find_symbol gev id = Some b <-> fst v = id /\ b = 1%positive).
Proof. simpl; intros; subst.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  unfold Genv.find_symbol, Genv.empty_genv. simpl.
  destruct (peq (fst v) id).
     subst id. rewrite PTree.gss.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
       split; intros. split; trivial. congruence.
       destruct H; subst. trivial.
  rewrite PTree.gso.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and False (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) a (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
    split; intros. rewrite PTree.gempty in H. inv H.
    destruct H; subst. congruence.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
  auto.
Qed.

Lemma add_globals_hack:
   forall vl gev prog_pub,
    list_norepet (map fst vl) ->
    gev = Genv.add_globals (Genv.empty_genv fundef type prog_pub) (rev vl) ->

   (forall id b, 0 <= Zpos b - 1 < Zlength vl ->
                           (Genv.find_symbol gev id = Some b <->
                            nth_error (map (@fst _ _) vl) (length vl - Pos.to_nat b)  = Some id)).
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
Proof. intros. subst.
     apply iff_trans with (nth_error (map fst (rev vl)) (nat_of_Z (Zpos b - 1)) = Some id).
   2: {
     rewrite map_rev; rewrite nth_error_rev.
             replace (length (map fst vl) - nat_of_Z (Zpos b - 1) - 1)%nat
                        with (length vl - Pos.to_nat b)%nat ; [intuition | ].
    rewrite map_length.
    transitivity (length vl - (nat_of_Z (Z.pos b-1)+1))%nat; try omega.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
    f_equal.
    change (Pos.to_nat b = (nat_of_Z (Z.pos b - 1) + nat_of_Z 1)%nat).
    rewrite <- nat_of_Z_plus by omega.
    rewrite <- Z2Nat.inj_pos. unfold nat_of_Z.
    f_equal. omega.
    rewrite map_length.
    rewrite Zlength_correct in H1.
    forget (Z.pos b-1) as i; forget (length vl) as n; clear - H1.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
    apply inj_lt_rev. rewrite nat_of_Z_max; auto.
    rewrite (Coqlib.Zmax_spec i 0). if_tac; omega.
  }
  rename H1 into Hb; revert H; induction vl; simpl rev; simpl map;
       simpl Genv.find_symbol; intros;
       try rewrite Zlength_nil in *.
      unfold Genv.find_symbol. rewrite PTree.gempty.
     intuition.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and False (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) a (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
       destruct a. inv H. rewrite Zlength_cons in Hb.
       destruct (eq_dec (Z.pos b-1) (Zlength vl)).
        clear IHvl Hb. rewrite e. rewrite Zlength_correct. rewrite nat_of_Z_of_nat.
        replace b with (Z.to_pos (1+ (Zlength vl)))
          by (rewrite <- e; replace (1 + (Z.pos b - 1)) with (Z.pos b) by omega;
                  apply Pos2Z.id).
        clear e b.
        rewrite <- Zlength_rev. rewrite <- rev_length.
         replace (length (rev vl)) with (length (rev vl) + 0)%nat by omega.
         rewrite map_app. rewrite <- map_length with (f:=@fst ident (globdef fundef type)).
        rewrite nth_error_app.
        apply iff_trans with (i=id); [ | simpl; split; intro; subst; auto; inv H; auto].
        rewrite In_rev in H2. rewrite <- map_rev in H2.
       rewrite <- list_norepet_rev in H3. rewrite <- map_rev in H3.
         forget (rev vl) as dl.
    assert (FSA := find_symbol_add_globals i g  id _ prog_pub H2 H3).
    { destruct dl.
      rewrite (FSA (Z.to_pos (1 + Zlength (@nil (ident * globdef fundef type))))).
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
      simpl. intuition.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
      replace (Z.to_pos (1 + Zlength (p :: dl))) with (1 + Z.to_pos (Zlength (p :: dl)))%positive ; auto.
      clear.
      rewrite Zlength_cons.      rewrite Zlength_correct.
      rewrite Z2Pos.inj_add; try solve [simpl; omega]. reflexivity.
    }
    spec IHvl ; [ omega |].
    specialize (IHvl H3).
    rewrite Genv.add_globals_app.
    unfold Genv.add_globals at 1. simpl fold_left.
    unfold Genv.find_symbol, Genv.add_global.
    simpl Genv.genv_symb.
    destruct (eq_dec id i).
    + subst i. rewrite PTree.gss.
      rewrite Genv.genv_next_add_globals.
      rewrite advance_next_length.
      simpl Genv.genv_next.
      rewrite map_app.
      rewrite In_rev in H2. rewrite <- map_rev in H2. rewrite Pos.add_sub.
      split; intro.
      - assert (H': b = Pos.of_nat (S (length (rev vl)))) by congruence.
        clear H; rename H' into H.
          subst b. elimtype False; apply n; clear.
          rewrite <- Zlength_rev. rewrite Zlength_correct. forget (length (rev vl)) as i.
          rewrite Zpos_Posofnat by omega. rewrite Nat2Z.inj_succ. unfold Z.succ.  omega.
     - elimtype False.
       assert (Z.pos b-1 >= 0) by (clear - Hb; omega).
       pose proof (Coqlib.nat_of_Z_eq _ H0).
       clear - H1 H H2 n.
       rewrite Zlength_correct in n. apply n. clear n.
       rewrite <- H1.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
       f_equal. clear - H H2.
       forget (nat_of_Z (Z.pos b-1)) as j.
       replace (length vl) with (length (map fst (rev vl)))
           by (rewrite map_length; rewrite rev_length; auto).
       forget (map fst (rev vl)) as al.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
       revert al H2 H; clear; induction j; destruct al; simpl; intros; auto. inv H; intuition.
       elimtype False; clear - H; induction j; inv H; auto.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
       f_equal. apply IHj; auto.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
    + rewrite PTree.gso by auto.
      rewrite map_app.
      destruct IHvl.
      split; intro.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
      - apply H in H1. rewrite nth_error_app1; auto.
        clear - n Hb. rewrite map_length. rewrite rev_length. rewrite Zlength_correct in Hb,n.
        assert (Z.pos b-1>=0) by omega.
        pose proof (Coqlib.nat_of_Z_eq _ H).
        forget (nat_of_Z(Z.pos b-1)) as j. rewrite <- H0 in *.
        destruct Hb. clear - H2 n. omega.
      - assert (nat_of_Z (Z.pos b-1) < length (map (@fst _ _) (rev vl)))%nat.
        { clear - Hb n H1.
          rewrite Zlength_correct in n. rewrite map_length; rewrite rev_length.
          assert (nat_of_Z (Z.pos b-1) <> length vl).
          { contradict n. rewrite <- n.
            rewrite Coqlib.nat_of_Z_eq; auto. omega. }
          forget (nat_of_Z (Z.pos b-1)) as j.
          clear - H1 H.
          assert (S (length vl) = length (map fst (rev vl) ++ map fst ((i, g) :: nil))).
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
          { simpl. rewrite app_length; rewrite map_length; rewrite rev_length; simpl; omega. }
          assert (j < S (length vl))%nat; [ | omega].
          rewrite H0. forget (map fst (rev vl) ++ map fst ((i, g) :: nil)) as al.
          clear - H1. revert al H1; induction j; destruct al; simpl in *; intros; inv H1; auto; try omega.
          specialize (IHj _ H0); omega. }
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
        rewrite nth_error_app1 in H1 by auto.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
        apply H0 in H1. auto.
Qed.

Lemma find_symbol_globalenv:
  forall (prog: program) i b,
   list_norepet (prog_defs_names prog) ->
  Genv.find_symbol (Genv.globalenv prog) i = Some b ->
  0 < Z.pos b <= Z_of_nat (length (prog_defs prog)) /\
  exists d, nth_error (prog_defs prog) (nat_of_Z (Z.pos b-1)) = Some (i,d).
Proof.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
intros.
unfold Genv.globalenv in H0.
change (AST.prog_public prog) with (prog_public prog) in H0.
change (AST.prog_defs prog) with (prog_defs prog) in H0.
assert (RANGE: 0 <= Z.pos b - 1 < Zlength (rev (prog_defs prog))). {
 rewrite <- (rev_involutive (prog_defs prog)) in H0.
 clear - H0.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
 revert H0; induction (rev (prog_defs prog));  simpl Genv.find_symbol; intros.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gfun (Ctypes.fundef function) type f0)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
 unfold Genv.find_symbol in H0. simpl in H0. rewrite PTree.gempty in H0; inv H0.
 rewrite Genv.add_globals_app in H0.
 simpl in H0. destruct a.
 destruct (eq_dec i0 i). subst.
 unfold Genv.add_global, Genv.find_symbol in H0. simpl in H0.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gfun (Ctypes.fundef function) type f0)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i' (@Gvar (Ctypes.fundef function) type v)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
 rewrite PTree.gss  in H0. inv H0.
 clear.
 split.
 match goal with |- _ <= Z.pos ?A - _ => pose proof (Zgt_pos_0  A); omega end.
 rewrite Zlength_cons.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
 induction l. simpl. omega.
 rewrite Zlength_cons.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
 Opaque Z.sub. simpl. Transparent Z.sub.
 rewrite Genv.add_globals_app.
   simpl Genv.genv_next.
 match goal with |- context [Pos.succ ?J] =>
  forget J as j
 end.
 clear - IHl.
 replace (Z.pos (Pos.succ j) - 1) with (Z.succ (Z.pos j - 1)). omega.
  unfold Z.succ.  rewrite Pos2Z.inj_succ.  omega.
 unfold Genv.add_global, Genv.find_symbol in IHl, H0. simpl in H0.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
 rewrite PTree.gso in H0 by auto.
 apply IHl in H0.
 rewrite Zlength_cons. omega.
 }
 split.
 rewrite Zlength_correct in RANGE.
 rewrite rev_length in RANGE. omega.
 rewrite <- list_norepet_rev in H.
 unfold prog_defs_names in H.
 change (AST.prog_defs prog) with (prog_defs prog) in H.
 rewrite <- map_rev in H.
 rewrite add_globals_hack in H0; [ | apply H | rewrite rev_involutive; auto | auto ].
 rewrite map_rev in H0.
 rewrite nth_error_rev in H0.
 rewrite list_map_nth in H0.
 match type of H0 with option_map _ ?A = _ =>
   revert H0; case_eq A; intros
 end.
 destruct p; simpl in H1. inv H1.
 exists g.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
 rewrite <- H0. f_equal.
 rewrite rev_length. rewrite map_length.
 clear - RANGE.
 rewrite Zlength_rev in RANGE. rewrite Zlength_correct in RANGE.
 rewrite <- (Coqlib.nat_of_Z_eq (Z.pos b)) in * by omega.
 unfold nat_of_Z in *. rewrite Z2Nat.inj_pos in *.
 forget (Pos.to_nat b) as n. clear b.
 replace (Z.of_nat n - 1) with (Z.of_nat (n-1)) by (rewrite inj_minus1 by omega; f_equal; auto).
 rewrite Nat2Z.id.
 omega.
 inv H1.
 rewrite rev_length. rewrite map_length.
 clear - RANGE. rewrite Zlength_correct in RANGE.
 rewrite rev_length in RANGE.
 forget (length (prog_defs prog)) as N.
 assert (Z_of_nat N > 0) by omega.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and False (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) a (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
 destruct N; inv H.
 assert (Pos.to_nat b > 0)%nat; [apply Pos2Nat.is_pos| omega].
Qed.*)

Lemma initial_core_ok: forall (prog: program) G n m,
      list_norepet (prog_defs_names prog) ->
      match_fdecs (prog_funct prog) G ->
      Genv.init_mem prog = Some m ->
     initial_rmap_ok m (initial_core (Genv.globalenv prog) G n).
Proof.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
intros.
rename H1 into Hm.
intros [b z]. simpl.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
unfold initial_core; simpl.
rewrite <- core_resource_at.
rewrite resource_at_make_rmap.
(* Goal: @eq resource match match @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n) loc end with | NO sh nsh => NO sh nsh | YES sh rsh k p => YES sh rsh k (preds_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) p) | PURE k p => PURE k (preds_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) p) end match @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc end *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
unfold initial_core'.
simpl in *.
if_tac; [ | rewrite core_NO; auto].
case_eq (@Genv.invert_symbol (Ctypes.fundef function) type
       (@Genv.globalenv (Ctypes.fundef function) type prog) b);
   intros;  try now (rewrite core_NO; auto).
case_eq (find_id i G); intros; [ | rewrite core_NO; auto].
apply Genv.invert_find_symbol in H2.
pose proof (Genv.find_symbol_not_fresh _ _ Hm H2).
unfold valid_block in H4.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
split; intros.
contradiction.
destruct (match_fdecs_exists_Gfun _ _ _ _ H3 H0) as [fd [? _]].
destruct f.
split; auto.
subst z.
destruct (find_symbol_globalenv _ _ _ H H2) as [RANGE [d ?]].
assert (d = Gfun fd).
clear - H H5 H1.
unfold prog_defs_names in H.
change (AST.prog_defs prog) with (prog_defs prog) in H.
(* Goal: forall (_ : @eq (option funspec) (find_id i G) (@Some funspec f)) (_ : match_fdecs (@prog_funct' (Ctypes.fundef function) type (@prog_defs function prog)) G), @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) (@prog_defs function prog)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
forget (prog_defs prog) as dl. forget (nat_of_Z (Z.pos b-1)) as n.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
revert dl H H5 H1; induction n; simpl; intros.
destruct dl; inv H1.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and False (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) a (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
inv H. simpl in H5.
destruct H5. inv H; auto.
apply (in_map (@fst ident (globdef fundef type))) in H. simpl in H;  contradiction.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and False (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) a (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
destruct dl; inv H1. inv H.
simpl in H5. destruct H5. subst.
clear - H2 H3. apply nth_error_in in H2.
apply (in_map (@fst ident (globdef fundef type))) in H2. simpl in *;  contradiction.
apply (IHn dl); auto.
(* end assert d = Gfun fd *)
subst d.
clear H5.
clear - RANGE H2 H1 H Hm.
unfold Genv.init_mem in Hm.
forget (Genv.globalenv prog) as ge.
change (AST.prog_defs prog) with (prog_defs prog) in Hm.
(* Goal: forall (_ : @eq (option funspec) (find_id i G) (@Some funspec f)) (_ : match_fdecs (@prog_funct' (Ctypes.fundef function) type (@prog_defs function prog)) G), @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) (@prog_defs function prog)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
forget (prog_defs prog) as dl.
rewrite <- (rev_involutive dl) in H1,Hm.
rewrite nth_error_rev in H1.
2 : { rewrite rev_length. clear - RANGE.
      destruct RANGE.
      apply inj_lt_iff. rewrite Coqlib.nat_of_Z_eq by omega. omega. }
rename H1 into H5.
replace (length (rev dl) - nat_of_Z (Z.pos b - 1) - 1)%nat
 with (length (rev dl) - nat_of_Z (Z.pos b))%nat in H5.
2 : { rewrite rev_length.
      clear - RANGE.
      replace (nat_of_Z (Z.pos b-1)) with (nat_of_Z (Z.pos b) - 1)%nat.
      assert (nat_of_Z (Z.pos b) <= length dl)%nat.
      destruct RANGE.
      apply inj_le_iff. rewrite Coqlib.nat_of_Z_eq by omega. auto.
      assert (nat_of_Z (Z.pos b) > 0)%nat. apply inj_gt_iff.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
      rewrite Coqlib.nat_of_Z_eq by omega.  simpl. omega.
      omega. destruct RANGE as [? _].
      apply nat_of_Z_lem1.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
      assert (nat_of_Z (Z.pos b) > 0)%nat. apply inj_gt_iff. simpl.
      pose proof (Pos2Nat.is_pos b); omega.
      omega. }
assert (0 < nat_of_Z (Z.pos b) <= length dl)%nat.
{ clear - RANGE.
  destruct RANGE; split.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  apply inj_lt_iff. rewrite Coqlib.nat_of_Z_eq; try omega. simpl. auto.
  apply inj_le_iff. rewrite Coqlib.nat_of_Z_eq; try omega. }
clear RANGE; rename H0 into RANGE.
unfold nat_of_Z in *. rewrite Z2Nat.inj_pos in *.
rewrite <- rev_length in RANGE.
forget (rev dl) as dl'; clear dl; rename dl' into dl.
destruct RANGE.
rewrite alloc_globals_rev_eq in Hm.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
revert m Hm H1 H5; induction dl; intros.
inv H5.
simpl in H1,Hm.
invSome.
specialize (IHdl _ Hm).
destruct (eq_dec (Pos.to_nat b) (S (length dl))).
+ rewrite e, minus_diag in H5. simpl in H5.
  inversion H5; clear H5; subst a.
  apply alloc_globals_rev_nextblock in Hm.
  rewrite Zlength_correct in Hm.
  rewrite <- inj_S in Hm. rewrite <- e in Hm.
  rewrite positive_nat_Z in Hm.  rewrite Pos2Z.id in Hm.
  subst b.
  clear IHdl H1 H0. clear dl e.
  unfold Genv.alloc_global in H6.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
  revert H6; case_eq (alloc m0 0 1); intros.
  unfold drop_perm in H6.
  destruct (range_perm_dec m1 b 0 1 Cur Freeable).
  unfold max_access_at, access_at; inv H6.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  simpl. apply alloc_result in H0. subst b.
  rewrite PMap.gss.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  simpl. auto.
  inv H6.
+ destruct IHdl.
  omega.
  replace (length (a::dl) - Pos.to_nat b)%nat with (S (length dl - Pos.to_nat b))%nat in H5.
  apply H5.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  simpl. destruct (Pos.to_nat b); omega.
  assert (b < nextblock m0)%positive.
  apply alloc_globals_rev_nextblock in Hm.
  rewrite Zlength_correct in Hm. clear - Hm n H1.
  rewrite Hm.
  apply Pos2Nat.inj_lt.
  pattern Pos.to_nat at 1; rewrite <- Z2Nat.inj_pos.
  rewrite Z2Pos.id by omega.
  rewrite Z2Nat.inj_succ by omega.
  rewrite Nat2Z.id. omega.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
  destruct (alloc_global_old _ _ _ _ H6 (b,0)) as [? ?]; auto.
  unfold max_access_at.
  rewrite <- H8.
  split; auto.
Qed.

Definition initial_jm (prog: program) m (G: funspecs) (n: nat)
        (H: Genv.init_mem prog = Some m)
        (H1: list_norepet (prog_defs_names prog))
        (H2: match_fdecs (prog_funct prog) G) : juicy_mem :=
  initial_mem m (initial_core (Genv.globalenv prog) G n)
           (initial_core_ok _ _ _ m H1 H2 H).

Lemma initial_jm_age (prog: program) m (G: funspecs) (n : nat)
        (H: Genv.init_mem prog = Some m)
        (H1: list_norepet (prog_defs_names prog))
        (H2: match_fdecs (prog_funct prog) G) :
age
    (initial_mem m (initial_core (Genv.globalenv prog) G (S n)) (initial_core_ok _ _ _ m H1 H2 H))
    (initial_mem m (initial_core (Genv.globalenv prog) G    n ) (initial_core_ok _ _ _ m H1 H2 H)).
Proof.
(* Goal: @age juicy_mem juicy_mem_ageable (initial_mem m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n)) (initial_core_ok prog G (S n) m H1 H2 H)) (initial_mem m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (initial_core_ok prog G n m H1 H2 H)) *)
apply age1_juicy_mem_unpack''; [ | reflexivity].
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
simpl.
(* Goal: @age rmap ag_rmap (inflate_initial_mem m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n))) (inflate_initial_mem m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) *)
unfold inflate_initial_mem in *.
(* Goal: @age rmap ag_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n)))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (inflate_initial_mem' m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n)))) (@eq ghost (ghost_of phi) (ghost_of (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n)))))) (make_rmap (inflate_initial_mem' m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n))) (ghost_of (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n))) (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n))) (inflate_initial_mem'_fmap m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n))) (ghost_of_approx (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n))))) (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (inflate_initial_mem' m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) (@eq ghost (ghost_of phi) (ghost_of (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))))) (make_rmap (inflate_initial_mem' m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (ghost_of (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (inflate_initial_mem'_fmap m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (ghost_of_approx (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)))) *)
match goal with |- context [ proj1_sig ?x ] => destruct x as (r & lev & bah & Hg1); simpl end.
(* Goal: @age rmap ag_rmap r (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (inflate_initial_mem' m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) (@eq ghost (ghost_of phi) (ghost_of (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))))) (make_rmap (inflate_initial_mem' m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (ghost_of (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (inflate_initial_mem'_fmap m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (ghost_of_approx (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)))) *)
match goal with |- context [ proj1_sig ?x ] => destruct x as (r' & lev' & bah' & Hg2); simpl end.
(* Goal: @age rmap ag_rmap r r' *)
apply rmap_age_i.
(* Goal: @eq nat (@level rmap ag_rmap r) (S (@level rmap ag_rmap r')) *)
(* Goal: forall l : AV.address, @eq resource (resource_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (resource_at r l)) (resource_at r' l) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
rewrite lev,lev'.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
unfold initial_core; simpl.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
rewrite !level_make_rmap. auto.
(* Goal: forall l : AV.address, @eq resource (resource_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (resource_at r l)) (resource_at r' l) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
intro loc.
(* Goal: @eq resource (resource_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (resource_at r loc)) (resource_at r' loc) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
rewrite bah, bah'.
(* Goal: @join resource Join_resource (inflate_initial_mem' m (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (@core resource Join_resource Sep_resource (inflate_initial_mem' m (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc)) (inflate_initial_mem' m (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) loc) *)
unfold inflate_initial_mem'.
(* Goal: @eq resource (resource_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n)) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n)) loc end | None => NO Share.bot bot_unreadable end) match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) loc end | None => NO Share.bot bot_unreadable end *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
destruct (access_at m loc Cur); [ | reflexivity].
(* Goal: @eq resource (resource_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) match p with | Freeable => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Writable => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Readable => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Nonempty => match resource_at (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n)) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => resource_at (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n)) loc end end) match p with | Freeable => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Writable => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Readable => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Nonempty => match resource_at (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => resource_at (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) loc end end *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
destruct p; unfold resource_fmap; f_equal; try apply preds_fmap_NoneP.
(* Goal: @eq (forall (i : list Type) (_ : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (_ : bool) (_ : environ), @pred rmap ag_rmap) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) (approx (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) a)) a0 a1))) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
unfold initial_core.
(* Goal: @join resource Join_resource match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc end | None => NO Share.bot bot_unreadable end (@core resource Join_resource Sep_resource match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc end | None => NO Share.bot bot_unreadable end) match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) loc end | None => NO Share.bot bot_unreadable end *)
rewrite !resource_at_make_rmap.
(* Goal: @eq resource match match @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n) loc end with | NO sh nsh => NO sh nsh | YES sh rsh k p => YES sh rsh k (preds_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) p) | PURE k p => PURE k (preds_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) p) end match @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc end *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
unfold initial_core'.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
if_tac; auto.
(* Goal: @eq resource match match match @Genv.invert_symbol (Ctypes.fundef function) type (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) (@fst block Z loc) with | Some id => match find_id id G with | Some (mk_funspec fsig cc A P Q P_ne Q_ne as f) => PURE (FUN fsig cc) (SomeP (SpecTT A) (fun ts : list Type => @fmap (dependent_type_functor_rec ts (SpecTT A)) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@packPQ A P Q ts))) | None => NO Share.bot bot_unreadable end | None => NO Share.bot bot_unreadable end with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => match @Genv.invert_symbol (Ctypes.fundef function) type (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) (@fst block Z loc) with | Some id => match find_id id G with | Some (mk_funspec fsig cc A P Q P_ne Q_ne as f) => PURE (FUN fsig cc) (SomeP (SpecTT A) (fun ts : list Type => @fmap (dependent_type_functor_rec ts (SpecTT A)) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@packPQ A P Q ts))) | None => NO Share.bot bot_unreadable end | None => NO Share.bot bot_unreadable end end with | NO sh nsh => NO sh nsh | YES sh rsh k p => YES sh rsh k (preds_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) p) | PURE k p => PURE k (preds_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) p) end match match @Genv.invert_symbol (Ctypes.fundef function) type (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) (@fst block Z loc) with | Some id => match find_id id G with | Some (mk_funspec fsig cc A P Q P_ne Q_ne as f) => PURE (FUN fsig cc) (SomeP (SpecTT A) (fun ts : list Type => @fmap (dependent_type_functor_rec ts (SpecTT A)) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) (@packPQ A P Q ts))) | None => NO Share.bot bot_unreadable end | None => NO Share.bot bot_unreadable end with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => match @Genv.invert_symbol (Ctypes.fundef function) type (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) (@fst block Z loc) with | Some id => match find_id id G with | Some (mk_funspec fsig cc A P Q P_ne Q_ne as f) => PURE (FUN fsig cc) (SomeP (SpecTT A) (fun ts : list Type => @fmap (dependent_type_functor_rec ts (SpecTT A)) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) (@packPQ A P Q ts))) | None => NO Share.bot bot_unreadable end | None => NO Share.bot bot_unreadable end end *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
unfold fundef.
destruct (Genv.invert_symbol (Genv.globalenv (program_of_program prog))
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
        (fst loc)); auto.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
destruct (find_id i G); auto.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
destruct f; auto.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
f_equal.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
simpl.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
f_equal.
(* Goal: @eq (forall (i : list Type) (_ : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (_ : bool) (_ : environ), @pred rmap ag_rmap) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1))) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
rewrite lev'.
(* Goal: @eq (forall (i : list Type) (_ : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (_ : bool) (_ : environ), @pred rmap ag_rmap) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) (approx (@level rmap ag_rmap (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) a)) a0 a1))) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
unfold initial_core.
(* Goal: @eq (forall (i : list Type) (_ : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (_ : bool) (_ : environ), @pred rmap ag_rmap) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds)))))))) (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds))))))))) (approx (@level rmap ag_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds))))))))) a)) a0 a1))) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
rewrite level_make_rmap.
(* Goal: @eq (forall (i : list Type) (_ : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (_ : bool) (_ : environ), @pred rmap ag_rmap) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a)) a0 a1))) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
extensionality ts x b rho.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (approx (S n) (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x)) b rho))) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
rewrite fmap_app.
match goal with
| |- ?A (?B ?C) = _ => change (A (B C)) with ((A oo B) C)
end.
rewrite approx_oo_approx' by omega.
rewrite approx'_oo_approx by omega.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
auto.
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
rewrite Hg1, Hg2.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
unfold initial_core; rewrite !ghost_of_make_rmap; auto.
Qed.

Lemma initial_core_ext_ok: forall {Z} (ora : Z) (prog: program) G n m,
      list_norepet (prog_defs_names prog) ->
      match_fdecs (prog_funct prog) G ->
      Genv.init_mem prog = Some m ->
     initial_rmap_ok m (initial_core_ext ora (Genv.globalenv prog) G n).
Proof.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
intros.
rename H1 into Hm.
intros [b z]. simpl.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
unfold initial_core_ext; simpl.
rewrite <- core_resource_at.
rewrite resource_at_make_rmap.
(* Goal: @eq resource match match @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G (S n) loc end with | NO sh nsh => NO sh nsh | YES sh rsh k p => YES sh rsh k (preds_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) p) | PURE k p => PURE k (preds_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) p) end match @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc end *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
unfold initial_core'.
simpl in *.
if_tac; [ | rewrite core_NO; auto].
case_eq (@Genv.invert_symbol (Ctypes.fundef function) type (@Genv.globalenv (Ctypes.fundef function) type prog) b);
   intros;  try now (rewrite core_NO; auto).
case_eq (find_id i G); intros; [ | rewrite core_NO; auto].
apply Genv.invert_find_symbol in H2.
pose proof (Genv.find_symbol_not_fresh _ _ Hm H2).
unfold valid_block in H4.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
split; intros.
contradiction.
destruct (match_fdecs_exists_Gfun _ _ _ _ H3 H0) as [fd [? _]].
destruct f.
split; auto.
subst z.
destruct (find_symbol_globalenv _ _ _ H H2) as [RANGE [d ?]].
assert (d = Gfun fd).
clear - H H5 H1.
unfold prog_defs_names in H.
change (AST.prog_defs prog) with (prog_defs prog) in H.
(* Goal: forall (_ : @eq (option funspec) (find_id i G) (@Some funspec f)) (_ : match_fdecs (@prog_funct' (Ctypes.fundef function) type (@prog_defs function prog)) G), @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) (@prog_defs function prog)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
forget (prog_defs prog) as dl. forget (nat_of_Z (Z.pos b-1)) as n.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
revert dl H H5 H1; induction n; simpl; intros.
destruct dl; inv H1.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and False (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) a (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
inv H. simpl in H5.
destruct H5. inv H; auto.
apply (in_map (@fst ident (globdef fundef type))) in H. simpl in H;  contradiction.
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and False (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
(* Goal: @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (or (@eq (prod ident (globdef (Ctypes.fundef function) type)) a (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd))) (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) dl)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
destruct dl; inv H1. inv H.
simpl in H5. destruct H5. subst.
clear - H2 H3. apply nth_error_in in H2.
apply (in_map (@fst ident (globdef fundef type))) in H2. simpl in *;  contradiction.
apply (IHn dl); auto.
(* end assert d = Gfun fd *)
subst d.
clear H5.
clear - RANGE H2 H1 H Hm.
unfold Genv.init_mem in Hm.
forget (Genv.globalenv prog) as ge.
change (AST.prog_defs prog) with (prog_defs prog) in Hm.
(* Goal: forall (_ : @eq (option funspec) (find_id i G) (@Some funspec f)) (_ : match_fdecs (@prog_funct' (Ctypes.fundef function) type (@prog_defs function prog)) G), @ex (Ctypes.fundef function) (fun fd : Ctypes.fundef function => and (@In (prod ident (globdef (Ctypes.fundef function) type)) (@pair ident (globdef (Ctypes.fundef function) type) i (@Gfun (Ctypes.fundef function) type fd)) (@prog_defs function prog)) (@eq type (type_of_fundef fd) (type_of_funspec f))) *)
forget (prog_defs prog) as dl.
rewrite <- (rev_involutive dl) in H1,Hm.
rewrite nth_error_rev in H1.
2 : {
  rewrite rev_length. clear - RANGE.
  destruct RANGE.
  apply inj_lt_iff. rewrite Coqlib.nat_of_Z_eq by omega. omega. }
rename H1 into H5.
replace (length (rev dl) - nat_of_Z (Z.pos b - 1) - 1)%nat
  with (length (rev dl) - nat_of_Z (Z.pos b))%nat in H5.
2 : { rewrite rev_length.
  clear - RANGE.
  replace (nat_of_Z (Z.pos b-1)) with (nat_of_Z (Z.pos b) - 1)%nat.
  assert (nat_of_Z (Z.pos b) <= length dl)%nat.
  destruct RANGE.
  apply inj_le_iff. rewrite Coqlib.nat_of_Z_eq by omega. auto.
  assert (nat_of_Z (Z.pos b) > 0)%nat. apply inj_gt_iff.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  rewrite Coqlib.nat_of_Z_eq by omega.  simpl. omega.
  omega. destruct RANGE as [? _].
  apply nat_of_Z_lem1.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  assert (nat_of_Z (Z.pos b) > 0)%nat. apply inj_gt_iff. simpl.
  pose proof (Pos2Nat.is_pos b); omega.
  omega. }
assert (0 < nat_of_Z (Z.pos b) <= length dl)%nat.
{ clear - RANGE.
  destruct RANGE; split.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  apply inj_lt_iff. rewrite Coqlib.nat_of_Z_eq; try omega. simpl. auto.
  apply inj_le_iff. rewrite Coqlib.nat_of_Z_eq; try omega. }
clear RANGE; rename H0 into RANGE.
unfold nat_of_Z in *. rewrite Z2Nat.inj_pos in *.
rewrite <- rev_length in RANGE.
forget (rev dl) as dl'; clear dl; rename dl' into dl.
destruct RANGE.
rewrite alloc_globals_rev_eq in Hm.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
revert m Hm H1 H5; induction dl; intros.
inv H5.
simpl in H1,Hm.
invSome.
specialize (IHdl _ Hm).
destruct (eq_dec (Pos.to_nat b) (S (length dl))).
+ rewrite e, minus_diag in H5. simpl in H5.
  inversion H5; clear H5; subst a.
  apply alloc_globals_rev_nextblock in Hm.
  rewrite Zlength_correct in Hm.
  rewrite <- inj_S in Hm. rewrite <- e in Hm.
  rewrite positive_nat_Z in Hm.  rewrite Pos2Z.id in Hm.
  subst b.
  clear IHdl H1 H0. clear dl e.
  unfold Genv.alloc_global in H6.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
  revert H6; case_eq (alloc m0 0 1); intros.
  unfold drop_perm in H6.
  destruct (range_perm_dec m1 b 0 1 Cur Freeable).
  unfold max_access_at, access_at; inv H6.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  simpl. apply alloc_result in H0. subst b.
  rewrite PMap.gss.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  simpl. auto.
  inv H6.
+ destruct IHdl.
  omega.
  replace (length (a::dl) - Pos.to_nat b)%nat with (S (length dl - Pos.to_nat b))%nat in H5.
  apply H5.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  simpl. destruct (Pos.to_nat b); omega.
  assert (b < nextblock m0)%positive.
  { apply alloc_globals_rev_nextblock in Hm.
    rewrite Zlength_correct in Hm. clear - Hm n H1.
    rewrite Hm.
    apply Pos2Nat.inj_lt.
    pattern Pos.to_nat at 1; rewrite <- Z2Nat.inj_pos.
    rewrite Z2Pos.id by omega.
    rewrite Z2Nat.inj_succ by omega.
    rewrite Nat2Z.id. omega. }
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
  destruct (alloc_global_old _ _ _ _ H6 (b,0)) as [? ?]; auto.
  unfold max_access_at.
  rewrite <- H8.
  split; auto.
Qed.

Definition initial_jm_ext {Z} (ora : Z) (prog: program) m (G: funspecs) (n: nat)
        (H: Genv.init_mem prog = Some m)
        (H1: list_norepet (prog_defs_names prog))
        (H2: match_fdecs (prog_funct prog) G) : juicy_mem :=
  initial_mem m (initial_core_ext ora (Genv.globalenv prog) G n)
           (initial_core_ext_ok _ _ _ _ m H1 H2 H).

Require Import VST.veric.ghost_PCM.
Lemma initial_jm_ext_eq : forall {Z} (ora : Z) (prog: program) m (G: funspecs) (n: nat)
        (H: Genv.init_mem prog = Some m)
        (H1: list_norepet (prog_defs_names prog))
        (H2: match_fdecs (prog_funct prog) G),
  join (m_phi (initial_jm prog m G n H H1 H2))
       (set_ghost (core (m_phi (initial_jm prog m G n H H1 H2))) (Some (ext_ghost ora, NoneP) :: nil) eq_refl)
       (m_phi (initial_jm_ext ora prog m G n H H1 H2)).
Proof.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
  intros.
(* Goal: @join rmap Join_rmap (m_phi (initial_jm prog m G n H H1 H2)) (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  apply resource_at_join2.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  - simpl.
(* Goal: @eq nat (@level rmap ag_rmap (inflate_initial_mem m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) (@level rmap ag_rmap (inflate_initial_mem m (@initial_world.initial_core_ext function Z ora (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) *)
    rewrite !inflate_initial_mem_level.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
    unfold initial_core, initial_core_ext; rewrite !level_make_rmap; auto.
(* Goal: @eq (forall (i : list Type) (_ : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (_ : bool) (_ : environ), @pred rmap ag_rmap) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds)))))))) (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds))))))))) (approx (@level rmap ag_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a2 : @ghost.G g => @ghost.valid g a2))) preds))))))))) a)) a0 a1))) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
  - unfold set_ghost; rewrite level_make_rmap.
(* Goal: @eq nat (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2)))) (@level rmap ag_rmap (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2))) *)
    rewrite level_core.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
    simpl.
(* Goal: @eq nat (@level rmap ag_rmap (inflate_initial_mem m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) (@level rmap ag_rmap (inflate_initial_mem m (@initial_world.initial_core_ext function Z ora (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) *)
    rewrite !inflate_initial_mem_level.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
    unfold initial_core, initial_core_ext; rewrite !level_make_rmap; auto.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
  - intros.
    unfold set_ghost; rewrite resource_at_make_rmap, <- core_resource_at.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
    simpl.
(* Goal: @join ghost ghost_join (ghost_of (inflate_initial_mem m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (ghost_of (inflate_initial_mem m (@initial_world.initial_core_ext function Z ora (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) *)
    unfold initial_core, initial_core_ext, inflate_initial_mem.
(* Goal: @join resource Join_resource match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc end | None => NO Share.bot bot_unreadable end (@core resource Join_resource Sep_resource match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc end | None => NO Share.bot bot_unreadable end) match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) loc end | None => NO Share.bot bot_unreadable end *)
    rewrite !resource_at_make_rmap.
(* Goal: @join resource Join_resource (inflate_initial_mem' m (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (@core resource Join_resource Sep_resource (inflate_initial_mem' m (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc)) (inflate_initial_mem' m (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) loc) *)
    unfold inflate_initial_mem'.
(* Goal: @join resource Join_resource match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc end | None => NO Share.bot bot_unreadable end (@core resource Join_resource Sep_resource match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc end | None => NO Share.bot bot_unreadable end) match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) loc end | None => NO Share.bot bot_unreadable end *)
    rewrite !resource_at_make_rmap.
(* Goal: @join resource Join_resource match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc end | None => NO Share.bot bot_unreadable end (@core resource Join_resource Sep_resource match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc end | None => NO Share.bot bot_unreadable end) match access_at m loc Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m loc)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP | Some (Nonempty as p) => match @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => @initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n loc end | None => NO Share.bot bot_unreadable end *)
    apply join_comm, core_unit.
  - unfold set_ghost; rewrite ghost_of_make_rmap.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
    simpl.
(* Goal: @join ghost ghost_join (ghost_of (inflate_initial_mem m (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (ghost_of (inflate_initial_mem m (@initial_world.initial_core_ext function Z ora (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) *)
    unfold initial_core, initial_core_ext, inflate_initial_mem.
(* Goal: @join ghost ghost_join (ghost_of (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (@proj1_sig rmap (fun phi0 : rmap => and (@eq nat (@level rmap ag_rmap phi0) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi0) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi0) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (inflate_initial_mem' m (@proj1_sig rmap (fun phi0 : rmap => and (@eq nat (@level rmap ag_rmap phi0) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi0) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi0) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))))) (@eq ghost (ghost_of phi) (ghost_of (@proj1_sig rmap (fun phi0 : rmap => and (@eq nat (@level rmap ag_rmap phi0) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi0) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi0) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))))))) (make_rmap (inflate_initial_mem' m (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) (ghost_of (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) (@level rmap ag_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) (inflate_initial_mem'_fmap m (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))) (ghost_of_approx (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) n (initial_world.initial_core_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (ghost_of (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (@proj1_sig rmap (fun phi0 : rmap => and (@eq nat (@level rmap ag_rmap phi0) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi0) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi0) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (inflate_initial_mem' m (@proj1_sig rmap (fun phi0 : rmap => and (@eq nat (@level rmap ag_rmap phi0) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi0) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi0) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))))) (@eq ghost (ghost_of phi) (ghost_of (@proj1_sig rmap (fun phi0 : rmap => and (@eq nat (@level rmap ag_rmap phi0) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi0) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi0) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))))))) (make_rmap (inflate_initial_mem' m (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))))) (ghost_of (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))))) (@level rmap ag_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))))) (inflate_initial_mem'_fmap m (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))))) (ghost_of_approx (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) n) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n)) (@eq ghost (ghost_of phi) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) (make_rmap (@initial_world.initial_core' function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) n (initial_world.initial_core_ext_obligation_1 function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n) (@eq_refl ghost (ghost_fmap (approx n) (approx n) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))))))))))) *)
    rewrite !ghost_of_make_rmap.
(* Goal: @join ghost ghost_join (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) *)
    constructor.
Qed.

(* 
Definition prog_vars (p: program) := prog_vars' (prog_defs p).
*)
Notation prog_vars := (@prog_vars function).

Lemma initial_jm_without_locks prog m G n H H1 H2:
  no_locks (m_phi (initial_jm prog m G n H H1 H2)).
Proof.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  simpl.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  unfold inflate_initial_mem; simpl.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  match goal with |- context [ proj1_sig ?a ] => destruct a as (phi & lev & E & ?) end; simpl.
(* Goal: no_locks phi *)
  unfold inflate_initial_mem' in E.
(* Goal: no_locks phi *)
  unfold resource_at in E.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
  unfold no_locks, "@"; intros.
(* Goal: not (@eq resource (@fst (forall _ : AV.address, resource) ghost (@snd nat rmap' (unsquash phi)) addr) (YES sh sh' (LK z z') P)) *)
  rewrite E.
(* Goal: not (@eq resource match access_at m addr Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m addr)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m addr)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m addr)) NoneP | Some (Nonempty as p) => match @fst (forall _ : AV.address, resource) ghost (@snd nat rmap' (unsquash (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) addr with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => @fst (forall _ : AV.address, resource) ghost (@snd nat rmap' (unsquash (@initial_world.initial_core function (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) addr end | None => NO Share.bot bot_unreadable end (YES sh sh' (LK z z') P)) *)
  destruct (access_at m addr); [ |congruence].
(* Goal: not (@eq resource match p with | Freeable => YES Share.top readable_share_top (VAL (contents_at m addr)) NoneP | Writable => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m addr)) NoneP | Readable => YES Ers readable_Ers (VAL (contents_at m addr)) NoneP | Nonempty => match @fst (forall _ : AV.address, resource) ghost (@snd nat rmap' (unsquash (@initial_world.initial_core_ext function Z ora (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) addr with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => @fst (forall _ : AV.address, resource) ghost (@snd nat rmap' (unsquash (@initial_world.initial_core_ext function Z ora (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) addr end end (YES sh sh' (LK z z') P)) *)
  destruct p; try congruence.
  destruct (fst ((snd (unsquash (initial_core (Genv.globalenv prog) G n)))) addr);
  congruence.
Qed.

Lemma initial_jm_ext_without_locks {Z} (ora : Z) prog m G n H H1 H2:
  no_locks (m_phi (initial_jm_ext ora prog m G n H H1 H2)).
Proof.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  simpl.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  unfold inflate_initial_mem; simpl.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  match goal with |- context [ proj1_sig ?a ] => destruct a as (phi & lev & E & ?) end; simpl.
(* Goal: no_locks phi *)
  unfold inflate_initial_mem' in E.
(* Goal: no_locks phi *)
  unfold resource_at in E.
(* Goal: forall loc : AV.address, @join resource Join_resource (resource_at (m_phi (initial_jm prog m G n H H1 H2)) loc) (resource_at (set_ghost (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@eq_refl ghost (ghost_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap (m_phi (initial_jm prog m G n H H1 H2))))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))))))) loc) (resource_at (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) loc) *)
  unfold no_locks, "@"; intros.
(* Goal: not (@eq resource (@fst (forall _ : AV.address, resource) ghost (@snd nat rmap' (unsquash phi)) addr) (YES sh sh' (LK z z') P)) *)
  rewrite E.
(* Goal: not (@eq resource match access_at m addr Cur with | Some (Freeable as p) => YES Share.top readable_share_top (VAL (contents_at m addr)) NoneP | Some (Writable as p) => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m addr)) NoneP | Some (Readable as p) => YES Ers readable_Ers (VAL (contents_at m addr)) NoneP | Some (Nonempty as p) => match @fst (forall _ : AV.address, resource) ghost (@snd nat rmap' (unsquash (@initial_world.initial_core_ext function Z ora (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) addr with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p0 => NO extern_retainer nonreadable_extern_retainer | PURE k p0 => @fst (forall _ : AV.address, resource) ghost (@snd nat rmap' (unsquash (@initial_world.initial_core_ext function Z ora (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) addr end | None => NO Share.bot bot_unreadable end (YES sh sh' (LK z z') P)) *)
  destruct (access_at m addr); try congruence.
(* Goal: not (@eq resource match p with | Freeable => YES Share.top readable_share_top (VAL (contents_at m addr)) NoneP | Writable => YES Ews (@writable_readable Ews writable_Ews) (VAL (contents_at m addr)) NoneP | Readable => YES Ers readable_Ers (VAL (contents_at m addr)) NoneP | Nonempty => match @fst (forall _ : AV.address, resource) ghost (@snd nat rmap' (unsquash (@initial_world.initial_core_ext function Z ora (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) addr with | NO sh n => NO extern_retainer nonreadable_extern_retainer | YES sh r k p => NO extern_retainer nonreadable_extern_retainer | PURE k p => @fst (forall _ : AV.address, resource) ghost (@snd nat rmap' (unsquash (@initial_world.initial_core_ext function Z ora (@Genv.globalenv (Ctypes.fundef function) type (@program_of_program function prog)) G n))) addr end end (YES sh sh' (LK z z') P)) *)
  destruct p; try congruence.
  destruct (fst ((snd (unsquash (initial_core_ext ora (Genv.globalenv prog) G n)))) addr);
   congruence.
Qed.

(* Specialization of stuff in initial_world:
Lemma level_initial_core ge G n : level (initial_core ge G n) = n.
Proof.
  apply level_make_rmap.
Qed.*)

Definition matchfunspecs (ge : genv) (G : funspecs) (Phi : rmap) : Prop :=
  forall (b : block) fsig cc A P Q,
    func_at'' fsig cc A P Q (b, 0%Z) Phi ->
    exists id P' Q' P'_ne Q'_ne,
      Genv.find_symbol ge id = Some b /\
      find_id id G = Some (mk_funspec fsig cc A P' Q' P'_ne Q'_ne) /\
      cond_approx_eq (level Phi) A P P' /\
      cond_approx_eq (level Phi) A Q Q'.

Lemma initial_jm_matchfunspecs prog m G n H H1 H2:
  matchfunspecs (globalenv prog) G (m_phi (initial_jm prog m G n H H1 H2)).
Proof.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  simpl.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  unfold inflate_initial_mem; simpl.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  match goal with |- context [ proj1_sig ?a ] => destruct a as (phi & lev & E & ?) end; simpl.
(* Goal: no_locks phi *)
  unfold inflate_initial_mem' in E.
(* Goal: no_locks phi *)
  unfold resource_at in E.
  intros b fsig cc A P Q FAT.
  unfold func_at'' in *.
  rewrite level_initial_core in lev.

  set (pp := SomeP _ _) in FAT.
  assert (Pi :
            initial_core (Genv.globalenv prog) G n @ (b, 0)
            = PURE (FUN fsig cc) (preds_fmap (approx n) (approx n) pp)).
  {
    simpl in FAT.
    pose proof FAT as E2.
    unfold "@" in *.
    rewrite E in FAT.
    destruct (access_at m (b, 0)) as [[]|]; simpl in E2; try congruence.
    set (r := fst ( _) _) in FAT at 2.
    destruct (fst ( (snd (unsquash (initial_core (Genv.globalenv prog) G n)))) (b, 0))
      as [t | t p k p0 | k p] eqn:E'''; simpl in E2; try congruence.
    subst r.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
    injection FAT as -> ->; f_equal. subst pp. f_equal.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
    simpl. f_equal.
    repeat extensionality.
    repeat (f_equal; auto).
  }

  clear -Pi lev.

  unfold initial_core in *.
  rewrite resource_at_make_rmap in Pi.
  unfold initial_core' in *.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
  if_tac in Pi. 2:tauto.
  simpl fst in Pi.
  unfold fundef in *.
  destruct (Genv.invert_symbol (Genv.globalenv prog) b) as [i|] eqn:Eb. 2: congruence.
  destruct (find_id i G) as [f0 |] eqn:Ei. 2:congruence.
  destruct f0 as [f1 c0 A0 P0 Q0 P_ne0 Q_ne0].

  subst pp.
  injection Pi as <- -> -> EE.
  apply inj_pair2 in EE.
  apply Genv.invert_find_symbol in Eb.
  unfold filter_genv in *.
  exists i, P0, Q0, P_ne0, Q_ne0.
  split. assumption.
  split. assumption.
  subst n.

(* Goal: @join ghost ghost_join (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) *)
  constructor.
  all: intros ts.
  all: apply equal_f_dep with (x := ts) in EE.
  all: extensionality a.
  all: apply equal_f_dep with (x := a) in EE.

  1: apply equal_f_dep with (x := true) in EE.
  2: apply equal_f_dep with (x := false) in EE.

  all: extensionality ge.
  all: apply equal_f_dep with (x := ge) in EE.
  all: simpl in *.
  all: symmetry; rewrite (* Epp',  *)<-EE.
  all: reflexivity.
Qed.

Lemma initial_jm_ext_matchfunspecs {Z} (ora : Z) prog m G n H H1 H2:
  matchfunspecs (globalenv prog) G (m_phi (initial_jm_ext ora prog m G n H H1 H2)).
Proof.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  simpl.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  unfold inflate_initial_mem; simpl.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
  match goal with |- context [ proj1_sig ?a ] => destruct a as (phi & lev & E & ?) end; simpl.
(* Goal: no_locks phi *)
  unfold inflate_initial_mem' in E.
(* Goal: no_locks phi *)
  unfold resource_at in E.
  intros b fsig cc A P Q FAT.
  unfold func_at'' in *.
  unfold initial_core_ext in lev; rewrite level_make_rmap in lev.

  set (pp := SomeP _ _) in FAT.
  assert (Pi :
            initial_core_ext ora (Genv.globalenv prog) G n @ (b, 0)
            = PURE (FUN fsig cc) (preds_fmap (approx n) (approx n) pp)).
  {
    simpl in FAT.
    pose proof FAT as E2.
    unfold "@" in *.
    rewrite E in FAT.
    destruct (access_at m (b, 0)) as [[]|]; simpl in E2; try congruence.
    set (r := fst ( _) _) in FAT at 2.
    destruct (fst ( (snd (unsquash (initial_core_ext ora (Genv.globalenv prog) G n)))) (b, 0))
      as [t | t p k p0 | k p] eqn:E'''; simpl in E2; try congruence.
    subst r.
(* Goal: @eq preds (SomeP (SpecTT A) (fun (i : list Type) (a : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx (@level rmap ag_rmap r') (approx (S n) (@packPQ A P Q i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (S n)) (approx (S n)) (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) a)) a0 a1)))) (SomeP (SpecTT A) (fun (ts : list Type) (a : _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) (a0 : bool) (a1 : environ) => approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) a) a0 a1))) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
    injection FAT as -> ->; f_equal. subst pp. f_equal.
(* Goal: no_locks (m_phi (@initial_jm_ext Z ora prog m G n H H1 H2)) *)
    simpl. f_equal.
    repeat extensionality.
    repeat (f_equal; auto).
  }

  clear -Pi lev.

  unfold initial_core_ext in *.
  rewrite resource_at_make_rmap in Pi.
  unfold initial_core' in *.
(* Goal: @eq (@pred rmap ag_rmap) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) (approx n (@packPQ A P Q ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx n) (approx n) x) b rho)) *)
(* Goal: @eq ghost (ghost_fmap (approx (@level rmap ag_rmap r')) (approx (@level rmap ag_rmap r')) (ghost_of r)) (ghost_of r') *)
  if_tac in Pi. 2:tauto.
  simpl fst in Pi.
  unfold fundef in *.
  destruct (Genv.invert_symbol (Genv.globalenv prog) b) as [i|] eqn:Eb. 2: congruence.
  destruct (find_id i G) as [f0 |] eqn:Ei. 2:congruence.
  destruct f0 as [f1 c0 A0 P0 Q0 P_ne0 Q_ne0].

  subst pp.
  injection Pi as <- -> -> EE.
  apply inj_pair2 in EE.
  apply Genv.invert_find_symbol in Eb.
  unfold filter_genv in *.
  exists i, P0, Q0, P_ne0, Q_ne0.
  split. assumption.
  split. assumption.
  subst n.

(* Goal: @join ghost ghost_join (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) (@cons (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)) (@Some (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds) (@pair (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds (@ext_ghost Z ora) NoneP)) (@nil (option (prod (@sigT ghost.Ghost (fun g : ghost.Ghost => @sig (@ghost.G g) (fun a : @ghost.G g => @ghost.valid g a))) preds)))) *)
  constructor.
  all: intros ts.
  all: apply equal_f_dep with (x := ts) in EE.
  all: extensionality a.
  all: apply equal_f_dep with (x := a) in EE.

  1: apply equal_f_dep with (x := true) in EE.
  2: apply equal_f_dep with (x := false) in EE.

  all: extensionality ge.
  all: apply equal_f_dep with (x := ge) in EE.
  all: simpl in *.
  all: symmetry; rewrite (* Epp',  *)<-EE.
  all: reflexivity.
Qed.
