Require Import Coq.Logic.ChoiceFacts.
Require Import Coq.Logic.FunctionalExtensionality.
Require Import Coq.Logic.ClassicalChoice.
Require Import Logic.lib.Coqlib.
Require Import Logic.lib.RelationPairs_ext.
Require Import Logic.GeneralLogic.KripkeModel.
Require Import Logic.SeparationLogic.Model.SeparationAlgebra.
Require Import Logic.SeparationLogic.Model.UpwardsClosure.
Require Import Logic.SeparationLogic.Model.DownwardsClosure.
Require Import Logic.SeparationLogic.Model.OrderedSA.
Require Import Logic.SeparationLogic.Model.OSAGenerators.

Require Import Coq.omega.Omega.


(***********************************)
(* ALGEBRAS ON NATURALS            *)
(***********************************)
 
Section nat_algs.

  Definition nat_leR: Relation nat := le.
  Definition nat_geR: Relation nat := ge.

  Instance po_nat_leR: PreOrder (@Krelation _ nat_leR) := Nat.le_preorder.

  Instance po_nat_geR: PreOrder (@Krelation _ nat_geR).
  Proof.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
    constructor.
    + hnf; intros; hnf; omega.
    + hnf; intros; hnf in *; omega.
  Qed.

  Definition indexAlg: @SeparationAlgebra nat equiv_Join.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  Proof. constructor; intros.
         - inversion H; congruence.
         - inversion H; inversion H0; subst.
(* Goal: @ex nat (fun myz : nat => and (@join nat (@equiv_Join nat) mxyz mxyz myz) (@join nat (@equiv_Join nat) mxyz myz mxyz)) *)
           exists mxyz; split; constructor; auto.
  Defined.

  (* upwards-closed*)
  Instance IndexAlg_uSA:
    @UpwardsClosedSeparationAlgebra _  nat_geR equiv_Join.
  Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
    hnf; intros.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
    inversion H; subst.
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@join nat (@equiv_Join nat) n1 n2 n) (and (@Krelation nat nat_geR m n1) (@Krelation nat nat_geR m n2)))) *)
    exists n, n. repeat split; auto.
  Qed.

  (* Increasing*)
  Instance IndexAlg_increasing:
    @IncreasingSeparationAlgebra _ nat_geR equiv_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    constructor; intro.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
    hnf; intros. inversion H; subst.
(* Goal: THeap_order x x *)
    hnf; reflexivity.
  Qed.

  (* Residual*)
  Instance IndexAlg_residual:
    @ResidualSeparationAlgebra _ nat_geR equiv_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    constructor; intro.
    exists n, n; split;
    constructor;
(* Goal: @Krelation Heap' monHeap_R' n n *)
    reflexivity.
  Qed.

  (* Unital *)
  Instance IndexAlg_unital:
    @UnitalSeparationAlgebra _ nat_geR equiv_Join.
  Proof.
    apply <- (@incr_unital_iff_residual _ nat_geR _ equiv_Join).
    + apply IndexAlg_residual.
    + apply IndexAlg_increasing.
  Qed.

  (* Minimum Algebra *)
  Inductive min_Join: nat -> nat -> nat -> Prop:=
  | min_j x y z: z <= x -> z <= y -> min_Join x y z.

  Definition minAlg: @SeparationAlgebra nat min_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
     constructor; intros.
     - inversion H; subst; constructor; auto.
     - inversion H; inversion H0; subst.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
       exists (Min.min my mz); split; constructor.
       + apply Min.le_min_l.
       + apply Min.le_min_r.
       + transitivity mxy; auto.
       + apply Min.min_glb.
(* Goal: le mxyz my *)
         * transitivity mxy; auto.
         * auto.
  Qed.

  (* downwards closure of Index algebra *)
  Lemma min_Join_is_downwards_closure: min_Join = @DownwardsClosure_J _ nat_geR equiv_Join.
  Proof.
(* Goal: @eq (forall (_ : nat) (_ : nat) (_ : nat), Prop) min_Join (@DownwardsClosure_J nat nat_geR (@equiv_Join nat)) *)
    extensionality a.
(* Goal: @eq (forall (_ : nat) (_ : nat), Prop) (min_Join a) (@DownwardsClosure_J nat nat_geR (@equiv_Join nat) a) *)
    extensionality b.
(* Goal: @eq (forall _ : nat, Prop) (min_Join a b) (@DownwardsClosure_J nat nat_geR (@equiv_Join nat) a b) *)
    extensionality c.
(* Goal: @eq Prop (min_Join a b c) (@DownwardsClosure_J nat nat_geR (@equiv_Join nat) a b c) *)
    unfold DownwardsClosure_J.
(* Goal: @eq Prop (min_Join a b c) (@ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@Krelation nat nat_geR a n1) (and (@Krelation nat nat_geR b n2) (@join nat (@equiv_Join nat) n1 n2 c))))) *)
    apply prop_ext.
(* Goal: iff (min_Join a b c) (@ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@Krelation nat nat_geR a n1) (and (@Krelation nat nat_geR b n2) (@join nat (@equiv_Join nat) n1 n2 c))))) *)
    split; intros.
    + simpl.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
      inversion H; subst.
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@Krelation nat nat_geR a n1) (and (@Krelation nat nat_geR b n2) (@join nat (@equiv_Join nat) n1 n2 c)))) *)
      exists c, c.
(* Goal: ht_ord c' c *)
      split; [| split]; hnf; auto.
    + destruct H as [a' [b' [? [? ?]]]].
(* Goal: min_Join a b c *)
      inversion H1; subst.
(* Goal: ht_ord c' c *)
      constructor; auto.
  Qed.

  (* upwards-closed *)
  Instance minAlg_uSA:
    @UpwardsClosedSeparationAlgebra _ nat_geR min_Join.
  Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
    hnf; intros.
    inversion H;  subst.
    exists m1, m2; split; constructor;
    first[ solve[transitivity m; auto] | reflexivity].
  Qed.


  (* downwards-closed *)
  Instance minAlg_dSA:
    @DownwardsClosedSeparationAlgebra _ nat_geR min_Join.
  Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
    hnf; intros.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
    inversion H; subst.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
    exists m; split ; constructor.
    - transitivity m1; auto.
    - transitivity m2; auto.
  Qed.

  (* Increasing *)
  Instance minAlg_increasing:
    @IncreasingSeparationAlgebra _ nat_geR min_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    constructor; intro.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
    hnf; intros. inversion H; subst; auto.
  Qed.

  (*Residual*)
  Instance minAlg_residual:
    @ResidualSeparationAlgebra _ nat_geR min_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    constructor; intro.
(* Goal: @ex nat (fun m : nat => @residue nat nat_geR min_Join n m) *)
    exists n, n; split.
(* Goal: @join nat min_Join n n n *)
(* Goal: @Krelation nat nat_geR n n *)
    constructor; reflexivity.
(* Goal: @Krelation Heap' monHeap_R' n n *)
    reflexivity.
  Qed.

  (* Unital *)
  Instance minAlg_unital:
    @UnitalSeparationAlgebra _ nat_geR min_Join.
  Proof.
(* Goal: @UnitalSeparationAlgebra THeap THeap_R THeap_Join *)
    apply incr_unital_iff_residual.
(* Goal: @IncreasingSeparationAlgebra nat nat_geR min_Join *)
(* Goal: @ResidualSeparationAlgebra nat nat_geR min_Join *)
    apply minAlg_increasing.
(* Goal: @ResidualSeparationAlgebra nat nat_geR min_Join *)
    apply minAlg_residual.
  Qed.
 
  
(** *Sum Algebra on NAT*)

  Inductive sum_Join: nat -> nat -> nat -> Prop:=
  |sum_j x y z: x + y = z -> sum_Join x y z.

  Definition sumAlg: @SeparationAlgebra _ sum_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
    constructor; intros.
    - inversion H; subst.
      rewrite Nat.add_comm;
(* Goal: ht_ord c' c *)
      constructor; auto.
    - inversion H; inversion H0; subst.
      exists (my+mz); split; try constructor; simpl;
(* Goal: le n (Init.Nat.add x n) *)
      omega.
  Qed.

  (* upwards-closed*)
  Instance sumAlg_uSA:
    @UpwardsClosedSeparationAlgebra _ nat_geR sum_Join.
  Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
    hnf; intros.
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@join nat sum_Join n1 n2 n) (and (@Krelation nat nat_leR m1 n1) (@Krelation nat nat_leR m2 n2)))) *)
    simpl in H0.
    inversion H;  subst.
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@join nat sum_Join n1 n2 n) (and (@Krelation nat nat_geR m1 n1) (@Krelation nat nat_geR m2 n2)))) *)
    destruct (le_lt_dec n m1).
    - exists n, 0; split; constructor; simpl; hnf; try omega.
    - exists m1, (n - m1); split; constructor; simpl; hnf in *; try omega.
  Qed.

  (* downwards-closed*)
  Instance sumAlg_dSA:
    @DownwardsClosedSeparationAlgebra _ nat_geR sum_Join.
  Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
    hnf; intros.
(* Goal: @ex nat (fun n : nat => and (@join nat sum_Join n1 n2 n) (@Krelation nat nat_leR n m)) *)
    simpl in H0, H1.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
    inversion H; subst.
    exists (n1 + n2); split; simpl; try constructor; hnf in *; try omega.
  Qed.

  (* The only increasing is 0*)
  Lemma sumAlg_zero_decreasing:
  @increasing nat nat_geR sum_Join 0.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  Proof. hnf; intros.
(* Goal: @increasing Prop discProp_R Prop_Join False *)
       hnf.
(* Goal: le n' n *)
       inversion H; subst; omega.
  Qed.

  Lemma sumAlg_zero_decreasing_only:
    forall x,
      @increasing nat nat_geR sum_Join x -> x = 0.
  Proof.
(* Goal: forall (n n' : Prop) (_ : @join Prop Prop_Join False n n'), @Krelation Prop discProp_R n n' *)
    intros.
    specialize (H 1 (x+1) ltac:(constructor; omega)).
(* Goal: @eq nat x O *)
    simpl in H. hnf in H; omega.
  Qed.
  
  (* Unital *)
  Instance sumAlg_unital:
    @UnitalSeparationAlgebra _ nat_geR sum_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
    constructor; intros.
    - exists 0; split.
      + exists n; split; simpl; try constructor; omega.
      + hnf; intros.
        inversion H; subst; simpl;
(* Goal: @Krelation nat nat_geR n n *)
        hnf; omega.
  Qed.
  
  (*Residual*)
  Instance sumAlg_residual:
    @ResidualSeparationAlgebra _ nat_geR sum_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    constructor; intro.
(* Goal: @ex nat (fun m : nat => @residue nat nat_leR sum_Join n m) *)
    exists 0, n; split.
(* Goal: @join nat sum_Join O n n *)
(* Goal: @Krelation nat nat_leR n n *)
    constructor; omega.
(* Goal: @Krelation nat nat_geR n n *)
    hnf; omega.
  Qed.

(** *Minimum Algebra on Positive integers*)
  
  Record natPlus: Type:=
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
   natp { nat_p:> nat;
      is_pos: 0 < nat_p}.


  Inductive sump_Join: natPlus -> natPlus -> natPlus -> Prop:=
  |sump_j (x y z:natPlus): x + y = z -> sump_Join x y z.

  Definition lep (n m:natPlus):= n <= m.

  Lemma lep_preorder: RelationClasses.PreOrder lep.
  Proof.
    constructor;
    hnf; intros;
    try apply Nat.le_preorder.
    destruct x, y, z;
    unfold lep in *; simpl in *;
(* Goal: le n (Init.Nat.add x n) *)
    omega.
  Qed.

  Definition natPlus_R: Relation natPlus:= lep.

  Definition po_natPlus_R: PreOrder (@Krelation _ natPlus_R) :=
    lep_preorder.

Definition sumpAlg: @SeparationAlgebra _ sump_Join.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
Proof. constructor; intros.
       - inversion H; subst.
         rewrite Nat.add_comm in H0;
(* Goal: ht_ord c' c *)
           constructor; auto.
       - inversion H; inversion H0; subst.
         exists (natp (my+mz)
                          ltac:(destruct my, mz; simpl in *; omega))
         ; split; try constructor; simpl;
(* Goal: le n (Init.Nat.add x n) *)
         omega.
Qed.

(* it is NOT upwards-closed*)
Instance sumpAlg_uSA:
  @UpwardsClosedSeparationAlgebra _ natPlus_R sump_Join.
Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@join nat sum_Join n1 n2 n) (and (@Krelation nat nat_leR m1 n1) (@Krelation nat nat_leR m2 n2)))) *)
  simpl in H0.
  inversion H;  subst.
Abort.
 
(* downwards-closed*)
Instance sumpAlg_dSA:
  @DownwardsClosedSeparationAlgebra _ natPlus_R sump_Join.
Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @ex nat (fun n : nat => and (@join nat sum_Join n1 n2 n) (@Krelation nat nat_leR n m)) *)
  simpl in H0, H1.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
  inversion H; subst.
  unfold lep in *.
  destruct m1, m2, m, n1, n2; simpl in *.
  exists (natp (nat_p3 + nat_p4) ltac:(simpl in *; omega));
(* Goal: le n (Init.Nat.add x n) *)
    split; simpl; try constructor; try omega.
(* Goal: @Krelation Heap' monHeap_R' n n *)
  reflexivity.
  unfold natPlus_R, lep in *; simpl.
  rewrite <- H2. unfold Krelation in *. simpl in *. omega.
Qed.
  
  (*it is NOT Residual*)
  Instance sumpAlg_residual:
    @ResidualSeparationAlgebra _ natPlus_R sump_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    constructor; intro.
    unfold residue.
  Abort.



(** *sum Algebra inverted*)

Definition suminvAlg: @SeparationAlgebra _ sum_Join.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
Proof. constructor; intros.
       - inversion H; subst.
         rewrite Nat.add_comm;
(* Goal: ht_ord c' c *)
         constructor; auto.
       - inversion H; inversion H0; subst.
         exists (my+mz)
         ; split; try constructor; simpl;
(* Goal: le n (Init.Nat.add x n) *)
         omega.
Qed.

(* upwards-closed*)
Instance suminvAlg_uSA:
  @UpwardsClosedSeparationAlgebra _ nat_leR sum_Join.
Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@join nat sum_Join n1 n2 n) (and (@Krelation nat nat_leR m1 n1) (@Krelation nat nat_leR m2 n2)))) *)
  simpl in H0.
  inversion H;  subst.
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@join nat sum_Join n1 n2 n) (and (@Krelation nat nat_leR m1 n1) (@Krelation nat nat_leR m2 n2)))) *)
  destruct (le_ge_dec m1 m2).
  - exists m1, (n- m1). split; constructor; simpl; hnf in *; try omega.
  - exists (n-m2), m2. split; constructor; simpl; hnf in *; try omega.
Qed.

(* downwards-closed*)
Instance suminvAlg_dSA:
  @DownwardsClosedSeparationAlgebra _ nat_leR sum_Join.
Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @ex nat (fun n : nat => and (@join nat sum_Join n1 n2 n) (@Krelation nat nat_leR n m)) *)
  simpl in H0, H1.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
  inversion H; subst.
  exists (n1 + n2); split; simpl; try constructor; hnf in *; try omega.
Qed.


  (*Residual*)
  Instance suminvAlg_increasing:
    @IncreasingSeparationAlgebra _ nat_leR sum_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    constructor; intro.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
    hnf; intros.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
    inversion H; subst.
(* Goal: @increasing Prop discProp_R Prop_Join False *)
    hnf. omega.
  Qed.

  
  (*Residual*)
  Instance suminvAlg_residual:
    @ResidualSeparationAlgebra _ nat_leR sum_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    constructor; intro.
(* Goal: @ex nat (fun m : nat => @residue nat nat_leR sum_Join n m) *)
    exists 0, n; split.
(* Goal: @join nat sum_Join O n n *)
(* Goal: @Krelation nat nat_leR n n *)
    constructor; omega.
(* Goal: @Krelation Heap' monHeap_R' n n *)
    reflexivity.
  Qed.
  
  (* Unital *)
  Instance suminvAlg_unital:
    @UnitalSeparationAlgebra _ nat_leR sum_Join.
  Proof.
(* Goal: @UnitalSeparationAlgebra THeap THeap_R THeap_Join *)
    apply incr_unital_iff_residual.
(* Goal: @IncreasingSeparationAlgebra nat nat_leR sum_Join *)
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    apply suminvAlg_increasing.
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    apply suminvAlg_residual.
  Qed.
End nat_algs.


(***********************************)
(* ALGEBRAS ON RATIONALS           *)
(***********************************)

Require Import Coq.QArith.QArith.
(*Require Import Coq.Reals.Rdefinitions.*)
(*Q overloads <= !*)
Require Import Ring.


(** *Minimum Algebra on Positive rationals*)
  (*
  Record QPlus: Type:=
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
   qp { q_p:> Q;
      qis_pos: (0 < q_p)}.


  Inductive sumqp_Join: QPlus -> QPlus -> QPlus -> Prop:=
  |sumqp_j (x y z:QPlus): (x + y) = z -> sumqp_Join x y z.

Definition leqp (n m:QPlus):= (n <= m).
Lemma leqp_preorder: RelationClasses.PreOrder leqp.
Proof.
  constructor;
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
  unfold leqp.
  - apply Qle_refl.
  - eapply Qle_trans; eauto.
Qed.

Definition QPlus_kiM: KripkeIntuitionisticModel QPlus:=
    Build_KripkeIntuitionisticModel _ _ leqp_preorder.

Definition qpAlg: @SeparationAlgebra _ sumqp_Join.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
Proof. constructor; intros.
       - inversion H; subst.
         destruct m1, m2, m.
         simpl in *.
         rewrite Qplus_comm in H0. ;
(* Goal: ht_ord c' c *)
           constructor; auto.
       - inversion H; inversion H0; subst.
         assert (HH: 0 < (my+mz)).
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@Krelation nat nat_geR a n1) (and (@Krelation nat nat_geR b n2) (@join nat (@equiv_Join nat) n1 n2 c)))) *)
         { destruct my, mz; simpl.
           replace 0 with (0+0) by reflexivity.
(* Goal: ht_ord c' c *)
           apply Qplus_lt_le_compat; auto.
(* Goal: ht_ord c' c *)
           apply Qlt_le_weak; auto. }
           
         exists (qp (my+mz) HH); split; simpl;
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@Krelation nat nat_geR a n1) (and (@Krelation nat nat_geR b n2) (@join nat (@equiv_Join nat) n1 n2 c)))) *)
         constructor; simpl.
(* Goal: @Krelation Heap' monHeap_R' n n *)
         reflexivity.
         rewrite <- H5, <-H1.
         apply Qplus_assoc.
Qed.

(* it is NOT upwards-closed*)
Instance sumqpAlg_dSA:
  @UpwardsClosedSeparationAlgebra _ sumqp_Join QPlus_kiM.
Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@join nat sum_Join n1 n2 n) (and (@Krelation nat nat_leR m1 n1) (@Krelation nat nat_leR m2 n2)))) *)
  simpl in H0.
  unfold leqp in H0.
  inversion H;  subst.
  rewrite <- H1 in H0.
  destruct (Qlt_le_dec (n/(1+1)) m1).
  assert (HH: 0 < n / (1 + 1)).
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
  { apply Qlt_shift_div_l.
    - rewrite <- (Qplus_0_l 0).
      apply Qplus_lt_le_compat.
(* Goal: le n (Init.Nat.add x n) *)
      unfold Qlt; simpl; omega.
(* Goal: le n (Init.Nat.add x n) *)
      unfold Qle; simpl; omega.
    - rewrite Qmult_0_l.
(* Goal: ht_ord c' c *)
      destruct n; auto. }
  exists (qp (n / (1 + 1)) HH), (qp (n / (1 + 1)) HH); split.
  - constructor; simpl.
    
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@Krelation nat nat_geR a n1) (and (@Krelation nat nat_geR b n2) (@join nat (@equiv_Join nat) n1 n2 c)))) *)
  simpl.
  - exists bindings_list
Abort.
 
(* downwards-closed*)
Instance sumpAlg_uSA:
  @DownwardsClosedSeparationAlgebra _ sump_Join natPlus_kiM.
Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @ex nat (fun n : nat => and (@join nat sum_Join n1 n2 n) (@Krelation nat nat_leR n m)) *)
  simpl in H0, H1.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
  inversion H; subst.
  unfold lep in *.
  destruct m1, m2, m, n1, n2; simpl in *.
  exists (natp (nat_p3 + nat_p4) ltac:(simpl in *; omega));
(* Goal: le n (Init.Nat.add x n) *)
    split; simpl; try constructor; try omega.
(* Goal: @Krelation Heap' monHeap_R' n n *)
  reflexivity.
(* Goal: @ex nat (fun n1 : nat => @ex nat (fun n2 : nat => and (@Krelation nat nat_geR a n1) (and (@Krelation nat nat_geR b n2) (@join nat (@equiv_Join nat) n1 n2 c)))) *)
  unfold lep; simpl.
  rewrite <- H2; omega.
Qed.
  
  (*it is NOT Residual*)
  Instance sumpAlg_residual:
    @ResidualSeparationAlgebra _ natPlus_kiM sump_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra nat nat_leR sum_Join *)
    constructor; intro.
    unfold residue.
  Abort.
*)

    
(***********************************)
(* Regular HEAPS                   *)
(***********************************)

Section heaps.
  Context (addr val: Type).

Definition Heap: Type := addr -> option val.

Instance Heap_Join: Join Heap :=
  @fun_Join _ _ (@option_Join _ (trivial_Join)).

Instance Heap_SA: SeparationAlgebra Heap :=
  @fun_SA _ _ _ (@option_SA _ _ (trivial_SA)).

(** * Discrete heap *)
Instance discHeap_R: Relation Heap := eq.
Instance po_discHeap_R: PreOrder Krelation := eq_preorder _.

Program Instance discHeap_ikiM: IdentityKripkeIntuitionisticModel Heap.

(*Upwards-closed*)
Instance discHeap_uSA:
  @UpwardsClosedSeparationAlgebra Heap discHeap_R Heap_Join.
Proof.
(* Goal: @UpwardsClosedSeparationAlgebra Prop discProp_R Prop_Join *)
  eapply ikiM_uSA.
Qed.

(*Downwards-closed*)
Instance discHeap_dSA:
  @DownwardsClosedSeparationAlgebra Heap discHeap_R Heap_Join.
Proof.
(* Goal: @DownwardsClosedSeparationAlgebra Prop discProp_R Prop_Join *)
  eapply ikiM_dSA.
Qed.

(*Empty heap is increasing element*)
Lemma discHeap_empty_increasing:
  @increasing Heap discHeap_R Heap_Join (fun _ => None).
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
Proof. hnf; intros.
(* Goal: @increasing Prop discProp_R Prop_Join False *)
       hnf.
       extensionality x; specialize (H  x).
(* Goal: @Krelation Heap' monHeap_R' n n *)
       inversion H; reflexivity.
Qed.

(*Unital*)
Instance discHeap_unital:
  @UnitalSeparationAlgebra Heap discHeap_R Heap_Join.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
  - exists (fun _ => None); split.
    + exists n; split.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
      * hnf; intros.
(* Goal: @join (option val) (@option_Join val (@equiv_Join val)) (@None val) (n x) (n x) *)
        unfold join.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
        destruct (n x); constructor.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
      * hnf; intros.
(* Goal: @Krelation Heap' monHeap_R' n n *)
        reflexivity.
    + hnf; intros.
(* Goal: @Krelation Heap' discHeap_R' n0 n' *)
      hnf; extensionality x.
(* Goal: @join (option htype) (@option_Join htype (@trivial_Join htype)) (@Some htype h) (@Some htype h0) (@Some htype h) *)
      specialize (H x).
(* Goal: @Krelation Heap' monHeap_R' n n *)
      inversion H; reflexivity.
Qed.

(*Residual*)
Instance discHeap_residual:
  @ResidualSeparationAlgebra Heap discHeap_R Heap_Join.
Proof. apply unital_is_residual; apply discHeap_unital. Qed.
  

(** * Monotonic heap*)

Instance monHeap_R: Relation Heap :=
  @pointwise_relation _ _ (@option01_relation _ eq).

Instance po_monHeap_R: PreOrder Krelation :=
  @pointwise_preorder _ _ _ (@option01_preorder _ _ (eq_preorder _)).

(* Upwards-closed*)
Instance monHeap_uSA:
  @UpwardsClosedSeparationAlgebra Heap monHeap_R Heap_Join.
Proof.
(* Goal: @UpwardsClosedSeparationAlgebra Heap monHeap_R Heap_Join *)
  apply fun_uSA.
(* Goal: @UpwardsClosedSeparationAlgebra (option val) (@option01_relation val (@eq val)) (@option_Join val (@trivial_Join val)) *)
  apply option_ord_uSA.
(* Goal: @UpwardsClosedSeparationAlgebra val (@eq val) (@equiv_Join val) *)
  apply (@ikiM_uSA val eq (eq_preorder _) (eq_ikiM)).
Qed.

(*Downwards-closed*)
Definition monHeap_dSA:
  @DownwardsClosedSeparationAlgebra Heap monHeap_R Heap_Join.
Proof.
(* Goal: @DownwardsClosedSeparationAlgebra Heap' monHeap_R' Heap_Join' *)
  eapply fun_dSA.
(* Goal: @DownwardsClosedSeparationAlgebra (option val) (@option01_relation val (@eq val)) (@option_Join val (@equiv_Join val)) *)
  eapply option_ord_dSA.
  - apply eq_preorder.
  - apply trivial_SA.
  - apply (@ikiM_dSA val eq (eq_preorder _) (eq_ikiM)).
  - apply trivial_incrSA.
Qed.

(* Increasing *)
Instance monHeap_increasing:
  @IncreasingSeparationAlgebra Heap monHeap_R Heap_Join.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @option01_relation val (@eq val) (n a) (n' a) *)
  specialize (H a).
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  inversion H; constructor.
  - reflexivity.
  - inversion H3. (*subst; reflexivity.*)
Qed.

(* Residual *)
Instance monHeap_residual:
  @ResidualSeparationAlgebra Heap monHeap_R Heap_Join.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: @ex (forall _ : nat, option htype) (fun m : forall _ : nat, option htype => @residue (forall _ : nat, option htype) THeap_R' THeap_Join' n m) *)
  exists (fun _ => None).
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @ex Heap' (fun n' : Heap' => and (@join Heap' Heap_Join' (fun _ : addr => @None val) n' n) (@Krelation Heap' monHeap_R' n n')) *)
  exists n; split.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  - hnf; intros x; destruct (n x); constructor.
  - reflexivity.
Qed.

(* Unital *)
Instance monHeap_unital:
  @UnitalSeparationAlgebra Heap monHeap_R Heap_Join.
Proof.
(* Goal: @UnitalSeparationAlgebra THeap THeap_R THeap_Join *)
  apply incr_unital_iff_residual.
(* Goal: @IncreasingSeparationAlgebra (Heap addr val) (monHeap_R addr val) heap_jn *)
  apply monHeap_increasing.
(* Goal: @ResidualSeparationAlgebra (Heap addr val) (monHeap_R addr val) heap_jn *)
  apply monHeap_residual.
Qed.

End heaps.

(***********************************)
(* The other nondisjoint HEAPS     *)
(***********************************)

Section heaps'.
  Context (addr val: Type).

Definition Heap': Type := addr -> option val.

Instance Heap_Join': Join Heap' :=
  @fun_Join _ _ (@option_Join _ (equiv_Join)).

Instance Heap_SA': SeparationAlgebra Heap' :=
  @fun_SA _ _ _ (@option_SA _ _ (equiv_SA)).

(** * Discrete heap *)
Instance discHeap_R': Relation Heap' :=
  eq.

Instance po_discHeap_R': PreOrder Krelation :=
  eq_preorder _.

Program Instance discHeap_ikiM': IdentityKripkeIntuitionisticModel Heap'.

(*Upwards-closed*)
Instance discHeap_uSA':
  @UpwardsClosedSeparationAlgebra Heap' discHeap_R' Heap_Join'.
Proof.
(* Goal: @UpwardsClosedSeparationAlgebra Prop discProp_R Prop_Join *)
  eapply ikiM_uSA.
Qed.

(*Downwards-closed*)
Instance discHeap_dSA':
  @DownwardsClosedSeparationAlgebra Heap' discHeap_R' Heap_Join'.
Proof.
(* Goal: @DownwardsClosedSeparationAlgebra Prop discProp_R Prop_Join *)
  eapply ikiM_dSA.
Qed.

(*Empty heap is increasing element*)
Lemma discHeap_empty_increasing':
  @increasing Heap' discHeap_R' Heap_Join' (fun _ => None).
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
Proof. hnf; intros.
(* Goal: @increasing Prop discProp_R Prop_Join False *)
       hnf.
       extensionality x; specialize (H  x).
(* Goal: @Krelation Heap' monHeap_R' n n *)
       inversion H; reflexivity.
Qed.

(*Unital*)
Instance discHeap_unital':
  @UnitalSeparationAlgebra Heap' discHeap_R' Heap_Join'.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
  - exists (fun _ => None); split.
    + exists n; split.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
      * hnf; intros.
(* Goal: @join (option val) (@option_Join val (@equiv_Join val)) (@None val) (n x) (n x) *)
        unfold join.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
        destruct (n x); constructor.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
      * hnf; intros.
(* Goal: @Krelation Heap' monHeap_R' n n *)
        reflexivity.
    + hnf; intros.
(* Goal: @Krelation Heap' discHeap_R' n0 n' *)
      hnf; extensionality x.
(* Goal: @join (option htype) (@option_Join htype (@trivial_Join htype)) (@Some htype h) (@Some htype h0) (@Some htype h) *)
      specialize (H x).
(* Goal: @Krelation Heap' monHeap_R' n n *)
      inversion H; reflexivity.
Qed.

(*Residual*)
Instance discHeap_residual':
  @ResidualSeparationAlgebra Heap' discHeap_R' Heap_Join'.
Proof. apply unital_is_residual; apply discHeap_unital'. Qed.

(** * Monotonic heap*)
Instance monHeap_R': Relation Heap' :=
  @pointwise_relation _ _ (@option01_relation _ eq).

Instance po_monHeap_R': PreOrder Krelation :=
  @pointwise_preorder _ _ _ (@option01_preorder _ _ (eq_preorder _)).

(* Upwards-closed*)
Instance monHeap_uSA':
  @UpwardsClosedSeparationAlgebra Heap' monHeap_R' Heap_Join'.
Proof.
(* Goal: @UpwardsClosedSeparationAlgebra Heap monHeap_R Heap_Join *)
  eapply fun_uSA.
(* Goal: @UpwardsClosedSeparationAlgebra (option val) (@option01_relation val (@eq val)) (@option_Join val (@trivial_Join val)) *)
  eapply option_ord_uSA.
(* Goal: @UpwardsClosedSeparationAlgebra val (@eq val) (@equiv_Join val) *)
  apply (@ikiM_uSA val eq (eq_preorder _) (eq_ikiM)).
Qed.

(*Downwards-closed*)
Definition monHeap_dSA':
  @DownwardsClosedSeparationAlgebra Heap' monHeap_R' Heap_Join'.
Proof.
(* Goal: @DownwardsClosedSeparationAlgebra Heap' monHeap_R' Heap_Join' *)
  eapply fun_dSA.
(* Goal: @DownwardsClosedSeparationAlgebra (option val) (@option01_relation val (@eq val)) (@option_Join val (@equiv_Join val)) *)
  eapply option_ord_dSA.
  - apply eq_preorder.
  - apply equiv_SA.
  - apply (@ikiM_dSA val eq (eq_preorder _) (eq_ikiM)).
  - apply equiv_incrSA.
Qed.

(* Increasing *)
Instance monHeap_increasing':
  @IncreasingSeparationAlgebra Heap' monHeap_R' Heap_Join'.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @option01_relation val (@eq val) (n a) (n' a) *)
  specialize (H a).
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  inversion H; constructor.
  - reflexivity.
  - inversion H3. subst; reflexivity.
Qed.

(* Residual *)
Instance monHeap_residual':
  @ResidualSeparationAlgebra Heap' monHeap_R' Heap_Join'.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: @ex (forall _ : nat, option htype) (fun m : forall _ : nat, option htype => @residue (forall _ : nat, option htype) THeap_R' THeap_Join' n m) *)
  exists (fun _ => None).
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @ex Heap' (fun n' : Heap' => and (@join Heap' Heap_Join' (fun _ : addr => @None val) n' n) (@Krelation Heap' monHeap_R' n n')) *)
  exists n; split.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  - hnf; intros x; destruct (n x); constructor.
  - reflexivity.
Qed.

(* Unital *)
Instance monHeap_unital':
  @UnitalSeparationAlgebra Heap' monHeap_R' Heap_Join'.
Proof.
(* Goal: @UnitalSeparationAlgebra THeap THeap_R THeap_Join *)
  apply incr_unital_iff_residual.
(* Goal: @IncreasingSeparationAlgebra Heap' monHeap_R' Heap_Join' *)
(* Goal: @ResidualSeparationAlgebra Heap' monHeap_R' Heap_Join' *)
  apply monHeap_increasing'.
(* Goal: @ResidualSeparationAlgebra Heap' monHeap_R' Heap_Join' *)
  apply monHeap_residual'.
Qed.
  
End heaps'.



(***********************************)
(* TYPED HEAPS                     *)
(***********************************)

Section typed_heaps.

  Inductive htype:=
  |char
  |short1
  |short2.

  Inductive ht_ord: htype -> htype -> Prop :=
    |htype_refl x: ht_ord x x
    |htype_sht1 : ht_ord char short1
    |htype_sht2 : ht_ord char short2.

  Instance ht_preorder: PreOrder ht_ord.
  Proof.
    constructor;
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
    hnf; intros.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
    - constructor.
    - inversion H; inversion H0; subst; subst; try solve [constructor].
  Qed.

  Notation THeap':= (nat -> option htype).
  Definition THvalid (TH:THeap'):=
     forall n, TH n = Some short1 <->
                   TH (S n) = Some short2.
  Record THeap: Type :=
    { theap:> nat -> option htype;
      th_wf1: THvalid theap}.
  
  Instance THeap_Join': Join THeap' :=
    @fun_Join _ _ (@option_Join _ (trivial_Join)).
  
  Inductive THeap_Join: Join THeap :=
  | th_join (h1 h2 h3: THeap): THeap_Join' h1 h2 h3 ->
                               THeap_Join h1 h2 h3.

Instance THeap_SA': @SeparationAlgebra _ THeap_Join':=
  @fun_SA _ _ _ (@option_SA _ _ (trivial_SA)).

Lemma THeap_Join_valid:
  forall {h1 h2 h3}, THeap_Join' h1 h2 h3 ->
              THvalid h1 ->
              THvalid h2 ->
              THvalid h3.
Proof.
  intros; intros n; assert (H' :=  H).
  specialize (H n); specialize (H' (S n));
  specialize (H0 n); specialize (H1 n);
  destruct H0 as [H0 H0']; destruct H1 as [H1 H1'].
(* Goal: iff (@eq (option htype) (h3 n) (@Some htype short1)) (@eq (option htype) (h3 (S n)) (@Some htype short2)) *)
  split; intros HH.
  - rewrite HH in H.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
    inversion H; subst.
    + symmetry in H4;
      rewrite (H1 H4) in H'.
(* Goal: @eq (option htype) (h3 (S n)) (@Some htype short2) *)
      inversion H'; try reflexivity; subst.
(* Goal: @Krelation Heap' monHeap_R' n n *)
      inversion H7; subst; reflexivity.
    + symmetry in H3;
      rewrite (H0 H3) in H'.
(* Goal: @eq (option htype) (h3 (S n)) (@Some htype short2) *)
      inversion H'; try reflexivity; subst.
(* Goal: @Krelation Heap' monHeap_R' n n *)
      inversion H7; subst; reflexivity.
    + inversion H5; subst.
  - rewrite HH in H'.
(* Goal: @eq (option htype) (h3 n) (@Some htype short1) *)
    inversion H'; subst.
    + symmetry in H4;
      rewrite (H1' H4) in H.
(* Goal: @eq (option htype) (h3 n) (@Some htype short1) *)
      inversion H; try reflexivity; subst.
(* Goal: @Krelation Heap' monHeap_R' n n *)
      inversion H7; subst; reflexivity.
    + symmetry in H3;
      rewrite (H0' H3) in H.
(* Goal: @eq (option htype) (h3 n) (@Some htype short1) *)
      inversion H; try reflexivity; subst.
(* Goal: @Krelation Heap' monHeap_R' n n *)
      inversion H7; subst; reflexivity.
    + inversion H5; subst.
Qed.
    
Instance THeap_SA: @SeparationAlgebra THeap THeap_Join.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
  - inversion H; subst.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
    constructor. apply join_comm; auto.
  - inversion H; inversion H0; subst.
    pose ( join_assoc _ _  _ _ _ H1 H5) as HH.
(* Goal: @ex THeap (fun myz : THeap => and (@join THeap THeap_Join my mz myz) (@join THeap THeap_Join mx myz mxyz)) *)
    destruct HH as [myz' [HH1 HH2]].
    assert (forall n, myz' n = Some short1 <->
                 myz' (S n) = Some short2).
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
    { eapply (THeap_Join_valid HH1).
(* Goal: THvalid (theap my) *)
(* Goal: THvalid (theap mz) *)
      apply my.
(* Goal: THvalid (theap mz) *)
      apply mz. }
(* Goal: ht_ord c' c *)
    exists (Build_THeap myz' H2); split; constructor; auto.
Qed.

Inductive THeap_order': THeap' -> THeap' -> Prop:=
| THeap_ord (h1 h2:THeap'):
    (forall (n:nat) (c:htype), h2 n = Some c ->
           exists (c':htype), h1 n = Some c' /\
                         ht_ord c' c) ->
THeap_order' h2 h1.

Definition THeap_order (h1 h2: THeap): Prop:=
  THeap_order' h1 h2.

Instance THeap_preorder': PreOrder THeap_order'.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
constructor.
- hnf; intros.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  exists c; split; auto; constructor.
- hnf; intros.
  inversion H;
    inversion H0; subst.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: @ex htype (fun c' : htype => and (@eq (option htype) (z n) (@Some htype c')) (ht_ord c' c)) *)
  apply H1 in H2; destruct H2 as [c0 [HH1 HH2]].
(* Goal: @ex htype (fun c' : htype => and (@eq (option htype) (z n) (@Some htype c')) (ht_ord c' c)) *)
  apply H4 in HH1; destruct HH1 as [c' [HH3 HH4]].
(* Goal: ht_ord c' c *)
  exists c'; split; auto.
(* Goal: ht_ord c' c *)
  transitivity c0; auto.
Qed.

Lemma THeap_preorder: PreOrder THeap_order.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
constructor.
- hnf; intros.
(* Goal: THeap_order x x *)
  hnf; reflexivity.
- hnf; intros.
(* Goal: THeap_order x z *)
  hnf in H, H0.
(* Goal: ht_ord c' c *)
  hnf; transitivity y; auto.
Qed.

Instance THeap_R': Relation THeap' := THeap_order'.

Instance po_THeap_R': PreOrder (@Krelation _ THeap_R').
Proof.
(* Goal: @PreOrder (forall _ : nat, option htype) (@Krelation (forall _ : nat, option htype) THeap_R') *)
  eapply THeap_preorder'.
Defined.

Instance THeap_R: Relation THeap := THeap_order.

Instance po_THeap_R: PreOrder (@Krelation _ THeap_R).
Proof.
(* Goal: @PreOrder THeap (@Krelation THeap THeap_R) *)
  eapply THeap_preorder.
Defined.

(*It is NOT up-closed*)
Instance THeap_uSA:
  @UpwardsClosedSeparationAlgebra THeap THeap_R THeap_Join.
Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
  hnf in H0.
  exists m1.
  pose (m2':= (fun n => match m1 n with
                       Some _ => None
                     | _ => m n
                     end)).
  assert (THvalid m2').
Abort.

(* downwards-closed*)
Instance THeap_dSA':
  @DownwardsClosedSeparationAlgebra _ THeap_R' THeap_Join'.
Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: min_Join a b c *)
  inversion H0; inversion H1; subst.
  exists (fun n => match n1 n with
           |Some x => Some x
           |_ => n2 n
(* Goal: and (@residue Prop discProp_R Prop_Join n False) (@increasing Prop discProp_R Prop_Join False) *)
           end); split.
  - hnf; intros.
(* Goal: @join (option htype) (@option_Join htype (@trivial_Join htype)) (n1 x) (n2 x) match n1 x with | Some x => @Some htype x | None => n2 x end *)
    destruct (n1 x) eqn:AA.
    + destruct (n2 x) eqn:BB.
      * specialize (H2 x); specialize (H5 x).
(* Goal: @join (option htype) (@option_Join htype (@trivial_Join htype)) (@Some htype h) (@Some htype h0) (@Some htype h) *)
        apply H2 in AA; destruct AA as [x' [AA1 AA2]].
(* Goal: @join (option htype) (@option_Join htype (@trivial_Join htype)) (@Some htype h) (@Some htype h0) (@Some htype h) *)
        apply H5 in BB; destruct BB as [x'' [BB1 BB2]].
(* Goal: @join (option htype) (@option_Join htype (@trivial_Join htype)) (@Some htype h) (@Some htype h0) (@Some htype h) *)
        specialize (H x).
(* Goal: @join (option htype) (@option_Join htype (@trivial_Join htype)) (@Some htype h) (@Some htype h0) (@Some htype h) *)
        rewrite AA1, BB1 in H.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
        inversion H; subst. inversion H7.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
      * constructor.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
    + destruct (n2 x); constructor.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  - constructor; intros.
(* Goal: @ex htype (fun c' : htype => and (@eq (option htype) (m n) (@Some htype c')) (ht_ord c' c)) *)
    destruct (n1 n) eqn:n1n.
    + apply H2 in n1n; destruct n1n as [c' [HH1 HH2]].
(* Goal: @ex htype (fun c' : htype => and (@eq (option htype) (m n) (@Some htype c')) (ht_ord c' c)) *)
      exists c';split.
      * specialize (H n).
(* Goal: @eq (option htype) (m n) (@Some htype c') *)
        rewrite HH1 in H.
(* Goal: @Krelation Heap' monHeap_R' n n *)
        inversion H; subst; try reflexivity.
(* Goal: @eq (option htype) (@Some htype c0) (@Some htype c') *)
        inversion H8.
      * inversion H3; subst; auto.
    + apply H5 in H3; destruct H3 as [c' [HH1 HH2]].
(* Goal: @ex htype (fun c' : htype => and (@eq (option htype) (m n) (@Some htype c')) (ht_ord c' c)) *)
      exists c';split.
      * specialize (H n).
(* Goal: @eq (option htype) (m n) (@Some htype c') *)
        rewrite HH1 in H.
(* Goal: @Krelation Heap' monHeap_R' n n *)
        inversion H; subst; try reflexivity.
(* Goal: @eq (option htype) (@Some htype c0) (@Some htype c') *)
        inversion H7.
      * auto.
Qed.
  
Instance THeap_dSA:
  @DownwardsClosedSeparationAlgebra THeap THeap_R THeap_Join.
Proof.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
  hnf in H0, H1.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
  inversion H; subst.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
  destruct (THeap_dSA' _ _ _ _ _ H2 H0 H1) as [n [HH1 HH2]].
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
  assert (HH: THvalid n).
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
  { apply (THeap_Join_valid HH1);
    first [apply n1 | apply n2]. }
  exists (Build_THeap n HH); split; auto;
(* Goal: ht_ord c' c *)
  constructor; auto.
Qed.

(* Increasing *)
Instance THeap_increasingSA':
  @IncreasingSeparationAlgebra _ THeap_R' THeap_Join'.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: @ex htype (fun c' : htype => and (@eq (option htype) (n' n0) (@Some htype c')) (ht_ord c' c)) *)
  specialize (H n0).
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
  rewrite H0 in H; inversion H; subst.
  - exists c; split; auto; reflexivity.
  - inversion H4.
Qed.

Instance THeap_increasingSA:
  @IncreasingSeparationAlgebra _ THeap_R THeap_Join.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
  simpl in *.
(* Goal: ht_ord c' c *)
  eapply THeap_increasingSA'; auto.
(* Goal: forall (n n' : Prop) (_ : @join Prop Prop_Join False n n'), @Krelation Prop discProp_R n n' *)
  hnf in H; hnf ; intros.
(* Goal: ht_ord c' c *)
  inversion H; subst; auto.
Qed.

(*Residual*)
Instance THeap_residualSA':
  @ResidualSeparationAlgebra _ THeap_R' THeap_Join'.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: @ex (forall _ : nat, option htype) (fun m : forall _ : nat, option htype => @residue (forall _ : nat, option htype) THeap_R' THeap_Join' n m) *)
  exists (fun _ => None).
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @Krelation Heap' monHeap_R' n n *)
  exists n; split; try reflexivity.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  hnf; intros.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  destruct (n x); constructor.
Qed.

Instance THeap_residualSA:
  @ResidualSeparationAlgebra _ THeap_R THeap_Join.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
  assert (THvalid (fun _ => None)).
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
  { constructor; intros HH; inversion HH. }
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
  exists (Build_THeap _ H).
(* Goal: @Krelation Heap' monHeap_R' n n *)
  exists n; split; try reflexivity.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  constructor.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  hnf; intros. destruct (n x); constructor.
Qed.

(*Unital*)
Instance THeap_UnitalSA':
  @UnitalSeparationAlgebra _ THeap_R' THeap_Join'.
Proof.
(* Goal: @UnitalSeparationAlgebra THeap THeap_R THeap_Join *)
  apply incr_unital_iff_residual.
(* Goal: @IncreasingSeparationAlgebra (forall _ : nat, option htype) THeap_R' THeap_Join' *)
(* Goal: @ResidualSeparationAlgebra (forall _ : nat, option htype) THeap_R' THeap_Join' *)
  apply THeap_increasingSA'.
(* Goal: @ResidualSeparationAlgebra (forall _ : nat, option htype) THeap_R' THeap_Join' *)
  apply THeap_residualSA'.
Qed.

Instance THeap_UnitalSA:
  @UnitalSeparationAlgebra _ THeap_R THeap_Join.
Proof.
(* Goal: @UnitalSeparationAlgebra THeap THeap_R THeap_Join *)
  apply incr_unital_iff_residual.
(* Goal: @IncreasingSeparationAlgebra THeap THeap_R THeap_Join *)
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  apply THeap_increasingSA.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  apply THeap_residualSA.
Qed.

End typed_heaps.

(***********************************)
(* Step-Index                      *)
(***********************************)

Section step_index.

(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
  Definition StepIndex_R (worlds: Type) {R: Relation worlds}:
    Relation (nat * worlds) :=
    @RelProd _ _ nat_geR R.

(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
  Definition po_StepIndex_R (worlds: Type) {R: Relation worlds} {po_R: PreOrder (@Krelation _ R)}:
    PreOrder (@Krelation _ (StepIndex_R worlds)):=
    @RelProd_Preorder _ _ _ _ po_nat_geR po_R.

  Definition StepIndex_Join (worlds: Type) {J: Join worlds}: Join (nat * worlds) :=
    @prod_Join _ _ equiv_Join J.

  Definition StepIndex_SA
             (worlds: Type)
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
             {J: Join worlds}
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
             {SA: SeparationAlgebra worlds}:
    @SeparationAlgebra (nat * worlds)
                       (StepIndex_Join worlds) := @prod_SA _ _ _ _ equiv_SA SA.

(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
  Definition StepIndex_uSA (worlds: Type) {R: Relation worlds}
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
             {J: Join worlds} {uSA: UpwardsClosedSeparationAlgebra worlds}:
    @UpwardsClosedSeparationAlgebra (nat * worlds) (StepIndex_R worlds) (StepIndex_Join worlds) :=
    @prod_uSA _ _ _ _  _ _ (@identity_uSA _ nat_geR) uSA.

  Definition StepIndex_Increasing
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
             (worlds: Type) {R: Relation worlds}
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
             {J: Join worlds} {incrSA: IncreasingSeparationAlgebra worlds}:
    @IncreasingSeparationAlgebra (nat * worlds)
                                  (StepIndex_R worlds)
                                  (StepIndex_Join worlds) :=
    @prod_incrSA _ _ _ _ _ _ IndexAlg_increasing incrSA.

  Definition StepIndex_Unital
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
             (worlds: Type) {R: Relation worlds}
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
             {J: Join worlds} {USA: UnitalSeparationAlgebra worlds}:
    @UnitalSeparationAlgebra (nat * worlds)
                                  (StepIndex_R worlds)
                                  (StepIndex_Join worlds) :=
    @prod_unitalSA _ _ _  _ _ _ IndexAlg_unital USA.

  Definition StepIndex_Residual
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
             (worlds: Type) {R: Relation worlds}
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
             {J: Join worlds} {rSA: ResidualSeparationAlgebra worlds}:
    @ResidualSeparationAlgebra (nat * worlds)
                                  (StepIndex_R worlds)
                                  (StepIndex_Join worlds) :=
    @prod_residualSA _ _ _  _ _ _ IndexAlg_residual rSA.

  (** *step-indexed HEAPS*)
  Context (addr val: Type).
  Notation heap:= (Heap addr val).

  Instance heap_jn: Join heap:= (Heap_Join addr val).

  Instance SIheap_Join: Join (nat * heap) := StepIndex_Join heap.

  (** *Monotonic, step-indexed heap *)
  Definition monSIheap_R := @StepIndex_R heap (monHeap_R addr val).

  Definition po_monSIheap_R := @po_StepIndex_R heap _ (po_monHeap_R addr val).

  (*Upwards-closed *)
  Instance monSIheap_uSA:
    @UpwardsClosedSeparationAlgebra _ monSIheap_R SIheap_Join.
  Proof.
(* Goal: @UpwardsClosedSeparationAlgebra (prod nat (Heap addr val)) discSIheap_R SIheap_Join *)
    eapply StepIndex_uSA.
(* Goal: @UpwardsClosedSeparationAlgebra (Heap addr val) (monHeap_R addr val) heap_jn *)
    apply monHeap_uSA.
  Qed.

  (* NOT Downwards-closed *)

  (* Increasing *)
  Instance monSIheap_increasing:
    @IncreasingSeparationAlgebra _ monSIheap_R SIheap_Join.
  Proof.
(* Goal: @IncreasingSeparationAlgebra (prod nat (Heap addr val)) monSIheap_R SIheap_Join *)
    eapply StepIndex_Increasing.
(* Goal: @IncreasingSeparationAlgebra (Heap addr val) (monHeap_R addr val) heap_jn *)
    apply monHeap_increasing.
  Qed.

  (*Unital *)
  Instance monSIheap_unital:
    @UnitalSeparationAlgebra _ monSIheap_R SIheap_Join.
  Proof.
(* Goal: @UnitalSeparationAlgebra (prod nat (Heap addr val)) discSIheap_R SIheap_Join *)
    eapply StepIndex_Unital.
(* Goal: @UnitalSeparationAlgebra (Heap addr val) (monHeap_R addr val) heap_jn *)
    apply monHeap_unital.
  Qed.

  (*Residual *)
  Instance monSIheap_residual:
    @ResidualSeparationAlgebra _ monSIheap_R SIheap_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra (prod nat (Heap addr val)) discSIheap_R SIheap_Join *)
    eapply StepIndex_Residual.
(* Goal: @ResidualSeparationAlgebra (Heap addr val) (monHeap_R addr val) heap_jn *)
    apply monHeap_residual.
  Qed.

  (** *Discrete, step-indexed heap *)
  Definition discSIheap_R:= @StepIndex_R heap (discHeap_R addr val).
  Definition po_discSIheap_R:= @po_StepIndex_R heap _ (po_discHeap_R addr val).

  (*Upwards-closed *)
  Instance discSIheap_uSA:
    @UpwardsClosedSeparationAlgebra _ discSIheap_R SIheap_Join.
  Proof.
(* Goal: @UpwardsClosedSeparationAlgebra (prod nat (Heap addr val)) discSIheap_R SIheap_Join *)
    eapply StepIndex_uSA.
(* Goal: @UpwardsClosedSeparationAlgebra (Heap addr val) (discHeap_R addr val) heap_jn *)
    apply discHeap_uSA.
  Qed.

  (* NOT Downwards-closed *)

  (* NOT Decreasing *)

  (*Unital *)
  Instance discSIheap_unital:
    @UnitalSeparationAlgebra _ discSIheap_R SIheap_Join.
  Proof.
(* Goal: @UnitalSeparationAlgebra (prod nat (Heap addr val)) discSIheap_R SIheap_Join *)
    eapply StepIndex_Unital.
(* Goal: @UnitalSeparationAlgebra (Heap addr val) (discHeap_R addr val) heap_jn *)
    apply discHeap_unital.
  Qed.

  (*Residual *)
  Instance discSIheap_residual:
    @ResidualSeparationAlgebra _ discSIheap_R SIheap_Join.
  Proof.
(* Goal: @ResidualSeparationAlgebra (prod nat (Heap addr val)) discSIheap_R SIheap_Join *)
    eapply StepIndex_Residual.
(* Goal: @ResidualSeparationAlgebra (Heap addr val) (discHeap_R addr val) heap_jn *)
    apply discHeap_residual.
  Qed.

End step_index.

(***********************************)
(* ALGEBRAS ON NATURALS            *)
(***********************************)

Section Prop_alg.

Instance Prop_Join: Join Prop := fun P Q R => (P \/ Q <-> R) /\ (P -> Q -> False).
  
Instance discProp_R: Relation Prop := fun P Q => P <-> Q.

Instance Prop_SA: SeparationAlgebra Prop.
Proof.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  constructor.
  + intros.
    hnf in *; tauto.
  + intros.
(* Goal: @ex Prop (fun myz : Prop => and (@join Prop Prop_Join my mz myz) (@join Prop Prop_Join mx myz mxyz)) *)
    exists (my \/ mz).
    split; hnf in *; tauto.
Qed.

Instance po_discProp_R: PreOrder Krelation.
Proof.
  constructor; constructor;
  hnf in *; tauto.
Qed.

Instance discProp_ikiM: IdentityKripkeIntuitionisticModel Prop.
Proof.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  constructor.
(* Goal: forall (n n' : Prop) (_ : @join Prop Prop_Join False n n'), @Krelation Prop discProp_R n n' *)
  intros.
(* Goal: ht_ord c' c *)
  apply prop_ext; auto.
Qed.

Instance discProp_uSA:
  @UpwardsClosedSeparationAlgebra Prop discProp_R Prop_Join.
Proof.
(* Goal: @UpwardsClosedSeparationAlgebra Prop discProp_R Prop_Join *)
  eapply ikiM_uSA.
Qed.
  
Instance discProp_dSA:
  @DownwardsClosedSeparationAlgebra Prop discProp_R Prop_Join.
Proof.
(* Goal: @DownwardsClosedSeparationAlgebra Prop discProp_R Prop_Join *)
  eapply ikiM_dSA.
Qed.

Instance discProp_unitSA:
  @UnitalSeparationAlgebra Prop discProp_R Prop_Join.
Proof.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
  constructor.
(* Goal: forall (n n' : Prop) (_ : @join Prop Prop_Join False n n'), @Krelation Prop discProp_R n n' *)
  intros.
(* Goal: @ex Prop (fun m : Prop => and (@residue Prop discProp_R Prop_Join n m) (@increasing Prop discProp_R Prop_Join m)) *)
  exists False.
(* Goal: and (@residue Prop discProp_R Prop_Join n False) (@increasing Prop discProp_R Prop_Join False) *)
  split.
  + hnf.
(* Goal: ht_ord c' c *)
    exists n; split; hnf; tauto.
  + hnf.
(* Goal: forall (n n' : Prop) (_ : @join Prop Prop_Join False n n'), @Krelation Prop discProp_R n n' *)
    intros.
    hnf in *; tauto.
Qed.

End Prop_alg.

Section pred_alg.

Context (A : Type).

Definition Pred: Type := A -> Prop.

Instance Pred_Join: Join Pred :=
  @fun_Join _ _ Prop_Join.

Instance Pred_SA: SeparationAlgebra Pred :=
  @fun_SA _ _ _ Prop_SA.

Instance discPred_R: Relation Pred := pointwise_relation _ discProp_R.
Instance po_discPred_R: PreOrder Krelation := pointwise_preorder _ _ po_discProp_R.

Instance discPred_uSA: @UpwardsClosedSeparationAlgebra Pred discPred_R Pred_Join :=
  fun_uSA _ _ discProp_uSA.

Instance discPred_dSA: @DownwardsClosedSeparationAlgebra Pred discPred_R Pred_Join :=
  fun_dSA _ _ discProp_dSA.

Instance discPred_unital: @UnitalSeparationAlgebra Pred discPred_R Pred_Join :=
  fun_unitSA _ _ discProp_unitSA.

End pred_alg.

(***********************************)
(* Resource Bounds                 *)
(***********************************)



(*
(** * Discrete heap *)
Instance discHeap_kiM: KripkeIntuitionisticModel Heap :=
  identity_kiM.

Program Instance discHeap_ikiM: IdentityKripkeIntuitionisticModel Heap.

(*Upwards-closed*)
Instance discHeap_dSA:
  @UpwardsClosedSeparationAlgebra Heap Heap_Join discHeap_kiM.
Proof.
(* Goal: @DownwardsClosedSeparationAlgebra Prop discProp_R Prop_Join *)
  eapply ikiM_dSA.
Qed.

(*Downwards-closed*)
Instance discHeap_uSA:
  @DownwardsClosedSeparationAlgebra Heap Heap_Join discHeap_kiM.
Proof.
(* Goal: @UpwardsClosedSeparationAlgebra Prop discProp_R Prop_Join *)
  eapply ikiM_uSA.
Qed.

(*Empty heap is decreasing element*)
Lemma discHeap_empty_decreasing:
  @nonpositive Heap discHeap_kiM Heap_Join (fun _ => None).
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
Proof. hnf; intros.
(* Goal: @increasing Prop discProp_R Prop_Join False *)
       hnf.
       extensionality x; specialize (H  x).
(* Goal: @Krelation Heap' monHeap_R' n n *)
       inversion H; reflexivity.
Qed.

(*Unital*)
Instance discHeap_unital:
  @UnitalSeparationAlgebra Heap discHeap_kiM Heap_Join.
Proof.
(* Goal: @ResidualSeparationAlgebra THeap THeap_R THeap_Join *)
  constructor; intros.
  - exists (fun _ => None); split.
    + exists n; split.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
      * hnf; intros.
(* Goal: @join (option val) (@option_Join val (@equiv_Join val)) (@None val) (n x) (n x) *)
        unfold join.
(* Goal: @UnitalSeparationAlgebra Prop discProp_R Prop_Join *)
        destruct (n x); constructor.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
      * hnf; intros.
(* Goal: @Krelation Heap' monHeap_R' n n *)
        reflexivity.
    + hnf; intros.
(* Goal: @Krelation Heap' discHeap_R' n0 n' *)
      hnf; extensionality x.
(* Goal: @join (option htype) (@option_Join htype (@trivial_Join htype)) (@Some htype h) (@Some htype h0) (@Some htype h) *)
      specialize (H x).
(* Goal: @Krelation Heap' monHeap_R' n n *)
      inversion H; reflexivity.
  - hnf; intros.
(* Goal: @ex THeap (fun n : THeap => and (@join THeap THeap_Join n1 n2 n) (@Krelation THeap THeap_R n m)) *)
    inversion H; subst.
    apply H0 in H1.
    inversion H1.
(* Goal: @Krelation Heap' monHeap_R' n n *)
    reflexivity.
Qed.









(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
Class SeparationAlgebra_unit (worlds: Type) {J: Join worlds} := {
  unit: worlds;
  unit_join: forall n, join n unit n;
  unit_spec: forall n m, join n unit m -> n = m
}.

(***********************************)
(* More examples                   *)
(***********************************)
(*
Program Definition nat_le_kiM: KripkeIntuitionisticModel nat := 
  Build_KripkeIntuitionisticModel nat (fun a b => a <= b) _.
Next Obligation.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  constructor; hnf; intros.
  + apply le_n.
  + eapply NPeano.Nat.le_trans; eauto.
Qed.

(* TODO: Probably don't need this one. *)
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
Program Definition SAu_kiM (worlds: Type) {J: Join worlds} {SA: SeparationAlgebra worlds} {SAu: SeparationAlgebra_unit worlds} : KripkeIntuitionisticModel worlds :=
  Build_KripkeIntuitionisticModel worlds (fun a b => exists b', join b b' a) _.
Next Obligation.
(* Goal: THeap_Join' (theap (Build_THeap (fun _ : nat => @None htype) H)) (theap n) (theap n) *)
  constructor; hnf; intros.
  + exists unit; apply unit_join.
  + destruct H as [? ?], H0 as [? ?].
    destruct (join_assoc _ _ _ _ _ H0 H) as [? [? ?]].
(* Goal: ht_ord c' c *)
    exists x2; auto.
Qed.

Definition Stack (LV val: Type): Type := LV -> val.

Definition StepIndex_kiM (worlds: Type) {kiM: KripkeIntuitionisticModel worlds}: KripkeIntuitionisticModel (nat * worlds) := @prod_kiM _ _ nat_le_kiM kiM.

Definition StepIndex_Join (worlds: Type) {J: Join worlds}: Join (nat * worlds) :=
  @prod_Join _ _ (equiv_Join _) J.

(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
Definition StepIndex_SA (worlds: Type) {J: Join worlds} {SA: SeparationAlgebra worlds}:
  @SeparationAlgebra (nat * worlds) (StepIndex_Join worlds) := @prod_SA _ _ _ _ (equiv_SA _) SA.

(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
Definition StepIndex_dSA (worlds: Type) {kiM: KripkeIntuitionisticModel worlds}
(* Goal: THvalid (fun _ : nat => @None htype) *)
(* Goal: @ex THeap (fun m : THeap => @residue THeap THeap_R THeap_Join n m) *)
           {J: Join worlds} {dSA: UpwardsClosedSeparationAlgebra worlds}:
  @UpwardsClosedSeparationAlgebra (nat * worlds) (StepIndex_Join worlds) (StepIndex_kiM worlds):= @prod_dSA _ _ _ _ _ _ (@identity_dSA _ nat_le_kiM) dSA.

*)*)
