The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Logic::Expr - logical expression parsing and related routines

SYNOPSIS

  use Logic::Expr::Parser;
  my $expr = Logic::Expr::Parser->new->from_string('Xv~Y');

  use Data::Dumper; 
  print Dumper $expr->solutions;

  $Logic::Expr::bools[1] = 0;    # fiddle with Y
  print $expr->solve;
  # (hopefully it is Y, better to find it through atoms)

  # build, invoke a solver function
  my $fn = $expr->codex;
     $fn->( X => 0, Y => 0 );

  # complicated, see the code
  $expr->walk( sub { ... }, sub { ... } );

  # clean out the atoms and booleans
  Logic::Expr::reset;

DESCRIPTION

Logic::Expr::Parser parses logic expressions of a particular syntax and returns a Logic::Expr object. This module in turn has various methods for solving all possible solutions, etc.

But What Is It Good For?

Good question! Let's say you walk into a bar on Vulcan, live on planet Earth, and cannot be on Vulcan and Earth. (I know, bear with me here, it's just an example.) So,

  V      walk into a bar on Vulcan
  E      live on planet Earth
  !(V&E) cannot be something-about Vulcan and living on Earth

This module can help solve logical problems such as the one presented, and also the harder ones that involve a Rabbi, etc. There's a link to a book, too, somewhere down below from here (or up, if you're on one of them newfangled devices that scroll in the wrong direction).

eg/solver may also be of interest under this module's distribution.

METHODS

Since module version 0.02 the atoms and bools methods have been removed; instead use %atoms or @bools in the module if need be.

codex

Returns a function reference built with walk (and a string eval!) that when called with suitable parameters calculates the logic of the expr.

  my $e  = Logic::Expr::Parser->new->from_string('Xv~Y');
  my $fn = $e->codex;
     $fn->( X => 0, Y => 0 );
expr

Returns parse tree, which could be a scalar reference, something more complicated involving array references and more, or undef if the object was not setup right.

new

Constructor. Probably should supply the expr parameter, as shown in the code for Logic::Expr::Parser.

solutions noprefix

Solves all possible @bools states returning an array reference of the results. The optional noprefix argument, if true, prevents each solution from having the state of @bools prefixed to each result.

The prior state of @bools is restored after this call.

solve

Solves the expr using the current state of @bools. @bools atoms are all set to true by default though could be changed manually.

walk opfn atomfn

Walks through the expr, calling the atomfn for each atom and opfn for each operator found. codex uses this to generate a string to eval into a subroutine reference.

FUNCTIONS

Most of these are available for export, mostly to the parser module, probably via the :all tag.

TRUE
FALSE
LE_NOT
LE_AND
LE_OR
LE_COND
LE_BICOND

Exportable constant functions, the lot of them.

reset

Clears out the atoms and bools structures. These are as of module version 0.02 class-based so that a set of logic expressions (e.g. ~(A&B), ~A, and ~B) can all share the same memory addresses for A and B without having to rewire each expression. The downside is that Logic::Expr::reset will likely need to be called prior to parsing a new, unrelated set of expressions.

BUGS

None known.

SEE ALSO

There are various other logic modules on CPAN. In particular Math::Logic::Predicate appears to contain section 2 material while this module only has section 1 material from

"A Modern Formal Logic Primer". Paul Teller. 1989.

https://tellerprimer.ucdavis.edu/

COPYRIGHT AND LICENSE

Copyright 2022 Jeremy Mates

This program is distributed under the (Revised) BSD License: https://opensource.org/licenses/BSD-3-Clause