separate scope setup in rule bodies into its own op
[scpubgit/DKit.git] / lib / DX / RuleSet.pm
index 74ca544..09a2ff5 100644 (file)
@@ -1,6 +1,7 @@
 package DX::RuleSet;
 
 use Moo;
+use DX::Op::SetupScope;
 use DX::Op::CallRule;
 use DX::Op::MemberOf;
 use DX::Op::ApplyConstraint;
@@ -12,6 +13,7 @@ use DX::Op::Not;
 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 { {} });
@@ -25,23 +27,27 @@ sub add_rule {
 
 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 {
@@ -57,7 +63,7 @@ sub _expand_op_not {
   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)
   );
 }
 
@@ -104,4 +110,12 @@ sub _expand_op_prop {
   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;