1 package DX::SearchState;
3 use Types::Standard qw(Maybe);
4 use DX::Step::InvokeNextPredicate;
7 has current_hypothesis => (is => 'ro', isa => Hypothesis, required => 1);
9 has next_step => (is => 'ro', isa => Maybe[Step]);
11 has propositions => (is => 'ro', isa => PropositionSequence, required => 1);
13 has alternatives => (is => 'ro', isa => AlternativeList, required => 1);
15 sub next_proposition { $_[0]->current_hypothesis->head_proposition }
18 my ($class, $hyp, $props) = @_;
20 current_hypothesis => $hyp,
22 next_step => DX::Step::InvokeNextPredicate->new(
23 proposition => $hyp->head_proposition,
25 propositions => $props,
31 my $hyp = $self->current_hypothesis;
32 return undef unless my $step = $self->next_step;
33 my ($first_alt, @rest_alt) = my @alt = @{$self->alternatives};
34 my ($new_hyp, $alt_step) = $step->apply_to($hyp);
37 current_hypothesis => $new_hyp,
44 next_step => DX::Step::InvokeNextPredicate->new(
45 proposition => $self->next_proposition,
50 return $self->but(next_step => $alt_step);
52 return undef unless $first_alt;
53 trace 'search.backtrack.rewind_to' => $first_alt->[1];
55 current_hypothesis => $first_alt->[0],
56 alternatives => \@rest_alt,
57 next_step => $first_alt->[1],
63 while ($state and @{$state->current_hypothesis->outstanding_propositions}) {
64 $state = $state->with_one_step;
66 trace 'search.solution.hyp' => $state->current_hypothesis if $state;
72 my ($first_alt, @rest_alt) = @{$self->alternatives};
73 return undef unless $first_alt;
74 trace 'search.backtrack.forced' => $first_alt->[0];
75 return ref($self)->new(
76 current_hypothesis => $first_alt->[0],
77 next_step => $first_alt->[1],
78 alternatives => \@rest_alt,
79 propositions => $self->propositions,
83 sub find_next_solution {
85 return undef unless my $bt = $self->force_backtrack;
86 return $bt->find_solution;