0

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?
  • They do have strict versions of Haskell. – PyRulez Sep 23 '14 at 20:23
  • 3
    If there isn't, it's not because this feature requires lazy evaluation. Just treat a polymorphic value as a function which is always implicitly called (with the appropriate typeclass dictionary). That's pretty much what Haskell does AFAIK. The only difference lazy/eager evaluation makes is whether you instantly run that call to completion or don't. – user7043 Sep 23 '14 at 20:34
  • @delnan oops! I was thinking instance Bool Truthy not the other way around, thanks for correcting me! – Jimmy Hoffa Sep 23 '14 at 20:56
  • @delnan: exactly, a value is isomorphic to a nullary constant function, you can just treat it as such. This is basically just overloading based on return types combined with typeclasses. Both features do exist in strict languages (typeclasses can easily be modelled as implicit values and implicit arguments in Scala, for example, and C++ can be tricked into return-type overloading, also the + operator in Java seems to be overloaded on return-type). – Jörg W Mittag Sep 23 '14 at 21:21
1

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_num\n";
        return $$self;
    }

    sub as_bool {
        my ($self) = @_;
        print "called as_bool\n";
        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.

  • Yes, I've been thinking for a while that implicit conversions and overloaded values are equivalent, or at least very closely related. That said, Python does not have either overloaded values or implicit conversion. If you pass a value to a function that expects an int the language will not implicitly invoke int (or __int__). – Laurence Gonsalves Sep 24 '14 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.