Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 阻止约束的Ambigous类型变量_Haskell_Typeclass - Fatal编程技术网

Haskell 阻止约束的Ambigous类型变量

Haskell 阻止约束的Ambigous类型变量,haskell,typeclass,Haskell,Typeclass,我在摆弄IO,不理解以下错误: * Ambiguous type variable `a0' arising from a use of `readLine' prevents the constraint `(Console a0)' from being solved. Probable fix: use a type annotation to specify what `a0' should be. These potential instance e

我在摆弄IO,不理解以下错误:

* Ambiguous type variable `a0' arising from a use of `readLine'
      prevents the constraint `(Console a0)' from being solved.
      Probable fix: use a type annotation to specify what `a0' should be.
      These potential instance exist:
        instance Console Int -- Defined at Tclass.hs:8:14
    * In the second argument of `(+)', namely `readLine'
      In the second argument of `($)', namely `(2 + readLine)'
      In the expression: putStrLn . show $ (2 + readLine)
   |
17 |     useInt =putStrLn . show $ (2+readLine)
代码

module Tclass where
    import System.Environment

    class Console a where
        writeLine::a->IO()
        readLine::IO a

    instance Console Int where
        writeLine= putStrLn . show 

        readLine = do
            a <- getLine
            let b= (read  a)::Int
            return b

    useInt::IO()
    useInt =putStrLn . show $ (2+readLine)  
模块Tclass where
导入系统。环境
类控制台a在哪里
writeLine::a->IO()
读线::IO a
实例控制台Int,其中
writeLine=putStrLn。显示
readLine=do

a
2
不仅是Haskell中的
Int
,而且是任何数字类型,包括
浮点、双精度、整数、
。它的类型是
numa=>a
——适合每种数字类型的多态类型

因此,您可以使用
(2::Int)
。然后你会发现
(2::Int)+readLine
是一个类型错误,因为
readLine::Int
是错误的,我们只得到
readLine::IO Int

你可以试试这个

useInt :: IO ()
useInt = do
   i <- readLine
   putStrLn . show (2 + i :: Int)
useInt::IO()
useInt=do

i
2
不仅是Haskell中的
Int
,而且是任何数字类型,包括
浮点、双精度、整数、
。它的类型是
numa=>a
——适合每种数字类型的多态类型

因此,您可以使用
(2::Int)
。然后你会发现
(2::Int)+readLine
是一个类型错误,因为
readLine::Int
是错误的,我们只得到
readLine::IO Int

你可以试试这个

useInt :: IO ()
useInt = do
   i <- readLine
   putStrLn . show (2 + i :: Int)
useInt::IO()
useInt=do

我不理解以下内容:
readLine
方法在被另一个方法使用时不应该返回
Int
,因为唯一的
IO
部分是以正确的紧凑方式完成的:
useInt::IO()useInt=putStrLn。show$((2::Int)+(@bercovicadrian
@bercovicadrian
readLine
是一个
ioint
,而不是
Int
。前一种类型说“如果你让我做I/O,你可以有一个Int”,后一种类型说“你可以有一个Int(没有I/O)”.The
。@Bercoviciarian也许你应该阅读monad教程来了解更多关于IO monad的知识,和/或看看哪些可以澄清某些方面。我不明白以下几点:
readLine
方法在被其他方法使用时不应该返回
Int
,因为唯一的
IO
部分是用完成的ode>aSo一个正确的紧凑方式应该是:
useInt::IO()useInt=putStrLn.show$((2::Int)+(@bercovicadrian
@bercovicadrian
readLine
是一个
ioint
,而不是
Int
。前者说“如果你让我做I/O,你可以有一个Int”,后者说“你可以有一个Int(没有I/O)”.The
。@Bercoviciarian也许你应该阅读monad教程以了解更多关于IO monad的信息,和/或看看哪些教程可以澄清某些方面。
putstrn.show
应始终替换为
print
putstrn.show
应始终替换为
print