Haskell 表示StateT函子实例定义的另一种方法

Haskell 表示StateT函子实例定义的另一种方法,haskell,functor,state-monad,Haskell,Functor,State Monad,我以以下方式实现了StateT的Functor实例 import Data.Tuple (swap) newtype StateT s m a = StateT { runStateT :: s -> m (a, s) } instance Functor m => Functor (StateT s m) where fmap f (StateT g) = StateT $ \s -> fmap (fmapSwapTwice f) (g s)

我以以下方式实现了
StateT
Functor
实例

import Data.Tuple (swap)

newtype StateT s m a =
    StateT { runStateT :: s -> m (a, s) }

instance Functor m => Functor (StateT s m) where
    fmap f (StateT g) = StateT $ \s -> fmap (fmapSwapTwice f) (g s)
        where fmapSwapTwice f tup = swap $ f <$> swap tup
import Data.Tuple(交换)
新类型StateT s m a=
StateT{runStateT::s->m(a,s)}
实例函子m=>函子(StateT s m),其中
fmap f(StateT g)=StateT$\s->fmap(fmapSwapTwice f)(g s)
其中fmapSwapTwice f tup=swap$f swap tup
但是我对我的解决方案并不完全满意,因为它需要我从
Data.Tuple
导入
swap
,并在Tuple上导入
fmap
两次,以便将
f
应用于第一个元素


对我来说,这似乎是一种足够常见的模式,它有自己的抽象,或者可能有一种替代方法来表达
函子
实例,我想不出它根本不需要
交换

利用元组的
双函数
实例。使用而不是
fmapSwapTwice