用于“显示”的Haskell模糊类型变量? [英] Haskell ambiguous type variable for "show"?

查看:138
本文介绍了用于“显示”的Haskell模糊类型变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$> 

c $ c> data MyType0 a = Tong1 a | Tong2 a导出显示
数据MyType1 a = Cons1 a | Cons2 a | Cons3 | Cons4派生Show
数据MyType2 a = MyType2 a派生Show

fun((Cons2 s):t:ts)(symseq,MyType2 msg)=(symseq,MyType2(msg ++ [ 你提供了错误的符号:++(show t)++请提供符号:++(show Cons3)]))
--fun((Cons2 s):t:ts)(symseq, MyType2 msg)=(symseq,MyType2(msg ++ [您提供了错误的符号:++(show t)++请提供符号:]))
fun _ syms = syms

ghci错误消息:

  showerr.hs:6:148:
使用`show'引起的约束中的模糊变量`a0':
(显示a0)
可能的修正:添加修改这些类型变量的类型签名
在`(++)'的第二个参数中,即`(show Cons3)'
在`(++)'的第二个参数中,即
`请提供符号:++(show Cons3)'
在`(++)'的第二个参数中,即
`(show t)++Pleas e提供符号:++(显示Cons3)'
失败,模块加载:无。



注意注释部分不会出现此错误。请解释为什么这是如此错误。



原始邮件保留在下面。



网站。我知道模糊的类型错误可以用于读取功能,但在这里我也可以获得它的显示功能。

code:

  main =运行<  -  getLine 
val< - getLine
运行
len - >打印。显示 。 len $(读取val)
rev - >打印。显示 。 rev $(读取val)
_ - > putStr错误的选项

rev :: [a] - > [a]
rev = foldl(flip(:))[]

len :: [a] - > Int
len = foldl(\ac _ - > ac + 1)0

  ideone_x0cMx.hs:4:46:
含糊不清的类型变量` a0'约束:
(读取a0)由于使用`read'
引起的可能的修复:添加一个修复这些类型变量的类型签名
在第二个参数`($)',即`(读取val)'
在表达式中:print。显示 。 len $(read val)
在另一种情况下:len - >打印。显示 。 len $(读取val)

ideone_x0cMx.hs:5:46:
在约束中含糊不清的类型变量`a1':
(读取a1)阅读'在ideone_x0cMx.hs:5:46-49
(显示a1)在ideone_x0cMx.hs:5:32-35使用`show'时读取'
可能的修复:添加一个类型签名,修复这些类型变量
在`($)'的第二个参数中,即`(read val)'
在表达式:print中。显示 。 rev $(read val)
在另一种情况下:rev - >打印。显示 。 rev $(读取val)
失败,模块加载:无。


解决方案

在子表达式中Cons3),没有上下文来确定 MyType1 的类型参数。



Cons3 是任何 MyType1 a 的构造函数,并调用 show >上限制 a Show 的一个实例,但除此之外,什么都不能推断出来。因此,类型变量是不明确的,并且由于没有数字约束,因此不能默认(除非启用 ExtendedDefaultRules )。

如果你写了

  show(Cons3`asTypeOf` t)

或 - 例如 -

  show(Cons3 :: MyType1 String)

there。


Please see this newly written code now, there is no "read" used, and still I get error on ambiguous "show":

data MyType0 a = Tong1 a | Tong2 a  deriving Show
data MyType1 a = Cons1 a | Cons2 a | Cons3 | Cons4 deriving Show
data MyType2 a = MyType2 a deriving Show

fun ((Cons2 s):t:ts) (symseq, MyType2 msg) = (symseq, MyType2 (msg ++ ["You provided wrong symbol: " ++ (show t) ++ " Please provide symbol: " ++ (show Cons3) ]))
--fun ((Cons2 s):t:ts) (symseq, MyType2 msg) = (symseq, MyType2 (msg ++ ["You provided wrong symbol: " ++ (show t) ++ " Please provide symbol: " ]))
fun _ syms                                  = syms

The ghci error msg:

showerr.hs:6:148:
    Ambiguous type variable `a0' in the constraint:
      (Show a0) arising from a use of `show'
    Probable fix: add a type signature that fixes these type variable(s)
    In the second argument of `(++)', namely `(show Cons3)'
    In the second argument of `(++)', namely
      `" Please provide symbol: " ++ (show Cons3)'
    In the second argument of `(++)', namely
      `(show t) ++ " Please provide symbol: " ++ (show Cons3)'
Failed, modules loaded: none.

note that the commented part doesn't give this error. please explain why this is so error.

original message is kept below.

I got the following code on a site. I know ambiguous type error can come for the "read" function but here i get it for the "show" function too. i find it strange and not understand.

code:

main = do run <- getLine
          val <- getLine
          case run of
              "len" -> print . show . len $ (read val)
              "rev" -> print . show . rev $ (read val)
              _ -> putStr "wrong option"

rev :: [a] -> [a]
rev = foldl (flip (:)) []

len :: [a] -> Int
len = foldl (\ac _ -> ac + 1) 0

When i load it in ghci i get error.

ideone_x0cMx.hs:4:46:
    Ambiguous type variable `a0' in the constraint:
      (Read a0) arising from a use of `read'
    Probable fix: add a type signature that fixes these type variable(s)
    In the second argument of `($)', namely `(read val)'
    In the expression: print . show . len $ (read val)
    In a case alternative: "len" -> print . show . len $ (read val)

ideone_x0cMx.hs:5:46:
    Ambiguous type variable `a1' in the constraints:
      (Read a1) arising from a use of `read' at ideone_x0cMx.hs:5:46-49
      (Show a1) arising from a use of `show' at ideone_x0cMx.hs:5:32-35
    Probable fix: add a type signature that fixes these type variable(s)
    In the second argument of `($)', namely `(read val)'
    In the expression: print . show . rev $ (read val)
    In a case alternative: "rev" -> print . show . rev $ (read val)
Failed, modules loaded: none.

解决方案

In the subexpression (show Cons3), there is no context to determine the type parameter of MyType1.

Cons3 is a constructor for any MyType1 a, and calling show on it restricts a to an instance of Show, but beyond that, nothing can be inferred. So the type variable is ambiguous, and since there is no numeric constraint, it cannot be defaulted (unless you enable ExtendedDefaultRules).

The type variable could be fixed if you write

show (Cons3 `asTypeOf` t)

or - for example -

show (Cons3 :: MyType1 String)

there.

这篇关于用于“显示”的Haskell模糊类型变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆