Andres Löh | 3 Oct 08:01 2013

Free monads

Hi everyone.

I'll follow Simon's lead, and ask a similar question with a similar
motivation. I'm going to talk about free monads at the upcoming
Haskell eXchange next Wednesday. I'll not limit myself to a particular
library, and I'm open to related approaches (e.g. "operational") as
well.

I'm also looking for as many compelling examples as possible. Like
Simon, I don't want to know anything secret or anything that you
wouldn't like me to include in my talk. Most useful are pointers to
existing libraries using free monads that I might have missed (for
example, because they're new or very specialized).

Thanks a lot for your help in advance.

Cheers,
  Andres

--

-- 
Andres Löh, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com
Dan Burton | 3 Oct 11:01 2013
Picon

Re: Free monads


-- Dan Burton


On Wed, Oct 2, 2013 at 11:01 PM, Andres Löh <andres <at> well-typed.com> wrote:
Hi everyone.

I'll follow Simon's lead, and ask a similar question with a similar
motivation. I'm going to talk about free monads at the upcoming
Haskell eXchange next Wednesday. I'll not limit myself to a particular
library, and I'm open to related approaches (e.g. "operational") as
well.

I'm also looking for as many compelling examples as possible. Like
Simon, I don't want to know anything secret or anything that you
wouldn't like me to include in my talk. Most useful are pointers to
existing libraries using free monads that I might have missed (for
example, because they're new or very specialized).

Thanks a lot for your help in advance.

Cheers,
  Andres

--
Andres Löh, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Alp Mestanogullari | 3 Oct 13:23 2013
Picon

Re: Free monads

Hi,

You might find free-game [1] to be quite original, and it may help for the audience that's you're talking about a game rather than category theoretic stuffs. I also second Dan's suggestion though.




On Thu, Oct 3, 2013 at 8:01 AM, Andres Löh <andres <at> well-typed.com> wrote:
Hi everyone.

I'll follow Simon's lead, and ask a similar question with a similar
motivation. I'm going to talk about free monads at the upcoming
Haskell eXchange next Wednesday. I'll not limit myself to a particular
library, and I'm open to related approaches (e.g. "operational") as
well.

I'm also looking for as many compelling examples as possible. Like
Simon, I don't want to know anything secret or anything that you
wouldn't like me to include in my talk. Most useful are pointers to
existing libraries using free monads that I might have missed (for
example, because they're new or very specialized).

Thanks a lot for your help in advance.

Cheers,
  Andres

--
Andres Löh, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries



--
Alp Mestanogullari
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
木下郁章 | 6 Oct 15:47 2013
Picon

Re: Free monads

I use the operational monad to express characters' behaviour in a game. You can see how it is used: https://github.com/fumieval/spellWire/blob/master/Types.hs#L92

I would be so happy if you  talk about my free-game, of course.

BTW, I'm told by Kazu to reply this thread. 

# I've sent to one address by mistake. I'll see to it that nothing goes wrong.


2013/10/3 Alp Mestanogullari <alpmestan <at> gmail.com>
Hi,

You might find free-game [1] to be quite original, and it may help for the audience that's you're talking about a game rather than category theoretic stuffs. I also second Dan's suggestion though.




On Thu, Oct 3, 2013 at 8:01 AM, Andres Löh <andres <at> well-typed.com> wrote:
Hi everyone.

I'll follow Simon's lead, and ask a similar question with a similar
motivation. I'm going to talk about free monads at the upcoming
Haskell eXchange next Wednesday. I'll not limit myself to a particular
library, and I'm open to related approaches (e.g. "operational") as
well.

I'm also looking for as many compelling examples as possible. Like
Simon, I don't want to know anything secret or anything that you
wouldn't like me to include in my talk. Most useful are pointers to
existing libraries using free monads that I might have missed (for
example, because they're new or very specialized).

Thanks a lot for your help in advance.

Cheers,
  Andres

--
Andres Löh, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries



--
Alp Mestanogullari

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries


_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Edward Kmett | 3 Oct 15:24 2013
Picon

Re: Free monads

We use the "Yielding IO" construction for the FFI from our programming language Ermine. 

That sits atop a free monad.

In "PHOAS for Free" I show PHOAS is also just a free monad, when you look at it right, so much of the syntax manipulation in agda/coq can be viewed as just another application of a free monad. Moreover, bound uses something that is "almost free", which is how I handle the rest of my syntax trees. ;)

I use similar free/operational monads for manipulating systems of equations for stochastic differential algebraic equations. 

An example of using it to model circuits:

type Real = Signal Double
type Resistance  = Real
type Inductance  = Real
type Capacitance = Real
type Current     = Real
type Voltage     = Real

data Pin = Pin { __v :: Voltage, __i :: Current }
makeLenses ''Pin

instance Connector Pin where
  cap = Pin <$> cap <*> cap
  equate (Pin v1 i1) (Pin v2 i2) = do
    v1 := v2
    i1 := i2

flop :: (Connector a, Connector b) => (a -> Model b) -> b -> Model a
flop f b = do
  top <- cap
  b' <- f top
  equate b b'
  return top

twoPin :: Pin -> Model (Pin, Voltage)
twoPin p = do
  n <- cap
  p^._i + n^._i := 0
  return (n, p^._v - n^._v)

basic :: Pin -> (Voltage -> Model ()) -> Model Pin
basic p k = do
  (n,u) <- twoPin p
  k u
  return n

resistor :: Resistance -> Pin -> Model Pin
resistor r p = basic p $ \u -> r * p^._i := u

inductor :: Inductance -> Pin -> Model Pin
inductor l p = basic p $ \u -> l * der (p^._i) := u

capacitor :: Capacitance -> Pin -> Model Pin
capacitor c p = basic p $ \u -> c * der u := p^._i

conductor :: Conductance -> Pin -> Model Pin
conductor g p = basic p $ \u -> p^._i := g * p^._v

-- | <at> transformer l1 m l2 <at> represents a transformer with
-- primary inductance <at> l1 <at> , coupling inductance <at> m <at> , and secondary inductance <at> l2 <at>
transformer :: Inductance -> Inductance -> Inductance -> Pin -> Model Pin
transformer l1 m l2 p <at> (Pin v1 i1) = do
  (n <at> (Pin v2 i2),u) <- twoPin
  v1 := l1 * der i1 + m * der i2
  v2 := m * der i1 + l2 * der i2
  return n

Then I can fold together circuits with things like:

circuit = do
  p   <- cap
  cn  <- capacitor 0.00047 =<< resistor 1000 p
  ind <- inductor 0.01 =<< resistor 2200 p
  acn <- acVoltageSource 12 p
  gn  <- ground
  cup [cn,ind,acn,gn]

or I can model stocks:

type Real = Signal Double
type Rate = Real

data Stock = Stock { _price, _drift, _volatility :: Real }
makeLenses ''Stock

instance Connector Stock where
  cap = Stock <$> cap <*> cap <*> cap
  equate (Stock p d v) (Stock p d2 v2) = do
    p1 := d1
    d1 := d2
    v1 := v2

stock :: Model Stock
stock = do
  model <at> (Stock s mu sigma) <- cap
  w <- brownianMotion
  der s := mu * s + sigma * der w
  assume (<=) s 0
  return model

-- <at> forward t r s <at> calculate the forward price of a stock <at> s <at> at time <at> t <at> assuming a risk free rate <at> r <at>
forward :: Time -> Rate -> Stock -> Model Price
forward end rate stock = do
  t <- now
  assume (<=) t end
  return $ stock^.price * exp (rate * (end - now))

I also use the free monad as part of a variant on Tim Sheard's 2-level unifier. If you look Wren Thornton's version of unification-fd the UTerm type is a free monad.

My machines package uses a CPS'd free monad (Plan) to build up an explicit fixed point (Machine).

wl-pprint-extras uses a free-monad based Doc to permit me to sprinkle annotations about color or whatever I want into the document.

That's most of what I can come up with off the top of my head.

-Edward


On Thu, Oct 3, 2013 at 2:01 AM, Andres Löh <andres <at> well-typed.com> wrote:
Hi everyone.

I'll follow Simon's lead, and ask a similar question with a similar
motivation. I'm going to talk about free monads at the upcoming
Haskell eXchange next Wednesday. I'll not limit myself to a particular
library, and I'm open to related approaches (e.g. "operational") as
well.

I'm also looking for as many compelling examples as possible. Like
Simon, I don't want to know anything secret or anything that you
wouldn't like me to include in my talk. Most useful are pointers to
existing libraries using free monads that I might have missed (for
example, because they're new or very specialized).

Thanks a lot for your help in advance.

Cheers,
  Andres

--
Andres Löh, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Mario Blažević | 3 Oct 20:38 2013

Re: Free monads

On 13-10-03 02:01 AM, Andres Löh wrote:
> I'm also looking for as many compelling examples as possible. Like
> Simon, I don't want to know anything secret or anything that you
> wouldn't like me to include in my talk. Most useful are pointers to
> existing libraries using free monads that I might have missed (for
> example, because they're new or very specialized).

	The most common use case is for coroutines, as evidenced by pipes and 
conduits. My old article from the Monad.Reader issue #19 covers some 
basics, so you may find it useful.

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Daniel Díaz Carrete | 3 Oct 20:51 2013
Picon

Re: Free monads

Andres Löh <andres <at> well-typed.com> writes:

> 
> Hi everyone.
> 
> I'll follow Simon's lead, and ask a similar question with a similar
> motivation. I'm going to talk about free monads at the upcoming
> Haskell eXchange next Wednesday. I'll not limit myself to a particular
> library, and I'm open to related approaches (e.g. "operational") as
> well.
> 
> I'm also looking for as many compelling examples as possible. Like
> Simon, I don't want to know anything secret or anything that you
> wouldn't like me to include in my talk. Most useful are pointers to
> existing libraries using free monads that I might have missed (for
> example, because they're new or very specialized).
> 
> Thanks a lot for your help in advance.
> 
> Cheers,
>   Andres
> 

I my "pianola" package, I use a free monad (in module Pianola.Protocol) to 
abstract away the connection details of RPC calls, along with the 
reading/writing from the handles.

I use another free monad (in module Pianola.Pianola, but not exported) to 
sequence observational interactions with an external object accessed through 
the network. The functor is the Kleisli arrow of a monad. The observations 
can return no results, or more than one result. Depending on the 
interpreter, more than one result can be treated as an error, or at least 
merit a warning. The current interpreter just chooses the first  result and 
keeps going.

Cheers.

http://hackage.haskell.org/package/pianola-0.1.1
http://hackage.haskell.org/package/pianola-0.1.1/docs/Pianola-Protocol.html
http://hackage.haskell.org/package/pianola-0.1.1/docs/src/Pianola-
Pianola.html

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
John Wiegley | 3 Oct 23:18 2013

Re: Free monads

>>>>> Andres Löh <andres <at> well-typed.com> writes:

> I'll follow Simon's lead, and ask a similar question with a similar
> motivation. I'm going to talk about free monads at the upcoming Haskell
> eXchange next Wednesday. I'll not limit myself to a particular library, and
> I'm open to related approaches (e.g. "operational") as well.

One way I think about is: for the same reason you might want to pass around a
list of numbers, rather than a sum or a length -- i.e., so that consume those
numbers in multiple, different ways -- so too you might want to pass a list of
"effects" (do this, do this, then do that) rather than a single, composite
effect.  In this sense, the free monad is just a free monoid in the category
of endofunctors, and offers the same utility.

--

-- 
John Wiegley
FP Complete                         Haskell tools, training and consulting
http://fpcomplete.com               johnw on #haskell/irc.freenode.net
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Heinrich Apfelmus | 4 Oct 10:46 2013
Picon

Re: Free monads

Andres Löh wrote:
> Hi everyone.
> 
> I'll follow Simon's lead, and ask a similar question with a similar
> motivation. I'm going to talk about free monads at the upcoming
> Haskell eXchange next Wednesday. I'll not limit myself to a particular
> library, and I'm open to related approaches (e.g. "operational") as
> well.
> 
> I'm also looking for as many compelling examples as possible. Like
> Simon, I don't want to know anything secret or anything that you
> wouldn't like me to include in my talk. Most useful are pointers to
> existing libraries using free monads that I might have missed (for
> example, because they're new or very specialized).

I have included a couple of examples with the operational package:

<https://github.com/HeinrichApfelmus/operational/tree/master/doc/examples#readme>

My personal highlights are:

BreadthFirstParsing.hs
An implementation of parser combinators that does not have the space 
leak associated to the depth-first approach  s -> [(a,s)] . Instead, 
parsers are evaluated in a breadth-first fashion, so that the input can 
be consumed in an on-line fashion. This is equivalent to Koen Classen's 
"parallel parsing processes", which I believe are used to implement the 
'Text.ReadP' module.

WebSessionState.lhs
A program is written as a sequence of instructions, in this case a web 
session ("shopping cart checkout"). However, unlike the code may 
suggest, the program actually exits before completing and continues at 
the last instruction when started. No persistent state is kept in RAM. I 
learned this idea from Peter Thiemann's WASH/CGI library.

TicTacToe.hs
Similar to the previous example, but dialed up a notch. A game is 
implemented using a monad that returns player input. Internally, 
however, the player input can come from many sources: user, computer, or 
even replays, depending on how the free monad is interpreted.

A less useful but still interesting example is `State.hs`, which 
implements the state monad using only  s -> a  instead of  s -> (a,s) .

Another example that I forgot to add when writing the package, but which 
also one of my highlights, is the probability monad. Using operational, 
it is possible to use the same code for both calculating complete 
probability distributions and sampling from said probability distributions.

Yet another example for free monads/operational is the sunroof package 
<http://hackage.haskell.org/package/sunroof-compiler>, which is an EDSL 
that compiles to JavaScript. JavaScript statements are sequenced in a 
monad, but the monad is then interpreted as a syntax tree ("deep 
embedding").

Best regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Kim-Ee Yeoh | 4 Oct 17:49 2013

Re: Free monads


On Thu, Oct 3, 2013 at 1:01 PM, Andres Löh <andres <at> well-typed.com> wrote:
I'm also looking for as many compelling examples as possible.

Hi Andres,

May I kindly suggest that you also consider negative examples? I.e. situations where free monads are _not_ suitable.

For pedagogical purposes, the more obviously unsuitable they are, the better.

-- Kim-Ee
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Andres Löh | 15 Oct 10:54 2013

Re: Free monads

Hi again.

I'd like to thank everyone who replied to my mail regarding examples
for the use of free monads.

I ended up using very few of the suggestions, due to time constraints.
If you want to look at the final result, it's available here:
http://skillsmatter.com/podcast/home/monads-for-free

I nevertheless gained a lot of insight by the examples provided. They
were therefore very helpful to me, also in the final stages of the
preparation of my talk. I feel motivated to produce a longer version
of the talk or a follow-up at some point, showing more examples.
Thanks again!

Cheers,
  Andres

--

-- 
Andres Löh, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com
Henning Thielemann | 15 Oct 22:57 2013
Picon

Re: Free monads


On Tue, 15 Oct 2013, Andres Löh wrote:

> I ended up using very few of the suggestions, due to time constraints.
> If you want to look at the final result, it's available here:
> http://skillsmatter.com/podcast/home/monads-for-free

Could you also give us the slides as PDF?

Thank you a lot!
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Andres Löh | 16 Oct 11:27 2013

Re: Free monads

Hi Henning.

> Could you also give us the slides as PDF?

Yes, of course:

http://www.andres-loeh.de/Free.pdf

Cheers,
  Andres

--

-- 
Andres Löh, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com
Henning Thielemann | 17 Oct 19:19 2013
Picon

Re: Free monads


On Wed, 16 Oct 2013, Andres Löh wrote:

> Hi Henning.
>
>> Could you also give us the slides as PDF?
>
> Yes, of course:
>
> http://www.andres-loeh.de/Free.pdf

Great! Very enlightening!
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries

Gmane