-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE PolyKinds #-} | ||
{-# LANGUAGE StandaloneKindSignatures #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
|
||
-- | A scratchpad for implementing Iceland Jack and Ed Kmett's | ||
-- categorical functor ideas. | ||
-- | ||
-- If possible, this ought to give us a kind generic functor to | ||
-- replace 'GBifunctor'. | ||
-- | ||
-- We also ought to be able to use the same tricks to get a kind | ||
-- generic Monoidal Functor class. | ||
module Data.Functor.Categorical where | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
import Control.Category | ||
import Data.Bifunctor qualified as Hask | ||
Check warning on line 20 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 8.10.7)
Check warning on line 20 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 8.10.7)
Check warning on line 20 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.0.2)
Check warning on line 20 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.0.2)
Check warning on line 20 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.2.4)
Check warning on line 20 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.2.4)
Check warning on line 20 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.4.5)
Check warning on line 20 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.4.5)
Check warning on line 20 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.6.2)
|
||
import Data.Functor qualified as Hask | ||
import Data.Functor.Contravariant qualified as Hask | ||
import Data.Functor.Contravariant (Op (..), Predicate (..)) | ||
import Data.Kind (Constraint, Type) | ||
import Data.Maybe (Maybe (..)) | ||
import Data.Profunctor qualified as Hask | ||
Check warning on line 26 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 8.10.7)
Check warning on line 26 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 8.10.7)
Check warning on line 26 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.0.2)
Check warning on line 26 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.0.2)
Check warning on line 26 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.2.4)
Check warning on line 26 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.2.4)
Check warning on line 26 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.4.5)
Check warning on line 26 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.4.5)
Check warning on line 26 in src/Data/Functor/Categorical.hs GitHub Actions / build (3.6, 9.6.2)
|
||
|
||
-------------------------------------------------------------------------------- | ||
|
||
type Functor :: (from -> to) -> Constraint | ||
class (Category (Dom f), Category (Cod f)) => Functor (f :: from -> to) where | ||
type Dom f :: from -> from -> Type | ||
type Cod f :: to -> to -> Type | ||
|
||
map :: Dom f a b -> Cod f (f a) (f b) | ||
|
||
type Nat :: Cat s -> Cat t -> Cat (s -> t) | ||
data Nat source target f f' where | ||
Nat :: (FunctorOf source target f, FunctorOf source target f') => (forall x. target (f x) (f' x)) -> Nat source target f f' | ||
|
||
-- TODO: | ||
-- instance Category (Nat (->) (->)) where | ||
-- id :: Nat (->) (->) a a | ||
-- id = Nat _ | ||
|
||
type Cat i = i -> i -> Type | ||
|
||
type FunctorOf :: Cat from -> Cat to -> (from -> to) -> Constraint | ||
class (Functor f, dom ~ Dom f, cod ~ Cod f) => FunctorOf dom cod f | ||
|
||
instance (Functor f, dom ~ Dom f, cod ~ Cod f) => FunctorOf dom cod f | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
newtype FromFunctor f a = FromFunctor (f a) | ||
deriving newtype (Hask.Functor) | ||
|
||
instance (Hask.Functor f) => Functor (FromFunctor f) where | ||
type Dom (FromFunctor f) = (->) | ||
type Cod (FromFunctor f) = (->) | ||
|
||
map :: (a -> b) -> FromFunctor f a -> FromFunctor f b | ||
map = Hask.fmap | ||
|
||
deriving via (FromFunctor []) instance Functor [] | ||
|
||
deriving via (FromFunctor Maybe) instance Functor Maybe | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
newtype FromContra f a = FromContra {getContra :: f a} | ||
deriving newtype (Hask.Contravariant) | ||
|
||
instance (Hask.Contravariant f) => Functor (FromContra f) where | ||
type Dom (FromContra f) = Op | ||
type Cod (FromContra f) = (->) | ||
|
||
map :: Dom (FromContra f) a b -> Cod (FromContra f) ((FromContra f) a) ((FromContra f) b) | ||
map = Hask.contramap . getOp | ||
|
||
deriving via (FromContra Predicate) instance Functor Predicate | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
-- TODO: | ||
-- newtype FromBifunctor f a b = FromBifunctor (f a b) | ||
|
||
-- instance (Hask.Bifunctor f) => Functor f where | ||
-- type Dom f = (->) | ||
-- type Cod f = (Nat (->) (->)) | ||
|
||
-- map :: Hask.Bifunctor f => (a -> b) -> Nat (->) (->) (f a) (f b) | ||
-- map = _ | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
-- TODO: | ||
-- newtype FromProfunctor f a b = FromProfunctor (f a b) | ||
|
||
-- instance (Hask.Profunctor f) => Functor f where | ||
-- type Dom f = Op | ||
-- type Cod f = (Nat (->) (->)) | ||
|
||
-- map :: (Hask.Profunctor f) => Op a b -> Nat (->) (->) (f a) (f b) | ||
-- map = _ | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Examples | ||
|
||
type EndofunctorOf :: Cat ob -> (ob -> ob) -> Constraint | ||
type EndofunctorOf cat = FunctorOf cat cat | ||
|
||
type Contravariant = FunctorOf Op (->) | ||
type Bifunctor = FunctorOf (->) (Nat (->) (->)) | ||
type Profunctor = FunctorOf Op (Nat (->) (->)) | ||
|
||
fmap' :: (EndofunctorOf (->) f) => (a -> b) -> f a -> f b | ||
fmap' = map | ||
|
||
contramap' :: (Contravariant f) => (a -> b) -> f b -> f a | ||
contramap' f = map (Op f) | ||
|
||
-- bimap' :: forall f a b c d. FunctorOf (->) (Nat (->) (->)) f => (a -> b) -> (c -> d) -> f a c -> f b d | ||
-- bimap' f g = _ | ||
|
||
-- dimap' :: FunctorOf Op (Nat (->) (->)) p => (a -> b) -> (c -> d) -> p b c -> p a d | ||
-- dimap' f g = _ |