2017-05-31 53 views
2

我试图从this Hackage page运行使用SQLite,简单的例子:哈斯克尔SQLite的,简单类型的错误

{-# LANGUAGE OverloadedStrings #-} 

module Main where 
import Database.SQLite.Simple 

main :: IO() 
main = do 
    conn <- open "test.db" 
    [[x]] <- query_ conn "select 2 + 2" 
    print x 

但我得到这个错误:

Prelude> :load sqltest.hs 
[1 of 1] Compiling Main    (sqltest.hs, interpreted) 

sqltest.hs:9:12: error: 
    • Ambiguous type variable ‘a0’ arising from a use of ‘query_’ 
     prevents the constraint ‘(Database.SQLite.Simple.FromField.FromField 
            a0)’ from being solved. 
     Probable fix: use a type annotation to specify what ‘a0’ should be. 
     These potential instances exist: 
     instance Database.SQLite.Simple.FromField.FromField Integer 
      -- Defined in ‘Database.SQLite.Simple.FromField’ 
     instance Database.SQLite.Simple.FromField.FromField a => 
       Database.SQLite.Simple.FromField.FromField (Maybe a) 
      -- Defined in ‘Database.SQLite.Simple.FromField’ 
     instance Database.SQLite.Simple.FromField.FromField Bool 
      -- Defined in ‘Database.SQLite.Simple.FromField’ 
     ...plus five others 
     ...plus 15 instances involving out-of-scope types 
     (use -fprint-potential-instances to see them all) 
    • In a stmt of a 'do' block: [[x]] <- query_ conn "select 2 + 2" 
     In the expression: 
     do { conn <- open "test.db"; 
      [[x]] <- query_ conn "select 2 + 2"; 
      print x } 
     In an equation for ‘main’: 
      main 
      = do { conn <- open "test.db"; 
        [[x]] <- query_ conn "select 2 + 2"; 
        print x } 

sqltest.hs:10:3: error: 
    • Ambiguous type variable ‘a0’ arising from a use of ‘print’ 
     prevents the constraint ‘(Show a0)’ from being solved. 
     Relevant bindings include x :: a0 (bound at sqltest.hs:9:5) 
     Probable fix: use a type annotation to specify what ‘a0’ should be. 
     These potential instances exist: 
     instance Show Ordering -- Defined in ‘GHC.Show’ 
     instance Show Integer -- Defined in ‘GHC.Show’ 
     instance Show FormatError -- Defined in ‘Database.SQLite.Simple’ 
     ...plus 28 others 
     ...plus 35 instances involving out-of-scope types 
     (use -fprint-potential-instances to see them all) 
    • In a stmt of a 'do' block: print x 
     In the expression: 
     do { conn <- open "test.db"; 
      [[x]] <- query_ conn "select 2 + 2"; 
      print x } 
     In an equation for ‘main’: 
      main 
      = do { conn <- open "test.db"; 
        [[x]] <- query_ conn "select 2 + 2"; 
        print x } 
Failed, modules loaded: none. 

我安装使用惊天动地的sqlite-简单。然后我试着将示例代码加载到GHCI中。我是Haskell的新手,只是尝试一些简单的例子。

回答

1

你可以query很多类型,因此不清楚x是什么类型。所有GHC知道约x是它的FromField(由于query_)和Show(由于print)的实例。

您必须指定您要查询的类型,例如

print (x :: Integer) 

printInt :: Int -> IO() 
printInt = print 

main = do 
    … 
    printInt x 
+0

这工作,谢谢。但接下来的问题是Hackage示例不正确?这里最大的问题是我认为示例代码是绝对正确的。我应该更加谨慎,因为我学习这门语言? – mitch

+0

从类型不明确的意义上讲,这是不正确的。在查询工作的意义上这是正确的。不过,我不确定这个软件包是否是学习Haskell的典型出发点。 – Zeta