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 {
17 $self->propositions->members->[
18 $self->current_hypothesis->resolved_propositions->resolved_count
23 my ($class, $hyp, $props) = @_;
25 current_hypothesis => $hyp,
27 next_step => DX::Step::InvokeNextPredicate->new(
28 proposition => $props->members->[0],
30 propositions => $props,
36 my $hyp = $self->current_hypothesis;
37 return undef unless my $step = $self->next_step;
38 my ($first_alt, @rest_alt) = my @alt = @{$self->alternatives};
39 my ($new_hyp, $alt_step) = $step->apply_to($hyp);
42 current_hypothesis => $new_hyp,
49 next_step => DX::Step::InvokeNextPredicate->new(
50 proposition => $self->next_proposition,
55 return $self->but(next_step => $alt_step);
57 return undef unless $first_alt;
58 trace 'search.backtrack.rewind_to' => $first_alt->[1];
60 current_hypothesis => $first_alt->[0],
61 alternatives => \@rest_alt,
62 next_step => $first_alt->[1],
68 while ($state and $state->next_proposition) {
69 $state = $state->with_one_step;
71 trace 'search.solution.hyp' => $state->current_hypothesis if $state;
77 my ($first_alt, @rest_alt) = @{$self->alternatives};
78 return undef unless $first_alt;
79 trace 'search.backtrack.forced' => $first_alt->[0];
80 return ref($self)->new(
81 current_hypothesis => $first_alt->[0],
82 next_step => $first_alt->[1],
83 alternatives => \@rest_alt,
84 propositions => $self->propositions,
88 sub find_next_solution {
90 return undef unless my $bt = $self->force_backtrack;
91 return $bt->find_solution;