Functional dependencies in Haskell

When you have a multiparameter typeclass, by default, the type variables are considered independently. So when the type inferencer is trying to figure out which instance of class Foo a b to choose, it has to determine a and b independently, then go look check to see if the instance exists. With functional dependencies, we … Read more

Is there a Haskell equivalent of OOP’s abstract classes, using algebraic data types or polymorphism?

Yes, you are correct, you are looking for algebraic data types. There is a great tutorial on them at Learn You a Haskell. For the record, the concept of an abstract class from OOP actually has three different translations into Haskell, and ADTs are just one. Here is a quick overview of the techniques. Algebraic … Read more

How does IncoherentInstances work?

Well this is quite complicated. Lets start with the ambiguous error: <interactive>:1:1: Ambiguous type variable `b0′ in the constraint: (Arity b0) arising from a use of `arity’ Probable fix: add a type signature that fixes these type variable(s) In the expression: arity foldr In an equation for `it’: it = arity foldr Normally, without overlapping … Read more

Explicitly import instances

The inability to control imports of instances is one of the trade-offs the Haskell typeclass system makes. Here’s an example in a hypothetical Haskell dialect where you can: Foo.hs: module Foo where data Foo = FooA | FooB deriving (Eq, Ord) Bar.hs: module Bar (myMap) where import Data.Map (Map) import qualified Data.Map as Map import … Read more