NAME
Amb - non-deterministic operators
SYNOPSIS
use Amb;
if ( amb(1,0)) {
print "failure";
die;
} else {
print "success"
}
will print 'failure' and then 'success'.
DESCRIPTION
There exist two kinds of non-deterministic operators, 'angelic' and 'demonic', that accept two parameters, and return one of them depending whether the result will lead to failure (die
, in perl world) or success. Angelic operators will return the parameter that won't lead to the branch that dies. Demonic operators, on the contrary, return the parameter that leads to the branch that dies.
The non-deterministic operators are usually implemented using continuations. Perl5 lacks these, so this implementation hacks the optree to achieve the result. The side effect of this is that branches won't backtrack, and can be thought of as non-deterministic operators implemented with goto
.
amb(arg1,arg2)
amb
, the only operator exported by default, is an angelic operator. Returns either arg1 or arg2, depending on which one won't lead do die
call. If all branches lead to die
call, dies itself.
angelic(arg1,arg2)
Same as amb
demonic(arg1,arg2)
Returns the argument that will lead to die
.
USAGE RESTIRICTIONS
Since amb()
is implemented with hacking op-tree, there are currently a number of untested calling combinations, that might fail or even coredump. For example, calling amb()
like
my $a = amb(@a);
if ( $a) ...
won't work. The only tested (and yet, up to a point) calling sequence is
if ( amb(@a) { ... } else { ... }
Other call styles are obliviously untested, so beware.
INSTALLATION
The module requires latest development version of B::Generate, which doesn't build on MSWin32. Apply patch from http://rt.perl.org/rt3/Public/Bug/Display.html?id=56536 and recompile perl.
LICENSE AND COPYRIGHT
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
Dmitry Karasik, <dmitry@karasik.eu.org>.