GHC issueshttps://gitlab.haskell.org/ghc/ghc/-/issues2020-04-26T15:36:54Zhttps://gitlab.haskell.org/ghc/ghc/-/issues/11224Program doesn't preserve semantics after pattern synonym inlining.2020-04-26T15:36:54Zanton.dubovikProgram doesn't preserve semantics after pattern synonym inlining.```hs
{-# 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 (...```hs
{-# 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.
<details><summary>Trac metadata</summary>
| 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 | |
</details>
<!-- {"blocked_by":[],"summary":"Program doesn't preserve semantics after pattern synonym inlining.","status":"New","operating_system":"","component":"Compiler","related":[],"milestone":"","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"7.10.2","keywords":[],"differentials":[],"test_case":"","architecture":"","cc":[""],"type":"Bug","description":"{{{#!hs\r\n{-# language\r\n PatternSynonyms\r\n , ViewPatterns\r\n #-}\r\n\r\nimport Text.Read\r\n\r\n-- pattern PRead :: () => Read a => a -> String\r\npattern PRead a <- (readMaybe -> Just a)\r\n\r\nfoo :: String -> Int\r\nfoo (PRead x) = (x::Int)\r\nfoo (PRead xs) = sum (xs::[Int])\r\nfoo _ = 666\r\n\r\nbar :: String -> Int\r\nbar (readMaybe -> Just x) = (x::Int)\r\nbar (readMaybe -> Just xs) = sum (xs::[Int])\r\nbar _ = 666\r\n\r\nmain :: IO ()\r\nmain = do\r\n print $ foo \"1\" -- 1\r\n print $ foo \"[1,2,3]\" -- 666 -- ???\r\n print $ foo \"xxx\" -- 666\r\n\r\n print $ bar \"1\" -- 1\r\n print $ bar \"[1,2,3]\" -- 6\r\n print $ bar \"xxx\" -- 666\r\n}}}\r\n\r\nI'm expecting that semantics of the function `foo` would not change if I inline pattern synonym.\r\n\r\nHowever it does. `bar` successfully matches string against list of ints, whereas `foo` apperently skips this pattern and fall back to the catch-all case.\r\n\r\nReproducible both in GHCi and GHC.","type_of_failure":"OtherFailure","blocking":[]} -->8.0.1https://gitlab.haskell.org/ghc/ghc/-/issues/15893View Patterns affect typechecking in an unpredictable manner2019-07-07T18:02:33ZVarun GandhiView Patterns affect typechecking in an unpredictable mannerCopying the text from my [StackOverflow question](https://stackoverflow.com/questions/53294823/viewpatterns-affects-typechecking-in-an-unpredictable-manner).
```
{-# LANGUAGE ViewPatterns #-}
import Data.Text (Text)
import qualified Da...Copying the text from my [StackOverflow question](https://stackoverflow.com/questions/53294823/viewpatterns-affects-typechecking-in-an-unpredictable-manner).
```
{-# LANGUAGE ViewPatterns #-}
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Vector.Unboxed as UV
import qualified Data.Vector.Generic as V
bar :: Int -> UV.Vector Char -> (Text, Text)
bar i v = (t_pre, t_post)
where
f = T.pack . V.toList
(f -> t_pre, f -> t_post) = V.splitAt i v
```
This gives an unexpected type error (tested with GHC 8.4.3), concerning ambiguous type variables and `f` being out of scope.
```
• Ambiguous type variable ‘v0’ arising from a use of ‘V.toList’
prevents the constraint ‘(V.Vector v0 Char)’ from being solved.
Relevant bindings include
f :: v0 Char -> Text (bound at Weird.hs:11:5)
Probable fix: use a type annotation to specify what ‘v0’ should be.
These potential instances exist:
instance V.Vector UV.Vector Char
-- Defined in ‘Data.Vector.Unboxed.Base’
...plus one instance involving out-of-scope types
instance primitive-0.6.3.0:Data.Primitive.Types.Prim a =>
V.Vector Data.Vector.Primitive.Vector a
-- Defined in ‘Data.Vector.Primitive’
• In the second argument of ‘(.)’, namely ‘V.toList’
In the expression: T.pack . V.toList
In an equation for ‘f’: f = T.pack . V.toList
|
11 | f = T.pack . V.toList
| ^^^^^^^^
Weird.hs:13:6: error:
Variable not in scope: f :: UV.Vector Char -> t
|
13 | (f -> t_pre, f -> t_post) = V.splitAt i v
| ^
Weird.hs:13:18: error:
Variable not in scope: f :: UV.Vector Char -> t1
|
13 | (f -> t_pre, f -> t_post) = V.splitAt i v
| ^
```
Some of the other answers on SO have done some digging (e.g. enabling FlexibleContexts / NoMonomorphismRestriction doesn't solve the issue). I haven't yet figured out how to minimize the example further to remove the dependency on Vector...
Is this a compiler bug or a documentation bug?
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | ------------ |
| Version | 8.4.3 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Compiler |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"View Patterns affect typechecking in an unpredictable manner","status":"New","operating_system":"","component":"Compiler","related":[],"milestone":"⊥","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"8.4.3","keywords":["ViewPatterns"],"differentials":[],"test_case":"","architecture":"","cc":[""],"type":"Bug","description":"Copying the text from my [https://stackoverflow.com/questions/53294823/viewpatterns-affects-typechecking-in-an-unpredictable-manner StackOverflow question].\r\n\r\n\r\n{{{\r\n{-# LANGUAGE ViewPatterns #-}\r\n\r\nimport Data.Text (Text)\r\nimport qualified Data.Text as T\r\nimport qualified Data.Vector.Unboxed as UV\r\nimport qualified Data.Vector.Generic as V\r\n\r\nbar :: Int -> UV.Vector Char -> (Text, Text)\r\nbar i v = (t_pre, t_post)\r\n where\r\n f = T.pack . V.toList\r\n (f -> t_pre, f -> t_post) = V.splitAt i v\r\n}}}\r\n\r\nThis gives an unexpected type error (tested with GHC 8.4.3), concerning ambiguous type variables and `f` being out of scope.\r\n\r\n{{{\r\n • Ambiguous type variable ‘v0’ arising from a use of ‘V.toList’\r\n prevents the constraint ‘(V.Vector v0 Char)’ from being solved.\r\n Relevant bindings include\r\n f :: v0 Char -> Text (bound at Weird.hs:11:5)\r\n Probable fix: use a type annotation to specify what ‘v0’ should be.\r\n These potential instances exist:\r\n instance V.Vector UV.Vector Char\r\n -- Defined in ‘Data.Vector.Unboxed.Base’\r\n ...plus one instance involving out-of-scope types\r\n instance primitive-0.6.3.0:Data.Primitive.Types.Prim a =>\r\n V.Vector Data.Vector.Primitive.Vector a\r\n -- Defined in ‘Data.Vector.Primitive’\r\n • In the second argument of ‘(.)’, namely ‘V.toList’\r\n In the expression: T.pack . V.toList\r\n In an equation for ‘f’: f = T.pack . V.toList\r\n |\r\n11 | f = T.pack . V.toList\r\n | ^^^^^^^^\r\n\r\nWeird.hs:13:6: error:\r\n Variable not in scope: f :: UV.Vector Char -> t\r\n |\r\n13 | (f -> t_pre, f -> t_post) = V.splitAt i v\r\n | ^\r\n\r\nWeird.hs:13:18: error:\r\n Variable not in scope: f :: UV.Vector Char -> t1\r\n |\r\n13 | (f -> t_pre, f -> t_post) = V.splitAt i v\r\n | ^\r\n}}}\r\n\r\nSome of the other answers on SO have done some digging (e.g. enabling FlexibleContexts / NoMonomorphismRestriction doesn't solve the issue). I haven't yet figured out how to minimize the example further to remove the dependency on Vector...\r\n\r\nIs this a compiler bug or a documentation bug?","type_of_failure":"OtherFailure","blocking":[]} -->⊥https://gitlab.haskell.org/ghc/ghc/-/issues/14552GHC panic on pattern synonym2019-07-07T18:16:32ZIcelandjackGHC panic on pattern synonym```hs
{-# Language RankNTypes, ViewPatterns, PatternSynonyms, TypeOperators, KindSignatures, PolyKinds, DataKinds, TypeFamilies, TypeInType, GADTs #-}
import Data.Kind
data family Sing a
type a --> b = (a, b) -> Type
type family (@@)...```hs
{-# Language RankNTypes, ViewPatterns, PatternSynonyms, TypeOperators, KindSignatures, PolyKinds, DataKinds, TypeFamilies, TypeInType, GADTs #-}
import Data.Kind
data family Sing a
type a --> b = (a, b) -> Type
type family (@@) (f::a --> b) (x::a) :: b
data Proxy a = Proxy
newtype Limit' :: (k --> Type) -> Type where
Limit' :: (forall xx. Proxy xx -> f@@xx) -> Limit' f
data Exp :: [Type] -> Type -> Type where
TLam' :: Proxy f
-> (forall aa. Proxy aa -> Exp xs (f @@ aa))
-> Exp xs (Limit' f)
pattern FOO f <- TLam' Proxy (($ Proxy) -> f)
```
---\>
```
$ ghci -ignore-dot-ghci 119-bug.hs
GHCi, version 8.3.20171122: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( 119-bug.hs, interpreted )
ghc-stage2: panic! (the 'impossible' happened)
(GHC version 8.3.20171122 for x86_64-unknown-linux):
ASSERT failed!
in_scope InScope {a_a1Mh b_a1Mi rep_a1MB r_a1MC}
tenv [a1MC :-> r_a1MC]
cenv []
tys [k_a1Mj[ssk:3]]
cos []
needInScope [a1Mj :-> k_a1Mj[ssk:3]]
Call stack:
CallStack (from HasCallStack):
callStackDoc, called at compiler/utils/Outputable.hs:1205:22 in ghc:Outputable
assertPprPanic, called at compiler/types/TyCoRep.hs:2136:51 in ghc:TyCoRep
checkValidSubst, called at compiler/types/TyCoRep.hs:2159:29 in ghc:TyCoRep
substTy, called at compiler/types/TyCoRep.hs:2364:41 in ghc:TyCoRep
substTyVarBndr, called at compiler/coreSyn/CoreSubst.hs:571:10 in ghc:CoreSubst
Call stack:
CallStack (from HasCallStack):
callStackDoc, called at compiler/utils/Outputable.hs:1147:37 in ghc:Outputable
pprPanic, called at compiler/utils/Outputable.hs:1203:5 in ghc:Outputable
assertPprPanic, called at compiler/types/TyCoRep.hs:2136:51 in ghc:TyCoRep
checkValidSubst, called at compiler/types/TyCoRep.hs:2159:29 in ghc:TyCoRep
substTy, called at compiler/types/TyCoRep.hs:2364:41 in ghc:TyCoRep
substTyVarBndr, called at compiler/coreSyn/CoreSubst.hs:571:10 in ghc:CoreSubst
Please report this as a GHC bug: http://www.haskell.org/ghc/reportabug
>
```
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | ------------ |
| Version | 8.3 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Compiler |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"GHC panic on pattern synonym","status":"New","operating_system":"","component":"Compiler","related":[],"milestone":"","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"8.3","keywords":["PatternSynonyms,","TypeInType,","ViewPatterns"],"differentials":[],"test_case":"","architecture":"","cc":[""],"type":"Bug","description":"{{{#!hs\r\n{-# Language RankNTypes, ViewPatterns, PatternSynonyms, TypeOperators, KindSignatures, PolyKinds, DataKinds, TypeFamilies, TypeInType, GADTs #-}\r\n\r\nimport Data.Kind\r\n\r\ndata family Sing a\r\n\r\ntype a --> b = (a, b) -> Type\r\n\r\ntype family (@@) (f::a --> b) (x::a) :: b\r\n\r\ndata Proxy a = Proxy\r\n\r\nnewtype Limit' :: (k --> Type) -> Type where\r\n Limit' :: (forall xx. Proxy xx -> f@@xx) -> Limit' f\r\n\r\ndata Exp :: [Type] -> Type -> Type where\r\n TLam' :: Proxy f\r\n -> (forall aa. Proxy aa -> Exp xs (f @@ aa))\r\n -> Exp xs (Limit' f)\r\n\r\npattern FOO f <- TLam' Proxy (($ Proxy) -> f)\r\n}}}\r\n\r\n--->\r\n\r\n{{{\r\n$ ghci -ignore-dot-ghci 119-bug.hs \r\nGHCi, version 8.3.20171122: http://www.haskell.org/ghc/ :? for help\r\n[1 of 1] Compiling Main ( 119-bug.hs, interpreted )\r\nghc-stage2: panic! (the 'impossible' happened)\r\n (GHC version 8.3.20171122 for x86_64-unknown-linux):\r\n\tASSERT failed!\r\n in_scope InScope {a_a1Mh b_a1Mi rep_a1MB r_a1MC}\r\n tenv [a1MC :-> r_a1MC]\r\n cenv []\r\n tys [k_a1Mj[ssk:3]]\r\n cos []\r\n needInScope [a1Mj :-> k_a1Mj[ssk:3]]\r\n Call stack:\r\n CallStack (from HasCallStack):\r\n callStackDoc, called at compiler/utils/Outputable.hs:1205:22 in ghc:Outputable\r\n assertPprPanic, called at compiler/types/TyCoRep.hs:2136:51 in ghc:TyCoRep\r\n checkValidSubst, called at compiler/types/TyCoRep.hs:2159:29 in ghc:TyCoRep\r\n substTy, called at compiler/types/TyCoRep.hs:2364:41 in ghc:TyCoRep\r\n substTyVarBndr, called at compiler/coreSyn/CoreSubst.hs:571:10 in ghc:CoreSubst\r\n Call stack:\r\n CallStack (from HasCallStack):\r\n callStackDoc, called at compiler/utils/Outputable.hs:1147:37 in ghc:Outputable\r\n pprPanic, called at compiler/utils/Outputable.hs:1203:5 in ghc:Outputable\r\n assertPprPanic, called at compiler/types/TyCoRep.hs:2136:51 in ghc:TyCoRep\r\n checkValidSubst, called at compiler/types/TyCoRep.hs:2159:29 in ghc:TyCoRep\r\n substTy, called at compiler/types/TyCoRep.hs:2364:41 in ghc:TyCoRep\r\n substTyVarBndr, called at compiler/coreSyn/CoreSubst.hs:571:10 in ghc:CoreSubst\r\n\r\nPlease report this as a GHC bug: http://www.haskell.org/ghc/reportabug\r\n\r\n> \r\n}}}","type_of_failure":"OtherFailure","blocking":[]} -->