[Haskell-cafe] ambiguous constraint errors

Ryan Ingram ryani.spam at gmail.com
Thu May 29 22:04:29 EDT 2008


On 5/28/08, Evan Laforge <qdunkan at gmail.com> wrote:
> I have two related questions:
>
> #1
>
> I'm getting some annoying type errors that I don't fully understand,
> and wind up having to do a workaround that I don't totally like.
> Here's a simplified version of my situation:
>
> data Ambi m = Ambi {
>    ambi_monad :: m Int
>    , ambi_int :: Int
>    }
>
> some_ambi :: Monad m => Ambi m
> some_ambi = Ambi (return 5) 10
>
> ambi_table :: Monad m => [(String, Ambi m)]
> ambi_table = [("default", some_ambi)]
>
> get_int :: String -> Maybe Int
> get_int sym = fmap ambi_int (lookup sym ambi_table)
>
> -----------
>
> get_int produces:
>    Ambiguous type variable `m' in the constraint:
>      `Monad m' arising from a use of `ambi_table' at ambi.hs:13:40-49

You can let the caller choose "m":

get_int :: Monad m => m () -> String -> Maybe Int
get_int _ sym = fmap ambi_int (lookup sym ambi_table)

which can be called like so:

get_int (error "shouldn't evaluate" :: IO ()) "test"


More information about the Haskell-Cafe mailing list