2017-10-25 8 views
7

なぜGHCiの私は、パターンマッチングによって構築この機能matchIntの型シグネチャにequality type制約をリストしない:単純なデータコンストラクタを使用する場合とは対照的にパターンマッチングで構築された関数は、Eq型制約を持ちますが、データコンストラクタを使用しているときはなぜですか?

$ ghci 
GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help 
Prelude> :{ 
Prelude| matchInt 1 = 3 
Prelude| matchInt _ = 4 
Prelude| :} 
Prelude> matchInt 1 
3 
Prelude> matchInt 22 
4 
Prelude> :t matchInt 
matchInt :: (Eq a, Num a, Num p) => a -> p 

を、全く等価型制約は存在しません。

$ ghci 
GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help 
Prelude> data Z = Y Int 
Prelude> :{ 
Prelude| matchDataInt (Y 1) = 3 
Prelude| matchDataInt _ = 4 
Prelude| :} 
Prelude> matchDataInt (Y 1) 
3 
Prelude> matchDataInt (Y 22) 
4 
Prelude> :t matchDataInt 
matchDataInt :: Num p => Z -> p 

実際Zのインスタンスがを比較することはできません。

Prelude> Y 22 == Y 33 
<interactive>:11:1: error: 
    • No instance for (Eq Z) arising from a use of ‘==’ 
    • In the expression: Y 22 == Y 33 
     In an equation for ‘it’: it = Y 22 == Y 33 

だから、もう一度、なぜ型制約としてmatchInt機能一覧平等ではなく、機能matchDataIntはいますか?

このquestionは関連しています。しかし、matchIntの平等テストが必要な場合は、matchDataIntにはなぜそれが必要ではないのですか? matchIntmatchDataIntは、動作するパターンマッチングで1に対してテストする必要がありますか?それは、具体的Intに一致する、およびInt sがすでにEqインスタンスを持っているので、

答えて

関連する問題