package DX::RuleSet;
use Moo;
+use DX::Op::SetupScope;
use DX::Op::CallRule;
use DX::Op::MemberOf;
use DX::Op::ApplyConstraint;
use DX::Op::ProposeAction;
use DX::Op::Materialize;
use DX::Op::Prop;
+use DX::Op::Exists;
use List::Util qw(reduce);
has rules => (is => 'ro', default => sub { {} });
sub _make_rule {
my ($self, $vars, @body) = @_;
- my $head = $self->_expand_and_link(DX::Op::Return->new, @body);
- [ $vars, $head ];
+ my $head = $self->expand_and_link(DX::Op::Return->new, @body);
+ DX::Op::SetupScope->new(arg_names => $vars, next => $head);
}
-sub _expand_and_link {
+sub expand_and_link {
my ($self, $last, @body) = @_;
return reduce { $b->but(next => $a) }
$last,
- reverse map $self->expand(@$_), @body;
+ reverse map $self->expand($_), @body;
}
sub expand {
- my ($self, $type, @rest) = @_;
- if ($self->can(my $expand_meth = "_expand_op_${type}")) {
- return $self->$expand_meth(@rest);
+ my ($self, $thing) = @_;
+ if (ref($thing) eq 'ARRAY') {
+ my ($type, @rest) = @$thing;
+ if ($self->can(my $expand_meth = "_expand_op_${type}")) {
+ return $self->$expand_meth(@rest);
+ }
+ return $self->_expand_call(@$thing);
}
- return $self->_expand_call($type, @rest);
+ return $thing;
}
sub _expand_call {
my ($self, @contents) = @_;
my $cut = DX::Op::Cut->new(next => DX::Op::Backtrack->new);
DX::Op::Not->new(
- body => $self->_expand_and_link($cut, @contents)
+ body => $self->expand_and_link($cut, @contents)
);
}
DX::Op::Prop->new(%new);
}
+sub _expand_op_exists {
+ my ($self, $vars, @body) = @_;
+ DX::Op::Exists->new(
+ vars => $vars,
+ body => $self->expand_and_link(DX::Op::Return->new, @body)
+ );
+}
+
1;