Print out a char in main method - haskell

I have two modules defined as follows:
Foo.hs
module Foo
where
class F t where
c :: t -> Char
instance F Double where
c s = 'x'
Main.hs
import Foo
main :: IO ()
main = do
print $ (c 2.0)
When I compile these two modules I get this error:
ghc Foo.hs Main.hs
[1 of 2] Compiling Foo ( Foo.hs, Foo.o )
[2 of 2] Compiling Main ( Main.hs, Main.o )
Main.hs:6:12: error:
• Ambiguous type variable ‘t0’ arising from a use of ‘c’
prevents the constraint ‘(F t0)’ from being solved.
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instance exist:
instance [safe] F Double -- Defined in ‘Foo’
• In the second argument of ‘($)’, namely ‘(c 2.0)’
In a stmt of a 'do' block: print $ (c 2.0)
In the expression: do { print $ (c 2.0) }
Main.hs:6:14: error:
• Ambiguous type variable ‘t0’ arising from the literal ‘2.0’
prevents the constraint ‘(Fractional t0)’ from being solved.
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instances exist:
instance Fractional Double -- Defined in ‘GHC.Float’
instance Fractional Float -- Defined in ‘GHC.Float’
...plus one instance involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘c’, namely ‘2.0’
In the second argument of ‘($)’, namely ‘(c 2.0)’
In a stmt of a 'do' block: print $ (c 2.0)
How do I fix this so it prints out 'x'?

You should just rename call to c.
EDIT: Try annotating 2.0 as Double:
print $ c (2.0 :: Double)
GHC doesn't know which type to use for 2.0, it could be any Fractional type. To fix this we force 2.0 to be of type Double by explicitly marking its type.

Related

Function length "a" does not compile, if OverloadedStrings extension is enabled

If "{-# LANGUAGE OverloadedStrings #-}" is included at the top of the source file, or in package.yaml (I am using stack), then
length "a" -- does not compile anymore.
However the custom function length' is working fine
length' :: [a] -> Int
length' xs = sum [1 | _ <- xs]
The package Data.String is imported - I think the problem is there, but, I am interested to see, if somebody had similar problem.
Stack and GHC versions:
Version 2.3.1, Git revision x86_64 hpack-0.33.0, ghc-8.8.3
I am using mac osx, but the same error is in Linux and windows too.
This is an error produced by stack:
/Users/admin1/Haskell/PROJECTS/orig1/src/Lib.hs:13:29: error:
• Ambiguous type variables ‘t0’,
‘a0’ arising from the literal ‘"a"’
prevents the constraint ‘(IsString (t0 a0))’ from being solved.
Probable fix: use a type annotation to specify what ‘t0’,
‘a0’ should be.
These potential instances exist:
instance (a ~ Char) => IsString [a] -- Defined in ‘Data.String’
...plus two instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘length’, namely ‘"a"’
In the first argument of ‘show’, namely ‘(length "a")’
In the second argument of ‘($)’, namely ‘show (length "a")’
|
13 | putStrLn $ show (length "a") -- does not work, if "- OverloadedStrings" is on
This is because length has a signature length :: Foldable f => f a -> Int, so it can be any Foldable type. If you use the OverloadedStrings extension, then "foo" is no longer a String, it can be any type IsString a => a, and multiple fo these can be Foldable f => f as as well.
What you can do is give the compiler a type hint, for example with:
length ("a" :: String)

Simple code producing so much error report

I am trying to do MannWhitney-U test as described here. Following is my code:
import Data.Vector as V
import Data.Vector.Unboxed as VU
import Statistics.Test.MannWhitneyU
sampleA = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4]
sampleB = [2.0, 4.0, 5.0, 5.0, 3.0, 4.0, 5.0, 6]
main = do
putStrLn "\nResult of mannWhitneyUtest: "
print (mannWhitneyUtest SamplesDiffer 0.05 (VU.fromList sampleA) (VU.fromList sampleB) )
However, the error is much longer than above code:
[1 of 1] Compiling Main ( rnmann.hs, rnmann.o )
rnmann.hs:6:12: error:
• Ambiguous type variable ‘t0’ arising from the literal ‘1.0’
prevents the constraint ‘(Fractional t0)’ from being solved.
Relevant bindings include sampleA :: [t0] (bound at rnmann.hs:6:1)
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instances exist:
instance Fractional Double -- Defined in ‘GHC.Float’
instance Fractional Float -- Defined in ‘GHC.Float’
...plus four instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: 1.0
In the expression: [1.0, 2.0, 3.0, 4.0, ....]
In an equation for ‘sampleA’: sampleA = [1.0, 2.0, 3.0, ....]
rnmann.hs:6:47: error:
• Ambiguous type variable ‘t0’ arising from the literal ‘4’
prevents the constraint ‘(Num t0)’ from being solved.
Relevant bindings include sampleA :: [t0] (bound at rnmann.hs:6:1)
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instances exist:
instance Num Integer -- Defined in ‘GHC.Num’
instance Num Double -- Defined in ‘GHC.Float’
instance Num Float -- Defined in ‘GHC.Float’
...plus two others
...plus 13 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: 4
In the expression: [1.0, 2.0, 3.0, 4.0, ....]
In an equation for ‘sampleA’: sampleA = [1.0, 2.0, 3.0, ....]
rnmann.hs:7:12: error:
• Ambiguous type variable ‘t0’ arising from the literal ‘2.0’
prevents the constraint ‘(Fractional t0)’ from being solved.
Relevant bindings include sampleB :: [t0] (bound at rnmann.hs:7:1)
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instances exist:
instance Fractional Double -- Defined in ‘GHC.Float’
instance Fractional Float -- Defined in ‘GHC.Float’
...plus four instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: 2.0
In the expression: [2.0, 4.0, 5.0, 5.0, ....]
In an equation for ‘sampleB’: sampleB = [2.0, 4.0, 5.0, ....]
rnmann.hs:7:47: error:
• Ambiguous type variable ‘t0’ arising from the literal ‘6’
prevents the constraint ‘(Num t0)’ from being solved.
Relevant bindings include sampleB :: [t0] (bound at rnmann.hs:7:1)
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instances exist:
instance Num Integer -- Defined in ‘GHC.Num’
instance Num Double -- Defined in ‘GHC.Float’
instance Num Float -- Defined in ‘GHC.Float’
...plus two others
...plus 13 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: 6
In the expression: [2.0, 4.0, 5.0, 5.0, ....]
In an equation for ‘sampleB’: sampleB = [2.0, 4.0, 5.0, ....]
rnmann.hs:11:16: error:
• Ambiguous type variable ‘t0’ arising from a use of ‘mannWhitneyUtest’
prevents the constraint ‘(Ord t0)’ from being solved.
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instances exist:
instance Ord Ordering -- Defined in ‘GHC.Classes’
instance Ord Integer
-- Defined in ‘integer-gmp-1.0.0.1:GHC.Integer.Type’
instance Ord PositionTest -- Defined in ‘Statistics.Test.Types’
...plus 26 others
...plus 25 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘print’, namely
‘(mannWhitneyUtest
SamplesDiffer 0.05 (VU.fromList sampleA) (VU.fromList sampleB))’
In a stmt of a 'do' block:
print
(mannWhitneyUtest
SamplesDiffer 0.05 (VU.fromList sampleA) (VU.fromList sampleB))
In the expression:
do { putStrLn
"\n\
\Result of mannWhitneyUtest: ";
print
(mannWhitneyUtest
SamplesDiffer 0.05 (VU.fromList sampleA) (VU.fromList sampleB)) }
rnmann.hs:11:47: error:
• No instance for (Fractional (Statistics.Types.PValue Double))
arising from the literal ‘0.05’
• In the second argument of ‘mannWhitneyUtest’, namely ‘0.05’
In the first argument of ‘print’, namely
‘(mannWhitneyUtest
SamplesDiffer 0.05 (VU.fromList sampleA) (VU.fromList sampleB))’
In a stmt of a 'do' block:
print
(mannWhitneyUtest
SamplesDiffer 0.05 (VU.fromList sampleA) (VU.fromList sampleB))
rnmann.hs:11:75: error:
• Ambiguous type variable ‘t0’ arising from a use of ‘VU.fromList’
prevents the constraint ‘(Unbox t0)’ from being solved.
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instances exist:
instance Unbox () -- Defined in ‘Data.Vector.Unboxed.Base’
instance (Unbox a, Unbox b) => Unbox (a, b)
-- Defined in ‘Data.Vector.Unboxed.Base’
instance (Unbox a, Unbox b, Unbox c) => Unbox (a, b, c)
-- Defined in ‘Data.Vector.Unboxed.Base’
...plus 9 others
...plus 16 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the fourth argument of ‘mannWhitneyUtest’, namely
‘(VU.fromList sampleB)’
In the first argument of ‘print’, namely
‘(mannWhitneyUtest
SamplesDiffer 0.05 (VU.fromList sampleA) (VU.fromList sampleB))’
In a stmt of a 'do' block:
print
(mannWhitneyUtest
SamplesDiffer 0.05 (VU.fromList sampleA) (VU.fromList sampleB))
I think the key problem is how to mention 0.05 as the Pvalue.
Thanks for your help.
Here's the super relevant bits of the error report.
Ambiguous type variable ‘t0’ arising from the literal ‘4’
Probable fix: use a type annotation to specify what ‘t0’ should be.
Is it an Int, Double, Float? The compiler can't tell since 2 it is ambiguous.
Adding a type signature ought to fix things.
sampleA :: [Double]
You can not use 0.05 as PValue, you can use the mkPValue :: (Ord a, Num a) => a -> PValue a to construct a PValue:
import Statistics.Types(mkPValue)
main = do
putStrLn "\nResult of mannWhitneyUtest: "
print $
mannWhitneyUtest SamplesDiffer (mkPValue 0.05) (VU.fromList sampleA) (VU.fromList sampleB)
This prints as result:
Result of mannWhitneyUtest:
Just Significant

Ambigous type variable that prevents the constraint

I am fiddling around with IO and i do not understand the following error :
* Ambiguous type variable `a0' arising from a use of `readLine'
prevents the constraint `(Console a0)' from being solved.
Probable fix: use a type annotation to specify what `a0' should be.
These potential instance exist:
instance Console Int -- Defined at Tclass.hs:8:14
* In the second argument of `(+)', namely `readLine'
In the second argument of `($)', namely `(2 + readLine)'
In the expression: putStrLn . show $ (2 + readLine)
|
17 | useInt =putStrLn . show $ (2+readLine)
Code
module Tclass where
import System.Environment
class Console a where
writeLine::a->IO()
readLine::IO a
instance Console Int where
writeLine= putStrLn . show
readLine = do
a <- getLine
let b= (read a)::Int
return b
useInt::IO()
useInt =putStrLn . show $ (2+readLine)
P.S i do not understand shouldn't the compiler infer the type of the instance for readLine and make the addition with 2 in the useInt method ?
2 is not only an Int in Haskell but it is of any numeric type, including Float,Double,Integer,.... Its type is Num a => a -- a polymorphic type fitting each numeric type.
So, you could use (2::Int) instead. Then you'll discover that (2::Int) + readLine is a type error, since readLine :: Int is wrong, we only get readLine :: IO Int.
You can try this, instead
useInt :: IO ()
useInt = do
i <- readLine
putStrLn . show (2 + i :: Int)

Why doesn't ghci provide the expected Ambiguous type variable error in this scenario?

I'm working through a Haskell book. It has the following example:
ghci> Right 3 >>= \x -> return (x + 100)
It expects that this blows up with this error:
<interactive>:1:0:
Ambiguous type variable `a' in the constraints:
`Error a' arising from a use of `it' at <interactive>:1:0-33
`Show a' arising from a use of `print' at <interactive>:1:0-33
Probable fix: add a type signature that fixes these type variable(s)
When I run it:
$ ghci
Prelude> Right 3 >>= \x -> return (x + 100)
I get:
Right 103
ie I don't get the error expected.
Now maybe the compiler or the library has changed - but I'm not sure how to verify that.
My question is: Why doesn't ghci provide the expected Ambiguous type variable error in this scenario?
This is due to -XExtendedDefaultRules being now enabled by default in GHCi. To turn it off (and get your expected error message):
ghci> :set -XNoExtendedDefaultRules
ghci> Right 3 >>= \x -> return (x + 100)
<interactive>:2:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘print’
prevents the constraint ‘(Show a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance (Show b, Show a) => Show (Either a b)
-- Defined in ‘Data.Either’
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 23 others
...plus 11 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In a stmt of an interactive GHCi command: print it
This extension adds a bunch of extra ways to default otherwise ambiguous code. In your case, you have an expression of type Num b => Either a b. Regular Haskell defaulting rules tell us the b should default to Integer. The extended rules (see the link above) additionally default a to ().

Is there a way to make GHC provide the type class constraints of typed holes?

Current behavior
Prelude> show _
<interactive>:7:6:
Found hole ‘_’ with type: a0
Where: ‘a0’ is an ambiguous type variable
Relevant bindings include it :: String (bound at <interactive>:7:1)
In the first argument of ‘show’, namely ‘_’
In the expression: show _
In an equation for ‘it’: it = show _
Desired behavior
It would be nice if GHC would also tell me that the typed hole has the Show type class constraint.
Misc
GHC Version 7.8.1
This is now fixed in GHC 8.0 thanks to #DominiqueDevriese's GHC ticket.
Due to extended type defaulting, this isn't immediately obvious in GHCi. With your example,
> show _
<interactive>:7:6: error:
• Found hole: _h :: ()
Or perhaps ‘_h’ is mis-spelled, or not in scope
• In the first argument of ‘show’, namely ‘_h’
In the expression: show _h
In an equation for ‘it’: it = show _h
• Relevant bindings include
it :: String (bound at <interactive>:7:1)
the type of the hole is defaulted to (). This is apparently the desired behavior, though there's an argument to be made that extended defaulting shouldn't apply to holes (as a common use for them is to get the compiler to tell you the inferred type).
Nevertheless, if you compile with GHC or disable extended default rules in GHCi (via :set -XNoExtendedDefaultRules), we see the result of the improvements:
<interactive>:3:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘show’
prevents the constraint ‘(Show a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
...plus 22 others
...plus 11 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: show _
In an equation for ‘it’: it = show _
<interactive>:3:6: error:
• Found hole: _ :: a0
Where: ‘a0’ is an ambiguous type variable
• In the first argument of ‘show’, namely ‘_’
In the expression: show _
In an equation for ‘it’: it = show _
• Relevant bindings include
it :: String (bound at <interactive>:3:1)
No currently its not possible.But it may be added to GHC as per the speculations.
Try it :: _ => _ in GHC 8.8+.

Resources