Ticket #2399 (new feature request)

Opened 5 months ago

Last modified 1 week ago

Template Haskell: support for view patterns

Reported by: fons Assigned to:
Priority: low Milestone: _|_
Component: Template Haskell Version: 6.9
Severity: normal Keywords:
Cc: rwbarton@math.harvard.edu, audreyt@audreyt.org, duncan2nd@gmx.de, reiner.pope@gmail.com, id@isaac.cedarswampstudios.org Difficulty: Unknown
Test Case: Operating System: Unknown/Multiple
Architecture: Unknown/Multiple

Description

TH's Pat type doesn't include a constructor for view patterns, which would ceratinly be useful.

As an example, Reid Barton suggested using view patterns for creating a regular expression quasiquoter (rx) [1].

For example, to check if a string matches certain regular expresion:

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

Using pattern views, the quasiquoter would expand to

example x = case x of
 ((=~".*foo.*")->True) -> "contains foo"
 _             -> "doesn't contain foo"

[1] http://www.haskell.org/pipermail/template-haskell/2008-June/thread.html#669

Change History

06/26/08 13:40:06 changed by rwbarton

  • cc set to rwbarton@math.harvard.edu.

06/30/08 00:16:24 changed by simonpj

  • priority changed from normal to low.
  • difficulty set to Unknown.
  • milestone set to _|_.

Yes, there are many ways in which Template Haskell lags the various GHC extensions, and view patterns are only one of them. Furthermore, view patterns are a rather new and experimental feature, so it seems premature to "bless" them in TH.

In any case, it's probably fairly easy to add a guard instead, since you are generating the program with a TH program.

Simon

06/30/08 23:11:04 changed by AudreyTang

Simon, it's difficult to add a guard there, because a QuasiQuoter? accepts the pattern-generation function of the form:

quoteExprPat :: String -> TH.PatQ

But in TH, Guard is part of Body, so a quasiquoter designated to the PatQ side cannot affect the BodyQ side.

As there seems to be no other Pat constructors that's useful for a [$rx|...|] at the pattern side, would you consider accepting a patch that adds ViewP for 6.10?

Cheers,

Audrey

(The context for [$rx|...|] is http://hackage.haskell.org/cgi-bin/hackage-scripts/package/regexqq btw.)

06/30/08 23:11:17 changed by AudreyTang

  • cc changed from rwbarton@math.harvard.edu to rwbarton@math.harvard.edu, audreyt@audreyt.org.

07/01/08 03:52:51 changed by simonpj

Ah I see. I'd missed the quasiquoter connnection.

One way to fix this would be to add view patterns to TH. But it made me wonder: why aren't pattern guards compositional? At the moment they are resolutely top-level. And, as you say, not part of patterns. But syntax aside it would make perfect sense to make them part of a pattern:

  pat ::= var |  C pat1 ... patn
       |  pat|quals

where qual is a pattern-guard "qualifier". So (again, syntax aside) you could write

  f (x | x>3) = ...

  g ([y] | Just z <- lookup y) = ...

That would push pattern guards into the syntax of patterns. Now view patterns are syntactic sugar:

  (exp -> pat)   means   (x | pat <- exp x)
  where x is fresh

The reverse encoding is also possible

  (pat | quals)  means   (f -> Just (v1,...,vn))
where
  (v1,...,vn) are the variables bound by pat
  f pat | quals = Just vs

I'm not sure I'm advocating this (yet!), but I hadn't thought of it before so I thought I'd jot it down.

If TH users agree about just how to beef up patterns, I'm not against adding it. I'm just a bit conscious that view patterns are experimental and the more concrete we pour the harder it becomes to change.

Simon

09/30/08 08:37:44 changed by simonmar

  • architecture changed from Unknown to Unknown/Multiple.

09/30/08 08:52:09 changed by simonmar

  • os changed from Unknown to Unknown/Multiple.

11/14/08 10:47:35 changed by duncan2nd

  • cc changed from rwbarton@math.harvard.edu, audreyt@audreyt.org to rwbarton@math.harvard.edu, audreyt@audreyt.org, duncan2nd@gmx.de.

11/26/08 05:17:55 changed by reinerp

  • cc changed from rwbarton@math.harvard.edu, audreyt@audreyt.org, duncan2nd@gmx.de to rwbarton@math.harvard.edu, audreyt@audreyt.org, duncan2nd@gmx.de, reiner.pope@gmail.com.

11/26/08 11:18:16 changed by guest

  • cc changed from rwbarton@math.harvard.edu, audreyt@audreyt.org, duncan2nd@gmx.de, reiner.pope@gmail.com to rwbarton@math.harvard.edu, audreyt@audreyt.org, duncan2nd@gmx.de, reiner.pope@gmail.com, id@isaac.cedarswampstudios.org.