Opened 8 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: Cc: cgibbard@…, hackage.haskell.org@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

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

comment:1 Changed 8 years ago by guest

  • Cc cgibbard@… added

comment:2 Changed 8 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.

Simon

comment:3 Changed 3 years ago by liyang

  • Cc hackage.haskell.org@… added
  • Type of failure set to None/Unknown

comment:4 Changed 21 months ago by thomie

  • Component changed from Compiler to Compiler (Parser)

comment:5 Changed 21 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.

comment:6 Changed 6 months ago by thomie

  • Keywords newcomer removed

nomeata: I'm removing the newcomer keyword from this ticket, as it didn't see any takers in 15 months. It might be too difficult or uninteresting, but please put the keyword back if you disagree.

By keeping the newcomers list fresh, I hope we will see newcomer activity pick up a little.

Note: See TracTickets for help on using tickets.