到目前为止,我遇到的每个monad(可以表示为数据类型)都有相应的monad变换器,或者可以有一个.有这样一个不能有一个单子吗?或者所有monad都有相应的变压器?
通过对应于monad的变换器t,m我的意思t Identity是同构的m.当然,它满足monad变压器法则,并且t n是任何monad的monad n.
我希望看到每个monad都有一个证明(理想情况下是建设性的证明),或者没有一个证明(带证明)的特定monad的例子.我对更多面向Haskell的答案以及(类别)理论答案感兴趣.
作为后续问题,是有一个单子m是有两个不同的变压器t1和t2?也就是说,t1 Identity同构t2 Identity和m,但有一个单子n这样t1 n是不是同构t2 n.
(IO并且ST有一个特殊的语义,所以我不在这里考虑它们,让我们完全忽略它们.让我们只关注可以使用数据类型构造的"纯"monad.)
Lately I have been playing with this type, which I understand to be an encoding of the free distributive functor (for tangential background on that, see this answer):
data Ev g a where
Ev :: ((g x -> x) -> a) -> Ev g a
deriving instance Functor (Ev g)
Run Code Online (Sandbox Code Playgroud)
The existential constructor ensures I can only consume an Ev g by supplying a polymorphic extractor forall x. g x -> x, and that the lift and lower …
具有类似QuickCheck promote功能的结构的仿函数的一般术语是什么,即形式的函数:
promote :: (a -> f b) -> f (a -> b)
Run Code Online (Sandbox Code Playgroud)
(这是反过来的flip $ fmap (flip ($)) :: f (a -> b) -> (a -> f b)).除了(->) r和之外,是否还有任何具有此类操作的仿函数Id?(我相信一定有).谷歌搜索'quickcheck promote'只显示了QuickCheck文档,该文档没有提供promote任何更一般的上下文AFAICS; 搜索"快速检查促销"的SO不会产生任何结果.
haskell functional-programming functor quickcheck category-theory
该Applicative类型类代表松懈monoidal函子是保留对输入功能类别的笛卡尔monoidal结构。
换句话说,给定规范同构见证(,)形成幺半群结构:
-- Implementations left to the motivated reader
assoc_fwd :: ((a, b), c) -> (a, (b, c))
assoc_bwd :: (a, (b, c)) -> ((a, b), c)
lunit_fwd :: ((), a) -> a
lunit_bwd :: a -> ((), a)
runit_fwd :: (a, ()) -> a
runit_bwd :: a -> (a, ())
Run Code Online (Sandbox Code Playgroud)
类型类及其定律可以等价地写成这样:
class Functor f => Applicative f
where
zip :: (f a, f b) -> f (a, b)
husk :: () -> f ()
-- …Run Code Online (Sandbox Code Playgroud) Foldable是一个超类Traversable,类似于Functor超类的Applicative和Monad.
类似的情况下Monad,它可以基本上实现fmap为
liftM :: Monad m => (a->b) -> m a -> m b
liftM f q = return . f =<< q
Run Code Online (Sandbox Code Playgroud)
我们也可以效仿foldMap的
foldLiftT :: (Traversable t, Monoid m) => (a -> m) -> t a -> m
foldLiftT f = fst . traverse (f >>> \x -> (x,x))
-- or: . sequenceA . fmap (f >>> \x -> (x, x))
Run Code Online (Sandbox Code Playgroud)
使用Monoid m …
我记得在某处读到过这样的类型不能是Traversable:
data Bar a = Bar a deriving(Show)
instance Functor Bar where
fmap f (Bar x) = Bar (f x)
instance Foldable Bar where
foldMap f (Bar x) = f x <> f x
Run Code Online (Sandbox Code Playgroud)
我记得的一点解释是,为了foldMap = foldMapDefault保持,Traversable实例必须多次访问其元素,这是合法实例无法做到的。但是,我不记得为什么合法实例不能这样做。考虑这个:
instance Traversable Bar where
sequenceA (Bar x) = Bar <$ x <*> x
Run Code Online (Sandbox Code Playgroud)
乍一看还不错。这样做有什么违法?
我有一个功能seperateFuncs,使得
seperateFuncs :: [a -> b] -> (a -> [b])
seperateFuncs xs = \x -> map ($ x) xs
Run Code Online (Sandbox Code Playgroud)
我想知道反过来是否存在,即是否存在功能
joinFuncs :: (a -> [b]) -> [a -> b]
Run Code Online (Sandbox Code Playgroud)
我认为不是(主要是因为列表不是固定的长度),但也许我会被证明是错的.那么问题是有一些数据类型f有一个函数::(a - > fb) - > f(a - > b)?
我试图通过在Javascript中实现他们的函数实例来提高我对Applicatives和Monads的理解.我对Haskell的了解有限,我希望我的问题有道理.
下面是我的实现fmap,<*>以及>>=为Functor,Applicative并Monad在Javascript类型类:
const fmap = f => g => x => f(g(x)); // B combinator
const apply = f => g => x => f(x) (g(x)); // S combinator
const bind = f => g => x => g(f(x)) (x); // ?
Run Code Online (Sandbox Code Playgroud)
我不确定bindHaskell实现的正确翻译是否正确:
(>>=) :: (r -> a) -> (a -> (r -> b)) -> r -> b
instance Monad ((->) r) …Run Code Online (Sandbox Code Playgroud) 我对这里描述的用于确定伴随仿函数的monad变换器的结构很感兴趣.这里有一些代码总结了基本思想:
{-# LANGUAGE MultiParamTypeClasses #-}
import Control.Monad
newtype Three g f m a = Three { getThree :: g (m (f a)) }
class (Functor f, Functor g) => Adjoint f g where
counit :: f (g a) -> a
unit :: a -> g (f a)
instance (Adjoint f g, Monad m) => Monad (Three g f m) where
return = Three . fmap return . unit
m >>= f = Three $ fmap (>>= counit . …Run Code Online (Sandbox Code Playgroud) haskell ×10
functor ×3
monads ×3
applicative ×2
traversable ×2
containers ×1
fold ×1
function ×1
list ×1
quickcheck ×1
state-monad ×1