[Template-haskell] two things I wanted from TH today

Isaac Jones ijones at syntaxpolice.org
Tue Apr 13 18:10:04 EDT 2004


Greetings.

FYI, I ran into two problems while trying to write something in TH
today that I thought I'd mention.  I'm not saying that these are vital
features, but that this is perhaps a vote for these :)

I wanted to write code to generate a function like this:

> foo :: Dynamic -> String
> foo val
>     | Just (a::String) <- fromDynamic val
>          = a
>     | Just (a::Int)    <- fromDynamic val
>          = show a
>     | Just (a::Float)  <- fromDynamic val
>          = show a

But it doesn't look like Pattern Guards are available in THSyntax yet.
I bravely started writing something to do this, but gave up, as did
someone else on IRC, since what I really want is a lot simpler.

So then I decided to settle for code like this:

> foo''  :: Dynamic -> String
> foo'' val
>     = case fromDynamic val of
>       Just (a::String) -> a
>       _ -> (case fromDynamic val of
>                   Just (a::Int) -> show a
>                   _ -> (case fromDynamic val of
>                               Just (a::Float) -> show a
>                               _ -> ""))

But then I found that type signatures on patterns also aren't
available.  In the end, I expect to be able to generate code like
this:

> foo'  :: Dynamic -> String
> foo' val
>     = case fromDynamic val of
>       Just a -> (a::String)
>       _ -> (case fromDynamic val of
>                   Just a -> show (a::Int)
>                   _ -> (case fromDynamic val of
>                               Just a -> show (a::Float)
>                               _ -> ""))

peace,

isaac

p.s.  As an aside, pattern guards are cool!  I've been avoiding almost
all extensions in my work projects, and am temporarily lifting that
self-imposed restriction.  I've learned some neat stuff in the past
week :)


More information about the template-haskell mailing list