我在Haskell中定义了自己的数据类型来表示单变量多项式。
data Polinomio a where
Pol :: (Num a) => a -> Integer -> Polinomio a -> Polinomio a
Cons :: (Num a) => a -> Polinomio a我在这里使用了GADT来约束a变量,使其属于Num Class。现在,我想为Functor类定义我自己的实例
instance Functor Polinomio where
fmap f (Cons x) = Cons $ f x
fmap f (Pol x g p) = Pol (f x) g (fmap f p)它不能编译给我这样的原因:
Polinomio_GADT.hs:31:23:
Could not deduce (Num b) arising from a use of `Cons'
from the context (Num a)
bound by a pattern with constructor
Cons :: forall a. Num a => a -> Polinomio a,
in an equation for `fmap'
at Polinomio_GADT.hs:31:13-18
Possible fix:
add (Num b) to the context of
the data constructor `Cons'
or the type signature for
fmap :: (a -> b) -> Polinomio a -> Polinomio b
In the expression: Cons
In the expression: Cons $ f x
In an equation for `fmap': fmap f (Cons x) = Cons $ f x
Polinomio_GADT.hs:32:26:
Could not deduce (Num b) arising from a use of `Pol'
from the context (Num a)
bound by a pattern with constructor
Pol :: forall a.
Num a =>
a -> Integer -> Polinomio a -> Polinomio a,
in an equation for `fmap'
at Polinomio_GADT.hs:32:13-21
Possible fix:
add (Num b) to the context of
the data constructor `Pol'
or the type signature for
fmap :: (a -> b) -> Polinomio a -> Polinomio b
In the expression: Pol (f x) g (fmap f p)
In an equation for `fmap':
fmap f (Pol x g p) = Pol (f x) g (fmap f p)
In the instance declaration for `Functor Polinomio'因此,我尝试使用语言扩展InstanceSigs将此约束添加到fmap定义中:
instance Functor Polinomio where
fmap :: (Num a,Num b) -> (a -> b) -> Polinomio a -> Polinomio b
fmap f (Cons x) = Cons $ f x
fmap f (Pol x g p) = Pol (f x) g (fmap f p)它不能从编译器中获取以下代码:
Polinomio_GADT.hs:31:13:
Predicate `(Num a, Num b)' used as a type
In the type signature for `fmap':
fmap :: (Num a, Num b) -> (a -> b) -> Polinomio a -> Polinomio b
In the instance declaration for `Functor Polinomio'你知道怎么解决这个问题吗?
发布于 2013-06-23 00:07:47
您的数据类型为
data Polinomio a where
Pol :: (Num a) => a -> Integer -> Polinomio a -> Polinomio a
Cons :: (Num a) => a -> Polinomio a现在看一下您对Functor的定义
instance Functor Polinomio where
fmap f (Cons x) = Cons $ f x
fmap f (Pol x g p) = Pol (f x) g (fmap f p)由于GADT的约束,ghc能够推断出x的约束Num a。但问题出在不受限制的函数f中,因为f :: a -> b, x :: Num a => a推断f x是b类型。因此它不能将f x限制为Cons所要求的Num b => b。
因此,正如Daniel所指出的,您不能向您的Functor类添加约束。您可以定义自己的受限函数器类,如here,也可以使用rmonad中的RFunctor。
https://stackoverflow.com/questions/17251891
复制相似问题