Custom patternmatching for ADTs and Emulating TH Pattern splicing (was quasiquoting and guards)
On Wed, Jun 25, 2008 at 11:19 PM, Reid Barton
Hi all,
I'd like to use quasiquotation to emulate bash's case statement in Haskell.
Nice Idea. I've been thinking about using quasiquoting patters for regular expression matching for a while. Never found the time to code it though.
I tried (ab)using view patterns, but they're not yet supported by Template Haskell.
Right, I think that would be the nicest approach (if they were supported of course and I would really like to see this implemented). Then, your initial expression ... example x = case x of [$rx|.*foo.*] -> "contains foo" _ -> "doesn't contain foo" Could be expanded to example x = case x of (match ".*foo.*" -> True) -> "contains foo" _ -> "doesn't contain foo" Sadly, I cannot think about any solution working with current version of GHC. I considered patternguards, but after all, they have the same problem you mentioned for standard ones ... -- Now, some unrequested comments about view patterns+quasiquotes: Pattern quasiquotes and patternviews match nicely, allowing to patternmatch ADT's using custom syntax. For example, imagine a vector ADT. It's closest view (against which standard patter matching is possible) would probably be a list. However, it would be more appropriate to be able to match using custom syntax other list patterns, say <1,2,3>, to patternmatch a vector with three specific elements. With quasiquoters and views one could do f :: VectorADT Int -> Result -- v is the quasiquoter for vectors f [$v|<1,2,3>] = ... which IMHO is much nicer than f :: VectorADT Int -> Result f (toList -> [1,2,3]) = ... In order to implement this, it would be required to * have view pattern support in TH. * have a Haskell pattern parser returning TH's AST (parseHaskellPat :: String -> Pat) (which as far as known no one has implemented yet). This parser could also serve to build a quasiquoter with which to emulate splicing patterns with Template Haskell, that is [p| |], which isn't currently implemented (see http://hackage.haskell.org/trac/ghc/ticket/1476). So, I think I'll get my hands dirty on it. My best bet is probably using Language.Haskell.Parser and then implement a translator from Language.Haskell.Syntax to Language.Haskell.TH.Syntax . Another option would be using GHC's Haskell parser and its translator from GHC's AST to Language.Haskell.TH.Syntax (basically steal the implementation of [| |]). However, since my knowledge of GHC's internals is almost null, I'm not sure if I should do that, whether it's even possible or if it would worth the effort. The GHC gurus can probably clarify this. What do think? I would certainly appreciate some feedback and suggestions before I start to code.
On Thu, Jun 26, 2008 at 02:35:41PM -0500, Alfonso Acosta wrote:
On Wed, Jun 25, 2008 at 11:19 PM, Reid Barton
wrote: Hi all,
I'd like to use quasiquotation to emulate bash's case statement in Haskell. If you haven't seen harp yet take a look at it. It's a preprocessor but it might be exactly what you're looking for? http://www.cs.chalmers.se/~d00nibro/harp/
Sincerly Marc
Have you tried quasiquotation? http://www.haskell.org/ghc/dist/current/docs/users_guide/template-haskell.ht...
I'm not sure it's what you want, but it's new (HEAD only) and maybe it'd help.
Simon
| -----Original Message-----
| From: template-haskell-bounces@haskell.org [mailto:template-haskell-
| bounces@haskell.org] On Behalf Of Alfonso Acosta
| Sent: 26 June 2008 20:36
| To: Reid Barton
| Cc: template-haskell@haskell.org
| Subject: [Template-haskell] Custom patternmatching for ADTs and Emulating TH
| Pattern splicing (was quasiquoting and guards)
|
| On Wed, Jun 25, 2008 at 11:19 PM, Reid Barton
participants (3)
-
Alfonso Acosta -
Marc Weber -
Simon Peyton-Jones