Gabriella439 / Gabriella439/Haskell-MMorph-Library

Add MCoyoneda

Open
#33 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
53
Forks
24
PR merge metrics
No merged PRs in 30d

Description

Sometimes it can be difficult to implement an `MFunctor` instance for a given type due to the lack of a `Monad` constraint on the result type of `hoist`. Especially if you only have access to what is exported in the API for that type (e.g. [1](http://stackoverflow.com/questions/41797501/implementing-an-mfunctor-instance-for-rvart/), [2](http://stackoverflow.com/questions/21391770/why-does-mfunctors-hoist-not-have-monad-n-constraint/)). However this can be worked around using a coyoneda construction like this (from my [answer](http://stackoverflow.com/questions/21391770/why-does-mfunctors-hoist-not-have-monad-n-constraint/41816815#41816815) in the second link):

{-# LANGUAGE RankNTypes, GADTs #-}

import Control.Monad.Morph

-- Slightly weaker than MFunctor due to the monad constraint on n.
class MFunctor' t where
hoist' :: (Monad m, Monad n) => (forall b. m b -> n b) -> t m a -> t n a

data MCoyoneda t n a where
MCoyoneda :: Monad m => (forall b. m b -> n b) -> t m a -> MCoyoneda t n a

liftMCoyoneda :: Monad m => t m a -> MCoyoneda t m a
liftMCoyoneda = MCoyoneda id

lowerMCoyoneda' :: (MFunctor' t, Monad n) => MCoyoneda t n a -> t n a
lowerMCoyoneda' (MCoyoneda f tma) = hoist' f tma

-- The result is actually slightly stronger than 'MFunctor', as we do not need
-- a monad for 'm' either.
hoistMCoyoneda :: (forall b. m b -> n b) -> MCoyoneda t m a -> MCoyoneda t n a
hoistMCoyoneda f (MCoyoneda trans tma) = MCoyoneda (f . trans) tma

instance MFunctor (MCoyoneda t) where
hoist = hoistMCoyoneda

Would this be a useful addition to `mmorph`?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the exported Control.Monad.Morph API and the existing MFunctor definitions, then compare the proposed MCoyoneda types and operations with the package's conventions. Confirm whether the addition belongs in the public API and verify that the package builds with the new abstraction and its expected hoist behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.