| 1 | package Logic::Relational::Goal::GeneratorStep; | ||||||||||||
| 2 | |||||||||||||
| 3 | 6 | ▶ | use v5.38; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 6 | ▶ | use experimental 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 6 | ▶ | use parent 'Logic::Relational::Goal'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 6 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | =head1 NAME | ||||||||||||
| 9 | |||||||||||||
| 10 | Logic::Relational::Goal::GeneratorStep - Represents a lazy step in a generator. | ||||||||||||
| 11 | |||||||||||||
| 12 | =cut | ||||||||||||
| 13 | |||||||||||||
| 14 | 12 | ▶ | sub new ( $class, %args ) { | ||||||||||
|
Subroutine
new: called CC=5 SCAR=17.5
Pod
undocumented
| |||||||||||||
| 15 | 12 | ▶ | my $iterator = $args{iterator} // croak "iterator is required"; | ||||||||||
|
Condition50%1/2
$args{"iterator"} // croak("iterator is required")
MC/DC0%0/1
$args{"iterator"} // croak("iterator is required")
$args{"iterator"}
Truth table50%1/2
A
A= $args{"iterator"}
| |||||||||||||
| 16 | 12 | ▶ | my $call_args = $args{call_args} // croak "call_args is required"; | ||||||||||
|
Condition50%1/2
$args{"call_args"} // croak("call_args is required")
MC/DC0%0/1
$args{"call_args"} // croak("call_args is required")
$args{"call_args"}
Truth table50%1/2
A
A= $args{"call_args"}
| |||||||||||||
| 17 | 12 | ▶ | my $key = $args{key} // croak "key is required"; | ||||||||||
|
Condition50%1/2
$args{"key"} // croak("key is required")
MC/DC0%0/1
$args{"key"} // croak("key is required")
$args{"key"}
Truth table50%1/2
A
A= $args{"key"}
| |||||||||||||
| 18 | 12 | ▶ | my $arity = $args{arity} // croak "arity is required"; | ||||||||||
|
Condition50%1/2
$args{"arity"} // croak("arity is required")
MC/DC0%0/1
$args{"arity"} // croak("arity is required")
$args{"arity"}
Truth table50%1/2
A
A= $args{"arity"}
| |||||||||||||
| 19 | |||||||||||||
| 20 | 12 | return bless { | |||||||||||
| 21 | iterator => $iterator, | ||||||||||||
| 22 | call_args => $call_args, | ||||||||||||
| 23 | key => $key, | ||||||||||||
| 24 | arity => $arity, | ||||||||||||
| 25 | }, $class; | ||||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | 186 | ▶ | sub expand ( $self, %args ) { | ||||||||||
|
Subroutine
expand: called CC=9 SCAR=22.6
Pod
documented
| |||||||||||||
| 29 | 186 | my $subst = $args{substitution}; | |||||||||||
| 30 | 186 | my $remaining = $args{remaining}; | |||||||||||
| 31 | |||||||||||||
| 32 | 186 | my $iterator = $self->{iterator}; | |||||||||||
| 33 | 186 | my $call_args = $self->{call_args}; | |||||||||||
| 34 | 186 | my $key = $self->{key}; | |||||||||||
| 35 | 186 | my $arity = $self->{arity}; | |||||||||||
| 36 | |||||||||||||
| 37 | 186 | require Logic::Relational::Unifier; | |||||||||||
| 38 | 186 | require Logic::Relational::State; | |||||||||||
| 39 | |||||||||||||
| 40 | 186 | ▶ | while ( my $candidate = $iterator->() ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 181 | ▶ | croak "Iterator for $key must yield an array reference of values" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | unless ref($candidate) eq 'ARRAY'; | ||||||||||||
| 43 | 181 | ▶ | croak sprintf( "Iterator for %s yielded %d values, but arity is %d", | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | $key, scalar(@$candidate), $arity ) | ||||||||||||
| 45 | unless scalar(@$candidate) == $arity; | ||||||||||||
| 46 | |||||||||||||
| 47 | 181 | my $new_subst = $subst; | |||||||||||
| 48 | 181 | my $ok = 1; | |||||||||||
| 49 | 181 | for my $i ( 0 .. $arity - 1 ) { | |||||||||||
| 50 | 196 | $new_subst = | |||||||||||
| 51 | Logic::Relational::Unifier::unify( $call_args->[$i], | ||||||||||||
| 52 | $candidate->[$i], $new_subst ); | ||||||||||||
| 53 | 196 | ▶ | if ( !$new_subst ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 0 | $ok = 0; | |||||||||||
| 55 | 0 | last; | |||||||||||
| 56 | } | ||||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 181 | ▶ | if ($ok) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 181 | require Logic::Relational::Goal::True; | |||||||||||
| 61 | my $succ = $args{state}->derive( | ||||||||||||
| 62 | 181 | substitution => $new_subst, | |||||||||||
| 63 | goals => [ Logic::Relational::Goal::True->new, @$remaining ], | ||||||||||||
| 64 | ); | ||||||||||||
| 65 | |||||||||||||
| 66 | my $backtrack = $args{state}->derive( | ||||||||||||
| 67 | 181 | substitution => $subst, | |||||||||||
| 68 | goals => [ $self, @$remaining ], | ||||||||||||
| 69 | ); | ||||||||||||
| 70 | |||||||||||||
| 71 | 181 | my @res; | |||||||||||
| 72 | 181 | ▶ | push @res, $succ if $succ; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 181 | ▶ | push @res, $backtrack if $backtrack; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 181 | return @res; | |||||||||||
| 75 | } | ||||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 5 | return (); | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 0 | ▶ | sub freshen ( $self, $var_map ) { | ||||||||||
|
Subroutine
freshen: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 82 | 0 | return $self; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 0 | ▶ | sub is_ground ( $self, $subst ) { | ||||||||||
|
Subroutine
is_ground: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 86 | 0 | for my $arg ( @{ $self->{call_args} } ) { | |||||||||||
| 87 | 0 | ▶ | return 0 unless $self->term_is_ground( $arg, $subst ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | } | ||||||||||||
| 89 | 0 | return 1; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | 0 | ▶ | sub as_string ($self) { | ||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 93 | 0 | return "generator_step(" . $self->{key} . ")"; | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 6 | 1; | |||||||||||