remove alternative addition handling from search state
[scpubgit/DX.git] / lib / DX / SearchState.pm
CommitLineData
9d759b64 1package DX::SearchState;
2
a1aab147 3use Types::Standard qw(Maybe);
e647e417 4use DX::Step::InvokeNextPredicate;
9d759b64 5use DX::Class;
6
3e465d5d 7has current_hypothesis => (is => 'ro', isa => Hypothesis, required => 1);
9d759b64 8
e647e417 9has next_step => (is => 'ro', isa => Maybe[Step]);
9d759b64 10
1350f664 11has propositions => (is => 'ro', isa => PropositionSequence, required => 1);
12
3e465d5d 13has alternatives => (is => 'ro', isa => AlternativeList, required => 1);
14
f25e6894 15sub next_proposition {
5787d20d 16 my ($self, $hyp) = @_;
17 $hyp ||= $self->current_hypothesis;
f25e6894 18 $self->propositions->members->[
5787d20d 19 $hyp->resolved_propositions->resolved_count
f25e6894 20 ];
21}
ccf0d4fe 22
3e465d5d 23sub new_for {
1350f664 24 my ($class, $hyp, $props) = @_;
3e465d5d 25 $class->new(
26 current_hypothesis => $hyp,
27 alternatives => [],
ccf0d4fe 28 next_step => DX::Step::InvokeNextPredicate->new(
f25e6894 29 proposition => $props->members->[0],
ccf0d4fe 30 ),
1350f664 31 propositions => $props,
3e465d5d 32 );
33}
9d759b64 34
35sub with_one_step {
36 my ($self) = @_;
37 my $hyp = $self->current_hypothesis;
e647e417 38 return undef unless my $step = $self->next_step;
5d243619 39 my ($new_ss) = $step->apply_to($self);
40 return $new_ss if $new_ss;
7506f4ca 41 my ($first_alt, @rest_alt) = @{$self->alternatives};
e647e417 42 return undef unless $first_alt;
43 trace 'search.backtrack.rewind_to' => $first_alt->[1];
44 return $self->but(
45 current_hypothesis => $first_alt->[0],
46 alternatives => \@rest_alt,
47 next_step => $first_alt->[1],
48 );
9d759b64 49}
50
9d759b64 51sub force_backtrack {
52 my ($self) = @_;
f458fa2c 53 my ($first_alt, @rest_alt) = @{$self->alternatives};
54 return undef unless $first_alt;
72e5c0e0 55 trace 'search.backtrack.forced' => $first_alt->[0];
9d759b64 56 return ref($self)->new(
57 current_hypothesis => $first_alt->[0],
e647e417 58 next_step => $first_alt->[1],
1350f664 59 alternatives => \@rest_alt,
60 propositions => $self->propositions,
9d759b64 61 );
62}
63
9d759b64 641;