Ad-hoc polymorphism (aka “constrained polymorphism”) is polymorphism that applies one or more type class constraints to what would’ve otherwise been a parametrically polymorphic type variable. Here, rather than representing a uniformity of behavior across all concrete applications, the purpose of ad-hoc polymorphism is to allow the functions to have different behavior for each instance. This ad-hoc-ness is constrained by the types in the type class that defines the methods and Haskell’s requirement that type class instances be unique for a given type. For any given combination of a type class and a type, such as Ord and Bool, there must only be one unique instance in scope. This makes it considerably easier to reason about type classes. See the following example for a disambiguation:
(+) :: Num a => a -> a ->
The + function is using ad-hoc polymorphism to require the Num type class.
c' :: a -> a -> a
This function is not ad-hoc polymorphic or constrained. It’s parametrically polymorphic in the variable a.
Ad-hoc polymorphism (aka “constrained polymorphism”) is polymorphism that applies one or more type class constraints to what would’ve otherwise been a parametrically polymorphic type variable. Here, rather than representing a uniformity of behavior across all concrete applications, the purpose of ad-hoc polymorphism is to allow the functions to have different behavior for each instance. This ad-hoc-ness is constrained by the types in the type class that defines the methods and Haskell’s requirement that type class instances be unique for a given type. For any given combination of a type class and a type, such as
Ord
andBool
, there must only be one unique instance in scope. This makes it considerably easier to reason about type classes. See the following example for a disambiguation:The
+
function is using ad-hoc polymorphism to require theNum
type class.This function is not ad-hoc polymorphic or constrained. It’s parametrically polymorphic in the variable
a
.