Sun Jul 8 08:47:08 EDT 2018

DSL, sharing and monad laws

This keeps coming back, so I put it in SeqLib.hs

Typically, in a monadic language with internal nodes, when the same
monadic value is passed to a 2-argument function like this:

f ma mb = do
  a <- ma
  b <- mb
  return $ add a b

The monad is evaluated twice.

There seems to be no simple way to avoid this.  Period.

If sharing is needed, use do notation.

If there is no fanout, it's ok to use applicative notation to build
nested expressions.

Note that in an expression, there can never be any sharing, except for
sharing introduced by combinators, and those could implement it
correctly.  E.g.

dup m = do a <- m; return (a, a)

So the point about sharing is moot, really.

The real issue is that a couple of interfaces are needed:

  a -> a -> m a

  a -> m a -> m a

  m a -> a -> m a

  m a -> m a -> m a

I see no good way to do this that is easy to use apart from
introducing a syntax manipulation step.