Problem with tuple with one element constrained

I've been unable to get the following (simplified) code to be accepted by GHCi (8.0.1 and 8.0.2) Prelude> let (x, y) = (1, return 2) <interactive>:1:5: error: • Could not deduce (Monad m0) from the context: Num t bound by the inferred type for ‘x’: Num t => t at <interactive>:1:5-24 The type variable ‘m0’ is ambiguous • When checking that the inferred type x :: forall a (m :: * -> *) t. (Num a, Num t, Monad m) => t is as general as its inferred signature x :: forall t. Num t => t and I don't seem to be able to provide a type signatures that GHCi is okay with Prelude> let { x :: Int; y :: Monad m => m Int; (x, y) = (1, return 2) } <interactive>:5:40: error: • Ambiguous type variable ‘m0’ prevents the constraint ‘(Monad m0)’ from being solved. • When checking that the inferred type x :: forall a (m :: * -> *) t (m1 :: * -> *). (Num a, Num t, Monad m) => t is as general as its signature x :: Int Prelude> let (x, y) = (1, return 2) :: (Int, Monad m => m Int) <interactive>:4:31: error: • Illegal qualified type: Monad m => m Int GHC doesn't yet support impredicative polymorphism • In an expression type signature: (Int, Monad m => m Int) In the expression: (1, return 2) :: (Int, Monad m => m Int) In a pattern binding: (x, y) = (1, return 2) :: (Int, Monad m => m Int) Prelude> let (x,y) = (1,return 2) :: Monad m => (Int, m Int) <interactive>:7:5: error: • Ambiguous type variable ‘m0’ prevents the constraint ‘(Monad m0)’ from being solved. • When checking that the inferred type x :: forall (m :: * -> *). Monad m => Int is as general as its inferred signature x :: Int It seems the constraint on y leaks to x where it isn't well formed? Any help and/or explanations would be greatly appreciated. In my actual code the assignment is from the return value of a function so I can't just split it into two separate statements. Thanks! -Tyson

Hello Tyson, Thus quoth Tyson Whitehead at 19:41 on Tue, Apr 18 2017:
I've been unable to get the following (simplified) code to be accepted by GHCi (8.0.1 and 8.0.2)
Prelude> let (x, y) = (1, return 2)
[...]
and I don't seem to be able to provide a type signatures that GHCi is okay with
GHCi handles polymorphic types of interactive expressions somewhat differently from the polymorphic types of expressions loaded from files. (I'd be happy if someone could reproduce the explanation which I saw a couple times but which I cannot find any more.) Anyway, I put the following in tuple.hs: func :: Monad m => (Int, m Int) func = (1, return 2) Then, ghci tuple.hs swallows the file no problem and allows me to do *Main> func :: (Int, Maybe Int) (1, Just 2) (I have GHCi 8.0.2.) -- Sergiu

Thanks Sergiu,
I know there was some defaulting, but didn't think much of it beyond that. Special GHCi handling makes sense. Maybe I'll open a ticket just in case this is an unintended effect.
Interesting observation about loading it from a file via typing it in. My code is actually a test case for use with the doctest package that I was just running manually to work out. I'm not sure if it does the equivalent of loading it from a file or not.
Ultimately I wound up doing the binding via a case statement instead of let. It seems GHCi is okay with that.
Cheers! -Tyon
On Tue, Apr 18, 2017, 17:01 Sergiu Ivanov

Thus quoth Tyson Whitehead at 02:19 on Thu, Apr 20 2017:
Interesting observation about loading it from a file via typing it in.
Initially I suspected the culprit was the monomorphism restriction being turned off by default (:showi language in GHCi, (great thanks to Brandon Allbery who mentioned this command on another thread)), but I don't really see a difference when I turn it off.
My code is actually a test case for use with the doctest package that I was just running manually to work out. I'm not sure if it does the equivalent of loading it from a file or not.
Oh, I see.
Ultimately I wound up doing the binding via a case statement instead of let. It seems GHCi is okay with that.
That's kind of funny. Seems somewhat inconsistent to my eye. -- Sergiu
participants (2)
-
Sergiu Ivanov
-
Tyson Whitehead