| 1 | package Logic::Relational::Substitution; | ||||||||||||||||||||||||||
| 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 | =head1 NAME | ||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||
| 8 | Logic::Relational::Substitution - Represents an immutable variable substitution mapping. | ||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||
| 12 | use Logic::Relational::Substitution; | ||||||||||||||||||||||||||
| 13 | my $subst = Logic::Relational::Substitution->new; | ||||||||||||||||||||||||||
| 14 | my $new_subst = $subst->bind($var_id => $val); | ||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||
| 18 | C<Logic::Relational::Substitution> stores logical variable bindings. It is | ||||||||||||||||||||||||||
| 19 | immutable (copy-on-write) to make depth-first backtracking search simple and | ||||||||||||||||||||||||||
| 20 | robust without needing manual rollbacks of variables. | ||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||
| 22 | =head1 METHODS | ||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||
| 24 | =head2 new | ||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||
| 26 | Constructor. Optionally takes a hash reference of initial bindings. | ||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||
| 28 | =head2 bind | ||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||
| 30 | Takes a variable ID and a value, returning a new C<Logic::Relational::Substitution> | ||||||||||||||||||||||||||
| 31 | object containing the new binding. | ||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||
| 33 | =head2 walk | ||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||
| 35 | Dereferences a term through the substitution. If the term is a variable and is bound, | ||||||||||||||||||||||||||
| 36 | recursively dereferences until it hits an unbound variable or a non-variable term. | ||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||
| 38 | =head2 reify | ||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||
| 40 | Recursively walks a term, replacing all bound variables within compound terms | ||||||||||||||||||||||||||
| 41 | with their bound values. Unbound variables remain unchanged. | ||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||
| 45 | 1348 | ▶ | sub new ( $class, $bindings = {} ) { | ||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||
| 46 | 1348 | return bless { bindings => $bindings }, $class; | |||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||
| 49 | 1204 | ▶ | sub bind ( $self, $var_id, $val ) { | ||||||||||||||||||||||||
|
Subroutine
bind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||
| 50 | 1204 | my $new_bindings = { %{ $self->{bindings} }, $var_id => $val }; | |||||||||||||||||||||||||
| 51 | 1204 | return ref($self)->new($new_bindings); | |||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||
| 54 | 13719 | ▶ | sub walk ( $self, $term ) { | ||||||||||||||||||||||||
|
Subroutine
walk: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||
| 55 | 13719 | ▶ | while ( ref($term) eq 'Logic::Relational::Variable' ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 56 | 6782 | my $id = $term->id; | |||||||||||||||||||||||||
| 57 | 6782 | ▶ | if ( exists $self->{bindings}{$id} ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 58 | 4291 | my $bound = $self->{bindings}{$id}; | |||||||||||||||||||||||||
| 59 | 4291 | ▶ | if ( ref($bound) eq 'Logic::Relational::Domain' ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 60 | 427 | last; | |||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||
| 62 | 3864 | $term = $bound; | |||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||
| 65 | 2491 | last; | |||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||
| 68 | 13719 | return $term; | |||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||
| 71 | 5638 | ▶ | sub reify ( $self, $term ) { | ||||||||||||||||||||||||
|
Subroutine
reify: called CC=13 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||
| 72 | 5638 | $term = $self->walk($term); | |||||||||||||||||||||||||
| 73 | 5638 | ▶ | if ( ref($term) eq 'Logic::Relational::Variable' ) { ## no critic (ProhibitCascadingIfElse) | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 74 | 1157 | return $term; | |||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||
| 76 | elsif ( ref($term) eq 'Logic::Relational::Term' ) { | ||||||||||||||||||||||||||
| 77 | 815 | my @reified_args = map { $self->reify($_) } @{ $term->args }; | |||||||||||||||||||||||||
| 78 | 815 | return ref($term)->new( | |||||||||||||||||||||||||
| 79 | functor => $term->functor, | ||||||||||||||||||||||||||
| 80 | args => \@reified_args, | ||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||
| 83 | elsif ( ref($term) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
| 84 | 556 | my @flat; | |||||||||||||||||||||||||
| 85 | 556 | for my $elem (@$term) { | |||||||||||||||||||||||||
| 86 | 1085 | my $r_elem = $self->reify($elem); | |||||||||||||||||||||||||
| 87 | 1085 | ▶ | if ( ref($r_elem) eq 'Logic::Relational::ArrayRest' ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 88 | 35 | my $inner = $r_elem->term; | |||||||||||||||||||||||||
| 89 | 35 | ▶ | if ( ref($inner) eq 'ARRAY' ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 90 | 23 | push @flat, @$inner; | |||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||
| 93 | 12 | push @flat, $r_elem; | |||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||
| 97 | 1050 | push @flat, $r_elem; | |||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||
| 100 | 556 | return \@flat; | |||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||
| 102 | elsif ( ref($term) eq 'Logic::Relational::ArrayRest' ) { | ||||||||||||||||||||||||||
| 103 | 41 | return ref($term)->new( $self->reify( $term->term ) ); | |||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||
| 105 | elsif ( ref($term) eq 'HASH' ) { | ||||||||||||||||||||||||||
| 106 | 3 | my %flat; | |||||||||||||||||||||||||
| 107 | 3 | for my $k ( keys %$term ) { | |||||||||||||||||||||||||
| 108 | 4 | my $r_val = $self->reify( $term->{$k} ); | |||||||||||||||||||||||||
| 109 | 4 | ▶ | if ( ref($r_val) eq 'Logic::Relational::ArrayRest' ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 110 | 0 | my $inner = $r_val->term; | |||||||||||||||||||||||||
| 111 | 0 | ▶ | if ( ref($inner) eq 'HASH' ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 112 | 0 | for my $sub_k ( keys %$inner ) { | |||||||||||||||||||||||||
| 113 | 0 | $flat{$sub_k} = $inner->{$sub_k}; | |||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||
| 116 | else { | ||||||||||||||||||||||||||
| 117 | 0 | $flat{$k} = $r_val; | |||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||
| 121 | 4 | $flat{$k} = $r_val; | |||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||
| 124 | 3 | return \%flat; | |||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||
| 126 | else { | ||||||||||||||||||||||||||
| 127 | 3066 | return $term; | |||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||
| 131 | 33 | 1; | |||||||||||||||||||||||||