Skip to content

Program doesn't preserve semantics after pattern synonym inlining.

{-# language
   PatternSynonyms
 , ViewPatterns
 #-}

import Text.Read

-- pattern PRead :: () => Read a => a -> String
pattern PRead a <- (readMaybe -> Just a)

foo :: String -> Int
foo (PRead x)  = (x::Int)
foo (PRead xs) = sum (xs::[Int])
foo _ = 666

bar :: String -> Int
bar (readMaybe -> Just x)  = (x::Int)
bar (readMaybe -> Just xs) = sum (xs::[Int])
bar _ = 666

main :: IO ()
main = do
  print $ foo "1"       -- 1
  print $ foo "[1,2,3]" -- 666 -- ???
  print $ foo "xxx"     -- 666

  print $ bar "1"       -- 1
  print $ bar "[1,2,3]" -- 6
  print $ bar "xxx"     -- 666

I'm expecting that semantics of the function foo would not change if I inline pattern synonym.

However it does. bar successfully matches string against list of ints, whereas foo apperently skips this pattern and fall back to the catch-all case.

Reproducible both in GHCi and GHC.

Trac metadata
Trac field Value
Version 7.10.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information