Skip to main content

fuzzy4

PyPI version Python 3.10+ License: MIT

A Python library for 4-valued fuzzy logic based on Belnap logic with product t-norm operations.

Overview

Traditional boolean logic has two values: True and False. Real-world knowledge often involves uncertainty (we don't know) and contradiction (conflicting evidence). Belnap's 4-valued logic addresses this by representing propositions as vectors (T, F):

State Vector Meaning
TRUE (1, 0) Confirmed, not refuted
FALSE (0, 1) Refuted, not confirmed
UNKNOWN (0, 0) Neither confirmed nor refuted
CONFLICT (1, 1) Both confirmed and refuted

This library extends Belnap logic with fuzzy values, where T and F are continuous values in [0, 1].

Installation

pip install fuzzy4

Quick Start

from fuzzy4 import FuzzyBool, TRUE, FALSE, UNKNOWN, CONFLICT

# Create fuzzy values
x = FuzzyBool(0.8, 0.2)  # 80% confirmed, 20% refuted
y = FuzzyBool(0.6, 0.3)  # 60% confirmed, 30% refuted

# Logical operations
result = x & y           # conjunction (AND)
result = x | y           # disjunction (OR)
result = ~x              # negation (NOT)
result = x >> y          # implication (x -> y)
result = x.iff(y)        # bi-implication (x <-> y)

# Accumulate evidence
result = x + y           # combines evidence from multiple sources
result += FuzzyBool(0.5, 0.1)           # in-place accumulation
result = FuzzyBool.accumulate(x, y, z)  # accumulate many values

# Check dominant state
print(x.dominant_state())  # "true", "false", "unknown", or "conflict"
print(x.is_true())         # True if predominantly true

Operations

Negation: ~x

Swaps confirmation and refutation:

x ~x
TRUE FALSE
UNKNOWN UNKNOWN
CONFLICT CONFLICT
FALSE TRUE
~FuzzyBool(0.8, 0.3)  # -> FuzzyBool(0.3, 0.8)

Conjunction: x & y

Both must confirm, any refutation propagates:

& TRUE UNKNOWN CONFLICT FALSE
TRUE TRUE UNKNOWN CONFLICT FALSE
UNKNOWN UNKNOWN UNKNOWN FALSE FALSE
CONFLICT CONFLICT FALSE CONFLICT FALSE
FALSE FALSE FALSE FALSE FALSE

Uses product t-norm for T and probabilistic s-norm for F:

  • T = Tx * Ty
  • F = Fx + Fy - Fx * Fy

Disjunction: x | y

Any confirmation suffices, both must refute:

| TRUE UNKNOWN CONFLICT FALSE
TRUE TRUE TRUE TRUE TRUE
UNKNOWN TRUE UNKNOWN TRUE UNKNOWN
CONFLICT TRUE TRUE CONFLICT CONFLICT
FALSE TRUE UNKNOWN CONFLICT FALSE

Uses probabilistic s-norm for T and product t-norm for F:

  • T = Tx + Ty - Tx * Ty
  • F = Fx * Fy

Implication: x >> y

Material implication: x -> y = ~x | y

>> TRUE UNKNOWN CONFLICT FALSE
TRUE TRUE UNKNOWN CONFLICT FALSE
UNKNOWN TRUE UNKNOWN TRUE UNKNOWN
CONFLICT TRUE TRUE CONFLICT CONFLICT
FALSE TRUE TRUE TRUE TRUE

Bi-implication: x.iff(y)

Equivalence: x <-> y = (x -> y) & (y -> x)

iff TRUE UNKNOWN CONFLICT FALSE
TRUE TRUE UNKNOWN CONFLICT FALSE
UNKNOWN UNKNOWN UNKNOWN TRUE UNKNOWN
CONFLICT CONFLICT TRUE CONFLICT CONFLICT
FALSE FALSE UNKNOWN CONFLICT TRUE

Evidence Accumulation: x + y

Combines evidence using probabilistic sum — useful for aggregating information from multiple sources:

+ TRUE UNKNOWN CONFLICT FALSE
TRUE TRUE TRUE CONFLICT CONFLICT
UNKNOWN TRUE UNKNOWN CONFLICT FALSE
CONFLICT CONFLICT CONFLICT CONFLICT CONFLICT
FALSE CONFLICT FALSE CONFLICT FALSE
weak = FuzzyBool(0.3, 0.0)     # weak confirmation
result = weak + weak + weak    # -> FuzzyBool(0.657, 0.0)

# In-place accumulation
total = UNKNOWN
for source in sources:
    total += source            # accumulates evidence iteratively

# Accumulate a list of values
FuzzyBool.accumulate([source1, source2, source3])

Properties:

  • More confirmations → T approaches 1
  • More refutations → F approaches 1
  • UNKNOWN is the neutral element

Scalar Operations

x = FuzzyBool(0.8, 0.6)
x * 0.5              # -> FuzzyBool(0.4, 0.3)
x / 2.0              # -> FuzzyBool(0.4, 0.3)

Component Extraction

Each FuzzyBool has four derived properties representing the degree of each logical state:

x = FuzzyBool(0.7, 0.4)

x.truth     # degree of pure truth (confirmed AND not refuted)
x.falsity   # degree of pure falsity (refuted AND not confirmed)
x.unknown   # degree of uncertainty (neither confirmed nor refuted)
x.conflict  # degree of contradiction (both confirmed and refuted)

x.as_dict()  # {"truth": ..., "falsity": ..., "unknown": ..., "conflict": ...}

Use Cases

  • Knowledge bases: Handle incomplete or contradictory information
  • Sensor fusion: Combine readings from multiple sensors with varying reliability
  • Expert systems: Model uncertainty in rule-based reasoning
  • Database queries: Represent null/unknown values with more nuance than SQL's 3-valued logic

Mathematical Background

This implementation uses:

  • Product t-norm for conjunction: a ⊗ b = a × b
  • Probabilistic s-norm for disjunction: a ⊕ b = a + b - a × b
  • Standard negation: ¬a = 1 - a

De Morgan's laws hold: ~(x & y) = ~x | ~y and ~(x | y) = ~x & ~y

Development

# Clone the repository
git clone https://github.com/mihail-gribov/fuzzy4.git
cd fuzzy4

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=fuzzy4

License

MIT License — see LICENSE for details.

References

  • Belnap, N. D. (1977). "A useful four-valued logic"
  • Fitting, M. (1994). "Kleene's Three Valued Logics and Their Children"

Release files for fuzzy4 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fuzzy4 0.1.0
File Size Uploaded
fuzzy4-0.1.0.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fuzzy4 0.1.0
File Interpreter ABI Platform
fuzzy4-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.3 kB

Release files / fuzzy4-0.1.0.tar.gz

Download URL fuzzy4-0.1.0.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b5be99e255a1802e2d26de2749ef188ffa81755a0bc30791151906659c92b10e
BLAKE2b-256 checksum
How to use checksums
9c72eac1eded5b2b87766d8fe37ae8d12c5842a1011db647caa29dd53ed180fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / fuzzy4-0.1.0-py3-none-any.whl

Download URL fuzzy4-0.1.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
85c053a9cf147055126770461c2599dcf5d76b34dd6c1a53924f6c103c726476
BLAKE2b-256 checksum
How to use checksums
006be000266c1f83952c7c8dc03fed5373545176be031b53508c0403b42bce07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page