[Haskell-cafe] Multiple Interpretations for a monad?

Ryan Ingram ryani.spam at gmail.com
Thu Feb 25 21:42:10 EST 2010

To take this a step further, there is the DSL:

 get :: m S
 put :: S -> m ()

and the concrete implementation

  m = State S

Of course, there are other monads which implement this DSL as well:

  m = StateT S IO

  m = Prompt StatePrompt
  data StatePrompt a where
       Get :: StatePrompt S
       Put :: S -> StatePrompt ()

The "Prompt" solution(1) encodes your program (object of type Prompt
StatePrompt a) into a way such that it can be used with *any*
interpreter, whether it is the (s -> (a,s)) of State, or lifted into
part of some larger DSL, or whatever.  For example, to lift into
StateT S IO:

interpretPrompt :: Prompt StatePrompt a -> StateT S IO a
interpretPrompt = runPromptM f where
    f :: StatePrompt a -> StateT S IO a
    f Get = get
    f (Put x) = put x

So, I think a better way to describe it is that a DSL could be
implemented by many monads, and some monads let you interpret the DSL
into another monad.  Even StateT is this way, in a sense; you
interpret it with runStateT:

   runStateT myProgram initialState :: IO (S, a)

Now you have another program in a different monad (IO) which you need
to interpret somehow.

  -- ryan

(1) http://hackage.haskell.org/package/MonadPrompt

2010/2/25 David Leimbach <leimy2k at gmail.com>:
> Monads aren't necessarily EDSLs by themselves but are often shipped with
> functions that provide what would make them an EDSL.  Take the State monad,
> it has at least a get and a put function to work with the state in the
> monad.  That get and put are commands that function only within the domain
> of the State monad, and therefore could be thought of as an embedded
> language that is used to work with the state encapsulated in the State Monad
> computation.
> The way I like to think of it, a Monad provides an environment or a context
> within which it is very convenient to express an EDSL, and that this style
> of coding should be encouraged!  :-)
> Dave
> 2010/2/25 Günther Schmidt <gue.schmidt at web.de>
>> Hi everyone,
>> in my attempts to remove boilerplate and thus to do more "abstraction" I
>> come across a number of interesting things and suggestions.
>> Especially blog posts from Dan Piponi and also Heinrich Apfelmus. I think
>> what they both are saying is that you can construct / implement some sort of
>> 2 layered monad which can then have more than one interpretation.
>> In the responses to one of my posts on DSLs Dan Piponi also points out
>> that he considers monads also to be DSLs. It didn't click with me when he
>> said it, but reading more of his blog posts made me remember that.
>> Now I know this is probably something obvious to most haskellers, but to
>> me it's not.
>> Did I understand this correctly so far?
>> Günther
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list