[Haskell-cafe] Template Haskell - substitution in pattern in a lambda

Patrick Caldon patc at pessce.net
Sun Jan 3 21:59:18 EST 2010


Antoine Latter wrote:
> On Sun, Jan 3, 2010 at 8:30 PM, Patrick Caldon <patc at pessce.net> wrote:
>   
>> I'm trying to write some template haskell which will transform:
>>
>> $(buildCP 0)  into \(SimpleM d1 d2 d3) (SimpleM _ _ _) -> (SimpleM d1 d2 d3)
>> $(buildCP 1)  into \(SimpleM _ d2 d3) (SimpleM d1 _ _) -> (SimpleM d1 d2 d3)
>> $(buildCP 1)  into \(SimpleM d1 _ d3) (SimpleM _ d2 _) -> (SimpleM d1 d2 d3)
>> and so on.
>>
>> Ultimately I want to generalize this to more variables.
>>
>> I can't seem to get anything to substitute for the pattern variables in a
>> lambda.  Is there a straightforward way of doing this?
>>
>>     
>
> Hello,
>
> It looks like you want to construct expressions with the LamE
> constructor, which is declared like so:
>
> LamE [Pat] Exp
>
>   

Thanks - I see how that could work, I'll try it.

But really I was wondering if there was something like:

buildCP2 :: Int -> ExpQ
buildCP2 k =
    [|\(SimpleM ~a1 ~a2 ~a3) (SimpleM ~b1 ~b2 ~b3) -> (SimpleM $e1 $e2 
$e3) |]
    where (e1,a1,b1) = bitToExprs 0 k
          (e2,a2,b2) = bitToExprs 1 k
          (e3,a3,b3) = bitToExprs 2 k

bitToExprs:: Int -> Int -> (ExpQ,PatQ,PatQ)

Where ~a1 would mean "look for something called a1 returning a pattern, 
and slot it into the pattern part of the lambda in the appropriate spot".

I'm guessing no such syntax exists?

Thanks again,

Patrick.



More information about the Haskell-Cafe mailing list