RE: Custom patternmatching for ADTs and Emulating TH Pattern splicing (was quasiquoting and guards)
Simon Peyton-Jones <[email protected]> Fri, 27 Jun 2008 09:48:38 +0100
| Newsgroups | gmane.comp.lang.haskell.template |
|---|---|
| Message-ID | <638ABD0A29C8884A91BC5FB5C349B1C32AE71E178B@EA-EXMSG-C334.europe.corp.microsoft.com> |
Have you tried quasiquotation? http://www.haskell.org/ghc/dist/current/doc= s/users_guide/template-haskell.html#th-quasiquotation I'm not sure it's what you want, but it's new (HEAD only) and maybe it'd he= lp. Simon | -----Original Message----- | From: [email protected] [mailto:template-haskell- | [email protected]] On Behalf Of Alfonso Acosta | Sent: 26 June 2008 20:36 | To: Reid Barton | Cc: [email protected] | 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 <[email protected]> | wrote: | > 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 =3D case x of | [$rx|.*foo.*] -> "contains foo" | _ -> "doesn't contain foo" | | Could be expanded to | | example x =3D 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>] =3D ... | | which IMHO is much nicer than | | f :: VectorADT Int -> Result | f (toList -> [1,2,3]) =3D ... | | 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. | _______________________________________________ | template-haskell mailing list | [email protected] | http://www.haskell.org/mailman/listinfo/template-haskell