A memo on `Haskell Type Equality Constraints’

Yet another thing to know in GHC type reasoning

Shuji Narazaki
text-is-saved
3 min readApr 25, 2017

--

source:

概要

は違うという話。つい上のように書いてしまって途方に暮れていた経験多数なのですが、なぜ下の方法だといいのでしょう。

なぜなら、型のマッチングはheadのマッチング後にcontextのマッチングという順序で行われるから:

What happens instead is that, when it sees myValue being used as a pair, GHC will first select the instance MyClass (a, b) without having even examined the constraints in the context. Only after it's already committed to using the instance MyClass (a, b) will it then examine the context and try to satisfy the MyClass a and MyClass b constraints.

この型を持つ変数の利用時に、二つ目の型パラメータに対応するフィールドを全く触らない場合では、そのsiteでは二つ目の型パラメータは未束縛な型(インスタンス)となる。これはもちろん一つ目の型パラメータと二つ目の型パラメータとが同一であるインスタンスにはマッチしない。従って利用可能なインスタンスがないというエラーになってしまう。もし型等価制約を持つinstanceでインスタンス化してあれば、この状況でC a bがまず選ばれ、その後で a ~ b という制約を付加して型推論が行われ、受理されるということのようだ。

なるほど。呼び出しの文脈では forall b . C a b としか型が定まらないのか。thePairの例でいうと fst(a, b) -> a な型を持っているのだからその引数は(a, b) な型を持てることがまず必要で(型変数が型にinstantiateされるのとは違う。変数(a, b)が変数(a, a)にunificationできるかといえばそれは無理)、その後でさらなる推論: a , b それぞれが問題ないかどうかを判定するということのようだ。

--

--

Shuji Narazaki
text-is-saved

Studying SAT solvers and symbolic computation (type and logic). Being into 円城塔, Greg Egan, Stephen Colbert, 酒見賢一, say a Sci. Fi. person.