Opened 7 years ago

Last modified 6 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 (Parser) Version: 6.10.1
Keywords: newcomer Cc: cgibbard@…,…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:


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 (5)

comment:1 Changed 7 years ago by guest

  • Cc cgibbard@… added

comment:2 Changed 7 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 2 years ago by liyang

  • Cc… added
  • Type of failure set to None/Unknown

comment:4 Changed 6 months ago by thomie

  • Component changed from Compiler to Compiler (Parser)

comment:5 Changed 6 months ago by nomeata

  • Keywords newcomer added

Marking as newcomer not because this is trivial, but because coming up with a solution (if there is one) should be possible without deep knowledge of GHC, and implementing it is also likely possible.

Note: See TracTickets for help on using tickets.