如何在Haskell的实例方法中应用函数约束? [英] How do you apply function constraints in instance methods in Haskell?

查看:176
本文介绍了如何在Haskell的实例方法中应用函数约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在Haskell中使用类型类



考虑类型类 T 的下面实现类型约束类函数 f

  class T t其中
f ::(Eq u)=& t - > u

data T_Impl = T_Impl_Bool Bool | T_Impl_Int Int | T_Impl_Float Float
实例T T_Impl其中
f(T_Impl_Bool x)= x
f(T_Impl_Int x)= x
f(T_Impl_Float x)= x






当我加载到GHCI 7.10.2时,我得到以下错误:


 无法与实际类型Float匹配预期类型'u'
'u'is由
限定的刚性类型变量f :: Eq u =>的类型签名。 T_Impl - > u
at generics.hs:6:5

相关绑定包括
f :: T_Impl - > u(bound at generics.hs:6:5)
在表达式中:x
在'f'的等式中:f(T_Impl_Float x)= x







我在做什么/了解错误?对我来说似乎是合理的,人们想要通过提供伴随的数据构造函数和函数实现来专门化实例中的类型类。


无法与实际类型 u Float '


特别混乱。为什么 u 不匹配 Float 如果 u 约束,它必须符合 类型( Float s do that afaik)?

解决方案

签名

  )=> t  - > u 

表示调用者可以选择 t 是 c c> code>在类方法中的 T (和 t 有一个隐式的 T t 约束)。



这并不意味着实现可以选择任何 u



因此,调用者可以使用 f 方式: t 在 T 中<$ p $ c> t

  f :: t  - > Bool 
f :: t - > Char
f :: t - > int
...

编译器抱怨你的实现不够通用所有这些情况。

 无法与实际类型Float的预期类型u匹配

表示您给了我一个 Float ,但您必须提供一个值一般类型 u (其中 u 将由呼叫者选择)


I'm learning how to use typeclasses in Haskell.

Consider the following implementation of a typeclass T with a type constrained class function f.

class T t where
    f :: (Eq u) => t -> u 

data T_Impl = T_Impl_Bool Bool | T_Impl_Int Int | T_Impl_Float Float 
instance T T_Impl where
    f (T_Impl_Bool x) = x
    f (T_Impl_Int x) = x
    f (T_Impl_Float x) = x


When I load this into GHCI 7.10.2, I get the following error:

Couldn't match expected type ‘u’ with actual type ‘Float’
      ‘u’ is a rigid type variable bound by
          the type signature for f :: Eq u => T_Impl -> u 
          at generics.hs:6:5

Relevant bindings include
  f :: T_Impl -> u (bound at generics.hs:6:5)
In the expression: x
In an equation for ‘f’: f (T_Impl_Float x) = x


What am I doing/understanding wrong? It seems reasonable to me that one would want to specialize a typeclass in an instance by providing an accompaning data constructor and function implementation. The part

Couldn't match expected type 'u' with actual type 'Float'

is especially confusing. Why does u not match Float if u only has the constraint that it must qualify as an Eq type (Floats do that afaik)?

解决方案

The signature

f :: (Eq u) => t -> u 

means that the caller can pick t and u as wanted, with the only burden of ensuring that u is of class Eq (and t of class T -- in class methods there's an implicit T t constraint).

It does not mean that the implementation can choose any u.

So, the caller can use f in any of these ways: (with t in class T)

f :: t -> Bool 
f :: t -> Char
f :: t -> Int
... 

The compiler is complaining that your implementation is not general enough to cover all these cases.

Couldn't match expected type ‘u’ with actual type ‘Float’ 

means "You gave me a Float, but you must provide a value of the general type u (where u will be chosen by the caller)"

这篇关于如何在Haskell的实例方法中应用函数约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆