[Haskell-cafe] How to fix ambiguous type variable?

Viktor Dukhovni ietf-dane at dukhovni.org
Thu Jun 13 06:45:57 UTC 2019


On Thu, Jun 13, 2019 at 11:15:23AM +0530, Sandeep.C.R via Haskell-Cafe wrote:

> This seems to work for me...
> 
> someCheck :: forall a. (Show a, Read a, Eq a) => String -> a -> Bool
> someCheck s v = (read . show . (read :: String -> a) $ s) == v
> 
> Probably requires 'ScopedTypeVariables'...

Yes.  Another variant is:

    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE TypeApplications #-}

    someCheck :: (Show a, Read a, Eq a) => String -> a -> Bool
    someCheck s (v :: a) = (read . show $ read @a s) == v

which amounts to the same thing, but is perhaps simpler.  For fun,
with GHC 8.6 and "BlockArguments" we can drop some parentheses:

    {-# LANGUAGE BlockArguments #-}
    {-# LANGUAGE TypeApplications #-}
    {-# LANGUAGE ScopedTypeVariables #-}

    someCheck :: forall a. (Show a, Read a, Eq a) => String -> a -> Bool
    someCheck s v = v == do read $ show $ read @a s

-- 
	Viktor.


More information about the Haskell-Cafe mailing list