Skip to main content

4-valued fuzzy logic library based on Belnap logic

Project description

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"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fuzzy4-0.1.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fuzzy4-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file fuzzy4-0.1.0.tar.gz.

File metadata

  • Download URL: fuzzy4-0.1.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for fuzzy4-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b5be99e255a1802e2d26de2749ef188ffa81755a0bc30791151906659c92b10e
MD5 d9ca7585740be2a94bfb2ea54c6fbe38
BLAKE2b-256 9c72eac1eded5b2b87766d8fe37ae8d12c5842a1011db647caa29dd53ed180fc

See more details on using hashes here.

File details

Details for the file fuzzy4-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fuzzy4-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for fuzzy4-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 85c053a9cf147055126770461c2599dcf5d76b34dd6c1a53924f6c103c726476
MD5 30702057338bdb2c050dc0757631f9e4
BLAKE2b-256 006be000266c1f83952c7c8dc03fed5373545176be031b53508c0403b42bce07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page