首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有因使用‘`return’而产生的(Monad m0)的实例

没有因使用‘`return’而产生的(Monad m0)的实例
EN

Stack Overflow用户
提问于 2016-03-11 02:33:20
回答 2查看 515关注 0票数 0

我尝试用haskell和Qt创建一个程序,它以图形的形式显示从haskell函数获得的一些点。

这是一个正常工作的示例,但重点在于返回函数及其参数,而不是代码的其余部分!

代码语言:javascript
复制
main :: IO ()
main = do
    clazz <- newClass [
        defMethod' "factorial" (\_ txt ->
            let n = read $ T.unpack txt :: Integer
            in return . T.pack . show $ product [1..n] :: IO Text)]
    ctx <- newObject clazz ()
    runEngineLoop defaultEngineConfig {
        initialDocument = fileDocument "exemple4.qml",
        contextObject = Just $ anyObjRef ctx}

下面是我的代码:

代码语言:javascript
复制
main :: IO ()
main = do
    clazz <- newClass [
        defMethod' "init_tableau" (\_  -> 
            return $ map (^2) [0..20])]
    ctx <- newObject clazz ()
    runEngineLoop defaultEngineConfig {
        initialDocument = fileDocument "exemple4.qml",
        contextObject = Just $ anyObjRef ctx}

它应该只返回i^2的21个第一次出现,从0开始。

下面是所有的错误:

代码语言:javascript
复制
Build FAILED

/home/lowley/Documents/haskell/Qt/lancer-qml4.hs: line 10, column 9:
  No instance for (MethodSuffix (m0 [b0]))
    arising from a use of defMethod'
  The type variables `m0', `b0' are ambiguous
  Possible fix: add a type signature that fixes these type variable(s)
  Note: there are several potential instances:
    instance (Marshal a, CanGetFrom a ~ Yes, MethodSuffix b) =>
             MethodSuffix (a -> b)
      -- Defined in `Graphics.QML.Objects'
    instance (Marshal a, CanReturnTo a ~ Yes) => MethodSuffix (IO a)
      -- Defined in `Graphics.QML.Objects'
  Possible fix:
    add an instance declaration for (MethodSuffix (m0 [b0]))
  In the expression:
    defMethod' "init_tableau" (\ _ -> return $ map (^ 2) [0 .. 20])
  In the first argument of `newClass', namely
    `[defMethod' "init_tableau" (\ _ -> return $ map (^ 2) [0 .. 20])]'
  In a stmt of a 'do' block:
    clazz <- newClass
               [defMethod' "init_tableau" (\ _ -> return $ map (^ 2) [0 .. 20])]
/home/lowley/Documents/haskell/Qt/lancer-qml4.hs: line 11, column 13:
  No instance for (Monad m0) arising from a use of `return'
  The type variable `m0' is ambiguous
  Possible fix: add a type signature that fixes these type variable(s)
  Note: there are several potential instances:
    instance Monad ((->) r) -- Defined in `GHC.Base'
    instance Monad IO -- Defined in `GHC.Base'
    instance Monad [] -- Defined in `GHC.Base'
    ...plus two others
  In the expression: return
  In the expression: return $ map (^ 2) [0 .. 20]
  In the second argument of defMethod', namely
    `(\ _ -> return $ map (^ 2) [0 .. 20])'
/home/lowley/Documents/haskell/Qt/lancer-qml4.hs: line 11, column 27:
  No instance for (Num b0) arising from a use of `^'
  The type variable `b0' is ambiguous
  Possible fix: add a type signature that fixes these type variable(s)
  Note: there are several potential instances:
    instance Num Double -- Defined in `GHC.Float'
    instance Num Float -- Defined in `GHC.Float'
    instance Integral a => Num (GHC.Real.Ratio a)
      -- Defined in `GHC.Real'
    ...plus three others
  In the first argument of `map', namely `(^ 2)'
  In the second argument of `($)', namely `map (^ 2) [0 .. 20]'
  In the expression: return $ map (^ 2) [0 .. 20]
/home/lowley/Documents/haskell/Qt/lancer-qml4.hs: line 11, column 31:
  No instance for (Enum b0)
    arising from the arithmetic sequence `0 .. 20'
  The type variable `b0' is ambiguous
  Possible fix: add a type signature that fixes these type variable(s)
  Note: there are several potential instances:
    instance Enum Double -- Defined in `GHC.Float'
    instance Enum Float -- Defined in `GHC.Float'
    instance Integral a => Enum (GHC.Real.Ratio a)
      -- Defined in `GHC.Real'
    ...plus 7 others
  In the second argument of `map', namely `[0 .. 20]'
  In the second argument of `($)', namely `map (^ 2) [0 .. 20]'
  In the expression: return $ map (^ 2) [0 .. 20]

我认为这个错误很小,应该很快纠正,但我所有的尝试都失败了。

谢谢你

编辑:有关信息:

代码语言:javascript
复制
Prelude> let liste=[0..20] in map (^2) liste
[0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400]
Prelude>

我试过了:

代码语言:javascript
复制
defMethod' "init_tableau" (\_  ->
            let a=map (^2) [0..20] 
            in return a)]

使用大致相同的错误

编辑2:

代码语言:javascript
复制
Prelude> :t map (^ 2) [0..20]
map (^ 2) [0..20] :: (Enum b, Num b) => [b]

编辑3:使用此代码:

代码语言:javascript
复制
main :: IO ()
main = do
    clazz <- newClass [ 
        defMethod' "init_tableau" (\_  -> 
            return ( map (^2) [0..20] ) :: IO [Integer] )]
    ctx <- newObject clazz ()
    runEngineLoop defaultEngineConfig {
        initialDocument = fileDocument "exemple4.qml",
        contextObject = Just $ anyObjRef ctx}

我得到了这个错误:

代码语言:javascript
复制
/home/lowley/Documents/haskell/hsqml-demo-samples-0.3.4.0/qml/tableau1.hs:11:9:
    Couldn't match type `MarshalMode Integer ICanReturnTo ()'
                  with `Yes'
    In the expression:
      defMethod'
        "init_tableau"
        (\ _ -> return (map (^ 2) [0 .. 20]) :: IO [Integer])
    In the first argument of `newClass', namely
      `[defMethod'
          "init_tableau"
          (\ _ -> return (map (^ 2) [0 .. 20]) :: IO [Integer])]'
    In a stmt of a 'do' block:
      clazz <- newClass
                 [defMethod'
                    "init_tableau"
                    (\ _ -> return (map (^ 2) [0 .. 20]) :: IO [Integer])]

我还是无法解决这个问题。

EN

回答 2

Stack Overflow用户

发布于 2016-03-11 08:20:47

我还没有测试过这一点,但我怀疑这里的问题是有许多合适的monad可以使用。如果您希望与工作示例完全并行,则可以通过向return表达式添加类型注释来显式地选择使用IO作为单体:

代码语言:javascript
复制
return $ map (^2) [0..20] :: IO [Integer]
票数 1
EN

Stack Overflow用户

发布于 2016-03-18 16:19:38

Daniel Wagner的建议修复了您询问的错误。

您现在有了另一个不相关的错误:

代码语言:javascript
复制
Couldn't match type `MarshalMode Integer ICanReturnTo ()' with `Yes'

现在考虑一下类型

代码语言:javascript
复制
defMethod' :: (Typeable obj, MethodSuffix ms) => String -> (ObjRef obj -> ms) -> Member obj

您正试图将(\_ -> return ( map (^2) [0..20] ) :: IO [Integer] )作为第二个参数传递给它。

那么我们可以用forall a. a -> IO [Integer]统一ObjRef obj -> ms吗?

好吧,第一部分是统一的,因为a是免费的。但是IO [Integer]MethodSuffix的一个实例吗?遵循类型类,你会发现这要求[Integer]Marshalfollow that的一个实例,你会发现我们不能这样做。我在文档中没有看到一个很好的Marshal基本实例列表,但我在source中找到了它们。

因此,如果您尝试返回[Int]Text而不是[Integer],那么这很可能是类型检查。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35924522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档