{-# OPTIONS --cubical-compatible --safe #-}
module Relation.Binary.Reasoning.Syntax where
open import Level using (Level; _⊔_; suc)
open import Relation.Nullary.Decidable.Core
  using (Dec; True; toWitness)
open import Relation.Nullary.Negation.Core using (contradiction)
open import Relation.Binary.Core using (Rel; REL; _⇒_)
open import Relation.Binary.Definitions
  using (_Respectsʳ_; Asymmetric; Trans; Sym; Reflexive)
open import Relation.Binary.PropositionalEquality.Core as ≡
  using (_≡_)
private
  variable
    a ℓ₁ ℓ₂ ℓ₃ ℓ₄ : Level
    A B C : Set a
    x y z : A
module begin-syntax
  (R : REL A B ℓ₁)
  {S : REL A B ℓ₂}
  (reflexive : R ⇒ S)
  where
  infix 1 begin_
  begin_ : R x y → S x y
  begin_ = reflexive
record SubRelation {A : Set a} (R : Rel A ℓ₁) ℓ₂ ℓ₃ : Set (a ⊔ ℓ₁ ⊔ suc ℓ₂ ⊔ suc ℓ₃) where
  field
    S : Rel A ℓ₂
    IsS : R x y → Set ℓ₃
    IsS? : ∀ (xRy : R x y) → Dec (IsS xRy)
    extract : ∀ {xRy : R x y} → IsS xRy → S x y
module begin-subrelation-syntax
  (R : Rel A ℓ₁)
  (sub : SubRelation R ℓ₂ ℓ₃)
  where
  open SubRelation sub
  infix 1 begin_
  begin_ : ∀ {x y} (xRy : R x y) → {s : True (IsS? xRy)} → S x y
  begin_ r {s} = extract (toWitness s)
module begin-equality-syntax
  (R : Rel A ℓ₁)
  (sub : SubRelation R ℓ₂ ℓ₃) where
  open begin-subrelation-syntax R sub public
    renaming (begin_ to begin-equality_)
module begin-apartness-syntax
  (R : Rel A ℓ₁)
  (sub : SubRelation R ℓ₂ ℓ₃) where
  open begin-subrelation-syntax R sub public
    renaming (begin_ to begin-apartness_)
module begin-strict-syntax
  (R : Rel A ℓ₁)
  (sub : SubRelation R ℓ₂ ℓ₃) where
  open begin-subrelation-syntax R sub public
    renaming (begin_ to begin-strict_)
module begin-membership-syntax
  (R : Rel A ℓ₁)
  (_∈_ : REL B A ℓ₂)
  (resp : _∈_ Respectsʳ R) where
  infix  1 step-∈
  step-∈ : ∀ (x : B) {xs ys} → R xs ys → x ∈ xs → x ∈ ys
  step-∈ x = resp
  syntax step-∈ x  xs⊆ys x∈xs  = x ∈⟨ x∈xs ⟩ xs⊆ys
module begin-contradiction-syntax
  (R : Rel A ℓ₁)
  (sub : SubRelation R ℓ₂ ℓ₃)
  (asym : Asymmetric (SubRelation.S sub))
  where
  open SubRelation sub
  infix 1 begin-contradiction_
  begin-contradiction_ : ∀ (xRx : R x x) {s : True (IsS? xRx)} →
                         ∀ {b} {B : Set b} → B
  begin-contradiction_ {x} r {s} = contradiction x<x (asym x<x)
    where
    x<x : S x x
    x<x = extract (toWitness s)
module _
  {R : REL A B ℓ₂}
  (S : REL B C ℓ₁)
  (T : REL A C ℓ₃)
  (step : Trans R S T)
  where
  forward : ∀ (x : A) {y z} → S y z → R x y → T x z
  forward x yRz x∼y = step {x} x∼y yRz
  
  module ∼-syntax where
    infixr 2 step-∼
    step-∼ = forward
    syntax step-∼ x yRz x∼y = x ∼⟨ x∼y ⟩ yRz
  
  module ≲-syntax where
    infixr 2 step-≲
    step-≲ = forward
    syntax step-≲ x yRz x≲y = x ≲⟨ x≲y ⟩ yRz
  
  module ≤-syntax where
    infixr 2 step-≤
    step-≤ = forward
    syntax step-≤ x yRz x≤y = x ≤⟨ x≤y ⟩ yRz
  
  module <-syntax where
    infixr 2 step-<
    step-< = forward
    syntax step-< x yRz x<y = x <⟨ x<y ⟩ yRz
  
  module ⊆-syntax where
    infixr 2 step-⊆
    step-⊆ = forward
    syntax step-⊆ x yRz x⊆y = x ⊆⟨ x⊆y ⟩ yRz
  
  module ⊂-syntax where
    infixr 2 step-⊂
    step-⊂ = forward
    syntax step-⊂ x yRz x⊂y = x ⊂⟨ x⊂y ⟩ yRz
  
  module ⊑-syntax where
    infixr 2 step-⊑
    step-⊑ = forward
    syntax step-⊑ x yRz x⊑y = x ⊑⟨ x⊑y ⟩ yRz
  
  module ⊏-syntax where
    infixr 2 step-⊏
    step-⊏ = forward
    syntax step-⊏ x yRz x⊏y = x ⊏⟨ x⊏y ⟩ yRz
  
  module ∣-syntax where
    infixr 2 step-∣
    step-∣ = forward
    syntax step-∣ x yRz x∣y = x ∣⟨ x∣y ⟩ yRz
  
  module ⟶-syntax where
    infixr 2 step-⟶
    step-⟶ = forward
    syntax step-⟶ x yRz x∣y = x ⟶⟨ x∣y ⟩ yRz
  
  module ⟶*-syntax where
    infixr 2 step-⟶*
    step-⟶* = forward
    syntax step-⟶* x yRz x∣y = x ⟶*⟨ x∣y ⟩ yRz
  module _
    {U : REL B A ℓ₄}
    (sym : Sym U R)
    where
    backward : ∀ x {y z} → S y z → U y x → T x z
    backward x yRz x≈y = forward x yRz (sym x≈y)
    
    module ≈-syntax where
      infixr 2 step-≈-⟩ step-≈-⟨
      step-≈-⟩ = forward
      step-≈-⟨ = backward
      syntax step-≈-⟩ x yRz x≈y = x ≈⟨ x≈y ⟩ yRz
      syntax step-≈-⟨ x yRz y≈x = x ≈⟨ y≈x ⟨ yRz
      
      infixr 2 step-≈ step-≈˘
      step-≈ = step-≈-⟩
      {-# WARNING_ON_USAGE step-≈
      "Warning: step-≈ was deprecated in v2.0.
      Please use step-≈-⟩ instead."
      #-}
      step-≈˘ = step-≈-⟨
      {-# WARNING_ON_USAGE step-≈˘
      "Warning: step-≈˘ and _≈˘⟨_⟩_ was deprecated in v2.0.
      Please use step-≈-⟨ and _≈⟨_⟨_ instead."
      #-}
      syntax step-≈˘ x yRz y≈x = x ≈˘⟨ y≈x ⟩ yRz
    
    module ≋-syntax where
      infixr 2 step-≋-⟩ step-≋-⟨
      step-≋-⟩ = forward
      step-≋-⟨ = backward
      syntax step-≋-⟩ x yRz x≋y = x ≋⟨ x≋y ⟩ yRz
      syntax step-≋-⟨ x yRz y≋x = x ≋⟨ y≋x ⟨ yRz
      
      infixr 2 step-≋ step-≋˘
      step-≋ = step-≋-⟩
      {-# WARNING_ON_USAGE step-≋
      "Warning: step-≋ was deprecated in v2.0.
      Please use step-≋-⟩ instead."
      #-}
      step-≋˘ = step-≋-⟨
      {-# WARNING_ON_USAGE step-≋˘
      "Warning: step-≋˘ and _≋˘⟨_⟩_ was deprecated in v2.0.
      Please use step-≋-⟨ and _≋⟨_⟨_ instead."
      #-}
      syntax step-≋˘ x yRz y≋x = x ≋˘⟨ y≋x ⟩ yRz
    
    module ≃-syntax where
      infixr 2 step-≃-⟩ step-≃-⟨
      step-≃-⟩ = forward
      step-≃-⟨ = backward
      syntax step-≃-⟩ x yRz x≃y = x ≃⟨ x≃y ⟩ yRz
      syntax step-≃-⟨ x yRz y≃x = x ≃⟨ y≃x ⟨ yRz
    
    module #-syntax where
      infixr 2 step-#-⟩ step-#-⟨
      step-#-⟩ = forward
      step-#-⟨ = backward
      syntax step-#-⟩ x yRz x#y = x #⟨ x#y ⟩ yRz
      syntax step-#-⟨ x yRz y#x = x #⟨ y#x ⟨ yRz
      
      infixr 2 step-# step-#˘
      step-# = step-#-⟩
      {-# WARNING_ON_USAGE step-#
      "Warning: step-# was deprecated in v2.0.
      Please use step-#-⟩ instead."
      #-}
      step-#˘ = step-#-⟨
      {-# WARNING_ON_USAGE step-#˘
      "Warning: step-#˘ and _#˘⟨_⟩_ was deprecated in v2.0.
      Please use step-#-⟨ and _#⟨_⟨_ instead."
      #-}
      syntax step-#˘ x yRz y#x = x #˘⟨ y#x ⟩ yRz
    
    module ⤖-syntax where
      infixr 2 step-⤖ step-⬻
      step-⤖ = forward
      step-⬻ = backward
      syntax step-⤖ x yRz x⤖y = x ⤖⟨ x⤖y ⟩ yRz
      syntax step-⬻ x yRz y⤖x = x ⬻⟨ y⤖x ⟩ yRz
    
    module ↔-syntax where
      infixr 2 step-↔-⟩ step-↔-⟨
      step-↔-⟩ = forward
      step-↔-⟨ = backward
      syntax step-↔-⟩ x yRz x↔y = x ↔⟨ x↔y ⟩ yRz
      syntax step-↔-⟨ x yRz y↔x = x ↔⟨ y↔x ⟨ yRz
    
    module ↭-syntax where
      infixr 2 step-↭-⟩ step-↭-⟨
      step-↭-⟩ = forward
      step-↭-⟨ = backward
      syntax step-↭-⟩ x yRz x↭y = x ↭⟨ x↭y ⟩ yRz
      syntax step-↭-⟨ x yRz y↭x = x ↭⟨ y↭x ⟨ yRz
      
      infixr 2 step-↭ step-↭˘
      step-↭ = forward
      {-# WARNING_ON_USAGE step-↭
      "Warning: step-↭ was deprecated in v2.0.
      Please use step-↭-⟩ instead."
      #-}
      step-↭˘ = backward
      {-# WARNING_ON_USAGE step-↭˘
      "Warning: step-↭˘ and _↭˘⟨_⟩_ was deprecated in v2.0.
      Please use step-↭-⟨ and _↭⟨_⟨_ instead."
      #-}
      syntax step-↭˘ x yRz y↭x = x ↭˘⟨ y↭x ⟩ yRz
module ≡-syntax
  (R : REL A B ℓ₁)
  (step : Trans _≡_ R R)
  where
  infixr 2 step-≡-⟩  step-≡-∣ step-≡-⟨
  step-≡-⟩ = forward R R step
  step-≡-∣ : ∀ x {y} → R x y → R x y
  step-≡-∣ x xRy = xRy
  step-≡-⟨ = backward R R step ≡.sym
  syntax step-≡-⟩ x yRz x≡y = x ≡⟨ x≡y ⟩ yRz
  syntax step-≡-∣ x xRy     = x ≡⟨⟩ xRy
  syntax step-≡-⟨ x yRz y≡x = x ≡⟨ y≡x ⟨ yRz
  
  infixr 2 step-≡ step-≡˘
  step-≡ = step-≡-⟩
  {-# WARNING_ON_USAGE step-≡
  "Warning: step-≡ was deprecated in v2.0.
  Please use step-≡-⟩ instead."
  #-}
  step-≡˘ = step-≡-⟨
  {-# WARNING_ON_USAGE step-≡˘
  "Warning: step-≡˘ and _≡˘⟨_⟩_ was deprecated in v2.0.
  Please use step-≡-⟨ and _≡⟨_⟨_ instead."
  #-}
  syntax step-≡˘ x yRz y≡x = x ≡˘⟨ y≡x ⟩ yRz
module ≡-noncomputing-syntax (R : REL A B ℓ₁) where
  private
    step : Trans _≡_ R R
    step ≡.refl xRy = xRy
  open ≡-syntax R step public
module end-syntax
  (R : Rel A ℓ₁)
  (reflexive : Reflexive R)
  where
  infix 3 _∎
  _∎ : ∀ x → R x x
  x ∎ = reflexive