What does this "ambiguous variable of type" a "mean in a constraint?

In this code, I am trying to get the first parameter in my working function to gobe a type of type of type. I see that in the type of the documentation family type, a similar function insertbelongs to the type class, whereas in my example below it is not.

I am new to family types, so maybe I am using them incorrectly, but what does this error mean?

{-# LANGUAGE TypeFamilies #-}

-- | key
class K a where
  -- | iterator for key
  type I a :: *
  mkI :: a -> I a

--| A map
data (K a) => M a b = M a b

insert :: (K a) => a -> b -> M a b -> M a b
insert = go mkI    -- <<< PROBLEM
  where
    go o a b m = m

An undefined variable of type `a 'in a constraint:

`K a'

  arising from an expression type signature at Data/Map2.hs:167:10-33

Possible fix: add a type signature that corrects these type variables

+5
source share
1 answer

This compiles:

{-# LANGUAGE TypeFamilies, GADTs, ScopedTypeVariables #-}

-- | key
class K a where
  -- | iterator for key
  type I a :: *
  mkI :: a -> I a

-- | A map
data M x y where
    M :: K a => a -> b -> M a b

insert :: forall a b. (K a) => a -> b -> M a b -> M a b
insert = go mkI
  where
    go :: (a -> I a) -> a -> b -> M a b -> M a b
    go o a b m = m

What have I changed and why?

-, , M, , -, GADT.

-, , GHC, . , , mkI , . , , , .

+10

All Articles