#!/usr/bin/env perl

use 5.022;
use strict;
use warnings;
use Math::DifferenceSet::Planar;

$| = 1;

my $NUM = qr/(?:0|[1-9][0-9]*)/;

while (@ARGV && $ARGV[0] =~ /^-(.+)/s) {
    my $opt = $1;
    shift @ARGV;
    last if $opt eq q[-];
    die "usage: pds_verify [file]...\n";
}

my $syntax  = 0;
my $range   = 0;
my $not     = 0;
my $proven  = 0;
while (<<>>) {
    s/^\s+//;
    my @e = split /\s+/;
    next if !@e;

    ++$syntax, next if grep { !/^$NUM\z/ } @e;
    my $check = Math::DifferenceSet::Planar->verify_elements(@e);
    ++$range, next if !defined $check;
    ++$not,   next if !$check;
    ++$proven;
    print "@e\n";
}

warn "$syntax set(s) syntactically wrong\n"  if $syntax;
warn "$range set(s) not normalized\n"        if $range;
warn "$not set(s) proven to be wrong\n"      if $not;
warn "$proven set(s) proven to be correct\n" if $proven;

exit($syntax + $range + $not ? 1 : 0);

__END__

=head1 NAME

pds_verify - verify lists of integers as planar difference sets

=head1 SYNOPSIS

  pds_verify [-q] [file]...

=head1 DESCRIPTION

This example program reads planar difference sets, one per line,
as integer numbers separated by whitespace, verifies them, and writes
those of them them back to standard output that are correct.  The program
concludes with a summary of the check results on standard error.  The exit
code is 0 (success) if no lines had to be filtered out, otherwise 1.

=head1 BUGS AND LIMITATIONS

The verification method of this tool is based on a full operator table
and thus does not scale very well.  Its answer is conclusive, though.

A faster verification method is to check the success of one of
the other example programs reading difference sets, like pds_info.
Their range is limited by the contents of the database of sample sets.
If within that range, their success is a valid verification, but they
might wrongly reject a counterexample to the conjecture that all cyclic
planar difference sets are Singer type.  Outside that range, everything
is rejected.

=head1 AUTHOR

Martin Becker, E<lt>becker-cpan-mp I<at> cozap.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2019-2025 by Martin Becker, Blaubeuren.

This library is free software; you can distribute it and/or modify it
under the terms of the Artistic License 2.0 (see the LICENSE file).

The license grants freedom for related software development but does
not cover incorporating code or documentation into AI training material.
Please contact the copyright holder if you want to use the library whole
or in part for other purposes than stated in the license.

=head1 DISCLAIMER OF WARRANTY

This library is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose.

=cut
