Ambiguity Issues in MultiParamTypeClasses

I have a class that is defined as follows:

class (Eq a, Show a, Typeable a) => Condition v a where

(and some methods)

I wanted to write a function that accepts two conditions that have the same "v".

analyze :: (Condition v a, Condition v b) => a -> b -> Bool

(Return type made by Bool for simplicity)

But when I try to compile this, I get

Could not deduce (Condition v0 b)
  arising from the ambiguity check foranalyzefrom the context (Condition v a, Condition v b)

So this cleanup does not output what I wanted to say, but instead introduced another variable of type v0.

Could you explain what is wrong with the wording of the type signature as I wrote it, and how to fix it? Fixes a problem of RankNTypes?

+4
source share
3 answers

Condition v a , v, a. v => analyze, v. , foo :: Show a => () foo :: Condition a x => x -> Bool. a? , Condition, , v a - ,

class Condition v a | a -> v

.

+3

, , . : - .

class (Eq a, Show a, Typeable a) => Condition v a where
  -- Introduce v into the type of analyze, without demanding that a
  -- value of type v be provided; instead, just give some value which
  -- indicates v as a type parameter (like a Proxy, for instance).
  analyze :: (Condition v a, Condition v b) => proxy v -> a -> b -> Bool

instance Condition Bool Bool where
  ...
instance Condition Int Bool where
  ...

-- Use a proxy to pick out the type of v.
exampleInt = analyze (Proxy :: Proxy Int) True False
exampleBool = analyze (Proxy :: Proxy Bool) True False

Typeable, , , Proxy , Data.Typeable .

: Condition a. , , , - .

+1

( ):

class (Eq a, Show a, Typeable a) => Condition a where
   type V a :: *

analyze :: (Condition a, Condition b, V a ~ V b) => a -> b -> Bool

OP: " , , ". GHC, - . TF ​​FD (, ), , , TF . , FD TF

+1

All Articles