2016-11-16 2 views
0

のインスタンスであるとき、エラー「『ショー』機能を見つけることができません」:debugFunction getDebugTreeパラメータは、次のエラーがトリガされたショー

data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Eq, Show) 

getDebugTree :: Tree Int 
getDebugTree = (Node 1 (Empty) (Node 2 Empty Empty)) 

debugFunction :: Show a => a -> Tree a -> Bool 
debugFunction _ _ = True 

を実行しているとき、私が読んだ

ERROR - Cannot find "show" function for: 
*** Expression : debugFunction getDebugTree 
*** Of type : Tree (Tree Int) -> Bool 

reverse []

ERROR: Cannot find "show" function for: * expression : reverse [] * of type : [a]

The top-level system for Hugs can only print values which belong to a type which can be shown, that is a type which belongs to the Show class. Now, the list type is an instance of Show, so what is wrong with reverse [] (which evaluates to [])? The problem is that the type of [] is polymorphic: [] :: [a] for all a. Not knowing a Hugs refuses to print a value of type [a]. Note that this behaviour applies to all polymorphic values. Given the definition

data Tree a = Empty | Node (Tree a) a (Tree a)

we have, on evaluating

Empty

the error message

ERROR: Cannot find "show" function for: * expression : Empty * of type : Tree a

Functions can be shown, but not very helpfully; printing any function results in

<< function>>

こと

私が知っている限り、ハグスはaを知らないので、それを印刷することを拒否します(ブールを印刷していますが?)だから私はショーのインスタンスにしようとしましたが、まだ動作しません、ここで何が問題なのですか?

答えて

3

debugFunctionを見てください。

debugFunction :: Show a => a -> Tree a -> Bool 

debugFunctionTree aである第二その2つの引数を期待。あなたはただそれを渡しているだけです。型チェッカーは、Tree Intを最初の引数として渡しているとみなし、a ~ Tree Intを推測します。したがってdebugFunction getDebugTreeTree (Tree Int)の第2引数を待つ関数です。です。

debugFunction getDebugTree :: Tree (Tree Int) -> Bool 

私はあなたが最初のものとして使用するIntを考え出す必要があることを意味debugFunctionの2番目の引数としてgetDebugTreeを使用することを目的と疑います。

debugFunction 0 getDebugTree :: Bool 

不足しているShowインスタンスに関するエラーメッセージは、REPL自体から発生します。 debugFunction getDebugTreeの結果を印刷しようとしていますが、機能を表示できないため、印刷できません。

+0

Ahh、ありがとう、私はまだHaskellに慣れています。 – Aeron

+1

@Aeron GHCiは、しばしばより良いエラーを生成します.HugsはもはやGHCの間に開発されていません。確かに、GHCiエラーには考えられる原因が含まれています:「(おそらく十分な引数に関数を適用していないのでしょうか?) – chi

関連する問題