open import Common.Types
module CC2.HeapContextPrecision where
open import Data.Nat
open import Data.Unit using (β€; tt)
open import Data.Bool using (true; false) renaming (Bool to πΉ)
open import Data.List
open import Data.Product using (_Γ_; β-syntax; projβ; projβ) renaming (_,_ to β¨_,_β©)
open import Data.Sum using (_β_; injβ; injβ)
open import Data.Maybe
open import Relation.Nullary using (Β¬_; Dec; yes; no)
open import Relation.Nullary.Negation using (contradiction)
open import Relation.Binary.PropositionalEquality using (_β‘_; refl; trans; subst; substβ; sym)
open import Function using (case_of_)
open import Syntax hiding (_β¨_)
open import Common.Utils
open import CC2.Statics
open import Memory.Heap Term Value hiding (Addr; aβ¦_β§_)
open import Memory.HeapContext
data _β*_ : (Ξ Ξβ² : Context) β Set where
β*-β
: [] β* []
β*-β· : β {Ξ Ξβ² A Aβ²} β A β Aβ² β Ξ β* Ξβ² β (A β· Ξ) β* (Aβ² β· Ξβ²)
β*ββ : β {Ξ Ξβ² A Aβ² x} β Ξ β* Ξβ² β Ξ β x β¦ A β Ξβ² β x β¦ Aβ² β A β Aβ²
β*ββ {x = 0} (β*-β· AβAβ² ΞβΞβ²) refl refl = AβAβ²
β*ββ {x = suc x} (β*-β· AβAβ² ΞβΞβ²) Ξβx Ξβ²βx = β*ββ ΞβΞβ² Ξβx Ξβ²βx
data _ββ_ : (Ξ£ Ξ£β² : HalfHeapContext) β Set where
β-β
: [] ββ []
β-β· : β {Ξ£ Ξ£β² T Tβ² n} β T βα΅£ Tβ² β Ξ£ ββ Ξ£β² β (β¨ n , T β© β· Ξ£) ββ (β¨ n , Tβ² β© β· Ξ£β²)
_ββ_ : (Ξ£ Ξ£β² : HeapContext) β Set
β¨ Ξ£α΄Έ , Ξ£α΄΄ β© ββ β¨ Ξ£α΄Έβ² , Ξ£α΄΄β² β© = (Ξ£α΄Έ ββ Ξ£α΄Έβ²) Γ (Ξ£α΄΄ ββ Ξ£α΄΄β²)
ββ-cons : β {Ξ£ Ξ£β² n β T Tβ²}
β T βα΅£ Tβ²
β Ξ£ ββ Ξ£β²
β cons-Ξ£ (aβ¦ β β§ n) T Ξ£ ββ cons-Ξ£ (aβ¦ β β§ n) Tβ² Ξ£β²
ββ-cons {β = low} TβTβ² β¨ left , right β© = β¨ β-β· TβTβ² left , right β©
ββ-cons {β = high} TβTβ² β¨ left , right β© = β¨ left , β-β· TβTβ² right β©
ββββ-forward : β {Ξ£ Ξ£β² T n}
β Ξ£ ββ Ξ£β²
β find _β_ Ξ£ n β‘ just T
β β[ Tβ² ] (find _β_ Ξ£β² n β‘ just Tβ²) Γ (T βα΅£ Tβ²)
ββββ-forward {β¨ nβ , T β© β· _} {β¨ nβ , Tβ² β© β· _} {n = n} (β-β· TβTβ² Ξ£βΞ£β²) eq
with n β nβ
... | no _ = ββββ-forward Ξ£βΞ£β² eq
... | yes refl with eq
... | refl = β¨ _ , refl , TβTβ² β©
ββββ-forward : β {Ξ£ Ξ£β² T n βΜ}
β Ξ£ ββ Ξ£β²
β let a = aβ¦ βΜ β§ n in
lookup-Ξ£ Ξ£ a β‘ just T
β β[ Tβ² ] (lookup-Ξ£ Ξ£β² a β‘ just Tβ²) Γ (T βα΅£ Tβ²)
ββββ-forward {βΜ = low} β¨ Ξ£α΄Έβ , _ β© Ξ£aβ‘T = ββββ-forward Ξ£α΄Έβ Ξ£aβ‘T
ββββ-forward {βΜ = high} β¨ _ , Ξ£α΄΄β β© Ξ£aβ‘T = ββββ-forward Ξ£α΄΄β Ξ£aβ‘T
ββββ-backward : β {Ξ£ Ξ£β² Tβ² n}
β Ξ£ ββ Ξ£β²
β find _β_ Ξ£β² n β‘ just Tβ²
β β[ T ] (find _β_ Ξ£ n β‘ just T) Γ (T βα΅£ Tβ²)
ββββ-backward {β¨ nβ , T β© β· _} {β¨ nβ , Tβ² β© β· _} {n = n} (β-β· TβTβ² Ξ£βΞ£β²) eq
with n β nβ
... | no _ = ββββ-backward Ξ£βΞ£β² eq
... | yes refl with eq
... | refl = β¨ _ , refl , TβTβ² β©
ββββ-backward : β {Ξ£ Ξ£β² Tβ² n βΜ}
β Ξ£ ββ Ξ£β²
β let a = aβ¦ βΜ β§ n in
lookup-Ξ£ Ξ£β² a β‘ just Tβ²
β β[ T ] (lookup-Ξ£ Ξ£ a β‘ just T) Γ (T βα΅£ Tβ²)
ββββ-backward {βΜ = low} β¨ Ξ£α΄Έβ , _ β© Ξ£aβ‘T = ββββ-backward Ξ£α΄Έβ Ξ£aβ‘T
ββββ-backward {βΜ = high} β¨ _ , Ξ£α΄΄β β© Ξ£aβ‘T = ββββ-backward Ξ£α΄΄β Ξ£aβ‘T
ββββ : β {Ξ£ Ξ£β² T Tβ² n βΜ}
β Ξ£ ββ Ξ£β²
β let a = aβ¦ βΜ β§ n in
lookup-Ξ£ Ξ£ a β‘ just T
β lookup-Ξ£ Ξ£β² a β‘ just Tβ²
β T βα΅£ Tβ²
ββββ {Ξ£} {Ξ£β²} {T} {Tβ²} {n} {βΜ} Ξ£βΞ£β² eq eqβ²
with ββββ-forward {βΜ = βΜ} Ξ£βΞ£β² eq
... | β¨ Tβ³ , eqβ³ , TβTβ³ β© with trans (sym eqβ²) eqβ³
... | refl = TβTβ³