| 1 | package Logic::Relational::Goal::Is; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 30 | ▶ | use v5.38; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 30 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 30 | ▶ | use parent 'Logic::Relational::Goal'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 30 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 30 | require Logic::Relational::Unifier; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Logic::Relational::Goal::Is - Evaluates arithmetic expressions and unifies results. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =cut | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 99 | ▶ | sub new ( $class, $target_var, $vars, $code ) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 99 | return bless { | |||||||||||||||||||||||||||
| 17 | target_var => $target_var, | ||||||||||||||||||||||||||||
| 18 | vars => $vars, | ||||||||||||||||||||||||||||
| 19 | code => $code, | ||||||||||||||||||||||||||||
| 20 | }, $class; | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub target_var ($self) { return $self->{target_var}; } | ||||||||||||||||||||||||||
|
Subroutine
target_var: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub vars ($self) { return $self->{vars}; } | ||||||||||||||||||||||||||
|
Subroutine
vars: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub code ($self) { return $self->{code}; } | ||||||||||||||||||||||||||
|
Subroutine
code: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 29 | ▶ | sub expand ( $self, %args ) { | ||||||||||||||||||||||||||
|
Subroutine
expand: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 29 | my $subst = $args{substitution}; | |||||||||||||||||||||||||||
| 29 | 29 | my $remaining = $args{remaining}; | |||||||||||||||||||||||||||
| 30 | 29 | my $state = $args{state}; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Verify all referenced input variables are ground | ||||||||||||||||||||||||||||
| 33 | 29 | for my $var ( @{ $self->{vars} } ) { | |||||||||||||||||||||||||||
| 34 | 40 | my $walked = $subst->walk($var); | |||||||||||||||||||||||||||
| 35 | 40 | ▶ | if ( ref($walked) eq 'Logic::Relational::Variable' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 1 | croak | |||||||||||||||||||||||||||
| 37 | "Arithmetic evaluation 'is' attempted to read unbound logical variable \$" | ||||||||||||||||||||||||||||
| 38 | . $var->name . "\n"; | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Reify input values | ||||||||||||||||||||||||||||
| 43 | 28 | my @vals = map { $subst->reify($_) } @{ $self->{vars} }; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Evaluate arithmetic closure | ||||||||||||||||||||||||||||
| 46 | 28 | my $result_val = $self->{code}->(@vals); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Unify target variable with calculated result | ||||||||||||||||||||||||||||
| 49 | my $new_subst = | ||||||||||||||||||||||||||||
| 50 | 28 | Logic::Relational::Unifier::unify( $self->{target_var}, $result_val, | |||||||||||||||||||||||||||
| 51 | $subst ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 28 | ▶ | if ($new_subst) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 28 | my $succ = $state->derive( | |||||||||||||||||||||||||||
| 55 | substitution => $new_subst, | ||||||||||||||||||||||||||||
| 56 | goals => $remaining, | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | 28 | ▶ | return $succ // (); | ||||||||||||||||||||||||||
|
Condition33%1/3
$succ // ()
MC/DC0%0/2
$succ // ()
$succ
()
Truth table33%1/3
A // B
A= $succB= ()
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | return (); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 32 | ▶ | sub freshen ( $self, $var_map ) { | ||||||||||||||||||||||||||
|
Subroutine
freshen: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | my $fresh_target = | ||||||||||||||||||||||||||||
| 65 | ref( $self->{target_var} ) | ||||||||||||||||||||||||||||
| 66 | && $self->{target_var}->can('freshen') | ||||||||||||||||||||||||||||
| 67 | ? $self->{target_var}->freshen($var_map) | ||||||||||||||||||||||||||||
| 68 | 32 | ▶ | : $self->{target_var}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $self->{"target_var"} and $self->{"target_var"}->can("freshen")
MC/DC0%0/2
ref $self->{"target_var"} and $self->{"target_var"}->can("freshen")
ref $self->{"target_var"}
$self->{"target_var"}->can("freshen")
Truth table33%1/3
A and B
A= ref $self->{"target_var"}B= $self->{"target_var"}->can("freshen")
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | my @fresh_vars = | ||||||||||||||||||||||||||||
| 71 | 45 | ▶ | map { ref($_) && $_->can('freshen') ? $_->freshen($var_map) : $_ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_ and $_->can("freshen")
MC/DC0%0/2
ref $_ and $_->can("freshen")
ref $_
$_->can("freshen")
Truth table33%1/3
A and B
A= ref $_B= $_->can("freshen")
| |||||||||||||||||||||||||||||
| 72 | 32 | @{ $self->{vars} }; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 32 | return ref($self)->new( $fresh_target, \@fresh_vars, $self->{code} ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub is_ground ( $self, $subst ) { | ||||||||||||||||||||||||||
|
Subroutine
is_ground: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | return 0 unless $self->term_is_ground( $self->{target_var}, $subst ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | for my $var ( @{ $self->{vars} } ) { | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | return 0 unless $self->term_is_ground( $var, $subst ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 0 | return 1; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 30 | 1; | |||||||||||||||||||||||||||