4 use DX::Op::SetupScope;
7 use DX::Op::ApplyConstraint;
10 use DX::Op::Backtrack;
13 use DX::Op::ProposeAction;
14 use DX::Op::ModifyAction;
15 use DX::Op::Materialize;
17 use DX::Op::Predicate;
18 use DX::Op::HasAction;
21 use List::Util qw(reduce);
23 has rules => (is => 'ro', default => sub { {} });
26 my ($self, $name, $vars, @cases) = @_;
27 my $full_name = join('/', $name, scalar @$vars);
28 push @{$self->rules->{$full_name}}, DX::Op::Predicate->new(
29 arg_names => $vars, arg_cases => \@cases
34 my ($self, $name, $vars, @body) = @_;
35 my $full_name = join('/', $name, scalar @$vars);
36 push @{$self->rules->{$full_name}}, $self->_make_rule($vars, @body);
41 my ($self, $vars, @body) = @_;
42 my $head = $self->expand_and_link(DX::Op::Return->new, @body);
43 DX::Op::SetupScope->new(arg_names => $vars, next => $head);
47 my ($self, $last, @body) = @_;
48 return reduce { $b->but(next => $a) }
50 reverse map $self->expand($_), @body;
54 my ($self, $thing) = @_;
55 if (ref($thing) eq 'ARRAY') {
56 my ($type, @rest) = @$thing;
57 if ($self->can(my $expand_meth = "_expand_op_${type}")) {
58 return $self->$expand_meth(@rest);
60 return $self->_expand_call(@$thing);
66 my ($self, $name, @args) = @_;
67 DX::Op::CallRule->new(rule_name => $name, rule_args => \@args);
70 sub _expand_op_cut { return DX::Op::Cut->new }
72 sub _expand_op_fail { return DX::Op::Backtrack->new }
75 my ($self, $contents) = @_;
76 my $cut = DX::Op::Cut->new(next => DX::Op::Backtrack->new);
78 body => $self->expand_and_link($cut, @$contents)
82 sub _expand_op_findall {
83 my ($self, $coll_name, $var_name, $contents) = @_;
85 coll_name => $coll_name,
86 var_name => $var_name,
87 body => $self->expand_and_link(DX::Op::Return->new, @$contents),
91 sub _expand_op_foreach {
92 my ($self, $var_name, $body, $each_body) = @_;
94 var_name => $var_name,
95 body => $self->expand_and_link(DX::Op::Return->new, @$body),
96 each_body => $self->expand_and_link(DX::Op::Return->new, @$each_body),
100 sub _expand_op_member_of {
101 my ($self, $member_var, $coll_var) = @_;
102 DX::Op::MemberOf->new(
103 member_var => $member_var,
104 coll_var => $coll_var,
108 sub _expand_op_constrain {
109 my ($self, $vars, $constraint) = @_;
110 DX::Op::ApplyConstraint->new(
112 constraint => $constraint
116 sub _expand_op_observe {
117 my ($self, $vars, $builder) = @_;
118 DX::Op::Observe->new(
125 my ($self, $vars, $builder) = @_;
126 DX::Op::ProposeAction->new(
132 sub _expand_op_react {
133 my ($self, $vars, $builder) = @_;
134 DX::Op::ModifyAction->new(
140 sub _expand_op_materialize {
142 DX::Op::Materialize->new;
145 sub _expand_op_prop {
146 my ($self, @args) = @_;
147 my %new; @new{qw(of name value)} = @args;
148 DX::Op::Prop->new(%new);
151 sub _expand_op_has_action {
152 my ($self, @args) = @_;
153 DX::Op::HasAction->new(arg_spec => \@args);