Brian Lewis | 20 Jun 00:23 2013

TH clause Pat selection

I want to use TH to generate functions like
foo :: c -> h
foo ... = ...
foo ... = ...
...
from lists of pairs :: [(c, h)]

For example, $(genFoo ''Int ''Bool [(0,False), (1,True)])
would generate
foo 0 = False
foo 1 = True

The problem is, I don't know how to generate the function's clauses.
"foo 0 = ..." seems to be a LitP pattern. But "foo True = ..." seems to
be a ConP pattern. The appropriate pattern depends on type c.

Here's code with more explanation and examples:
http://hpaste.org/90163
Geoffrey Mainland | 20 Jun 16:23 2013
Picon

Re: TH clause Pat selection

Have you tried dataToPatQ and dataToExpQ?

http://hackage.haskell.org/packages/archive/template-haskell/latest/doc/html/Language-Haskell-TH-Quote.html

Geoff

On 06/19/2013 11:23 PM, Brian Lewis wrote:
> I want to use TH to generate functions like
> foo :: c -> h
> foo ... = ...
> foo ... = ...
> ...
> from lists of pairs :: [(c, h)]
>
> For example, $(genFoo ''Int ''Bool [(0,False), (1,True)])
> would generate
> foo 0 = False
> foo 1 = True
>
> The problem is, I don't know how to generate the function's clauses.
> "foo 0 = ..." seems to be a LitP pattern. But "foo True = ..." seems to
> be a ConP pattern. The appropriate pattern depends on type c.
>
> Here's code with more explanation and examples:
> http://hpaste.org/90163
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
(Continue reading)

Ozgur Akgun | 20 Jun 22:47 2013
Picon

Re: TH clause Pat selection

Hi.

On 19 June 2013 23:23, Brian Lewis <brian <at> lorf.org> wrote:
The problem is, I don't know how to generate the function's clauses.
"foo 0 = ..." seems to be a LitP pattern. But "foo True = ..." seems to
be a ConP pattern. The appropriate pattern depends on type c.

I've used haskell-src-meta for this purpose before. See:


Hope this helps,
Ozgur

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane