[Template-haskell] quasi quotes and Q monad

Tomasz Zielonka tomasz.zielonka at gmail.com
Mon Jan 2 12:22:25 EST 2006


On Mon, Jan 02, 2006 at 05:16:36PM +0100, Ch. A. Herrmann wrote:
> The value I want to pass for x is of type Exp. Of course, I could turn
> this type into (Q Exp) by applying return, but this artificial instance 
> of the Q monad would come from nowhere, not being connected with the
> regular instance used, e.g., for the fresh name generation.

It seems that you are a bit confused with monads. The only monad you
have here is the Q type constructor. Applying "return" to your value
you'll get a Q-action, which can be one of parts used to construct
bigger and bigger Q-actions with >>=. It's a usual way of doing things
with monads, don't be afraid.

Haskell is designed in such a way, that when you're facing a possibility
of undefined behaviour, etc, you see some "danger" signs in your code,
like uses of unsafePerformIO (unsafe!!!). In all other cases that
typechecker should reject badly behaving programs.

> Especially, I have the following questions:
> * Is there a simple solution to this problem? If so, please tell me and
>   forget about the following questions.

You gave it.

> * Is the quasi quote mechanism at all appropriate for what I want to do
>   or should one better change to the concrete AST representation? That 
> 
>   would be unfortunate because my aim is to develop Template Haskell
>   examples which demonstrate ease of use.

As long as it works, it should be appropriate. I used it in this way
and it was a lot easier than writing those huge, baroque AST
representations. After all that's what quasi-quote is for - to make your
life easier!

> * If return is used to turn an expression into monadic form before
>   splicing, is it possible that
>   (a) the consistency of fresh name generation is lost, even if one
>       does the name generation for the spliced expression oneself,

No, everything should be fine.

Best regards
Tomasz

-- 
I am searching for a programmer who is good at least in some of
[Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland


More information about the template-haskell mailing list