Skip to content

Instantly share code, notes, and snippets.

@Lysxia
Last active July 15, 2019 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lysxia/04039e4ca6f7a3740281e4e3583ae971 to your computer and use it in GitHub Desktop.
Save Lysxia/04039e4ca6f7a3740281e4e3583ae971 to your computer and use it in GitHub Desktop.
Monads indexed by categories
{-# LANGUAGE RankNTypes, TypeFamilies, PolyKinds, DataKinds #-}
module CatMonad where
import Control.Category as C
import Data.Kind
class CatMonad (m :: forall (x :: k) (y :: k). c x y -> Type -> Type) where
type Id m :: c x x
type Cat m (f :: c x y) (g :: c y z) :: c x z
xpure :: a -> m (Id m) a
xbind :: m f a -> (a -> m g b) -> m (Cat m f g) b
data Edge x y = E
newtype IWriter cat i j (q :: Edge i j) a = IWriter { runIWriter :: (a, cat i j) }
instance Category cat => CatMonad (IWriter cat) where
type Id (IWriter cat) = E
type Cat (IWriter cat) _ _ = E
xpure a = IWriter (a, C.id)
xbind (IWriter (a, f)) k =
let IWriter (b, g) = k a in
IWriter (b, f C.>>> g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment