-- Generated from content/part1/Isomorphism.md by scripts/extract_and_sync.hs
-- Do not edit directly; edit the literate Markdown chapter instead.

{-# LANGUAGE DependentTypes #-}
{-# OPTIONS_GHC -fno-enable-rewrite-rules #-}
{-# LINE 36 "content/part1/Isomorphism.md" #-}
module PLFH.Part1.Isomorphism
  ( Iff (..)
  , iffRefl
  , iffSym
  , iffTrans
  , FunExt
  , plus'
  , sameApp
  , samePlus
  , Iso (..)
  , identityRoundTrip
  , isoRefl
  , isoSym
  , composeLeft
  , isoTrans
  , Embed (..)
  , embedRefl
  , embedTrans
  , isoImpliesEmbed
  , embedImpliesIff
  , binEmbedding
  ) where

import Prelude hiding ((.))
import Data.Function.Dependent ((.))
import Data.Kind (Type)
import GHC.Matchability (U)
import Data.Nat.Dependent (Nat (..), plus)
import Data.Type.Equality ((:~:) (Refl), trans)
import PLFH.Part1.Induction (cong, fromTo, plusComm)
import PLFH.Part1.Naturals (Bin (..), fromBin, toBin)

{-# LINE 78 "content/part1/Isomorphism.md" #-}
-- From Data.Function.Dependent:
-- (.) :: forall {a} {b} {m :: Matchability} (c :: b %m -> Type).
--        (foreach (y :: b) -> c y)
--     -> foreach (f :: a -> b)
--     -> foreach (x :: a)
--     -> c (f x)
-- (g . f) x = g (f x)

{-# LINE 108 "content/part1/Isomorphism.md" #-}
-- | Function extensionality stated as an explicit type hypothesis (no axiom).
type FunExt (a :: Type) (b :: Type) =
  forall (f g :: a %U -> b). (foreach (x :: a) -> f x :~: g x) -> f :~: g

-- | Addition defined by recursion on its second argument (`_+′_` in PLFA).
plus' :: Nat -> Nat -> Nat
plus' m Zero     = m
plus' m (Succ n) = Succ (plus' m n)

-- | `plus` and `plus'` agree pointwise on all natural numbers (`same-app`).
sameApp :: foreach (m n :: Nat) -> plus m n :~: plus' m n
sameApp m Zero     = plusComm m Zero
sameApp m (Succ n) = trans (plusComm m (Succ n)) (cong Succ (trans (plusComm n m) (sameApp m n)))

-- | Given function extensionality, `plus` and `plus'` are equal as functions (`same`).
samePlus :: FunExt Nat (Nat %U -> Nat) -> FunExt Nat Nat -> plus :~: plus'
samePlus ext1 ext2 = ext1 (\m -> ext2 (\n -> sameApp m n))

{-# LINE 140 "content/part1/Isomorphism.md" #-}
-- | Polymorphic isomorphism witness between `a` and `b`.
data Iso a b where
  MkIso :: foreach (to :: a -> b) -> foreach (from :: b -> a)
        -> (foreach (x :: a) -> from (to x) :~: x)
        -> (foreach (y :: b) -> to (from y) :~: y)
        -> Iso a b

-- | Symmetry: reverse an isomorphism.
isoSym :: Iso a b -> Iso b a
isoSym (MkIso to from fromTo' toFrom) = MkIso from to toFrom fromTo'

{-# LINE 159 "content/part1/Isomorphism.md" #-}
-- | Round-trip identity at any lifted type.
identityRoundTrip :: foreach (x :: a) -> id (id x) :~: x
identityRoundTrip _ = Refl

-- | Generic reflexivity of isomorphism (`≃-refl`).
isoRefl :: Iso a a
isoRefl = MkIso id id identityRoundTrip identityRoundTrip

{-# LINE 180 "content/part1/Isomorphism.md" #-}
-- | Cancel the inner round trip, then use the outer left inverse.
composeLeft :: foreach (f :: a -> b) -> foreach (g :: b -> a)
            -> foreach (h :: b -> c) -> foreach (k :: c -> b)
            -> (foreach (x :: a) -> g (f x) :~: x)
            -> (foreach (y :: b) -> k (h y) :~: y)
            -> foreach (x :: a) -> g (k (h (f x))) :~: x
composeLeft f g _ _ gf kh x = trans (cong g (kh (f x))) (gf x)

-- | Generic transitivity of isomorphism (`≃-trans`), composing maps directly
-- with `Data.Function.Dependent.(.)`.
isoTrans :: forall a b c. Iso a b -> Iso b c -> Iso a c
isoTrans (MkIso f g gf fg) (MkIso h k kh hk) =
  MkIso @a @c (h . f) (g . k)
    (composeLeft f g h k gf kh)
    (composeLeft k h g f hk fg)

{-# LINE 208 "content/part1/Isomorphism.md" #-}
-- | Polymorphic embedding witness: `a` embeds faithfully in `b`.
data Embed a b where
  MkEmbed :: foreach (to :: a -> b) -> foreach (from :: b -> a)
          -> (foreach (x :: a) -> from (to x) :~: x)
          -> Embed a b

{-# LINE 222 "content/part1/Isomorphism.md" #-}
-- | Every isomorphism gives an embedding.
isoImpliesEmbed :: Iso a b -> Embed a b
isoImpliesEmbed (MkIso to from left _) = MkEmbed to from left

-- | Generic reflexivity of embedding (`≲-refl`).
embedRefl :: Embed a a
embedRefl = isoImpliesEmbed isoRefl

-- | Generic transitivity of embedding (`≲-trans`).
embedTrans :: forall a b c. Embed a b -> Embed b c -> Embed a c
embedTrans (MkEmbed f g gf) (MkEmbed h k kh) =
  MkEmbed @a @c (h . f) (g . k) (composeLeft f g h k gf kh)

-- | Every embedding gives a logical equivalence.
embedImpliesIff :: Embed a b -> Iff a b
embedImpliesIff (MkEmbed to from _) = Iff to from

{-# LINE 247 "content/part1/Isomorphism.md" #-}
-- | Natural numbers embed in binary numerals (`ℕ ≲ Bin`).
binEmbedding :: Embed Nat Bin
binEmbedding = MkEmbed toBin fromBin fromTo

{-# LINE 281 "content/part1/Isomorphism.md" #-}
-- | Logical equivalence (bi-implication) between propositions `a` and `b`.
data Iff a b = Iff
  { toIff   :: a -> b
  , fromIff :: b -> a
  }

-- | Reflexivity of logical equivalence.
iffRefl :: Iff a a
iffRefl = Iff id id

-- | Symmetry of logical equivalence.
iffSym :: Iff a b -> Iff b a
iffSym (Iff f g) = Iff g f

-- | Transitivity of logical equivalence.
iffTrans :: Iff a b -> Iff b c -> Iff a c
iffTrans (Iff f1 g1) (Iff f2 g2) = Iff (f2 . f1) (g1 . g2)