View pattern in expression context [GHC-66228]

Language extension: ViewPatterns

A view pattern allows pattern matching on the result of a function. While most kinds of patterns look just like the expressions that construct the data that the pattern recognizes, view patterns don’t correspond directly to an expression. This means that using them where an expression is expected is an error.

Examples

Missing lambda

Message

NotLambda.hs:3:9: error: [GHC-66228]
    View pattern in expression context: x -> x
  |
3 | five = (x -> x) 5
  |         ^^^^^^

Explanation

In this example, the programmer forgot the \ at the beginning of their anonymous function. This caused the parser to recognize the function as a view pattern instead of a lambda.

NotLambda.hs
Before
module NotLambda where

five = (x -> x) 5
After
module NotLambda where

five = (\x -> x) 5
View pattern in expression

Message

ViewPatternExpr.hs:3:6: error: [GHC-66228]
    View pattern in expression context: sqrt -> y
  |
3 | h = (sqrt -> y)
  |      ^^^^^^^^^

Explanation

In this context, the view pattern is being used instead of a function. The issue can be corrected by not using a view pattern.

ViewPatternExpr.hs
Before
module ViewPatternExpr where

h = (sqrt -> y)
After
module ViewPatternExpr where

h = sqrt