为什么Haskell无法推断出这种类型

时间:2019-05-05 15:29:42

标签: haskell algebra type-variables

我一直在尝试编写程序以在任意字段(一种数学结构)上实现多项式。我选择Haskell作为编程语言,并使用了GADTs语言扩展。但是,我不明白为什么GHCi无法推论a的约束。

上下文:

-- irreducible.hs

{-# LANGUAGE GADTs #-}

infixl 6 .+
infixl 7 .*

class Ring a where
  (.+) :: a -> a -> a
  (.*) :: a -> a -> a
  fneg :: a -> a
  fzero :: a
  funit :: a

class (Ring a) => Field a where
  finv :: a -> a

data Polynomial a where 
  Polynomial :: (Field a) => [a] -> Char -> Polynomial a

instance (Show a) => Show (Polynomial a) where
  show (Polynomial (a0:ar) x)
    = show a0
      ++ concatMap (\(a, k) -> "+" ++ show a ++ x:'^':show k) (zip ar [0..])
  show (Polynomial [] _) = show (fzero::a)

解释:环是定义了加法和乘法的事物,其中加法形成一个(实际上是abelian)组,而乘法则形成一个类半体。字段是定义了乘法逆的环。字段上的多项式由一系列系数和一个字符表示。字符,例如'x',表示此多项式与未知变量x有关。对于写为Polynomial [] 'x'的零多项式,我希望它显示基础字段的零元素。

在GHCi上运行后,我得到了:

irreducible.hs:59:28: error:
    • Could not deduce (Show a0) arising from a use of ‘show’
      from the context: Show a
        bound by the instance declaration at irreducible.hs:55:10-40
      or from: Field a
        bound by a pattern with constructor:
                   Polynomial :: forall a. Field a => [a] -> Char -> Polynomial a,
                 in an equation for ‘show’
        at irreducible.hs:59:9-23
      The type variable ‘a0’ is ambiguous
      These potential instances exist:
        instance (Show a, Show b) => Show (Either a b)
          -- Defined in ‘Data.Either’
        instance Show Ordering -- Defined in ‘GHC.Show’
        instance Show Integer -- Defined in ‘GHC.Show’
        ...plus 25 others
        ...plus 87 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the expression: show (fzero :: a)
      In an equation for ‘show’:
          show (Polynomial [] _) = show (fzero :: a)
      In the instance declaration for ‘Show (Polynomial a)’
   |
59 |   show (Polynomial [] _) = show (fzero::a)
   |                            ^^^^^^^^^^^^^^^

irreducible.hs:59:34: error:
    • Could not deduce (Ring a1) arising from a use of ‘fzero’
      from the context: Show a
        bound by the instance declaration at irreducible.hs:55:10-40
      or from: Field a
        bound by a pattern with constructor:
                   Polynomial :: forall a. Field a => [a] -> Char -> Polynomial a,
                 in an equation for ‘show’
        at irreducible.hs:59:9-23
      Possible fix:
        add (Ring a1) to the context of
          an expression type signature:
            forall a1. a1
    • In the first argument of ‘show’, namely ‘(fzero :: a)’
      In the expression: show (fzero :: a)
      In an equation for ‘show’:
          show (Polynomial [] _) = show (fzero :: a)
   |
59 |   show (Polynomial [] _) = show (fzero::a)
   |            

现在让我们集中讨论有问题的部分:

instance (Show a) => Show (Polynomial a) where
  show (Polynomial (a0:ar) x) = show a0 ++ [...]
  show (Polynomial [] _) = show (fzero::a)

我认为Polynomial a保证aField的实例,这意味着aRing的实例。因此,像fzero::a一样调用42::Int应该是合理的。此外,我已经写了Show a作为约束,并且Polynomial a的构造函数的形状为Polynomial [a] Char,因此它也应该知道a0的类型是{的实例{1}}。

显然,口译员的看法不同。我在哪里弄错了?

1 个答案:

答案 0 :(得分:2)

摘自arrowd的评论:

代码很好,但是需要使用ScopedTypeVariables扩展名,这使得a中的类型变量fzero :: a引用了先前介绍的a