什么是免费单子? [英] What are free monads?

查看:78
本文介绍了什么是免费单子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了 Free Monad 这个字词弹出 every 现在 然后为有一段时间,但每个人似乎都在使用/讨论它们,而没有给出它们的解释。那么,什么是免费单子? (我会说我熟悉单子和Haskell的基础知识,但对类别理论只有非常粗略的理解。)

解决方案

爱德华Kmett的答案显然很棒。但是,这有点技术性。这可能是一个更容易理解的解释。

自由单体只是将函子转化为单子的一般方法。也就是说,给定任何函子 f Free f 是一个monad。这不会很有用,除非你得到一对函数

  liftFree :: Functor f => f a  - > Free f a 
foldFree :: Functor f => (f r→r)→> Free f r - > r

第一个让你进入你的monad,第二个给你一个b
$ b

更一般地说,如果X是一个带有一些额外东西P的Y,那么一个自由X是一种获得方式没有获得任何额外的Y到X。例子:monoid(X)是一个具有额外结构(P)的集合(Y),基本上说它有一个操作(你可以想到加法)和一些身份(如零)。



so

  class Monoid m其中
mempty: :m
mappend :: m - > m - > m

现在,我们都知道名单

  data [a] = [] | a:[a] 

好吧,给定任何类型 t 我们知道 [t] 是一个monoid

  instance Monoid [t]其中
mempty = []
mappend =(++)

,所以列表是自由幺半群超过集合(或Haskell类型)。



好吧,所以自由monad是相同的想法。我们拿一个函数,并且给一个monad。事实上,由于monad可以被看作是endo函子类别中的monoids,所以list的定义

  data [a] = [] | a:[a] 

看起来很像自由单体的定义

  data Free fa = Pure a | Roll(f(Free fa))

并且Monad实例与列表的Monoid实例相似

   - 它需要是一个函数
实例Functor f => Functor(Free f)其中
fmap f(Pure a)= Pure(fa)
fmap f(Roll x)= Roll(fmap(fmap f)x)

- - 这与(++)基本相同
concatFree :: Functor f => Free f(Free f a) - >免费f a
concatFree(Pure x)= x
concatFree(Roll y)= Roll(fmap concatFree y)

实例Functor f => Monad(Free f)其中
return = Pure - 就像[]
x>> = f = concatFree(fmap fx) - 这是绑定的标准concatMap定义

现在,我们得到了两个操作

   - 这与\x  - >基本相同。 [x] 
liftFree :: Functor f => f a - >免费f a
liftFree x = Roll(fmap Pure x)

- 这与折叠列表基本相同
foldFree :: Functor f => (f r→r)→> Free f r - > r
foldFree _(Pure a)= a
foldFree f(Roll x)= f(fmap(foldFree f)x)


I've seen the term Free Monad pop up every now and then for some time, but everyone just seems to use/discuss them without giving an explanation of what they are. So: what are free monads? (I'd say I'm familiar with monads and the Haskell basics, but have only a very rough knowledge of category theory.)

解决方案

Edward Kmett's answer is obviously great. But, it is a bit technical. Here is a perhaps more accessible explanation.

Free monads are just a general way of turning functors into monads. That is, given any functor f Free f is a monad. This would not be very useful, except you get a pair of functions

liftFree :: Functor f => f a -> Free f a
foldFree :: Functor f => (f r -> r) -> Free f r -> r

the first of these lets you "get into" your monad, and the second one gives you a way to "get out" of it.

More generally, if X is a Y with some extra stuff P, then a "free X" is a a way of getting from a Y to an X without gaining anything extra.

Examples: a monoid (X) is a set (Y) with extra structure (P) that basically says it has an operations (you can think of addition) and some identity (like zero).

so

class Monoid m where
   mempty  :: m
   mappend :: m -> m -> m

now, we all know lists

data [a] = [] | a : [a]

well, given any type t we know that [t] is a monoid

instance Monoid [t] where
  mempty   = []
  mappend = (++)

and so lists are the "free monoid" over sets (or in Haskell types).

Okay, so free monads are the same idea. We take a functor, and give back a monad. In fact, since monads can be seen as monoids in the category of endo functors, the definition of a list

data [a] = [] | a : [a]

looks a lot like the definition of free monads

data Free f a = Pure a | Roll (f (Free f a))

and the Monad instance has a similarity to the Monoid instance for lists

--it needs to be a functor
instance Functor f => Functor (Free f) where
  fmap f (Pure a) = Pure (f a)
  fmap f (Roll x) = Roll (fmap (fmap f) x)

--this is the same thing as (++) basically
concatFree :: Functor f => Free f (Free f a) -> Free f a
concatFree (Pure x) = x
concatFree (Roll y) = Roll (fmap concatFree y)

instance Functor f => Monad (Free f) where
  return = Pure -- just like []
  x >>= f = concatFree (fmap f x)  --this is the standard concatMap definition of bind

now, we get our two operations

-- this is essentially the same as \x -> [x]
liftFree :: Functor f => f a -> Free f a
liftFree x = Roll (fmap Pure x)

-- this is essentially the same as folding a list
foldFree :: Functor f => (f r -> r) -> Free f r -> r
foldFree _ (Pure a) = a
foldFree f (Roll x) = f (fmap (foldFree f) x)

这篇关于什么是免费单子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆