Haskell supports overloaded values, where a single overloaded value can behave
sort of like a superposition of values each with a different type. For example, here’s a simple type class:
class Truthy t where
truthy :: Integer -> t
instance Truthy Integer where
truthy x = x -- some computation that results in an Integer
instance Truthy Bool where
truthy x = x /= 0 -- some computation that results in a Bool
Passing a number to truthy
returns an overloaded value of type Truthy t => t
. This same value can be used as an Integer
or a Bool
:
Prelude> let y = truthy 17
Prelude> if y then y else 42
17
Note that y
is used in a Bool
context (the condition) and also in a numeric
context (the then
clause, which needs to match the type of the numeric else
clause).
Haskell also uses lazy evaluation, so it isn’t surprising that the two
results of truthy 17
are computed lazily. This means that while an overloaded
value is conceptually a bundle of values of different types, only the instances that are actually used perform computation.
Are there any programming languages that have overloaded values like this but which use strict evaluation?
If so, what are the evaluation semantics of overloaded values like? Are overloaded values:
- strict (meaning that you have to perform computations for instances you’ll never use)
- non-strict (which would be inconsistent with non-overloaded values)
- something else?
7
This is a pretty bog-standard feature of many programming languages that support some kind of operator overloading. However, most languages that do so only support this for built-in types such as booleans, strings, or numeric types.
In Perl, a variable doesn’t generally have an actual type. Instead, it’s coerced to various types however the context requires it. The +
operator coerces its operands to numbers, while not
extracts a boolean representation. User-defined objects can hook into this coercion process. Example:
package Truthy {
use Scalar::Util 'looks_like_number';
# this sets up overloading
use overload '0+' => &as_num, 'bool' => &as_bool;
# ignore this constructor code
sub new {
my ($class, $value) = @_;
# this is as close as vanilla Perl gets to type checking ... bleh.
die "$value must be a number" if not looks_like_number $value;
return bless $value => $class;
}
sub as_num {
my ($self) = @_;
print "called as_numn";
return $$self;
}
sub as_bool {
my ($self) = @_;
print "called as_booln";
return $self != 0;
}
}
my $y = Truthy->new(17);
print +($y ? $y : 42), "n";
While Perl is a strict language, the coercion methods are only executed whenever the coercion is actually requested. However, the value is not cached in any way.
The Python language is similar, except that the overloading mechanism uses specially named methods such as __str__
and __int__
.
One interesting language that actually allows arbitrary coercions even between user-defined types is Scala. We can register “implicit” methods that supply conversions. They are only executed when that conversion is requested, so that these conversions end up having the same semantics as Perl’s or Python’s overloading mechanism.
class Truthy(val x: Int) { }
implicit def asInt(t: Truthy): Int = t.x
implicit def asBool(t: Truthy): Boolean = t.x != 0
val x = new Truthy(17)
val result: Int = if (x) x else 42
You may have noticed that I’m not using type classes (or their equivalents in OOP languages: interfaces, traits) anywhere. Most languages do not support return-type polymorphism. Instead, I expressed your code in terms of coercions which ends up doing the same thing, by very different means.
1