Code is below. I'd like to have parameters to my function that are only restricted by type class. I call a function of the type class on them and then I can use them. But I'm getting various errors as I'm trying to do this.
{-# LANGUAGE RankNTypes, TypeSynonymInstances, FlexibleInstances, UndecidableInstances #-}
class PlotValue a where
value :: a -> Double
instance PlotValue Double where
value = id
--instance PlotValue Int where
--value x = fromIntegral x
instance (Integral a) => PlotValue a where
value x = fromIntegral x
instance PlotValue String where
value x = 5
type Input = (PlotValue a, PlotValue b) => (Maybe a, Maybe b)
test :: Input -> String
test (Just a, Just b) = (show $ value a) ++ (show $ value b)
main = do
putStrLn (show ( test (Just "strl", Just 6.4)))
Current errors (though they change a little depending on what I try):
Test5.hs:17:5:
Couldn't match expected type `Input' against inferred type `(a, b)'
In the pattern: (Just a, Just b)
In the definition of `test':
test (Just a, Just b) = (show $ value a) ++ (show $ value b)
Test5.hs:20:30:
Couldn't match expected type `a' against inferred type `[Char]'
`a' is a rigid type variable bound by
the polymorphic type
`forall a b. (PlotValue a, PlotValue b) => (Maybe a, Maybe b)'
at Test5.hs:20:19
In the first argument of `Just', namely `"strl"'
In the expression: Just "strl"
In the first argument of `test', namely `(Just "strl", Just 6.4)'
Test5.hs:20:43:
Could not deduce (Fractional b)
from the context (PlotValue a, PlotValue b)
arising from the literal `6.4' at Test5.hs:20:43-45
Possible fix:
add (Fractional b) to the context of
the polymorphic type
`forall a b. (PlotValue a, PlotValue b) => (Maybe a, Maybe b)'
In the first argument of `Just', namely `6.4'
In the expression: Just 6.4
In the first argument of `test', namely `(Just "strl", Just 6.4)'