Re: [Haskell-cafe] What are free Monads?

2010-03-02 Thread Sebastian Fischer


On Mar 2, 2010, at 5:59 AM, Dan Doel wrote:


 http://haskell.org/haskellwiki/Free_monad
 http://haskell.org/haskellwiki/Free_structure


Nice, thank you for writing this.


Feel free to make suggestions/changes.



I enjoyed reading it although Section 3 is challenging for people  
(like me) who know algebra but do not know the exact meaning of the  
mentioned terminology from CT even if they have read about it before.  
It would be helpful to add intuitive explanations. For example, after


Simplest (in the sense we want) structures in that category  
will then
either be initial or terminal, and thus, freeness can be defined  
in terms

of such universal constructions.

I missed sentences

Intuitively, initial means that ... and thus relates to the  
informal

description because ...

Final means ... and expresses the informal idea of ...

Similarly, subsequent uses of CT terminology (like 'forgetful functor'  
and 'natural transformation') could be related to intuitions given  
before (or new ones).


Sebastian


--
Underestimating the novelty of the future is a time-honored tradition.
(D.G.)



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] What are free Monads?

2010-03-01 Thread Henning Thielemann


On Sat, 27 Feb 2010, Dan Doel wrote:


Free structures originate (I think) in algebra. There you'll find talk of free
groups, free rings, free monoids, etc.


How about turning this post into a HaskellWiki article in 
Category:Glossary ?

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] What are free Monads?

2010-03-01 Thread Dan Doel
On Monday 01 March 2010 12:50:21 pm Henning Thielemann wrote:
 On Sat, 27 Feb 2010, Dan Doel wrote:
  Free structures originate (I think) in algebra. There you'll find talk of
  free groups, free rings, free monoids, etc.
 
 How about turning this post into a HaskellWiki article in
 Category:Glossary ?

Here's a first draft.

  http://haskell.org/haskellwiki/Free_monad
  http://haskell.org/haskellwiki/Free_structure

Feel free to make suggestions/changes.

-- Dan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] What are free Monads?

2010-02-27 Thread Daniel Peebles
Given any functor you can get a monad for free!

data Free f a = Either a (f (Free f a))

Not sure about unfree, but there are cofree comonads that are pretty closely
related, and give you a comonad given a functor:

data Cofree f a = (a, f (Cofree f a))

I'm sure the more categorically minded can tell you way more.

Hope this helps,
Dan

2010/2/27 Günther Schmidt gue.schm...@web.de

 Hello,

 I see the term free monad quite a lot, but don't really see an
 explanation what a free monad is. What sets a monad free and why in this day
 and age are there unfree monads?

 Günther


 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] What are free Monads?

2010-02-27 Thread Günther Schmidt

Hello Daniel,

that looks lovely, but it doesn't help me much :)

Günther

Am 27.02.10 10:06, schrieb Daniel Peebles:

Given any functor you can get a monad for free!

data Free f a = Either a (f (Free f a))

Not sure about unfree, but there are cofree comonads that are pretty 
closely related, and give you a comonad given a functor:


data Cofree f a = (a, f (Cofree f a))

I'm sure the more categorically minded can tell you way more.

Hope this helps,
Dan

2010/2/27 Günther Schmidt gue.schm...@web.de mailto:gue.schm...@web.de

Hello,

I see the term free monad quite a lot, but don't really see an
explanation what a free monad is. What sets a monad free and why
in this day and age are there unfree monads?

Günther


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org mailto:Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] What are free Monads?

2010-02-27 Thread Dan Doel
On Saturday 27 February 2010 3:54:25 am Günther Schmidt wrote:
 I see the term free monad quite a lot, but don't really see an
 explanation what a free monad is. What sets a monad free and why in this
 day and age are there unfree monads?

Free structures originate (I think) in algebra. There you'll find talk of free 
groups, free rings, free monoids, etc. The plain English explanation is that 
you want to take some 'underlying' structure, and promote it into the 
structure in question, but to do so in the simplest way possible in a sense. 
In the algebra case, the underlying structure is typically a set, so you'll 
have the free monoid over a set, the free group over a set, etc.

Monoids are pretty simple, so they're probably easiest to explain. So, monoids 
are:

  A set M
  A binary operation m : M x M - M
  An identity element e : M

and follow the laws:

  a `m` (b `m` c) = (a `m` b) `m` c
  e `m` a = a = a `m` e

So, for a free monoid over a set S, we want to produce a structure satisfying 
the above, with an additional constraint that we need to have an:

  i : S - M

to inject elements of S into the monoid. And by simplest above, we mean 
something like:

  1) All the elements of M should be required to exist either by i, or by the
 operations for the structure.
  2) The only equational laws that should hold for the structure should be
 those that are required to hold for structures of that type.

These may be kind of vague, and I apologize. They can be made more precise in 
category theory (at least). So, for the above rules, we get that the free 
monoid over a set is the monoid of lists of elements of that set with 
concatenation and nil.

  M = [S]
  e = []
  m = (++)
  i = \x - [x] -- \x - x : []

  [] ++ xs = xs = xs ++ []
  xs ++ (ys ++ zs) = (xs ++ ys) ++ zs

In category theory, this is usually presented in terms of adjoint functors. 
What you do is specify a forgetful or underlying functor, from, say, the 
category of monoids to the category of sets. Then, a left adjoint to that 
functor is called (I believe) the free monoid functor, and it takes sets to 
the free monoid thereover. I don't really have the wherewithal to give an 
explanation of adjoint functors, but adjunctions are what capture the two 
rules for free things above.

So, now we want free monads over a (endo)functor F. As it turns out, monads 
are monoid objects in the category of endofunctors over a category, which is a 
generalization of the above monoids to categories other than sets. So, we can 
hopefully use the above intuitive idea of a free monoid to figure out what 
might be going on with free monads.

So, first we have an endofunctor F which is going to be analogous to the 
underlying set. And we'll need some type of injection

  i : F - M

M being the functor part of the free monad over F. But arrows in the category 
in question are natural transformations, so in Haskell, this would be more 
like:

  i :: forall a. F a - M a

Monads are also required to have a couple operations:

  return :: forall a. a - M a
  join   :: forall a. M (M a) - M a

And they should satisfy some monad laws. I can't explain to you how to figure 
out what M, return and join should be, because I don't know how myself; I'm 
kind of winging it at this point. But Daniel Peebles has given the right 
answer. It's datatype:

  data M a = Return a | Roll (F (M a))

with:

  return = Return

  join (Return m) = m
  join (Roll fmm) = Roll (fmap join fmm)

  i f = Roll (fmap Return f)
  -- this should look vaguely similar to to \x - x : []

which, you might notice, is similar in character to the free monoid above. In 
the free monoid, you can inject elements of the set, and you can multiply 
together lists to get arbitrarily long strings of elements of the underlying 
set. In the free monad, there's a way to 'inject' the functor into the monad, 
and you can 'multiply' in the monad to get arbitrarily deep composition 
'strings' of F (by repeatedly Rolling).

There are also cofree structures, as was mentioned. The difference is that 
while free functors are left adjoints to underlying functors, cofree functors 
are right adjoints. I don't have much to say beyond that, though.

Anyhow, hope that gave some insight.

-- Dan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe