{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

-----------------------------------------------------------------------------

-- |

-- Copyright   :  (C) 2021 Koz Ross

-- License     :  BSD-style (see the file LICENSE)

--

-- Maintainer  :  Koz Ross <koz.ross@retro-freedom.nz>

-- Stability   :  Experimental

-- Portability :  GHC only

--

-- Provides a way to attach an identity to any semigroupoid.

----------------------------------------------------------------------------

module Data.Semigroupoid.Categorical (
  Categorical(..),
  runCategorical
  ) where

import Control.Category (Category (id, (.)))
import Data.Semigroupoid (Semigroupoid (o))
import Prelude ()

-- | Attaches an identity.

--

-- @since 5.3.6

data Categorical s a b where
  Id :: Categorical s a a
  Embed :: s a b -> Categorical s a b

-- | @since 5.3.6

instance (Semigroupoid s) => Semigroupoid (Categorical s) where
  Categorical s j k
Id o :: forall j k i.
Categorical s j k -> Categorical s i j -> Categorical s i k
`o` Categorical s i j
y = Categorical s i j
y
  Categorical s j k
x `o` Categorical s i j
Id = Categorical s j k
x
  Embed s j k
x `o` Embed s i j
y = forall (s :: * -> * -> *) a b. s a b -> Categorical s a b
Embed (s j k
x forall {k} (c :: k -> k -> *) (j :: k) (k :: k) (i :: k).
Semigroupoid c =>
c j k -> c i j -> c i k
`o` s i j
y)

-- | @since 5.3.6

instance (Semigroupoid s) => Category (Categorical s) where
  id :: forall a. Categorical s a a
id = forall (s :: * -> * -> *) a. Categorical s a a
Id
  . :: forall b c a.
Categorical s b c -> Categorical s a b -> Categorical s a c
(.) = forall {k} (c :: k -> k -> *) (j :: k) (k :: k) (i :: k).
Semigroupoid c =>
c j k -> c i j -> c i k
o

-- | @since 5.3.6

runCategorical :: (a ~ b => r) -> (s a b -> r) -> Categorical s a b -> r
runCategorical :: forall a b r (s :: * -> * -> *).
((a ~ b) => r) -> (s a b -> r) -> Categorical s a b -> r
runCategorical (a ~ b) => r
r s a b -> r
_  Categorical s a b
Id = (a ~ b) => r
r
runCategorical (a ~ b) => r
_ s a b -> r
f (Embed s a b
x) = s a b -> r
f s a b
x