因使用“打印"而导致类型不明确的类型变量"b1" [英] Ambiguous type variable ‘b1’ arising from a use of ‘print’

查看:75
本文介绍了因使用“打印"而导致类型不明确的类型变量"b1"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下无法编译的代码段:

I have the following code snippet, that does not compile:

{-# LANGUAGE OverloadedStrings, TypeFamilies, MultiParamTypeClasses #-}

module Main where


class Add a b where 
    type SumTy a b
    plus :: a -> b -> SumTy a b

instance Add Integer Double where
    type SumTy Integer Double = Double
    plus x y = fromIntegral x + y

instance Add Double Integer where
    type SumTy Double Integer = Double
    plus x y = x + fromIntegral y

main :: IO ()
main = do

  print $ plus (5::Integer) 6
  print $ plus (5.0::Double) 6.0

错误消息是:

app/Main.hs:25:3: error:
    • Ambiguous type variable ‘b1’ arising from a use of ‘print’
      prevents the constraint ‘(Show
                                  (SumTy Integer b1))’ from being solved.
      Probable fix: use a type annotation to specify what ‘b1’ should be.
      These potential instances exist:
        instance Show BS.ByteString
          -- Defined in ‘Data.ByteString.Internal’
        instance Show Ordering -- Defined in ‘GHC.Show’
        instance Show Integer -- Defined in ‘GHC.Show’
        ...plus 24 others
        ...plus 22 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In a stmt of a 'do' block: print $ plus (5 :: Integer) 6
      In the expression:
        do print $ plus (5 :: Integer) 6
           print $ plus (5.0 :: Double) 6.0
      In an equation for ‘main’:
          main
            = do print $ plus (5 :: Integer) 6
                 print $ plus (5.0 :: Double) 6.0
   |
25 |   print $ plus (5::Integer) 6
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^

使用打印"产生的歧义类型变量"b1"是什么意思?我在任何地方都看不到b1.

what does Ambiguous type variable ‘b1’ arising from a use of ‘print’ mean? I can not see anywhere b1.

推荐答案

plus (5::Integer) 6

第一个参数的类型为Integer.第二个数字的类型可以是任何数字类型,我们假设Num b1称为b1.因此,我们正在调用该函数

The type of the first argument is Integer. The type of the second one can be any numeric type, let's call that b1, assuming Num b1. So, we are calling the function

plus :: Integer -> b1 -> SumTy Integer b1

,但是周围没有匹配的实例. Add Integer Double只有一个实例,但是我们不能调用该实例,因为毕竟以后的程序员可能还会为Add Integer Integer添加另一个实例,我们需要为该实例做好准备. (请注意,其他实例甚至可能位于另一个模块中,因此,GHC不能简单地假设不检查所有模块,就不会存在该实例,这会导致效率低下.)

but there is no matching instance around. There's only an instance for Add Integer Double, but we can't call that one, since after all the programmer later on might also add another instance for, say, Add Integer Integer and we need to be ready for that one. (Note that the additional instance might even be in another module, so GHC can not simply assume it does not exist without checking all the modules, which would be inefficient.)

您需要指定6参数的类型,否则调用将不明确.

You need to specify the type for the 6 argument, otherwise the call is ambiguous.

这篇关于因使用“打印"而导致类型不明确的类型变量"b1"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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