简体   繁体   中英

why does the compiler give error on the following haskell code?

I am trying to study the type classes in haskell. I write the following script and the raised an error. I am unable to understand why the compiler thinks of v as an concrete type while it is just a parameter for class Boxer.

data Box1 a b = Box1 Double a [b]

class Boxer v where
  foo :: (v a b) -> Double

instance Boxer (Box1 a b) where
  foo (Box1 r s t) = r

it raises an error in line 7:8:

Couldn't match type `v' with `Box1'
  `v' is a rigid type variable bound by
      the type signature for foo :: v a b -> Double at file1.hs:4:10
Expected type: v a b
  Actual type: Box1 a b
Relevant bindings include
  foo :: v a b -> Double (bound at file1.hs:7:3)
In the pattern: Box1 r s t
In an equation for `foo': foo (Box1 r s t) = r
Failed, modules loaded: none.

In your instance, the compiler has to instantiate v with Box1 ab . In particular, it has to instantiate vab with something like (Box1 ab) ab – except both a variables come from a different place; they're actually disambiguated to (Box1 ab) a1 b1 . Which is the same as Box1 ab a1 b1 .

foo :: Box1 a b a1 b1 -> Double

Does that make any sense?

The problem is that you're confusing a (type) function , namely Box1 , with the result of applying said function to some type arguments. The kinds don't match:

GHCi> :k Boxer
Boxer :: (* -> * -> *) -> Constraint
GHCi> :k (Box1 Int String)
Box1 Int String :: *

* -> * -> * is the kind of a type function / type constructor with two arguments, so that's what Boxer needs. Whereas Box1 ab is simply a type, with no arguments. Doesn't match! OTOH,

GHCi> :k Box1
Box1 :: * -> * -> *

The particular problem was being caused by improper indentation. Though there was another thing I was doing wrong. So the following version compiled:

data Box1 a b = Box1 Double a [b]

class Boxer v where
  foo :: (v a b) -> Double

instance Boxer Box1 where
  foo (Box1 r s t) = r

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM