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;
18 use DX::Op::Predicate;
19 use List::Util qw(reduce);
21 has rules => (is => 'ro', default => sub { {} });
24 my ($self, $name, $vars, @cases) = @_;
25 my $full_name = join('/', $name, scalar @$vars);
26 push @{$self->rules->{$full_name}}, DX::Op::Predicate->new(
27 arg_names => $vars, arg_cases => \@cases
32 my ($self, $name, $vars, @body) = @_;
33 my $full_name = join('/', $name, scalar @$vars);
34 push @{$self->rules->{$full_name}}, $self->_make_rule($vars, @body);
39 my ($self, $vars, @body) = @_;
40 my $head = $self->expand_and_link(DX::Op::Return->new, @body);
41 DX::Op::SetupScope->new(arg_names => $vars, next => $head);
45 my ($self, $last, @body) = @_;
46 return reduce { $b->but(next => $a) }
48 reverse map $self->expand($_), @body;
52 my ($self, $thing) = @_;
53 if (ref($thing) eq 'ARRAY') {
54 my ($type, @rest) = @$thing;
55 if ($self->can(my $expand_meth = "_expand_op_${type}")) {
56 return $self->$expand_meth(@rest);
58 return $self->_expand_call(@$thing);
64 my ($self, $name, @args) = @_;
65 DX::Op::CallRule->new(rule_name => $name, rule_args => \@args);
68 sub _expand_op_cut { return DX::Op::Cut->new }
70 sub _expand_op_fail { return DX::Op::Backtrack->new }
73 my ($self, @contents) = @_;
74 my $cut = DX::Op::Cut->new(next => DX::Op::Backtrack->new);
76 body => $self->expand_and_link($cut, @contents)
80 sub _expand_op_member_of {
81 my ($self, $member_var, $coll_var) = @_;
82 DX::Op::MemberOf->new(
83 member_var => $member_var,
84 coll_var => $coll_var,
88 sub _expand_op_constrain {
89 my ($self, $vars, $constraint) = @_;
90 DX::Op::ApplyConstraint->new(
92 constraint => $constraint
96 sub _expand_op_observe {
97 my ($self, $vars, $builder) = @_;
105 my ($self, $vars, $builder) = @_;
106 DX::Op::ProposeAction->new(
112 sub _expand_op_react {
113 my ($self, $vars, $builder) = @_;
114 DX::Op::ModifyAction->new(
120 sub _expand_op_materialize {
121 my ($self, $var_name) = @_;
122 DX::Op::Materialize->new(var_name => $var_name);
125 sub _expand_op_prop {
126 my ($self, @args) = @_;
127 my %new; @new{qw(of name value)} = @args;
128 DX::Op::Prop->new(%new);
131 sub _expand_op_exists {
132 my ($self, $vars, @body) = @_;
135 body => $self->expand_and_link(DX::Op::Return->new, @body)