Reputation:
ts ::(RealFrac a,Integral a)=>a
ts =5
ghci > ts
* 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 a, Show b) => Show (Either a b)
-- Defined in `Data.Either'
instance Show Ordering -- Defined in `GHC.Show'
instance Show Integer -- Defined in `GHC.Show'
...plus 24 others
...plus 80 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In a stmt of an interactive GHCi command: print it
I know it can be solve by avoid using some function together to prevent the result with constrain (RealFrac a,Integral a)
togather, but is there any other possible way to solve it that only dealing with the result?
Upvotes: 0
Views: 86
Reputation: 18249
The problem GHCi has here is simply that it doesn't know what type to use for ts
. All it knows it that it has to be an instance of RealFrac
and of Integral
- and also of Show
(since asking for a result to be displayed in GHCi implicitly requires a `Show instance, so GHCi knows how to display it as a string in your terminal). And this isn't enough for it to know exactly which type to use - which is what it is complaining about.
The usual way to solve this is to specify the type you want explicitly, such as (note that this will NOT work as I explain below, but it shows the usual way to solve such an "ambiguous type variable" error in GHCi):
ts :: Int
As I said, this won't work, because the type you choose has to be compatible with the signature of ts
- which here means it has to be an instance of both RealFrac
and Integral
. (And show
, as I mentioned.) Int
is not an instance of RealFrac
, so GHCi will complain about this if you try the above.
Unfortunately for you, there is NO type which has instances of both RealFrac
and Integral
in the standard library. This wouldn't really make sense anyway, because Integral
is for things which behave like whole numbers, and RealFrac
for things which behave like non-whole numbers. But that doesn't stop you, in theory, writing instances of both for a particular type. Or writing an instance of RealFrac
for say Int
, or Integral
for say Double
- it's hard to see how such an instance would have value, but Haskell doesn't rule it out. (You will need to supply the necessary superclass instances too, such as Real
and Fractional
for RealFrac
, and Real
and Enum
for Integral
.)
So it's really not clear what you're trying to achieve here. But that's why you're getting errors - to summarise:
Upvotes: 1