Opened 5 years ago

Last modified 13 months ago

#2742 new feature request

The -> in ViewPatterns binds more weakly than infix data constructors.

Reported by: guest Owned by:
Priority: normal Milestone:
Component: Compiler Version: 6.10.1
Keywords: Cc: cgibbard@…,…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:


The following code, essentially taken from the ViewPatterns page on the wiki doesn't seem to parse correctly.

mymap f [] = []
mymap f (x : mymap f -> xs) = f x : xs

However, this does:

mymap f [] = []
mymap f (x : (mymap f -> xs)) = f x : xs

(though it triggers bug #2395 about overlapping patterns)

It would seem nicer to make the view pattern arrow bind tighter than any infix data constructors.

Change History (3)

comment:1 Changed 5 years ago by guest

  • Cc cgibbard@… added

comment:2 Changed 5 years ago by simonpj

  • Difficulty set to Unknown
  • Milestone set to _|_
  • Type changed from bug to feature request

I agree with you. Here's why it's tricky. Haskell allows a pattern like this

  case x of 
     a:as -> rhs

If view patterns bind more tightly than infix ops, then presumably this would be ok

  case x of
     a : f as -> bs -> rhs

and now we get an awkward ambiguity between the two uses of ->.

The change I made to the parser was this:

  • Remove this production from texp:
    	| fexp '->' exp   { LL $ EViewPat $1 $3 }
  • Add the same production to exp10.

That added a new shift/reduce conflict, described above, which gets resolved the wrong way.

If someone wants to investigate alternatives, that'd be great, so I'll leave this open as a feature request. It's a pure parser question so it does not require deep GHC knowledge.


comment:3 Changed 13 months ago by liyang

  • Cc… added
  • Type of failure set to None/Unknown
Note: See TracTickets for help on using tickets.