/
Predicates.pm
427 lines (342 loc) · 8.45 KB
/
Predicates.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#
# Copyright (c) 2014-2015 Christian Jaeger, copying@christianjaeger.ch
#
# This is free software, offered under either the same terms as perl 5
# or the terms of the Artistic License version 2 or the terms of the
# MIT License (Expat version). See the file COPYING.md that came
# bundled with this file.
#
=head1 NAME
FP::Predicates
=head1 SYNOPSIS
package Foo;
use FP::Predicates;
*is_age= both *is_natural0, sub { $_[0] < 130 };
# ^ wrap in BEGIN { } to employ namespace cleaning;
# or assign to a scalar instead (my $is_age), of course;
# or use an inline expression (second line below)
use FP::Struct [[*is_string, "name"], [*is_age, "age"]];
# use FP::Struct [[*is_string, "name"],
# [both (*is_natural0, less_than 130), "age"]];
_END_
=head1 DESCRIPTION
Useful as predicates for FP::Struct field definitions.
These are simple functions expecting one value and returning a
boolean. They are composable with `maybe`, `complement`, `either`,
`all_of`/`both`.
This is a functional approach to achieve the same aim as
`Moose::Util::TypeConstraints`, which basically uses a syntactical
sublanguage instead (implemented as a mix of functions and string
interpretation). It was written because it's way simpler. The drawback
is that (currently) there's no way to get a nice message string from
them to say why a match fails. Perhaps it would be possible to do so
using more introspection? (That would be nice because message
generation would be fully automatic and hence consistent.) Or,
alternatively, modifying the functions to compose messages themselves
when they fail (still mostly automatic), e.g. using message objects
that are false.
=cut
package FP::Predicates;
@ISA="Exporter"; require Exporter;
@EXPORT=qw(
is_pure
is_pure_object
is_pure_class
is_string
is_nonnullstring
is_natural0
is_natural
is_even is_odd
is_boolean01
is_booleanyesno
is_boolean
is_hash
is_array
is_procedure
is_class_name
instance_of
is_instance_of
is_subclass_of
is_filehandle
is_filename
is_sequence
less_than
greater_than
less_equal
greater_equal
is_zero
maybe
is_defined
is_true
true
is_false
false
complement
either
all_of both
);
@EXPORT_OK=qw(
is_coderef
);
%EXPORT_TAGS=(all=>[@EXPORT,@EXPORT_OK]);
use strict; use warnings; use warnings FATAL => 'uninitialized';
use Chj::TEST;
use FP::Pure;
use Chj::BuiltinTypePredicates 'is_filehandle';
# ^ should probably move more lowlevel predicates there
# XX check for read-only flags?
# is_pure returns true for non-references, going with the assumption
# that the caller created a copy of those anyway, in which case there
# is no reason for fear from mutations from scopes before it got
# control of the value:
sub is_pure ($) {
length (ref $_[0]) ? UNIVERSAL::isa ($_[0], "FP::Pure")
: 1
}
sub is_pure_object ($) {
length ref $_[0] and UNIVERSAL::isa ($_[0], "FP::Pure")
}
sub is_pure_class ($) {
is_class_name $_[0] and UNIVERSAL::isa ($_[0], "FP::Pure")
}
sub is_string ($) {
my ($v)=@_;
(defined $v
and not ref $v) # relax?
}
sub is_nonnullstring ($) {
my ($v)=@_;
(defined $v
and not ref $v # relax?
and length $v)
}
sub is_natural0 ($) {
my ($v)=@_;
(defined $v
and not ref $v # relax?
and $v=~ /^\d+\z/)
}
sub is_natural ($) {
my ($v)=@_;
(defined $v
and not ref $v # relax?
and $v=~ /^\d+\z/ and $v)
}
# XX careful these do not check for number types first
sub is_even ($) {
($_[0] & 1) == 0
}
sub is_odd ($) {
($_[0] & 1)
}
TEST { [map { is_even $_ } -3..3] }
['',1,'',1,'',1,''];
TEST { [map { is_odd $_ } -3..3] }
[1,0,1,0,1,0,1];
TEST { [map { is_even $_ } 3,3.1,4,4.1,-4.1] }
# XX what should it give?
['','',1,1,1];
# no `is_` prefix as those are not the final predicates (they are
# curried forms of < and > etc.):
# names? (number versus string comparison) (wish Perl hat generics
# for those instead..)
sub less_than ($) {
my ($x)=@_;
sub ($) {
$_[0] < $x
}
}
sub greater_than ($) {
my ($x)=@_;
sub ($) {
$_[0] > $x
}
}
sub less_equal ($) {
my ($x)=@_;
sub ($) {
$_[0] <= $x
}
}
sub greater_equal ($) {
my ($x)=@_;
sub ($) {
$_[0] >= $x
}
}
sub is_zero ($) {
$_[0] == 0
}
# strictly 0 or 1
sub is_boolean01 ($) {
not ref ($_[0]) # relax?
and $_[0]=~ /^[01]\z/
}
sub is_booleanyesno ($) {
my ($v)=@_;
not ref $v
and $v eq "yes" or $v eq "no"
}
# undef, 0, "", or 1
sub is_boolean ($) {
not ref ($_[0]) # relax?
and (! $_[0]
or
$_[0] eq "1");
}
sub is_hash ($) {
defined $_[0] and ref ($_[0]) eq "HASH"
}
sub is_array ($) {
defined $_[0] and ref ($_[0]) eq "ARRAY"
}
# Usually you should prefer `is_procedure` (see below) over this, as
# we like to pass globs as subroutine place holders, too.
sub is_coderef ($) {
defined $_[0] and ref ($_[0]) eq "CODE"
}
# Should this be called `is_subroutine` or `is_sub` instead, to cater
# for the traditional naming in Perl? But then Perl itself is
# inconsistent, too, calling those code refs, which matches the
# is_coderef naming above.
sub is_procedure ($) {
defined $_[0] and
(ref ($_[0]) eq "CODE"
or
(ref \($_[0]) eq "GLOB" ? *{$_[0]}{CODE} ? 1 : '' : ''))
# XX: also check for objects that overload '&'?
}
TEST { is_procedure [] } '';
TEST { is_procedure \&is_procedure } 1;
TEST { is_procedure *is_procedure } 1;
TEST { is_procedure *fifu } '';
my $classpart_re= qr/\w+/;
sub is_class_name ($) {
my ($v)= @_;
! length ref ($v) and $v=~ /^(?:${classpart_re}::)*$classpart_re\z/;
}
sub instance_of ($) {
my ($cl)=@_;
is_class_name $cl or die "need class name string, got: $cl";
sub ($) {
length ref $_[0] ? UNIVERSAL::isa ($_[0], $cl) : ''
}
}
sub is_instance_of ($$) {
my ($v,$cl)=@_;
# is_class_name $cl or die "need class name string, got: $cl";
length ref $v ? UNIVERSAL::isa ($v, $cl) : ''
}
sub is_subclass_of ($$) {
my ($v,$cl)=@_;
# is_class_name $cl or die "need class name string, got: $cl";
!length ref $v and UNIVERSAL::isa ($v, $cl);
}
TEST { my $v= "IO"; is_instance_of $v, "IO" } '';
TEST { my $v= bless [], "IO"; is_instance_of $v, "IO" } 1;
TEST { my $v= "IO"; is_subclass_of $v, "IO" } 1;
TEST { require Chj::IO::File;
is_subclass_of "Chj::IO::File", "IO" } 1;
# is_filename in Chj::BuiltinTypePredicates
TEST {[ map { is_filehandle $_ }
"STDOUT", undef,
*STDOUT, *STDOUT{IO}, \*STDOUT,
*SMK69GXDB, *SMK69GXDB{IO}, \*SMK69GXDB,
bless (\*WOFWEOXVV, "ReallyNotIO"),
do { open my $in, $0 or die $!;
#warn "HM".<$in>; # works
bless $in, "MightActullyBeIO" }
]}
['', '',
'', 1, 1,
'', '', '',
'',
1
];
# should probably be in a filesystem lib instead?
sub is_filename ($) {
my ($v)=@_;
(is_nonnullstring ($v)
and !($v=~ m|/|)
and !($v eq ".")
and !($v eq ".."))
}
# can't be in `FP::Sequence` since that package is for OO, well, what
# to do about it?
use FP::Lazy; # sigh dependency, too.
sub is_sequence ($);
sub is_sequence ($) {
length ref $_[0] ?
(UNIVERSAL::isa($_[0], "FP::Sequence")
or
# XX evil: inlined `is_promise`
UNIVERSAL::isa($_[0], "FP::Lazy::Promise")
&& is_sequence (force $_[0]))
: '';
}
sub maybe ($) {
@_==1 or die "wrong number of arguments";
my ($pred)=@_;
sub ($) {
my ($v)=@_;
defined $v ? &$pred ($v) : 1
}
}
# (this would also be a candidate for FP::Ops)
sub is_defined ($) {
defined $_[0]
}
sub is_true ($) {
!!$_[0]
}
# (this would also be a candidate as 'not' with a different name for
# FP::Ops)
sub is_false ($) {
@_==1 or die "wrong number of arguments";
!$_[0]
}
sub true {
1
}
sub false {
0
}
sub complement ($) {
@_==1 or die "wrong number of arguments";
my ($f)=@_;
sub {
! &$f(@_)
}
}
TEST {
my $t= complement (\&is_natural);
[map { &$t($_) } (-1,0,1,2,"foo")]
} [1,1,'','',1];
sub either {
my (@fn)=@_;
sub {
for my $fn (@fn) {
my $v= &$fn;
return $v if $v;
}
0
}
}
TEST {
my $t= either \&is_natural, \&is_boolean;
[map { &$t($_) } (-1,0,1,2,"foo")]
} [0,1,1,2,0];
sub all_of {
my (@fn)=@_;
sub {
for my $fn (@fn) {
return '' unless &$fn;
}
1
}
}
sub both ($$) {
@_==2 or die "expecting 2 arguments";
all_of (@_)
}
1