Rand Stats

Logic::Ternary

zef:FCO

Actions Status

NAME

Logic::Ternary — Ternary logic for Raku

SYNOPSIS

use Logic::Ternary;

my $t = True;
my $f = False;
my $u = Unknown;

# Negation
dd not3 $t;              # Logic::Ternary::False
dd not3 $u;              # Logic::Ternary::Unknown

# Conjunction/Disjunction/xor (operands are coerced to Ternary)
dd $t and3 $u;            # Logic::Ternary::Unknown
dd $t or3  $u;            # Logic::Ternary::True
dd $t xor3 $t;            # Logic::Ternary::False

# Coercions
dd  1 .Ternary;          # Logic::Ternary::True
dd -3 .Ternary;          # Logic::Ternary::False
dd  0 .Ternary;          # Logic::Ternary::Unknown
dd Bool::True.Ternary;   # Logic::Ternary::True
dd Bool.Ternary;         # Logic::Ternary::Unknown

DESCRIPTION

Logic::Ternary implements a three-valued logic for Raku:

Values are Logic::Ternary::True, Logic::Ternary::Unknown, and Logic::Ternary::False.

Operators

These operators always coerce both operands with .Ternary:

Quick examples

use Logic::Ternary;

my $u = Unknown;

# and3 behavior
dd $u and3 True;   # Logic::Ternary::Unknown
dd $u and3 False;  # Logic::Ternary::False

# or3 behavior
dd True or3 $u;    # Logic::Ternary::True
dd $u or3 False;   # Logic::Ternary::Unknown

# Mixed types are coerced
dd 1 and3 0;       # Logic::Ternary::Unknown
dd -2 or3  1;      # Logic::Ternary::True

dd not3 1;         # Logic::Ternary::False

Exports

By default, use Logic::Ternary; exports the enum names

and the operators:

You can customize which enum names are exported by passing options to the use line:

use Logic::Ternary;                 # True/Unknown/False
use Logic::Ternary <True>;          # same as default
use Logic::Ternary <True3>;         # True3/Unknown3/False3
use Logic::Ternary <KnownTrue>;     # KnownTrue/Unknown/KnownFalse
use Logic::Ternary <Yes Maybe No>;  # custom names
use Logic::Ternary <none>;          # no constants; operators only
say (not3 1).Int;                   # -1
say Logic::Ternary::True.raku;      # 'Logic::Ternary::True'

AUTHOR

Fernando Corrêa de Oliveira fco@cpan.org

COPYRIGHT AND LICENSE

Copyright 2025 Fernando Corrêa de Oliveira

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.