2012-06-03 16 views
7

メソッドを(バイナリ)関数に実装して、関数endendunctionを区別できるようにします(a -> a)。擬似Haskellのコードのような関数の "show"の実装

何か:

instance Show (a->b) where 
    show fun = "<<Endofunction>>" if a==b 
    show fun = "<<Function>>" if a\=b 

がどのように私は2つのケースを区別することができますか?

+0

それはまったく良い「ショー」インスタンスではありません。 'isEndo ::(a-> b) - > Bool'のようなものを書いて、簡単なガードを使って適切なテキストを作成してください。 – leftaroundabout

+2

'const 3 'は、エンド機能または機能ですか? –

+1

@DanielWagner:はい。 – Ashe

答えて

15

あなたはいくつかの拡張機能を有効にする必要があります

{-# LANGUAGE OverlappingInstances, FlexibleInstances #-} 
module FunShow where 

instance Show ((->) a a) where 
    show _ = "<<Endofunction>>" 

instance Show ((->) a b) where 
    show _ = "<<Function>>" 

あなたはOverlappingInstancesを必要とするインスタンスa -> bもendofunctionsと一致するので、オーバーラップがあります、そしてあなたは、インスタンス宣言で型変数がある言語の標準的な任務のでFlexibleInstancesを必要とするので、異なる。

*FunShow> show not 
"<<Endofunction>>" 
*FunShow> show fst 
"<<Function>>" 
*FunShow> show id 
"<<Endofunction>>"