------------------------------------------------------------------------
-- Assumptions used to state some theorems in Graded.FullReduction
------------------------------------------------------------------------

open import Graded.Modality
open import Definition.Typed.Restrictions

module Graded.FullReduction.Assumptions
  {a} {M : Set a}
  (𝕄 : Modality M)
  (R : Type-restrictions M)
  where

open Modality 𝕄
open Type-restrictions R

open import Graded.Modality.Properties 𝕄
open import Graded.Mode 𝕄

open import Tools.Bool
open import Tools.Function
open import Tools.Product
open import Tools.PropositionalEquality
import Tools.Reasoning.PartialOrder
open import Tools.Sum as ⊎

private variable
  p q r : M

-- The theorems in Graded.FullReduction are proved under the
-- assumption that the following property holds.

record Full-reduction-assumptions : Set a where
  no-eta-equality
  field
    -- If the unit type (with Ξ·-equality) is allowed, then πŸ™Β β‰€Β πŸ˜.
    πŸ™β‰€πŸ˜ : Unit-allowed β†’ πŸ™ ≀ 𝟘

    -- If a Ξ£-type with Ξ·-equality and the "first component
    -- quantity"Β p is allowed, then either pΒ β‰‘Β πŸ™, or pΒ β‰‘Β πŸ˜, 𝟘ᡐ is
    -- allowed and πŸ™Β β‰€Β πŸ˜.
    β‰‘πŸ™βŠŽπŸ™β‰€πŸ˜ : Ξ£β‚š-allowed p q β†’ p ≑ πŸ™ ⊎ p ≑ 𝟘 Γ— T 𝟘ᡐ-allowed Γ— πŸ™ ≀ 𝟘

-- An alternative way to state Full-reduction-assumptions.

record Full-reduction-assumptionsβ€² : Set a where
  no-eta-equality
  field
    -- If the unit type (with η-equality) is allowed, then 𝟘 must be
    -- the largest quantity.
    β‰€πŸ˜ : Unit-allowed β†’ p ≀ 𝟘

    -- If a Ξ£-type with Ξ·-equality and the "first component
    -- quantity"Β p is allowed, then pΒ Β·_ must be increasing.
    Β·-increasing : Ξ£β‚š-allowed p q β†’ r ≀ p Β· r

    -- If a Ξ£-type with Ξ·-equality and the "first component
    -- quantity"Β p is allowed, and ⌞ p ⌟ isΒ πŸ™α΅, then pΒ β‰€Β πŸ™ must hold.
    βŒžβŒŸβ‰‘πŸ™α΅β†’β‰€πŸ™ : Ξ£β‚š-allowed p q β†’ ⌞ p ⌟ ≑ πŸ™α΅ β†’ p ≀ πŸ™

-- Full-reduction-assumptions is logically equivalent to
-- Full-reduction-assumptionsβ€².

Full-reduction-assumptions⇔Full-reduction-assumptionsβ€² :
  Full-reduction-assumptions ⇔ Full-reduction-assumptionsβ€²
Full-reduction-assumptions⇔Full-reduction-assumptionsβ€² =
    (Ξ» as β†’ record
       { β‰€πŸ˜ = Ξ» {p = p} β†’
           Unit-allowed  β†’βŸ¨ πŸ™β‰€πŸ˜ as ⟩

           πŸ™ ≀ 𝟘         β†’βŸ¨ (Ξ» πŸ™β‰€πŸ˜ β†’ begin

             p                   β‰‘Λ˜βŸ¨ Β·-identityΛ‘ _ ⟩
             πŸ™ Β· p               β‰€βŸ¨ Β·-monotoneΛ‘ πŸ™β‰€πŸ˜ ⟩
             𝟘 Β· p               β‰‘βŸ¨ Β·-zeroΛ‘ _ ⟩
             𝟘                   ∎) ⟩

           p ≀ 𝟘         β–‘
       ; Β·-increasing = Ξ» {p = p} {q = q} {r = r} β†’
           Ξ£β‚š-allowed p q                        β†’βŸ¨ β‰‘πŸ™βŠŽπŸ™β‰€πŸ˜ as ⟩

           p ≑ πŸ™ ⊎ p ≑ 𝟘 Γ— T 𝟘ᡐ-allowed Γ— πŸ™ ≀ 𝟘  β†’βŸ¨ (Ξ» { (inj₁ refl) β†’ begin

             r                                             β‰‘Λ˜βŸ¨ Β·-identityΛ‘ _ ⟩
             πŸ™ Β· r                                         ∎
                                                       ; (injβ‚‚ (refl , _ , πŸ™β‰€πŸ˜)) β†’ begin
             r                                             β‰‘Λ˜βŸ¨ Β·-identityΛ‘ _ ⟩
             πŸ™ Β· r                                         β‰€βŸ¨ Β·-monotoneΛ‘ πŸ™β‰€πŸ˜ ⟩
             𝟘 · r                                         ∎
                                                       }) ⟩
           r ≀ p Β· r                             β–‘
       ; βŒžβŒŸβ‰‘πŸ™α΅β†’β‰€πŸ™ = Ξ» {p = p} {q = q} β†’
           Ξ£β‚š-allowed p q                        β†’βŸ¨ β‰‘πŸ™βŠŽπŸ™β‰€πŸ˜ as ⟩
           p ≑ πŸ™ ⊎ p ≑ 𝟘 Γ— T 𝟘ᡐ-allowed Γ— πŸ™ ≀ 𝟘  β†’βŸ¨ ⊎.map ≀-reflexive (Ξ» (pβ‰‘πŸ˜ , ok , _) β†’ (ok , pβ‰‘πŸ˜)) ⟩
           p ≀ πŸ™ ⊎ T 𝟘ᡐ-allowed Γ— p ≑ 𝟘          β†’βŸ¨ βŒžβŒŸβ‰‘πŸ™β†’β‡”βŠŽπŸ˜α΅Γ—β‰‘πŸ˜ .projβ‚‚ ⟩
           (⌞ p ⌟ ≑ πŸ™α΅ β†’ p ≀ πŸ™)                  β–‘
       })
  , (Ξ» as β†’ record
       { πŸ™β‰€πŸ˜    = β‰€πŸ˜ as
       ; β‰‘πŸ™βŠŽπŸ™β‰€πŸ˜ = Ξ» {p = p} {q = q} β†’
           Ξ£β‚š-allowed p q                          β†’βŸ¨ (Ξ» ok β†’ Β·-increasing as ok , βŒžβŒŸβ‰‘πŸ™α΅β†’β‰€πŸ™ as ok) ⟩
           πŸ™ ≀ p Β· πŸ™ Γ— (⌞ p ⌟ ≑ πŸ™α΅ β†’ p ≀ πŸ™)        β†’βŸ¨ (Ξ» (πŸ™β‰€p1 , βŒžβŒŸβ‰‘πŸ™α΅β†’β‰€πŸ™) β†’
                                                          subst (_ ≀_) (Β·-identityΚ³ _) πŸ™β‰€p1
                                                        , βŒžβŒŸβ‰‘πŸ™β†’β‡”βŠŽπŸ˜α΅Γ—β‰‘πŸ˜ .proj₁ βŒžβŒŸβ‰‘πŸ™α΅β†’β‰€πŸ™) ⟩
           πŸ™ ≀ p Γ— (p ≀ πŸ™ ⊎ T 𝟘ᡐ-allowed Γ— p ≑ 𝟘)  β†’βŸ¨ (Ξ» where
                                                        (πŸ™β‰€p , inj₁ pβ‰€πŸ™)         β†’ inj₁ (≀-antisym pβ‰€πŸ™ πŸ™β‰€p)
                                                        (πŸ™β‰€πŸ˜ , injβ‚‚ (ok , refl)) β†’ injβ‚‚ (refl , ok , πŸ™β‰€πŸ˜)) ⟩
           p ≑ πŸ™ ⊎ p ≑ 𝟘 Γ— T 𝟘ᡐ-allowed Γ— πŸ™ ≀ 𝟘    β–‘
       })
  where
  open Full-reduction-assumptions
  open Full-reduction-assumptionsβ€²
  open Tools.Reasoning.PartialOrder ≀-poset