次のコードdoesntのコンパイル:Haskellのクラスの依存関係
class Foo f where
getInt :: f -> Int
class Bar b where
getFooFromBar :: Foo f => b -> f
someFunction :: Bar b => b -> Int
someFunction bar = getInt $ getFooFromBar bar
エラーがCould not deduce (Foo f) arising from a use of 'getInt' from the context (Bar b)
である私は、次のように私はBar
のクラスを変更することでエラーを修正することができます知っている:
class Foo f => Bar f b where
getFooFromBar :: b -> f
しかし、私がしなかった場合は、のすべてのインスタンス署名にf
を追加する必要があります。
Foo f
の制約は、クラス全体ではなく、getFooFromBar
のシグネチャだけにすることでこれを行う方法はありますか?
私はあなたがgetFooFromBar' 'のタイプを再考をお勧めします - それは'あいまいなために態勢を整えて見えます型変数 'エラー。 – Alec
@Alec完全なエラーは実際には '型変数 'f0'があいまいです。 – duplode
[Haskell Type-Ambiguityのケースを理解する]の可能な複製(http://stackoverflow.com/questions/21220655/understanding-a-case-of-haskell-type-ambiguity) – Cactus