quasiquoting and guards

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

quasiquoting and guards

Reid Barton
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
_______________________________________________
template-haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/template-haskell
Reply | Threaded
Open this post in threaded view
|

Re: quasiquoting and guards

Claus Reinke
> 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"

Assuming that regexqq produces (String -> Maybe [String]), and
works rather better for expressions than for patterns, you could
simply lift mplus to functions, and define your own caseRE (your
example doesn't really make use of case/pattern-matching):

    import Control.Monad
    import Data.Char
    import Data.List

    (matchA .|. matchB) x = matchA x `mplus` matchB x

    (regex .->. rhs) x =  regex x >> return rhs -- (or >>=, if prefered)

    caseRE x matches otherwise = maybe otherwise id (matches x)

    a x = Just [this,that]
      where (this,that) = span isSpace x
    b x = Nothing

    test = caseRE "hello world"
             (   (b .->. "hi")
             .|. (a .->. "ho"))
                 "otherwise"

Then you can either fiddle with operator priorities, or drop
the thin facade and use MonadPlus directly;-)

You might also want to be able to refer to match results in
the rhs, no matter whether that is possible in bash - this would
seem to need control over both lhs and rhs anyway.

Hth,
Claus
 

> 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
> _______________________________________________
> template-haskell mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/template-haskell
_______________________________________________
template-haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/template-haskell
Reply | Threaded
Open this post in threaded view
|

Re: quasiquoting and guards

Bugzilla from alfonso.acosta@gmail.com
In reply to this post by Reid Barton
On Wed, Jun 25, 2008 at 11:19 PM, Reid Barton <[hidden email]> wrote:

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

I just created a ticket requesting it:
http://hackage.haskell.org/trac/ghc/ticket/2399

I really hope you get the rx quasiquoter to work and make it public.
It would allow to ensure that all regular expressions used are
syntactically correct at compile time, which would certianly be cool.
_______________________________________________
template-haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/template-haskell
Reply | Threaded
Open this post in threaded view
|

Re: quasiquoting and guards

Reid Barton
On Thu, Jun 26, 2008 at 03:17:09PM -0500, Alfonso Acosta wrote:
> I really hope you get the rx quasiquoter to work and make it public.
> It would allow to ensure that all regular expressions used are
> syntactically correct at compile time, which would certianly be cool.

In fact, Matt Morrow has already published an early version of the
regexqq package on hackage, which allows one to use regular
expressions as functions String -> Maybe [String] with compile-time
checking.  We're working on adding support for regular expressions as
patterns, hence this thread.

Regards,
Reid Barton
_______________________________________________
template-haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/template-haskell