6
{-# LANGUAGE PatternSynonyms, ViewPatterns #-}

data Quun = Foo | Bar | Oink Quun

fooey :: Quun -> Bool
fooey Foo = True
fooey (Oink Yum) = True
fooey _ = False

pattern Yum <- (fooey -> True)

This doesn't compile (at least in GHC-7.10.2)

/tmp/wtmpf-file10227.hs:1:1:
    Recursive pattern synonym definition with following bindings:
      foo (defined at /tmp/wtmpf-file10227.hs:(6,1)-(8,13))
      Yum (defined at /tmp/wtmpf-file10227.hs:10:1-28)

Sure, for simple directly self-referring patterns this would make sense. But is there some fundamental reason why even a view-pattern mediated layout as above isn't possible? I can't find this convincing; after all it's possible to inline the view pattern and get a perfectly harmless (well... at least, allowed) definition:

fooey :: Quun -> Bool
fooey Foo = True
fooey (Oink (fooey -> True)) = True
fooey _ = False

pattern Yum <- (fooey -> True)

So, are such synonyms just not available yet for technical reasons, and will we get them in the future?

leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
  • Author of `PatternSynonyms` here: this is a good point and worth opening a ticket for. This might not be easy to implement, though, because, if I remember right, GHC doesn't expose much information about "why" something ends up in the set of free variables. – Cactus Feb 17 '16 at 02:05

1 Answers1

5

Some recursive patterns are problematic, like

f :: [()] -> Bool
f L = True
f _ = False

pattern L <- () : L

What is this supposed to desugar to?

Patterns aren't first-class values. They are just replaced with their definitions where they appear. For such a language, recursive definitions are not generally sensible.

Reid Barton
  • 14,951
  • 3
  • 39
  • 49
  • 2
    Well, I didn't suggest that plain bidirectional pattern synonyms should get allowed to be recursive. Only with view patterns can this make sense; but then it could allow some nice applications indeed. – leftaroundabout Feb 16 '16 at 20:43
  • 2
    Well `L` is a recursive pattern synonym and needs to be rejected, so it matches the title of your question. I agree that when the recursion goes through a function, it might be okay. – Reid Barton Feb 16 '16 at 20:54