Haskell “如何打印函数的结果”;加上「;“来自课堂”;加上「;从「;“类型功能的乐趣”;

Haskell “如何打印函数的结果”;加上「;“来自课堂”;加上「;从「;“类型功能的乐趣”;,haskell,typeclass,type-families,Haskell,Typeclass,Type Families,下面是这里的代码 这是我正在尝试运行的代码: main = print $ show (add 1 1) 结果是: No instance for (Show (SumTy a0 b0)) arising from a use of `show' Possible fix: add an instance declaration for (Show (SumTy a0 b0)) In the second argument of `($)', namely `sho

下面是这里的代码

这是我正在尝试运行的代码:

main = print $ show (add 1 1)
结果是:

No instance for (Show (SumTy a0 b0))
      arising from a use of `show'
    Possible fix: add an instance declaration for (Show (SumTy a0 b0))
    In the second argument of `($)', namely `show (add 1 1)'
    In the expression: print $ show (add 1 1)
    In an equation for `main': main = print $ show (add 1 1)
我尝试过几件事,比如到处放“数据”:

结果1

Not a data constructor: `a'
结果2(删除“实例(数量a)”后)

如添加一些功能:

class Add a b where
    type SumTy a b
    add :: a -> b -> SumTy a b
    s :: SumTy a b -> String

instance Add Integer Double where
    type SumTy Integer Double = Double
    add x y = fromIntegral x + y
    s (SumTy _ x) = show x

main = print $ show (s (add 1 2.0) )
因此:

Not in scope: data constructor `SumTy'

正如你可能已经注意到的那样,我陷入困境,因此任何帮助对我来说都是无价的。:)

问题在于没有足够的上下文来确定要使用的
Add
实例,因此无法确定结果的类型。由于ghc不知道使用哪种类型,它报告了最常见的问题,因此对于通用的
SumTy a b
,没有
Show
实例:

No instance for (Show (SumTy a0 b0))
      arising from a use of `show'
    Possible fix: add an instance declaration for (Show (SumTy a0 b0))
    In the second argument of `($)', namely `show (add 1 1)'
    In the expression: print $ show (add 1 1)
    In an equation for `main': main = print $ show (add 1 1)
不过,这里并不需要建议的“可能的修复”。您需要指定要添加的参数类型,以便确定要使用的实例,从而确定结果类型:

*TyFun> show (add (1 :: Int) (1 :: Int))
"2"
*TyFun> show (add (1 :: Integer) (1 :: Integer))
"2"
*TyFun> show (add (1 :: Integer) (1 :: Double))
"2.0"
*TyFun> show (add (1 :: Integer) (1 :: Float))

<interactive>:7:1:
    No instance for (Show (SumTy Integer Float))
      arising from a use of `show'
    Possible fix:
      add an instance declaration for (Show (SumTy Integer Float))
    In the expression: show (add (1 :: Integer) (1 :: Float))
    In an equation for `it':
        it = show (add (1 :: Integer) (1 :: Float))

<interactive>:7:7:
    No instance for (Add Integer Float) arising from a use of `add'
    Possible fix: add an instance declaration for (Add Integer Float)
    In the first argument of `show', namely
      `(add (1 :: Integer) (1 :: Float))'
    In the expression: show (add (1 :: Integer) (1 :: Float))
    In an equation for `it':
        it = show (add (1 :: Integer) (1 :: Float))
*TyFun>show(添加(1::Int)(1::Int))
"2"
*TyFun>show(添加(1::Integer)(1::Integer))
"2"
*TyFun>show(添加(1::整数)(1::双精度))
"2.0"
*TyFun>show(添加(1::整数)(1::浮点))
:7:1:
没有(显示(总和整数浮点))的实例
因使用“show”而引起的
可能的解决方案:
为(Show(SumTy整数浮点))添加实例声明
在表达式中:show(add(1::Integer)(1::Float))
在“it”的方程式中:
it=show(add(1::Integer)(1::Float))
:7:7:
没有因使用“Add”而产生的(Add Integer Float)实例
可能的修复方法:为添加实例声明(添加整数浮点)
在show的第一个参数中,即
`(添加(1::整数)(1::浮点))'
在表达式中:show(add(1::Integer)(1::Float))
在“it”的方程式中:
it=show(add(1::Integer)(1::Float))

回答得很好。非常感谢你。也许你可以帮我写这个“s”函数(从我上面的代码)也?我不懂如何写“s”的签名。有可能吗?@panurg,在您的实例声明中,您是针对
SumTy
进行模式匹配的,这是不正确的:
SumTy
只是类型别名,而不是数据构造函数(这是编译器告诉您的)。您在实例中定义了
SumTy
等于
Double
,因此在该实例中的任何地方,类型
SumTy Integer Double
的使用都等同于普通
Double
。只要用
x
替换你的
(SumTy\ux)
,它就会起作用。@VladimirMatveev我不知道如何使用你在帖子中描述的“s”。我在尝试:
s(2.0::Double)
我得到了这样一个结果:在
s的第一个参数中,type
SumTy a0 b0'与
Double'不匹配,即
(2.0::Double)'你能解释一下我应该如何使用它吗?@panurg,我想你是对的。我确信这会起作用,但我试着运行它,得到的正是你的错误。我不知道为什么会发生这种情况,也许有更了解情况的人会回答。@panurg我以为你试图添加
s
,因为你在调用
add
和打印结果时遇到了问题。对于关联的类型同义词
type SumTy a b
,您不能使用它,因为无法从
SumTy a b
确定类型
a
b
,不同的类型对可以映射到相同的
SumTy
(它们已经这样做了,
Integer
Double
两个顺序都映射到
Double
),因此编译器无法知道要使用哪个实例。
No instance for (Show (SumTy a0 b0))
      arising from a use of `show'
    Possible fix: add an instance declaration for (Show (SumTy a0 b0))
    In the second argument of `($)', namely `show (add 1 1)'
    In the expression: print $ show (add 1 1)
    In an equation for `main': main = print $ show (add 1 1)
*TyFun> show (add (1 :: Int) (1 :: Int))
"2"
*TyFun> show (add (1 :: Integer) (1 :: Integer))
"2"
*TyFun> show (add (1 :: Integer) (1 :: Double))
"2.0"
*TyFun> show (add (1 :: Integer) (1 :: Float))

<interactive>:7:1:
    No instance for (Show (SumTy Integer Float))
      arising from a use of `show'
    Possible fix:
      add an instance declaration for (Show (SumTy Integer Float))
    In the expression: show (add (1 :: Integer) (1 :: Float))
    In an equation for `it':
        it = show (add (1 :: Integer) (1 :: Float))

<interactive>:7:7:
    No instance for (Add Integer Float) arising from a use of `add'
    Possible fix: add an instance declaration for (Add Integer Float)
    In the first argument of `show', namely
      `(add (1 :: Integer) (1 :: Float))'
    In the expression: show (add (1 :: Integer) (1 :: Float))
    In an equation for `it':
        it = show (add (1 :: Integer) (1 :: Float))