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

Ian Lynagh igloo at earth.li
Thu Apr 15 22:56:57 EDT 2004


On Tue, Apr 13, 2004 at 05:10:04PM -0400, Isaac Jones wrote:
> 
> 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.

Hmm, we currently have

data Body = GuardedB [(Exp,Exp)]
          | NormalB Exp

It looks like we would want either

----- 1
data Body = GuardedB [(Guard,Exp)]
          | NormalB Exp

data Guard = NormalG Exp
           | PatG [Stmt]
-----

or

----- 2
data Body = GuardedB [(Exp,Exp)]
          | PatGuardedB [(Guard,Exp)]
          | NormalB Exp

data Guard = NormalG Exp
           | PatG [Stmt]
-----

or

----- 3
data Body = GuardedB [(Exp,Exp)]
          | PatGuardedB [([Stmt],Exp)]
          | NormalB Exp
-----

1 is the simplest overall, but I think it would set a bad precedent as
we wouldn't want future extensions to cause TH programs to break.

2 and 3 have the advantage that Haskell98+TH users can pretend nothing
happened. I think I prefer 2 over 3 as it's simpler to see what happens
when a GuardedB and PatGuardedB are concatenated.

So I propose option 2. Does anyone disagree?

> 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.

This one should be just be a matter of adding the alternative.


Thanks
Ian



More information about the template-haskell mailing list