------------------------------------------------------------------------
-- A modality for linear types.
------------------------------------------------------------------------

open import Tools.Bool
open import Tools.Level
open import Tools.Nullary
open import Tools.Sum

open import Graded.Modality.Instances.Zero-one-many false as πŸ˜πŸ™Ο‰
open import Graded.Modality.Variant lzero

module Graded.Modality.Instances.Linearity
  -- The modality variant.
  (variant : Modality-variant)
  (open Modality-variant variant)
  -- If there is no dedicated natrec-star operator, then 𝟘ᡐ must not
  -- be allowed.
  (variant-ok : Β¬ βŠ›-available β†’ Β¬ T 𝟘ᡐ-allowed)
  where

open πŸ˜πŸ™Ο‰ renaming (Zero-one-many to Linearity) public

open import Graded.Modality Linearity
open import Graded.FullReduction.Assumptions
import Graded.Modality.Properties

open import Definition.Typed.Restrictions Linearity

open import Tools.Empty
open import Tools.Function
open import Tools.Product
open import Tools.PropositionalEquality
open import Tools.Unit

private variable
  rs : Type-restrictions

-- A "linear types" modality.

linearityModality : Modality
linearityModality = zero-one-many-greatest variant (flip variant-ok)

-- The "linear types" modality has a well-behaved zero.

linearity-has-well-behaved-zero : Has-well-behaved-zero (Modality.semiring-with-meet linearityModality)
linearity-has-well-behaved-zero = zero-one-many-has-well-behaved-zero

open Graded.Modality.Properties linearityModality

-- An instance of Type-restrictions is suitable for the full reduction
-- theorem if
-- * Unit-allowed does not hold,
-- * Ξ£β‚š-allowed 𝟘 p does not hold, and
-- * Ξ£β‚š-allowed ω p does not hold.

Suitable-for-full-reduction :
  Type-restrictions β†’ Set
Suitable-for-full-reduction rs =
  Β¬ Unit-allowed Γ—
  (βˆ€ p β†’ Β¬ Ξ£β‚š-allowed 𝟘 p) Γ—
  (βˆ€ p β†’ Β¬ Ξ£β‚š-allowed Ο‰ p)
  where
  open Type-restrictions rs

-- Given an instance of Type-restrictions one can create a "suitable"
-- instance.

suitable-for-full-reduction :
  Type-restrictions β†’ βˆƒ Suitable-for-full-reduction
suitable-for-full-reduction rs =
    record rs
      { Unit-allowed = βŠ₯
      ; Ξ Ξ£-allowed   = Ξ» b p q β†’
          Ξ Ξ£-allowed b p q Γ— p β‰’ 𝟘 Γ— p β‰’ Ο‰
      }
  , idαΆ 
  , (Ξ» _ β†’ (_$ refl) βˆ˜β†’ proj₁ βˆ˜β†’ projβ‚‚)
  , (Ξ» _ β†’ (_$ refl) βˆ˜β†’ projβ‚‚ βˆ˜β†’ projβ‚‚)
  where
  open Type-restrictions rs

-- The full reduction assumptions hold for linearityModality and any
-- "suitable" Type-restrictions.

full-reduction-assumptions :
  Suitable-for-full-reduction rs β†’
  Full-reduction-assumptions linearityModality rs
full-reduction-assumptions (Β¬Unit , ¬𝟘 , ¬ω) = record
  { πŸ™β‰€πŸ˜    = βŠ₯-elim βˆ˜β†’ Β¬Unit
  ; β‰‘πŸ™βŠŽπŸ™β‰€πŸ˜ = Ξ» where
      {p = 𝟘} ok β†’ βŠ₯-elim (¬𝟘 _ ok)
      {p = Ο‰} ok β†’ βŠ₯-elim (¬ω _ ok)
      {p = πŸ™} _  β†’ inj₁ refl
  }