Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why annotating a lambda type doesn't require -XScopedTypeVariables?

Tags:

haskell

This requires -XScopedTypeVariables

handle(\(_::SomeException) -> return Nothing)

but this doesn't

handle((\_ -> return Nothing)::SomeException -> IO (Maybe Integer))

If :: is allowed to annotated types inside function body, why does it require a pragma to annotate a local variable?

like image 720
Sawyer Avatar asked Feb 20 '14 13:02

Sawyer


1 Answers

More generally than that: standard Haskell doesn't allow signatures in patterns, but does allow any expression to be given a signature. The following are all valid:

main :: IO ()
main = do
   x <- readLn
   print $ 5 + x

main' = (\y -> do {
   x <- readLn;
   print $ y + x } ) :: Int -> IO ()

main'' y = do
   x <- readLn :: IO Int
   print $ y + x :: IO ()

but none of these are

main''' = do
   (x :: Int) <- readLn
   print $ 5 + x

main''' = (\(y :: Int) -> do {
   x <- readLn;
   print $ y + x } ) :: Int -> IO ()

main'''' (y :: Int) = do
   x <- readLn :: IO Int
   print $ y + x :: IO ()

Apparently, it was just not envisioned that signatures in patterns might be useful. But they sure are, so ScopedTypeVariables introduced that possibility.

like image 75
leftaroundabout Avatar answered Sep 21 '22 00:09

leftaroundabout