没有(Show b0)使用'print'类型变量`b0'很暧昧

时间:2017-03-15 12:02:05

标签: haskell

我试图通过学习你是一个很好的Haskell来完成应用实例!书。

我的代码在这里:

class (Functor f) => Applicative f where  
    pure :: a -> f a  
    (<*>) :: f (a -> b) -> f a -> f b

instance Applicative Maybe where  
    pure = Just  
    Nothing <*> _ = Nothing  
    (Just f) <*> something = fmap f something  

main = do 
    print $ Just (+3) <*> Just 9 
    print $ pure (+3) <*> Just 10
    print $ pure (+3) <*> Just 9 
    print $ Just (++"hahah") <*> Nothing  
    print $ Nothing <*> Just "woot"

我收到了此错误消息:

No instance for (Show b0) arising from a use of `print'
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 Show Double -- Defined in `GHC.Float'
  instance Show Float -- Defined in `GHC.Float'
  instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
    -- Defined in `GHC.Real'
  ...plus 23 others
In the expression: print
In a stmt of a 'do' block: print $ Nothing <*> Just "woot"
In the expression:
  do { print $ Just (+ 3) <*> Just 9;
       print $ pure (+ 3) <*> Just 10;
       print $ pure (+ 3) <*> Just 9;
       print $ Just (++ "hahah") <*> Nothing;
       .... }

为什么会失败,我该如何解决?

1 个答案:

答案 0 :(得分:3)

问题出在最后print行。基本上,您使用String -> b应用函数<*>。只是碰巧你使用Nothing来包装函数,所以最后没有函数。该应用程序的结果是Maybe b b,其中a是任意的,但只有那些Show Maybe a的实例为b生成实例。但请记住,您有一个Show,它不一定是print的实例。但是Show要求其参数是b的实例。由于您无法证明ShowMaybe b的实例,因此您无法证明Showprint的实例,因此Maybe b不适用于该值类型为(Nothing :: Maybe (String -> Int)) <*> ...

要使它消失,您必须指定您想要包含的确切类型的函数。所以结果有一些已知的类型:

print

或者你可以只提供作为print (... :: Maybe Int) 的输入传递的整个表达式的类型:

   UPDATE your_tablename 
   SET SetType = 'CATEGORY_'+ CONVERT(NVarchar(2),SetType)