Type Inference in Perl 5

Ovid on 2006-02-07T17:53:49

Devel::TypeCheck is one of those projects I desperately want to see gain some traction, but it hasn't. Part of the reason for this is the following code from B::TypeCheck (which, believe it or not, is formatted for clarity):

# Fully qualified terminal types
our $IO
  = Devel::TypeCheck::Type::Mu->new( Devel::TypeCheck::Type::Io->new() );
our $PV = Devel::TypeCheck::Type::Mu->new(
    Devel::TypeCheck::Type::Kappa->new(
        Devel::TypeCheck::Type::Upsilon->new(
            Devel::TypeCheck::Type::Pv->new()
        )
    )
);
our $IV = Devel::TypeCheck::Type::Mu->new(
    Devel::TypeCheck::Type::Kappa->new(
        Devel::TypeCheck::Type::Upsilon->new(
            Devel::TypeCheck::Type::Nu->new(
                Devel::TypeCheck::Type::Iv->new()
            )
        )
    )
);
our $DV = Devel::TypeCheck::Type::Mu->new(
    Devel::TypeCheck::Type::Kappa->new(
        Devel::TypeCheck::Type::Upsilon->new(
            Devel::TypeCheck::Type::Nu->new(
                Devel::TypeCheck::Type::Dv->new()
            )
        )
    )
);

OK, got that? Just to give you a heads up, Devel::TypeCheck::Type::Kappa, according to the docs, is a type representing a scalar. Using this information and judicious use of the aliased module, we can do this:

use aliased 'Devel::TypeCheck::Type::Kappa', 'Scalar';

Which will allow us to do this:

# Fully qualified terminal types
our $IO = All->new( IO->new() );
our $PV = All->new( Scalar->new( Printable->new( String->new() ) ) );
our $IV = All->new(
    Scalar->new( Printable->new( Number->new( Integer->new() ) ) ) );
our $DV
  = All->new( Scalar->new( Printable->new( Number->new( Double->new() ) ) ) );

Now that's still tough to see exactly where this is going, but it's much more readable. I suspect that if this is the code we saw, some intrepid folks might actually submit patches to get Devel::TypeCheck to work on something other than 5.8.1.

Mind you, type inference really can't work on Perl 5, but we might get an 80% solution. The work is on the CPAN (it was a Summer of Code project) and I'd love to see where this could take us.