Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/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 postgresql连接函数类型错误_Postgresql_Haskell - Fatal编程技术网

理解Haskell postgresql连接函数类型错误

理解Haskell postgresql连接函数类型错误,postgresql,haskell,Postgresql,Haskell,我现在是java程序员,正在阅读和学习haskell。我正在尝试编写一个简单的程序,使用HDBC postgres驱动程序连接(和断开)到postgres数据库。为了简单起见,我没有任何其他逻辑 它正在抛出函数类型错误。我正确地缩进了代码,如果我删除了disconnect,那么它将与定义的类型一起工作 有人能解释一下我在定义这个函数的类型时遗漏了什么吗?我感谢你的帮助 谢谢 示例代码: import Database.HDBC import Database.HDBC.PostgreSQL im

我现在是java程序员,正在阅读和学习haskell。我正在尝试编写一个简单的程序,使用HDBC postgres驱动程序连接(和断开)到postgres数据库。为了简单起见,我没有任何其他逻辑

它正在抛出函数类型错误。我正确地缩进了代码,如果我删除了disconnect,那么它将与定义的类型一起工作

有人能解释一下我在定义这个函数的类型时遗漏了什么吗?我感谢你的帮助

谢谢

示例代码:

import Database.HDBC
import Database.HDBC.PostgreSQL
import Database.HaskellDB
import Database.HaskellDB.HDBC.PostgreSQL

tryConnect :: Int  -> (Database -> IO Connection) -> ()   
tryConnect id =
   do
     c <- postgresqlConnect [("host","dbhost"),("dbname","db1"),("user","user1"),("password","test")]
     disconnect c
     return ()
import Database.HDBC
导入Database.HDBC.PostgreSQL
导入数据库.HaskellDB
导入数据库.HaskellDB.HDBC.PostgreSQL
tryConnect::Int->(数据库->IO连接)->()
tryConnect id=
做

c问题是您没有为
postgresqlConnect
提供足够的参数。它的类型签名是
[(String,String)]->(Database->ma)->ma
,但您只提供了第一个参数。给
postgresqlConnect
第二个参数应该可以解决这个问题,并且您可以将类型声明更改回
Int->IO()

编辑:下面的答案是完全错误的。我的错

嗯,类型签名是
tryConnect::Int->(数据库->IO连接)->()
。通常,这表示函数接受
Int
(数据库->IO连接)
并返回
()
,但函数定义中提供的唯一参数是
id
。因此,实际上您有一个函数,它接受一个
Int
,并返回一个类型签名为
(数据库->IO连接)->()
的新函数

这很好,只是函数体与此签名不匹配。
do
表达式返回的是一个
IO()
值,而不是预期的函数,因此会出现一个错误,因为编译器得到的返回值与预期值不同


总之,在类型签名中似乎有一个参数没有在实际函数中使用。请从类型签名中删除该函数,或者将该函数更改为
tryConnect id func=…
而不是
tryConnect id=…

谢谢Jeffrey。类型由我定义,而不是由函数定义。我之前不清楚。最初我尝试使用这种类型,tryConnect::Int->IO()。这会抛出以下异常My bad..注释被破坏。{无法将“do”表达式的stmt中的预期类型
IO()'与推断类型
(数据库->MA)->b'进行匹配:c在问题中编写的函数中,尝试将末尾的
()
更改为
IO()
,然后查看是否编译。我尝试更改为IO()并且仍然收到错误。它正在抱怨disconnect函数。下面是错误:{无法匹配预期的类型
(数据库->IO连接)->a'与'do'表达式的stmt中的推断类型
IO()':disconnect c}
   Couldn't match expected type `(Database -> IO Connection) -> a'
       against inferred type `IO ()'
In a stmt of a 'do' expression: disconnect c
In the expression:
    do { c <- postgresqlConnect
                [("host", "dbhost"), ("dbname", "db1"), ....];
         disconnect c;
         return () }
In the definition of `insrt':
    insrt id
            = do { c <- postgresqlConnect [("host", "dbhost"), ....];
                   disconnect c;
                   return () }