| 1 | package Logic::Relational::Unifier; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 33 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 33 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||
| 6 | 33 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 33 | ▶ | use builtin 'blessed'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 33 | ▶ | no warnings 'experimental::builtin'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | Logic::Relational::Unifier - Performs structural unification of logic terms. | ||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | use Logic::Relational::Unifier qw(unify); | ||||||||||||||||||||||||||||||||||||
| 17 | my $new_subst = unify($term1, $term2, $subst); | ||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||
| 21 | C<Logic::Relational::Unifier> provides structural unification. It implements | ||||||||||||||||||||||||||||||||||||
| 22 | the standard unification algorithm with an occurs check to prevent infinite terms. | ||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||
| 24 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||
| 26 | =head2 unify | ||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||
| 28 | Performs unification of two terms under a given C<Logic::Relational::Substitution>. | ||||||||||||||||||||||||||||||||||||
| 29 | Returns the updated substitution if unification succeeds, or C<undef> if it fails. | ||||||||||||||||||||||||||||||||||||
| 30 | Raises an exception if the occurs check fails. | ||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||
| 32 | =head2 bind_variable | ||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | Binds a variable to a term, checking that the variable does not occur inside | ||||||||||||||||||||||||||||||||||||
| 35 | the term (occurs check). | ||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||
| 37 | =head2 occurs_in | ||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | Helper function. Returns true if the variable ID occurs inside the dereferenced term. | ||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||
| 43 | 33 | our @EXPORT_OK = qw(unify); | |||||||||||||||||||||||||||||||||||
| 44 | 33 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||
| 46 | 2523 | ▶ | sub unify ( $left, $right, $subst ) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
unify: called CC=20 SCAR=30.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 47 | 2523 | $left = $subst->walk($left); | |||||||||||||||||||||||||||||||||||
| 48 | 2523 | $right = $subst->walk($right); | |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 2523 | ▶ | if ( ref($left) eq 'Logic::Relational::Variable' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 51 | 899 | return bind_variable( $left, $right, $subst ); | |||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||
| 54 | 1624 | ▶ | if ( ref($right) eq 'Logic::Relational::Variable' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 55 | 232 | return bind_variable( $right, $left, $subst ); | |||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | # Constants | ||||||||||||||||||||||||||||||||||||
| 59 | 1392 | ▶ | my $is_const_l = !ref($left) || ref($left) eq 'Logic::Relational::Atom'; | ||||||||||||||||||||||||||||||||||
|
Condition100%3/3
!ref($left) || ref $left eq 'Logic::Relational::Atom'
MC/DC100%2/2
!ref($left) || ref $left eq 'Logic::Relational::Atom'
!ref($left)
ref $left eq 'Logic::Relational::Atom'
Truth table100%3/3
not(A) || B
A= !ref($left)B= ref $left eq 'Logic::Relational::Atom'
| |||||||||||||||||||||||||||||||||||||
| 60 | 1392 | ▶ | my $is_const_r = !ref($right) || ref($right) eq 'Logic::Relational::Atom'; | ||||||||||||||||||||||||||||||||||
|
Condition100%3/3
!ref($right) || ref $right eq 'Logic::Relational::Atom'
MC/DC100%2/2
!ref($right) || ref $right eq 'Logic::Relational::Atom'
!ref($right)
ref $right eq 'Logic::Relational::Atom'
Truth table100%3/3
not(A) || B
A= !ref($right)B= ref $right eq 'Logic::Relational::Atom'
| |||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | 1392 | ▶ | if ( $is_const_l && $is_const_r ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$is_const_l and $is_const_r
MC/DC50%1/2
$is_const_l and $is_const_r
$is_const_l
$is_const_r
Truth table66%2/3
A and B
A= $is_const_lB= $is_const_r
| |||||||||||||||||||||||||||||||||||||
| 63 | 521 | ▶ | my $val_l = ref($left) ? $left->value : $left; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 64 | 521 | ▶ | my $val_r = ref($right) ? $right->value : $right; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 65 | 521 | ▶ | return $val_l eq $val_r ? $subst : undef; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||
| 68 | # Compound terms | ||||||||||||||||||||||||||||||||||||
| 69 | 871 | ▶ | if ( ref($left) eq 'Logic::Relational::Term' | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $left eq "Logic::Relational::Term" and ref $right eq "Logic::Relational::Term"
MC/DC50%1/2
ref $left eq "Logic::Relational::Term" and ref $right eq "Logic::Relational::Term"
ref $left eq "Logic::Relational::Term"
ref $right eq "Logic::Relational::Term"
Truth table66%2/3
A and B
A= ref $left eq "Logic::Relational::Term"B= ref $right eq "Logic::Relational::Term"
| |||||||||||||||||||||||||||||||||||||
| 70 | && ref($right) eq 'Logic::Relational::Term' ) | ||||||||||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||||||||||
| 72 | return undef | ||||||||||||||||||||||||||||||||||||
| 73 | 791 | ▶ | unless $left->functor eq $right->functor | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$left->functor eq $right->functor and $left->arity == $right->arity
MC/DC0%0/2
$left->functor eq $right->functor and $left->arity == $right->arity
$left->functor eq $right->functor
$left->arity == $right->arity
Truth table33%1/3
A and B
A= $left->functor eq $right->functorB= $left->arity == $right->arity
| |||||||||||||||||||||||||||||||||||||
| 74 | && $left->arity == $right->arity; | ||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | 791 | for my $i ( 0 .. $left->arity - 1 ) { | |||||||||||||||||||||||||||||||||||
| 77 | 1203 | $subst = unify( $left->arg($i), $right->arg($i), $subst ); | |||||||||||||||||||||||||||||||||||
| 78 | 1203 | ▶ | return undef unless $subst; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||
| 80 | 455 | return $subst; | |||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | 80 | ▶ | if ( ref($left) eq 'ARRAY' && ref($right) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $left eq "ARRAY" and ref $right eq "ARRAY"
MC/DC50%1/2
ref $left eq "ARRAY" and ref $right eq "ARRAY"
ref $left eq "ARRAY"
ref $right eq "ARRAY"
Truth table66%2/3
A and B
A= ref $left eq "ARRAY"B= ref $right eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||
| 84 | 70 | return unify_arrays( $left, $right, $subst ); | |||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||
| 87 | 10 | ▶ | if ( ref($left) eq 'HASH' && ref($right) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $left eq "HASH" and ref $right eq "HASH"
MC/DC0%0/2
ref $left eq "HASH" and ref $right eq "HASH"
ref $left eq "HASH"
ref $right eq "HASH"
Truth table33%1/3
A and B
A= ref $left eq "HASH"B= ref $right eq "HASH"
| |||||||||||||||||||||||||||||||||||||
| 88 | 10 | return unify_hashes( $left, $right, $subst ); | |||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | 0 | return undef; | |||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||
| 94 | 10 | ▶ | sub unify_hashes ( $left, $right, $subst ) { ## no critic (ProhibitExcessComplexity) | ||||||||||||||||||||||||||||||||||
|
Subroutine
unify_hashes: called CC=22 SCAR=43.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 95 | my @slurp_keys_l = | ||||||||||||||||||||||||||||||||||||
| 96 | 10 | grep { ref( $left->{$_} ) eq 'Logic::Relational::ArrayRest' } | |||||||||||||||||||||||||||||||||||
| 97 | keys %$left; | ||||||||||||||||||||||||||||||||||||
| 98 | my @slurp_keys_r = | ||||||||||||||||||||||||||||||||||||
| 99 | 10 | grep { ref( $right->{$_} ) eq 'Logic::Relational::ArrayRest' } | |||||||||||||||||||||||||||||||||||
| 100 | keys %$right; | ||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||
| 102 | 10 | ▶ | if ( scalar @slurp_keys_l > 1 || scalar @slurp_keys_r > 1 ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @slurp_keys_l > 1 or scalar @slurp_keys_r > 1
MC/DC50%1/2
scalar @slurp_keys_l > 1 or scalar @slurp_keys_r > 1
scalar @slurp_keys_l > 1
scalar @slurp_keys_r > 1
Truth table66%2/3
A or B
A= scalar @slurp_keys_l > 1B= scalar @slurp_keys_r > 1
| |||||||||||||||||||||||||||||||||||||
| 103 | 1 | croak | |||||||||||||||||||||||||||||||||||
| 104 | "Only one slurp/rest value is allowed in a hash reference pattern"; | ||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||
| 107 | 9 | my $slurp_k_l = $slurp_keys_l[0]; | |||||||||||||||||||||||||||||||||||
| 108 | 9 | my $slurp_k_r = $slurp_keys_r[0]; | |||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | 9 | ▶ | if ( defined $slurp_k_l && defined $slurp_k_r ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
defined $slurp_k_l and defined $slurp_k_r
MC/DC0%0/2
defined $slurp_k_l and defined $slurp_k_r
defined $slurp_k_l
defined $slurp_k_r
Truth table66%2/3
A and B
A= defined $slurp_k_lB= defined $slurp_k_r
| |||||||||||||||||||||||||||||||||||||
| 111 | 0 | my %std_keys_l = map { $_ => 1 } grep { $_ ne $slurp_k_l } keys %$left; | |||||||||||||||||||||||||||||||||||
| 112 | 0 | my %std_keys_r = map { $_ => 1 } grep { $_ ne $slurp_k_r } keys %$right; | |||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | 0 | for my $k ( keys %std_keys_l ) { | |||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | return undef unless exists $right->{$k}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 116 | 0 | $subst = unify( $left->{$k}, $right->{$k}, $subst ); | |||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | return undef unless $subst; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||
| 119 | 0 | for my $k ( keys %std_keys_r ) { | |||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return undef unless exists $left->{$k}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | 0 | return unify( $left->{$slurp_k_l}->term, $right->{$slurp_k_r}->term, | |||||||||||||||||||||||||||||||||||
| 124 | $subst ); | ||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||
| 126 | elsif ( defined $slurp_k_l ) { | ||||||||||||||||||||||||||||||||||||
| 127 | 2 | my %std_keys_l = map { $_ => 1 } grep { $_ ne $slurp_k_l } keys %$left; | |||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||
| 129 | 2 | for my $k ( keys %std_keys_l ) { | |||||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | return undef unless exists $right->{$k}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 131 | 2 | $subst = unify( $left->{$k}, $right->{$k}, $subst ); | |||||||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | return undef unless $subst; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||
| 135 | my %remainder = | ||||||||||||||||||||||||||||||||||||
| 136 | 2 | map { $_ => $right->{$_} } | |||||||||||||||||||||||||||||||||||
| 137 | 2 | grep { !exists $std_keys_l{$_} } keys %$right; | |||||||||||||||||||||||||||||||||||
| 138 | 2 | return unify( $left->{$slurp_k_l}->term, \%remainder, $subst ); | |||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||
| 140 | elsif ( defined $slurp_k_r ) { | ||||||||||||||||||||||||||||||||||||
| 141 | 0 | my %std_keys_r = map { $_ => 1 } grep { $_ ne $slurp_k_r } keys %$right; | |||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||
| 143 | 0 | for my $k ( keys %std_keys_r ) { | |||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | return undef unless exists $left->{$k}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 145 | 0 | $subst = unify( $left->{$k}, $right->{$k}, $subst ); | |||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | return undef unless $subst; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||
| 149 | my %remainder = | ||||||||||||||||||||||||||||||||||||
| 150 | 0 | map { $_ => $left->{$_} } | |||||||||||||||||||||||||||||||||||
| 151 | 0 | grep { !exists $std_keys_r{$_} } keys %$left; | |||||||||||||||||||||||||||||||||||
| 152 | 0 | return unify( \%remainder, $right->{$slurp_k_r}->term, $subst ); | |||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||||||||||||||||||
| 155 | 7 | ▶ | return undef unless scalar keys %$left == scalar keys %$right; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 156 | 6 | for my $k ( keys %$left ) { | |||||||||||||||||||||||||||||||||||
| 157 | 12 | ▶ | return undef unless exists $right->{$k}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 158 | 12 | $subst = unify( $left->{$k}, $right->{$k}, $subst ); | |||||||||||||||||||||||||||||||||||
| 159 | 12 | ▶ | return undef unless $subst; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||
| 161 | 5 | return $subst; | |||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||
| 165 | 70 | ▶ | sub unify_arrays ( $left, $right, $subst ) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
unify_arrays: called CC=16 SCAR=27.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 166 | 70 | my $M = scalar @$left; | |||||||||||||||||||||||||||||||||||
| 167 | 70 | my $N = scalar @$right; | |||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | 70 | my $i = 0; | |||||||||||||||||||||||||||||||||||
| 170 | 70 | ▶ | while ( $i < $M || $i < $N ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i < $M or $i < $N
MC/DC100%2/2
$i < $M or $i < $N
$i < $M
$i < $N
Truth table100%3/3
A or B
A= $i < $MB= $i < $N
| |||||||||||||||||||||||||||||||||||||
| 171 | 104 | ▶ | my $has_rest_l = | ||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$i < $M && ref $left->[$i] eq 'Logic::Relational::ArrayRest'
MC/DC100%2/2
$i < $M && ref $left->[$i] eq 'Logic::Relational::ArrayRest'
$i < $M
ref $left->[$i] eq 'Logic::Relational::ArrayRest'
Truth table100%3/3
A && B
A= $i < $MB= ref $left->[$i] eq 'Logic::Relational::ArrayRest'
| |||||||||||||||||||||||||||||||||||||
| 172 | ( $i < $M && ref( $left->[$i] ) eq 'Logic::Relational::ArrayRest' ); | ||||||||||||||||||||||||||||||||||||
| 173 | 104 | ▶ | my $has_rest_r = | ||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$i < $N && ref $right->[$i] eq 'Logic::Relational::ArrayRest'
MC/DC100%2/2
$i < $N && ref $right->[$i] eq 'Logic::Relational::ArrayRest'
$i < $N
ref $right->[$i] eq 'Logic::Relational::ArrayRest'
Truth table100%3/3
A && B
A= $i < $NB= ref $right->[$i] eq 'Logic::Relational::ArrayRest'
| |||||||||||||||||||||||||||||||||||||
| 174 | ( $i < $N && ref( $right->[$i] ) eq 'Logic::Relational::ArrayRest' ); | ||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||
| 176 | 104 | ▶ | if ( $has_rest_l && $has_rest_r ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$has_rest_l and $has_rest_r
MC/DC100%2/2
$has_rest_l and $has_rest_r
$has_rest_l
$has_rest_r
Truth table100%3/3
A and B
A= $has_rest_lB= $has_rest_r
| |||||||||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | croak "rest/slurp must be the last element of an array" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$i == $M - 1 and $i == $N - 1
MC/DC0%0/2
$i == $M - 1 and $i == $N - 1
$i == $M - 1
$i == $N - 1
Truth table33%1/3
A and B
A= $i == $M - 1B= $i == $N - 1
| |||||||||||||||||||||||||||||||||||||
| 178 | unless $i == $M - 1 && $i == $N - 1; | ||||||||||||||||||||||||||||||||||||
| 179 | 1 | return unify( $left->[$i]->term, $right->[$i]->term, $subst ); | |||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||
| 181 | elsif ($has_rest_l) { | ||||||||||||||||||||||||||||||||||||
| 182 | 3 | ▶ | croak "rest/slurp must be the last element of an array" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 183 | unless $i == $M - 1; | ||||||||||||||||||||||||||||||||||||
| 184 | 2 | my $remainder = [ @{$right}[ $i .. $#$right ] ]; | |||||||||||||||||||||||||||||||||||
| 185 | 2 | return unify( $left->[$i]->term, $remainder, $subst ); | |||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||
| 187 | elsif ($has_rest_r) { | ||||||||||||||||||||||||||||||||||||
| 188 | 25 | ▶ | croak "rest/slurp must be the last element of an array" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 189 | unless $i == $N - 1; | ||||||||||||||||||||||||||||||||||||
| 190 | 25 | my $remainder = [ @{$left}[ $i .. $#$left ] ]; | |||||||||||||||||||||||||||||||||||
| 191 | 25 | return unify( $remainder, $right->[$i]->term, $subst ); | |||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||
| 194 | 75 | ▶ | if ( $i >= $M || $i >= $N ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i >= $M or $i >= $N
MC/DC100%2/2
$i >= $M or $i >= $N
$i >= $M
$i >= $N
Truth table100%3/3
A or B
A= $i >= $MB= $i >= $N
| |||||||||||||||||||||||||||||||||||||
| 195 | 27 | return undef; | |||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||
| 198 | 48 | $subst = unify( $left->[$i], $right->[$i], $subst ); | |||||||||||||||||||||||||||||||||||
| 199 | 48 | ▶ | return undef unless $subst; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 200 | 46 | $i++; | |||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||
| 202 | 12 | return $subst; | |||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||
| 205 | 1131 | ▶ | sub bind_variable ( $var, $val, $subst ) { ## no critic (ProhibitExcessComplexity) | ||||||||||||||||||||||||||||||||||
|
Subroutine
bind_variable: called CC=21 SCAR=34.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 206 | 1131 | ▶ | if ( ref($val) eq 'Logic::Relational::Variable' && $var->id == $val->id ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $val eq "Logic::Relational::Variable" and $var->id == $val->id
MC/DC100%2/2
ref $val eq "Logic::Relational::Variable" and $var->id == $val->id
ref $val eq "Logic::Relational::Variable"
$var->id == $val->id
Truth table100%3/3
A and B
A= ref $val eq "Logic::Relational::Variable"B= $var->id == $val->id
| |||||||||||||||||||||||||||||||||||||
| 207 | 1 | return $subst; | |||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||
| 210 | 1130 | ▶ | if ( occurs_in( $var->id, $val, $subst ) ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 211 | 3 | my $var_str = $var->as_string; | |||||||||||||||||||||||||||||||||||
| 212 | 3 | ▶ | my $val_str = blessed($val) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($val) and $val->can("as_string")
MC/DC50%1/2
builtin::blessed($val) and $val->can("as_string")
builtin::blessed($val)
$val->can("as_string")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->can("as_string")
| |||||||||||||||||||||||||||||||||||||
| 213 | && $val->can('as_string') ? $val->as_string : "$val"; | ||||||||||||||||||||||||||||||||||||
| 214 | 3 | croak "Occurs check failed: cannot bind $var_str to $val_str\n"; | |||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||
| 217 | 1127 | my $d_var = $subst->{bindings}{ $var->id }; | |||||||||||||||||||||||||||||||||||
| 218 | 1127 | my $has_d_var = ( ref($d_var) eq 'Logic::Relational::Domain' ); | |||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||
| 220 | 1127 | ▶ | if ( ref($val) eq 'Logic::Relational::Variable' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 221 | 145 | my $d_val = $subst->{bindings}{ $val->id }; | |||||||||||||||||||||||||||||||||||
| 222 | 145 | my $has_d_val = ( ref($d_val) eq 'Logic::Relational::Domain' ); | |||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||
| 224 | 145 | ▶ | if ( $has_d_var && $has_d_val ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
$has_d_var and $has_d_val
MC/DC50%1/2
$has_d_var and $has_d_val
$has_d_var
$has_d_val
Truth table66%2/3
A and B
A= $has_d_varB= $has_d_val
| |||||||||||||||||||||||||||||||||||||
| 225 | 1 | my $d_new = $d_var->intersect($d_val); | |||||||||||||||||||||||||||||||||||
| 226 | 1 | ▶ | return unless $d_new; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 227 | 1 | ▶ | if ( $d_new->is_bound ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 228 | 0 | my $v = $d_new->bound_value; | |||||||||||||||||||||||||||||||||||
| 229 | 0 | return $subst->bind( $var->id, $v )->bind( $val->id, $v ); | |||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||
| 231 | 1 | return $subst->bind( $var->id, $val )->bind( $val->id, $d_new ); | |||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||
| 233 | elsif ($has_d_var) { | ||||||||||||||||||||||||||||||||||||
| 234 | 0 | return $subst->bind( $val->id, $var ); | |||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||
| 236 | elsif ($has_d_val) { | ||||||||||||||||||||||||||||||||||||
| 237 | 12 | return $subst->bind( $var->id, $val ); | |||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||
| 239 | else { | ||||||||||||||||||||||||||||||||||||
| 240 | 132 | return $subst->bind( $var->id, $val ); | |||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||
| 243 | elsif ( ref($val) eq 'Logic::Relational::Domain' ) { | ||||||||||||||||||||||||||||||||||||
| 244 | 22 | ▶ | if ($has_d_var) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 245 | 0 | my $d_new = $d_var->intersect($val); | |||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | return unless $d_new; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | if ( $d_new->is_bound ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 248 | 0 | return $subst->bind( $var->id, $d_new->bound_value ); | |||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||
| 250 | 0 | return $subst->bind( $var->id, $d_new ); | |||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||
| 252 | else { | ||||||||||||||||||||||||||||||||||||
| 253 | 22 | ▶ | if ( $val->is_bound ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 254 | 0 | return $subst->bind( $var->id, $val->bound_value ); | |||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||
| 256 | 22 | return $subst->bind( $var->id, $val ); | |||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||
| 259 | else { | ||||||||||||||||||||||||||||||||||||
| 260 | 960 | ▶ | if ($has_d_var) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 261 | 119 | ▶ | return unless $val =~ /^-?\d+$/x; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 262 | 119 | ▶ | return unless $d_var->contains($val); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 263 | 119 | return $subst->bind( $var->id, $val ); | |||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||
| 265 | else { | ||||||||||||||||||||||||||||||||||||
| 266 | 841 | return $subst->bind( $var->id, $val ); | |||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||
| 271 | 1266 | ▶ | sub occurs_in ( $var_id, $term, $subst ) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
occurs_in: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 272 | 1266 | $term = $subst->walk($term); | |||||||||||||||||||||||||||||||||||
| 273 | 1266 | ▶ | if ( ref($term) eq 'Logic::Relational::Variable' ) { ## no critic (ProhibitCascadingIfElse) | ||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||
| 274 | 169 | return $term->id == $var_id; | |||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||
| 276 | elsif ( ref($term) eq 'Logic::Relational::Term' ) { | ||||||||||||||||||||||||||||||||||||
| 277 | 1 | for my $arg ( @{ $term->args } ) { | |||||||||||||||||||||||||||||||||||
| 278 | 1 | ▶ | return 1 if occurs_in( $var_id, $arg, $subst ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||
| 281 | elsif ( ref($term) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||
| 282 | 77 | for my $arg (@$term) { | |||||||||||||||||||||||||||||||||||
| 283 | 110 | ▶ | return 1 if occurs_in( $var_id, $arg, $subst ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||
| 286 | elsif ( ref($term) eq 'Logic::Relational::ArrayRest' ) { | ||||||||||||||||||||||||||||||||||||
| 287 | 20 | return occurs_in( $var_id, $term->term, $subst ); | |||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||
| 289 | elsif ( ref($term) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||
| 290 | 4 | for my $val ( values %$term ) { | |||||||||||||||||||||||||||||||||||
| 291 | 5 | ▶ | return 1 if occurs_in( $var_id, $val, $subst ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||
| 294 | 1074 | return 0; | |||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||
| 297 | 33 | 1; | |||||||||||||||||||||||||||||||||||