Why can I not make String an instance of a typeclass?

Given : data Foo = FooString String … class Fooable a where --(is this a good way to name this?) toFoo :: a -> Foo I want to make String an instance of Fooable : instance Fooable String where toFoo = FooString GHC then complains: Illegal instance declaration for `Fooable String' (All instance types must be of the form (T t1 ... tn) where T is not a synonym. Use -XTy

为什么我不能让String成为一个类型类的实例?

鉴于 : data Foo = FooString String … class Fooable a where --(is this a good way to name this?) toFoo :: a -> Foo 我想让String成为Fooable一个实例: instance Fooable String where toFoo = FooString GHC然后抱怨说: Illegal instance declaration for `Fooable String' (All instance types must be of the form (T t1 ... tn) where T is not a synonym. Use -XTypeSynonymInstance

HasResolution typeclass

I just looked up the HasResolution typeclass and it has a single method, resolution that is declared as follows: class HasResolution a where ... resolution :: p a -> Integer I don't understand the p in the above declaration. Where does it come from and what does it mean? It's just a proxy. If you had class HasResolution a where resolution :: Integer you'd get yelle

HasResolution typeclass

我刚刚抬头的HasResolution类型类,它有一个方法, resolution该声明如下: class HasResolution a where ... resolution :: p a -> Integer 我不明白上述声明中的p 。 它来自哪里,它是什么意思? 这只是一个代理。 如果你有 class HasResolution a where resolution :: Integer 你会大声吼叫,因为编译器无法推断你调用resolution时需要的HasResolution实例。 具体来说, resolution :: HasResolution a

The purpose of the Traversable typeclass

Could someone please explain to me, what is the purpose of the typeclass Traversable ? The typeclass definition is: class (Functor t, Foldable t) => Traversable (t :: * -> *) where So Traversable is a Functor t and Foldable t . The traverse function is a member of Traversable and has the following signature: traverse :: Applicative f => (a -> f b) -> t a -> f (t b) Why d

Traversable typeclass的用途

有人能向我解释,类型类Traversable的目的是什么? 类型类定义是: class (Functor t, Foldable t) => Traversable (t :: * -> *) where 所以Traversable是Functor t和Foldable t 。 traverse函数是Traversable的成员,具有以下签名: traverse :: Applicative f => (a -> f b) -> t a -> f (t b) 为什么结果必须包含在应用程序中? 它有什么意义? 我有以下例子: module ExercisesTraversable wh

How do we know if a typeclass is a sub

If we do let add ab = a+b then add :: Num a => a -> a -> a . We also know that 1.5 :: Fractional a => a . And add 1 1.5 works flawlessly. If I understand correctly every type which has an instance of Fractional has also an instance of Num , but how is this fact made explicit? It's in the definition of the Fractional class: class Num a => Fractional a where ... You c

我们如何知道类型类是否是一个子类?

如果我们let add ab = a+b然后add :: Num a => a -> a -> a 。 我们也知道1.5 :: Fractional a => a 。 并完美地add 1 1.5作品。 如果我理解正确,每个具有Fractional实例的类型都有一个Num实例,但这个事实是如何明确的? 它在Fractional类的定义中: class Num a => Fractional a where ... 你可以看到它的输入:i Fractional GHCi中的:i Fractional ,或者Haddock文档中的:i Fractional 。

Is a typeclass constraint discouraged in a typeclass method?

I know that constraints in data are discouraged in Haskell. For instance, data Eq b => Bar b = Bar b is not possible without a deprecated extension. I also hear that even data Bar b = Eq b => Bar b is not common and even discouraged. (Is this right BTW?) Is the same true for constraints in typeclasses ? For instance, is doing something like class Foo a where foo :: Eq b =>

typeclass方法中的typeclass约束是不鼓励的吗?

我知道Haskell 中的数据约束是不鼓励的。 例如, data Eq b => Bar b = Bar b 没有弃用的扩展名是不可能的。 我甚至也听到了 data Bar b = Eq b => Bar b 并不常见,甚至不鼓励。 (这是顺利吗?) 类型类中的约束是否也是如此? 例如,正在做类似的事情 class Foo a where foo :: Eq b => a -> b 在Haskell中也不鼓励? 它通常在真实代码中看到吗? 从使用的角度来看,类型类方法与普通的多态函

What is the difference between traits in Rust and typeclasses in Haskell?

Traits in Rust seem at least superficially similar to typeclasses in Haskell, however I've seen people write that there are some differences between them. I was wondering exactly what these differences are. At the basic level, there's not much difference, but they're still there. Haskell describes functions or values defined in a typeclass as 'methods', just as traits des

Rust和Haskell中的类型类的特性有什么区别?

Rust中的特性看起来至少与Haskell中的类型类似,但是我看到人们写道他们之间有一些区别。 我很想知道这些差异究竟是什么。 在基本层面上,没有太大的区别,但他们仍然存在。 Haskell将typeclass中定义的函数或值描述为“方法”,正如特征描述它们所包含的对象中的OOP方法一样。 然而,Haskell处理这些问题的方式不同,将它们视为单独的值,而不是将它们固定为一个对象,因为OOP会导致他们这样做。 这是关于最明显的表面水平

Typeclasses and GADTs

I'm putting together a geometry library in haskell. I'm not intending to release it, it's just a project that I'm using to improve my knowledge of the language. I have a Local datatype, with the following definition data Local a where MkLocal :: (Vectorise a) => ReferenceFrame -> a -> Local a A reference frame is a Vector pointing to the origin of the frame and a

类型类和GADT

我在haskell中放置了一个几何库。 我不打算发布它,这只是一个我用来提高我对该语言知识的项目。 我有一个Local数据类型,具有以下定义 data Local a where MkLocal :: (Vectorise a) => ReferenceFrame -> a -> Local a 参考框架是一个指向框架起点的矢量和一个代表框架旋转的角度,它们都被定义为“绝对”参考框架(嘿,它不是现实世界!)。 Vectorise几何是一个可逆变换到Vector列表中的几何。 我想到Loc

Haskell: Typeclass implies other typeclass

Is it possible to have a typeclass imply another typeclass in Haskell? For example let's say there is a bunch of "things" that can be ordered by an "attribute": data Person = Person { name :: String, age :: Int } Person p1 <= Person p1 = (age p1) <= (age p2) To avoid repetition one could define a "orderable by key" type class class OrdByKey o where o

Haskell:Typeclass暗示其他类型

是否有可能有一个类型类型暗示Haskell中的另一个类型类? 例如,让我们说有一堆“事物”可以通过“属性”进行排序: data Person = Person { name :: String, age :: Int } Person p1 <= Person p1 = (age p1) <= (age p2) 为了避免重复,可以定义一个“按键可按类型”类 class OrdByKey o where orderKey :: (Ord r) => o -> r x <= y = (orderKey x) <= (orderKey y) 然后Person的实例声明可能如下所示

What is the Comonad typeclass in Haskell?

What is the Comonad typeclass in Haskell? As in Comonad from Control.Comonad in the comonad package (explanations of any other packages that provide a Comonad typeclass are also welcome). I've vaguely heard about Comonad, but all I really know about it is that is provides extract :: wa -> a , sort of a parallel to Monad's return :: a -> ma . Bonus points for noting "real li

Haskell中的Comonad类型类是什么?

Haskell中的Comonad类型类是什么? 正如Comonad包中的Control.Comonad中的Comonad一样(也提供对提供Comonad类型类的任何其他包的解释)。 我隐约听说过Comonad,但我真正知道的是,它提供了extract :: wa -> a ,类似于Monad的return :: a -> ma 。 在“真实”代码中注明Comonad的“真实生活”用途的加分。 这些链接可能会有所帮助: 评估细胞自动机是共同的。 特别是,“无论何时你看到大量的数据结构都是通过大量小

Should I use typeclasses or not?

I have some difficulties to understand when use and when not use typeclass in my code. I mean create my own, and not use already defined typeclasses, of course. By example (very stupid example), should I do: data Cars = Brakes | Wheels | Engine data Computers = Processor | RAM | HardDrive class Repairable a where is_reparaible :: a -> Bool instance Repairable Cars where is_repai

我应该使用typeclasses或不?

我在使用时以及在代码中不使用typeclass时遇到一些困难。 我的意思是创建我自己的,当然不使用已经定义的类型类。 举例(非常愚蠢的例子),我应该这样做: data Cars = Brakes | Wheels | Engine data Computers = Processor | RAM | HardDrive class Repairable a where is_reparaible :: a -> Bool instance Repairable Cars where is_repairable (Brakes) = True is_repairable (Wheels) = False i