Making a typeclass, cannot deduce from context

I'm using the Servant libary, and I would like to automatically map results into error codes. Servant expects the type: Either (Int, String) a . For example, if I have a model function of type: IO (Maybe User) . I would like to turn that in to (404, "Not Found") on Nothing, and the User if it's there. To do this, I'm writing a typeclass! class Servile a where toSt

制作一个类型类,不能从上下文中推导出来

我使用仆人库,我想自动将结果映射到错误代码。 仆人期望的类型: Either (Int, String) a 。 例如,如果我有一个类型为: IO (Maybe User)的模型函数。 我想在Nothing上将(404, "Not Found")和User如果它存在) 要做到这一点,我正在写一个typeclass! class Servile a where toStatus :: ToJSON val => a -> Either (Int, String) val instance ToJSON a => Servile (Maybe a) where toStat

Are type synonyms with typeclass constraints possible?

Feel free to change the title, I'm just not experienced enough to know what's really going on. So, I was writing a program loosely based on this, and wrote this (as it is in the original) type Row a = [a] type Matrix a = [Row a] Nothing special there. However, I found myself writing a couple of functions with a type like this: Eq a => Row a -> ... So I thought that perhaps

是否可以使用类型类约束的类型同义词?

随意更改标题,我只是没有足够的经验知道真正发生了什么。 所以,我在这个基础上松散地编写了一个程序,并且写了这个程序(因为它是原始的) type Row a = [a] type Matrix a = [Row a] 没有什么特别的。 但是,我发现自己用这样的一个类型编写了几个函数: Eq a => Row a -> ... 所以我想,也许我可以将这个约束写入类型同义词定义中,因为在我看来,它不应该那么复杂,对吗? 如果编译器可以在函数中使用它,

Using the Maybe Monad in "reverse"

Let's say I have a number of functions: f :: a -> Maybe a g :: a -> Maybe a h :: a -> Maybe a And I want to compose them in the following way: If f returns Nothing, compute g. If g returns Nothing, compute h. If any of them compute Just a, stop the chain. And the whole composition (h . g . f) should of course return Maybe a. This is the reverse of the typical use of the Maybe

在“反向”中使用Maybe Monad

假设我有很多功能: f :: a -> Maybe a g :: a -> Maybe a h :: a -> Maybe a 我想以下面的方式来编写它们:如果f返回Nothing,则计算g。 如果g返回Nothing,则计算h。 如果他们中的任何一个计算Just a,则停止链。 而整个作品(例如f)当然应该返回Maybe a。 这与Maybe monad的典型用法相反,如果返回Nothing则通常停止计算。 链式计算这样的Haskell成语是什么? mplus正是你正在寻找的,是MonadPlus类型类

Declare list instance of a type class

I'm learning Haskell type classes through UPENN Haskell lecture notes, making my own type class with example code: class Listable a where toList :: a -> [Int] instance Listable Int where toList x = [x] instance Listable Bool where toList True = [1] toList False = [0] It works with Int and Bool but ghci fails when I add an instance of [Int] : instance Listable [Int] wh

声明一个类型类的列表实例

我通过UPENN Haskell讲​​义学习Haskell类型类,使用自己的类型类创建示例代码: class Listable a where toList :: a -> [Int] instance Listable Int where toList x = [x] instance Listable Bool where toList True = [1] toList False = [0] 它适用于Int和Bool ,但当添加[Int]的实例时, ghci失败: instance Listable [Int] where toList = id 错误: 'Listable [Int]'的非法实例

error Couldn't match expected type ‘Char’ with actual type ‘[Char]’

I am trying to build a string representation for the show function of a typeclass representing a polynomial. I keep getting type errors of a mismatch from 'Char' to '[Char]', but from my understanding haskell's "append" function should be able to concatenate a Char to a string/[Char]. I don't understand where the problem lies, or where to look for a solution ba

错误无法匹配预期类型'Char'与实际类型'[Char]'

我正在尝试为代表多项式的类型类的show函数构建一个字符串表示。 我不断从'Char'到'[Char]'获取不匹配的类型错误,但是从我的理解来看,haskell的“append”函数应该能够将Char连接到字符串/ [Char]。 我不明白问题出在哪里,或者根据我收到的错误在哪里寻找解决方案。 这里是错误的代码: newtype Poly a = P [a] instance (Num a, Show a) => Show (Poly a) where show p = ["" : form (p !! i) i |

Can one compose types in a Haskell instance declaration?

I've written a Haskell typeclass and it would be convenient to declare instances of it using types of the form (a -> m _) , where m is of kind (* -> *) , such as a monad, and _ is a slot to be left unsaturated. I know how to write newtype X amb = X (a -> mb) , and declaring an instance for X am . But what I'm looking for is to instead use the bare, unwrapped -> type, if that

可以在Haskell实例声明中组合类型吗?

我写了一个Haskell类型类,并且使用form (a -> m _)类型来声明它的实例是很方便的,其中m是类型的(* -> *) ,比如monad,而_是一个插槽将保持不饱和状态。 我知道如何编写newtype X amb = X (a -> mb) ,并为X am声明一个实例。 但是我正在寻找的是使用裸露的,未包装的->类型,如果可能的话。 如果想要为表单类型(a -> _)声明实例,那么你可以写: instance Foo a ((->) a) where ... 但我不知道如何/

Illegal instance declaraion when using tuples

Messing around in Haskell getting to know type classes more intimately, but I've hit a bit of a roadblock. For whatever reason I'm not allowed to make an instance of my Vector class. I'm being told that it's an illegal instance declaration because I don't have distinct type variables? What's going on here? class Vector v where vplus :: v -> v -> v vmult :: Nu

使用元组时非法实例声明

在Haskell中仔细研究类型类更加亲密,但我遇到了一些障碍。 无论出于何种原因,我不允许创建我的Vector类的实例。 我被告知这是一个非法的实例声明,因为我没有独特的类型变量? 这里发生了什么? class Vector v where vplus :: v -> v -> v vmult :: Num a => v -> a -> v instance Num a => Vector (a, a) where (a, b) `vplus` (c, d) = (a + c, b + d) (a, b) `vmult` m = (a * m, b * m)

not clear which instance is chosen by Haskell

I have the following Haskell code using overlapping instances; I tried to implement a function which yield the funcion's type as a String, or -- more generally speaking -- does different things for different function types: {-# OPTIONS_GHC -fglasgow-exts #-} {-# LANGUAGE OverlappingInstances, IncoherentInstances #-} module Test where data TypeString = MKTS String instance Show TypeStrin

不清楚Haskell选择了哪个实例

我有以下使用重叠实例的Haskell代码; 我尝试实现一个将函数的类型作为String来生成的函数,或者 - 更一般地说 - 对不同的函数类型做不同的事情: {-# OPTIONS_GHC -fglasgow-exts #-} {-# LANGUAGE OverlappingInstances, IncoherentInstances #-} module Test where data TypeString = MKTS String instance Show TypeString where show (MKTS s) = s class ShowType b c where theType :: (b -> c) -> TypeStr

Haskell: how to get through 'no instance for'?

I am learning Haskell. I am on the 8th chapter of this book. The main thing I've learned so far is that Haskell is very unfriendly to me and it bites my ass where possible. Moreover... Heck! Enough mourning, to business. Here is the code: module GlobRegex ( globToRegex, matchesGlob ) where import Text.Regex.Posix import Text.Regex.Posix.String import Text.Regex.Base.RegexLike dat

Haskell:如何通过“无实例”?

我正在学习Haskell。 我在本书的第8章。 到目前为止,我学到的主要内容是Haskell对我来说非常不友好,它会在可能的情况下咬我的屁股。 而且......哎呀! 足够的哀悼,对生意。 代码如下: module GlobRegex ( globToRegex, matchesGlob ) where import Text.Regex.Posix import Text.Regex.Posix.String import Text.Regex.Base.RegexLike data CaseOpt = Case | NoCase deriving (Eq) matchesGlob :: String -&g

Make a typeclass instance automatically an instance of another

What I'd like to achieve is that any instance of the following class ( SampleSpace ) should automatically be an instance of Show , because SampleSpace contains the whole interface necessary to create a String representation and hence all possible instances of the class would be virtually identical. {-# LANGUAGE FlexibleInstances #-} import Data.Ratio (Rational)

让一个typeclass实例自动成为另一个实例

我想要实现的是以下类( SampleSpace )的任何实例都应该自动成为Show一个实例,因为SampleSpace包含创建String表示所需的整个接口,因此该类的所有可能实例都将几乎相同。 {-# LANGUAGE FlexibleInstances #-} import Data.Ratio (Rational) class SampleSpace space where events :: Ord a => space a ->