Algebraic Data Types for perl6, inspired by Haskell
Switch branches/tags
Clone or download
timo Merge pull request #5 from JJ/master
Adds `use` and semicolon
Latest commit a423ac7 Nov 21, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib fixes for newer perl6 semantics Jan 23, 2016
logotype added a superb logo Mar 24, 2013
t fixes for newer perl6 semantics Jan 23, 2016
.travis.yml Changes to zef Nov 19, 2017
META6.json Add mandatory "perl" META field Oct 26, 2016 Adds `use` and semicolon Nov 19, 2017

Algebraic Data Types

This module implements algebraic data types inspired by the Haskell syntax. See below for limitations of the current implementation.

Here's a code example for you:

use ADT; 

# define the algebraic data type
my $adt = q{Tree = Branch Tree left, Tree right | Leaf Str storage};
my %res = create_adt($adt);

# capture the Tree class
my \Tree = %res<Tree>;

# create a tree using named parameters
my $t =
# pretty-print the tree
say $t.gist;

# create a tree using positional arguments
my $t2 =
say $t2.gist;

# capture the subtypes to pattern-match in a given block.
my \Branch = %res<Branch>;
my \Leaf = %res<Leaf>;

# map over a whole tree changing all storages.
sub treemap($t, *&code) {
    given $t {
        when Branch { return$t.left, &code), treemap($t.right, &code)) }
        when Leaf { return$ }

# multiply every leaf node by 10
say treemap($t2, * * 10).gist;


Compile-time availability

Since rakudo doesn't yet allow the EXPORT sub of a package to return a hash of things to export, the ADT cannot be used to define multi subs, because the symbols are not available at compile time.

When EXPORT works, code like this will be possible:

use ADT "data Tree = Branch Tree left, Tree right | Leaf Str storage";
multi sub treemap(Branch $t, *&c) { ... }
multi sub treemap(Leaf $l, *&c) { ... }

Compile-time checks

One of the neat features of ADT is that the compiler can ensure, that the program is prepared to deal with every defined case (Branch or Leaf, Just or Nothing, ...). A macro can probably be devised to implement such a compile-time check.

"is parsed" macros

When Rakudo gets support for "is parsed" macros, definitons of ADT can move from the "use" line into the mainline of the code, so that they look tidyer and the programmer can put comments nearby etc.