lib/Logic/Relational/StdLib.pm

filter: Statementstmt 96.4% 82 / 85 Branchbran 66.6% 4 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 12 / 15 Podpod 0.0% 0 / 1 totaltotal 88.3% 99 / 112 CC 5 SCAR 5.4
CC
5
Cov
93%
__ANON__
3.2
export_to_program
2.0
__ANON__
2.0
CRAP 1.7
« blib/lib/Logic/Relational/Variable.pm ↑ Summary lib/Logic/Relational/Goal/EndCall.pm »
1 package Logic::Relational::StdLib;
2  
3 10 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Logic::Relational qw(variable call all guard);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Logic::Relational::DSL qw(rest is_goal true_goal);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Logic::Relational::StdLib - Standard Relational Logic Library.
11  
12 =cut
13  
14 21 ▶sub export_to_program ($program) {
Subroutine export_to_program: called CC=2 SCAR=6.9
Pod undocumented
15 21 ▶ return if $program->{_has_stdlib_exported}++;
Branch: 1 branch
BranchTrueFalseTotal
if $program->{"_has_stdlib_exported"}++ 0 21 21
16  
17 # 1. append(L1, L2, Res)
18 21 my $l = variable('L');
19 21 my $h = variable('H');
20 21 my $t = variable('T');
21 21 my $l2 = variable('L2');
22 21 my $out_t = variable('OutT');
23  
24 21 $program->fact( append => [], $l, $l );
25 21 $program->rule(
26 head => call( append => [ $h, rest($t) ], $l2, [ $h, rest($out_t) ] ),
27 body => call( append => $t, $l2, $out_t )
28 );
29  
30 # 2. member(X, List)
31 21 my $mx = variable('X');
32 21 my $my = variable('Y');
33 21 my $mrest = variable('Rest');
34  
35 21 $program->fact( member => $mx, [ $mx, rest($mrest) ] );
36 21 $program->rule(
37 head => call( member => $mx, [ $my, rest($mrest) ] ),
38 body => call( member => $mx, $mrest )
39 );
40  
41 # 2b. not_member(X, List)
42 21 my $nmx = variable('X');
43 21 my $nmy = variable('Y');
44 21 my $nmrest = variable('Rest');
45  
46 21 $program->fact( not_member => $nmx, [] );
47 21 $program->rule(
48 head => call( not_member => $nmx, [ $nmy, rest($nmrest) ] ),
49 body => all(
50 Logic::Relational::Goal::Not->new(
51 Logic::Relational::Goal::Unify->new( $nmx, $nmy )
52 ),
53 call( not_member => $nmx, $nmrest )
54 )
55 );
56  
57 # 3. length(List, Len)
58 21 my $lh = variable('H');
59 21 my $lt = variable('T');
60 21 my $llen = variable('Len');
61 21 my $ltlen = variable('TLen');
62  
63 21 $program->fact( list_length => [], 0 );
64 21 $program->fact( length => [], 0 );
65  
66 $program->rule(
67 head => call( list_length => [ $lh, rest($lt) ], $llen ),
68 body => all(
69 call( list_length => $lt, $ltlen ),
70 0 ▶ is_goal( $llen, [$ltlen], sub ($val) { return $val + 1 } )
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 21 )
72 );
73 $program->rule(
74 head => call( length => [ $lh, rest($lt) ], $llen ),
75 body => all(
76 call( length => $lt, $ltlen ),
77 4 ▶ is_goal( $llen, [$ltlen], sub ($val) { return $val + 1 } )
Subroutine __ANON__: called CC=1 SCAR=0.0
78 21 )
79 );
80  
81 # 4. reverse(List, Rev)
82 21 my $rl = variable('L');
83 21 my $rrev = variable('Rev');
84 21 my $rh = variable('H');
85 21 my $rt = variable('T');
86 21 my $racc = variable('Acc');
87  
88 21 $program->rule(
89 head => call( reverse_list => $rl, $rrev ),
90 body => call( reverse_acc => $rl, [], $rrev )
91 );
92 21 $program->rule(
93 head => call( reverse => $rl, $rrev ),
94 body => call( reverse_acc => $rl, [], $rrev )
95 );
96 21 $program->fact( reverse_acc => [], $racc, $racc );
97 21 $program->rule(
98 head => call( reverse_acc => [ $rh, rest($rt) ], $racc, $rrev ),
99 body => call( reverse_acc => $rt, [ $rh, rest($racc) ], $rrev )
100 );
101  
102 # 5. select(X, List, Rest)
103 21 my $sx = variable('X');
104 21 my $sy = variable('Y');
105 21 my $sxs = variable('Xs');
106 21 my $sys = variable('Ys');
107 21 my $szs = variable('Zs');
108  
109 21 $program->fact( select => $sx, [ $sx, rest($sxs) ], $sxs );
110 21 $program->rule(
111 head => call( select => $sx, [ $sy, rest($sys) ], [ $sy, rest($szs) ] ),
112 body => call( select => $sx, $sys, $szs )
113 );
114  
115 # 6. permutation(List, Perm)
116 21 my $px = variable('X');
117 21 my $pxs = variable('Xs');
118 21 my $pys = variable('Ys');
119 21 my $pzs = variable('Zs');
120  
121 21 $program->fact( permutation => [], [] );
122 21 $program->rule(
123 head => call( permutation => [ $px, rest($pxs) ], $pzs ),
124 body => all(
125 call( permutation => $pxs, $pys ),
126 call( select => $px, $pzs, $pys )
127 )
128 );
129  
130 # 7. succ(X, Y)
131 21 my $sx_val = variable('X');
132 21 my $sy_val = variable('Y');
133 $program->rule(
134 head => call( succ => $sx_val, $sy_val ),
135 1 ▶ body => is_goal( $sy_val, [$sx_val], sub ($v) { return $v + 1 } )
Subroutine __ANON__: called CC=1 SCAR=0.0
136 21 );
137  
138 # 8. min(X, Y, Min) & max(X, Y, Max)
139 21 my $mx1 = variable('X');
140 21 my $my1 = variable('Y');
141  
142 $program->rule(
143 head => call( min => $mx1, $my1, $mx1 ),
144 1 ▶ body => guard( [ $mx1, $my1 ], sub ( $a, $b ) { return $a <= $b } )
Subroutine __ANON__: called CC=1 SCAR=0.0
145 21 );
146 $program->rule(
147 head => call( min => $mx1, $my1, $my1 ),
148 1 ▶ body => guard( [ $mx1, $my1 ], sub ( $a, $b ) { return $a > $b } )
Subroutine __ANON__: called CC=1 SCAR=0.0
149 21 );
150  
151 $program->rule(
152 head => call( max => $mx1, $my1, $mx1 ),
153 1 ▶ body => guard( [ $mx1, $my1 ], sub ( $a, $b ) { return $a >= $b } )
Subroutine __ANON__: called CC=1 SCAR=0.0
154 21 );
155 $program->rule(
156 head => call( max => $mx1, $my1, $my1 ),
157 0 ▶ body => guard( [ $mx1, $my1 ], sub ( $a, $b ) { return $a < $b } )
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 21 );
159  
160 # 9. between(Low, High, Val) generator
161 $program->generator(
162 between => 3,
163 1 ▶ sub ( $low, $high, $val ) {
Subroutine __ANON__: called CC=3 SCAR=11.7
164 0 ▶ return sub { return () }
Subroutine __ANON__: not called CC=1 SCAR=6.9
165 1 ▶ unless defined $low && defined $high;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $low and defined $high 0 1 1
Condition33%1/3
defined $low and defined $high
!ll&&!rl&&r
001
MC/DC0%0/2
defined $low and defined $high
defined $low defined $high
Truth table33%1/3
A and B
A= defined $lowB= defined $high
ABresult
0X0
100
111
166 1 my $curr = $low;
167 4 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
168 4 ▶ return () if $curr > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $curr > $high 1 3 4
169 3 my $res = $curr++;
170 3 return [ $low, $high, $res ];
171 1 };
172 }
173 21 );
174  
175 21 return;
176 }
177  
178 10 1;
« blib/lib/Logic/Relational/Variable.pm ↑ Summary lib/Logic/Relational/Goal/EndCall.pm »