| 1 | package Logic::Relational::Goal::CellAt; | ||
| 2 | |||
| 3 | use v5.38; | ||
| 4 | use experimental 'signatures'; | ||
| 5 | use parent 'Logic::Relational::Goal'; | ||
| 6 | use Logic::Relational::Unifier qw(unify); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Logic::Relational::Goal::CellAt - Goal that binds the value of an array element at a ground index. | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | sub new ( $class, $board, $index, $value ) { | ||
| 15 | return bless { board => $board, index => $index, value => $value }, $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub expand ( $self, %args ) { | ||
| 19 | my $subst = $args{substitution}; | ||
| 20 | my $remaining = $args{remaining}; | ||
| 21 | my $state = $args{state}; | ||
| 22 | |||
| 23 | my $board_val = $subst->walk( $self->{board} ); | ||
| 24 | my $idx_val = $subst->walk( $self->{index} ); | ||
| 25 | |||
| 26 | # Ensure they are ground | ||
| 27 | return () if ref($board_val) ne 'ARRAY' || ref($idx_val) ne ''; | ||
| 28 | |||
| 29 | my $cell = $board_val->[$idx_val]; | ||
| 30 | my $new_subst = unify( $self->{value}, $cell, $subst ); | ||
| 31 | if ($new_subst) { | ||
| 32 | my $succ = $state->derive( | ||
| 33 | substitution => $new_subst, | ||
| 34 | goals => $remaining, | ||
| 35 | ); | ||
| 36 | return $succ // (); | ||
| 37 | } | ||
| 38 | return (); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub freshen ( $self, $var_map ) { | ||
| 42 | require Logic::Relational::DSL; | ||
| 43 | return ref($self)->new( | ||
| 44 | Logic::Relational::DSL::freshen_val( $self->{board}, $var_map ), | ||
| 45 | Logic::Relational::DSL::freshen_val( $self->{index}, $var_map ), | ||
| 46 | Logic::Relational::DSL::freshen_val( $self->{value}, $var_map ) | ||
| 47 | ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub is_ground ( $self, $subst ) { | ||
| 51 | return | ||
| 52 | $self->term_is_ground( $self->{board}, $subst ) | ||
| 53 | && $self->term_is_ground( $self->{index}, $subst ) | ||
| 54 | && $self->term_is_ground( $self->{value}, $subst ); | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; |