open import NotSoFresh.Base
module NotSoFresh.Lemmas (laws : Laws) where

open Laws laws

open import Data.Maybe using (Maybe;just;nothing)
open import Data.Product using (;_×_;_,_)
open import Data.Sum using (_⊎_ ; inj₁ ; inj₂)
open import Data.Empty using (; ⊥-elim)
open import Function using (_∘_;_∶_)
open import Relation.Nullary
open import Relation.Binary
open import Relation.Binary.PropositionalEquality using (_≡_;_≢_;inspect;_with-≡_;refl;sym;trans;cong;subst;_≗_)

nameOf↼-≡→failed-export↼ :
               {α β} { : α  β} {a : Name β}
               nameOf↼   a  export↼ {α = α}  a  nothing
nameOf↼-≡→failed-export↼ {= } .{nameOf↼ } refl = export↼∘nameOf↼-fails

failed-export↼→nameOf↼-≡ :  {α β} { : α  β} {a : Name β}
                        export↼  a  nothing  nameOf↼   a
failed-export↼→nameOf↼-≡
  = export↼-injective  trans export↼∘nameOf↼-fails  sym

export↼' :  {α β} ( : α  β) (a : Name β)  nameOf↼ {α}   a  (nameOf↼ {α}   a × Name α)
export↼' {α}  a with inspect (export↼  a)
... | nothing with-≡ eq = inj₁ (failed-export↼→nameOf↼-≡ eq)
... | just a' with-≡ eq with nameOf↼  ≟Name a
...      | yes nameOf↼ℓ≡a = ((_  _)  λ())
                          (trans (sym eq) (nameOf↼-≡→failed-export↼ nameOf↼ℓ≡a))
...      | no  nameOf↼ℓ≢a = inj₂ (nameOf↼ℓ≢a , a')


safeExport↼ :  {α β} ( : α  β) (a : Name β)
               nameOf↼ {α}   a  Name α
safeExport↼  a ℓ≢a with export↼'  a
... | inj₁ ℓ≡a = ⊥-elim (ℓ≢a ℓ≡a)
... | inj₂ (_ , b) = b