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
にはなぜそれが必要ではないのですか? matchInt
とmatchDataInt
は、動作するパターンマッチングで1に対してテストする必要がありますか?それは、具体的Int
に一致する、およびInt
sがすでにEq
インスタンスを持っているので、