Haskell 没有 Show 的实例

6

这是我的代码:

type Niveles = (Int, Int, Int)
type Persona = (String, Niveles)    
type Pocion = (String, [(String, Int, [Efectos])])
type Ingredientes = [(String, Int, [Efectos])]
type Efectos = Niveles -> Niveles

aplicar3 f (a,b,c) = (f a, f b, f c)

invertir3 (a,b,c) = (c,b,a)

fst3 (a,_,_) = a
snd3 (_,b,_) = b
trd3 (_,_,c) = c

personas = [("Harry",(11, 5, 4)), ("Ron",(6,4,6)), ("Hermione",(8,12,2)), ("Draco",(7,9,6))]

f1 (ns,nc,nf) = (ns+1,nc+2,nf+3)
f2 = aplicar3 (max 7)
f3 (ns,nc,nf)
    | ns >= 8 = (ns,nc,nf+5)
    | otherwise = (ns,nc,nf-3)

misPociones :: [Pocion]
misPociones = [
    ("Felix Felices",[("Escarabajos Machacados",52,[f1,f2]),("Ojo de Tigre Sucio",2,[f3])]),
    ("Multijugos",[("Cuerno de Bicornio en Polvo",10, [invertir3, (\(a,b,c) -> (a,a,c))]),("Sanguijuela hormonal",54,[(aplicar3 (*2)), (\(a,b,c) -> (a,a,c)) ])]),
    ("Flores de Bach",[("Orquidea Salvaje",8,[f3]), ("Rosita",1,[f1])])]


efectosDePocion pocion = map trd3 (elementos pocion)

elementos :: Pocion -> Ingredientes
elementos (_, ingredientes) = ingredientes

我在最后一段代码中遇到了问题,当我尝试使用函数“elementos”时:

elementos ("Felix Felices",[("Escarabajos Machacados",52,[f1,f2]),("Ojo de Tigre Sucio",2,[f3])])

我收到了一个错误信息:

<interactive>:311:1:
    No instance for (Show ((Int, Int, Int) -> (Int, Int, Int)))
      arising from a use of `print'
    Possible fix:
      add an instance declaration for
      (Show ((Int, Int, Int) -> (Int, Int, Int)))
    In a stmt of an interactive GHCi command: print it

有人可以向我解释一下吗?我该如何修复它?

2个回答

14
简而言之,该函数的求值是正确的 - 问题出在在ghci中对其求值。
你的函数调用返回一个Ingredientes类型的值,它是[(String, Int, [Efectos])]类型的同义词。 ghci试图将返回值打印到控制台上,因此它试图在其上调用show。但是,Efectos是一个函数的同义词。 ghci告诉你它不知道如何显示一个函数,因为它不是类型类Show的成员:No instance for (Show ((Int, Int, Int) -> (Int, Int, Int)))
这可能不会影响你 - 在ghci中求值任何函数都会得到类似的错误。

5
请在您的.hs文件中添加以下代码。
instance Show (a -> b) where
         show a= "funcion"

现在 ghci 将能够打印“funcion”(函数)。
祝你好运!

1
这段代码解决了问题,但是@Benesh的答案绝对更有趣——它告诉我们为什么会发生这种情况。 - mgarciaisaia
1
虽然这样做可以解决问题,但请不要真的这样做。这是一个孤立实例,本身就很糟糕,而且完全抛弃了它的参数。这有可能破坏现有的代码。其他人也可能没有意识到这个实例在范围内,并惊讶于字符串“funcion”随机出现。如果您需要此功能,则使用newtype Func a b = Func(a-> b)或类似方法,并为Show提供单独的实例。 - Lazersmoke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接