------------------------------------------------------------------------
-- A non-interference result.
------------------------------------------------------------------------

open import Graded.Modality
open import Graded.Usage.Restrictions
import Definition.Untyped
open import Definition.Typed.EqualityRelation
open import Definition.Typed.Restrictions
open import Graded.Erasure.LogicalRelation.Fundamental.Assumptions
open import Graded.Erasure.Target as T using (Strictness)
open import Tools.Nat using (Nat)

module Graded.Erasure.Consequences.Non-interference
  {a} {M : Set a}
  (open Definition.Untyped M)
  {𝕄 : Modality M}
  (open Modality 𝕄)
  (TR : Type-restrictions 𝕄)
  (UR : Usage-restrictions 𝕄)
  ⦃ 𝟘-well-behaved : Has-well-behaved-zero M semiring-with-meet ⦄
  ⦃ eqrel : EqRelSet TR ⦄
  {k : Nat}
  {Δ : Con Term k}
  (FA : Fundamental-assumptions TR UR Δ)
  {str : Strictness}
  where

open Fundamental-assumptions FA

open import Definition.Typed TR
open import Definition.Typed.Properties TR
open import Definition.Typed.Substitution TR
import Definition.LogicalRelation.Fundamental TR as F
open import Definition.LogicalRelation.Substitution TR

open import Graded.Context 𝕄
open import Graded.Usage 𝕄 UR
open import Graded.Modality.Properties 𝕄
open import Graded.Mode 𝕄

open import Graded.Erasure.Extraction 𝕄
open import Graded.Erasure.LogicalRelation.Assumptions TR

private

  as : Assumptions
  as = record
    { ⊢Δ                    = well-formed
    ; inc                   = Fundamental-assumptions.inc FA
    ; str                   = str
    ; is-reduction-relation = ⇒*-is-reduction-relation
    }

open import Graded.Erasure.LogicalRelation as
open import Graded.Erasure.LogicalRelation.Hidden as
open import Graded.Erasure.LogicalRelation.Fundamental TR UR

open Fundamental FA

open import Tools.Function

private variable
  Γ : Con Term _
  t : Term _
  γ : Conₘ _

-- A simple non-interference property.
--
-- Note that some assumptions are given as module parameters.

non-interference :
  Γ ⊢ t ∷ ℕ → γ ▸[ 𝟙ᵐ ] t →
  ∀ {σ σ′} →
  Δ ⊢ˢʷ σ ∷ Γ →
  σ ® σ′ ∷[ 𝟙ᵐ ] Γ ◂ γ →
  t [ σ ] ® erase str t T.[ σ′ ] ∷ℕ
non-interference {Γ} {t} {γ} ⊢t ▸t {σ} {σ′} ⊢σ σ®σ′ =
                                                   $⟨ fundamental ⊢t ▸t ⟩

  γ ▸ Γ ⊩ʳ t ∷[ 𝟙ᵐ ] ℕ                             ⇔⟨ ▸⊩ʳ∷⇔ ⟩→

  (∀ {σ σ′} → Δ ⊩ˢ σ ∷ Γ → σ ® σ′ ∷[ 𝟙ᵐ ] Γ ◂ γ →
   t [ σ ] ® erase str t T.[ σ′ ] ∷ ℕ ◂ 𝟙)         →⟨ (λ hyp → hyp (F.fundamental-⊩ˢ∷ (wfTerm ⊢t) ⊢σ) σ®σ′) ⟩

  t [ σ ] ® erase str t T.[ σ′ ] ∷ ℕ ◂ 𝟙           →⟨ ®∷→®∷◂ω non-trivial ⟩

  t [ σ ] ® erase str t T.[ σ′ ] ∷ ℕ               ⇔⟨ ®∷ℕ⇔ ⟩→

  t [ σ ] ® erase str t T.[ σ′ ] ∷ℕ                □