[Template-haskell] quasiquoting and guards

Reid Barton rwbarton at math.harvard.edu
Thu Jun 26 00:19:20 EDT 2008


Hi all,

I'd like to use quasiquotation to emulate bash's case statement in
Haskell.  That is, I'd like to turn

example x = case x of
  [$rx|.*foo.*] -> "contains foo"
  _             -> "doesn't contain foo"

into (assuming an appropriate match :: String -> String -> Bool)

example x = case x of
  s | match ".*foo.*" s -> "contains foo"
  _                     -> "doesn't contain foo"

But it seems like I can't do so, because despite appearances, the
difference between the two cases in the second example is not in
"what's to the left of ->".  Instead, the first has a GuardedB body
while the second has a NormalB body.  (Having reread the Haskell
Report, I now understand why this is, but it was surprising at first.)
Whatever my definition of the quasiquoter rx is, the first example is
going to expand to something with two NormalB bodies, so I can't
achieve the desired expansion.

Is there a clever workaround?  I tried (ab)using view patterns, but
they're not yet supported by Template Haskell.  

Regards,
Reid Barton


More information about the template-haskell mailing list