Skip to content

Quick Start

Get started with FuzzyInfer in 5 minutes!

Installation

pip install fuzzy-infer

Your First Inference

Example 1: Classical Logic

Let's start with the classic Socrates syllogism:

from fuzzy_infer import fuzzy

# Create knowledge base and run inference
kb = (
    fuzzy()
    .fact("is-human", ["socrates"])
    .fact("is-greek", ["socrates"])
    .rule(
        when=("is-human", "?x"),
        then=("is-mortal", "?x")
    )
    .rule(
        when=[("is-human", "?x"), ("is-greek", "?x")],
        then=("is-philosopher", "?x", 0.8)  # 80% confidence
    )
    .infer()
)

# Query results
print(kb.query("is-mortal", ["socrates"]))
# Output: [Fact(predicate='is-mortal', args=['socrates'], degree=1.0)]

print(kb.query("is-philosopher", ["socrates"]))
# Output: [Fact(predicate='is-philosopher', args=['socrates'], degree=0.8)]

Example 2: Fuzzy Animal Classification

Real-world reasoning with uncertainty:

from fuzzy_infer import FuzzySession

with FuzzySession() as session:
    # Add fuzzy facts about animals
    session.facts(
        ("has-wings", ["robin"], 1.0),
        ("has-feathers", ["robin"], 0.95),
        ("small-size", ["robin"], 0.8),
        ("has-wings", ["bat"], 0.9),
        ("has-fur", ["bat"], 1.0),
        ("nocturnal", ["bat"], 0.95)
    )

    # Add classification rules
    session.rule(
        when=[("has-wings", "?x"), ("has-feathers", "?x")],
        then=("is-bird", "?x"),
        name="bird-classifier"
    )

    session.rule(
        when=[("has-wings", "?x"), ("has-fur", "?x")],
        then=("is-mammal", "?x", 0.9),
        name="flying-mammal"
    )

    # Run inference and check results
    session.infer()

    birds = session.query("is-bird")
    print(f"Birds found: {birds}")
    # Birds found: [robin with degree 0.95]

    mammals = session.query("is-mammal")
    print(f"Mammals found: {mammals}")
    # Mammals found: [bat with degree 0.81]

Command-Line Usage

Interactive REPL

# Start interactive session
fuzzy-infer repl

> fact is-bird robin 0.9
> fact has-wings robin 1.0
> rule bird-flight: is-bird ?x -> can-fly ?x 0.85
> infer
> query can-fly
can-fly(robin) = 0.765

Unix Pipeline

# Create facts file
cat > facts.jsonl << EOF
{"type":"fact","pred":"is-bird","args":["robin"],"degree":0.9}
{"type":"fact","pred":"is-bird","args":["eagle"],"degree":1.0}
EOF

# Create rules file
cat > rules.jsonl << EOF
{"type":"rule","name":"can-fly","when":[{"pred":"is-bird","args":["?x"]}],"then":[{"action":"add","pred":"can-fly","args":["?x"],"degree":0.9}]}
EOF

# Run inference pipeline
cat facts.jsonl | fuzzy-infer run rules.jsonl | fuzzy-infer query can-fly

Pattern Variables

Pattern variables (starting with ?) enable powerful rule matching:

from fuzzy_infer import fuzzy

# Family relationships
kb = (
    fuzzy()
    # Facts about family
    .fact("parent", ["alice", "bob"])
    .fact("parent", ["bob", "charlie"])
    .fact("parent", ["alice", "diana"])

    # Rules for relationships
    .rule(
        when=("parent", "?x", "?y"),
        then=("child", "?y", "?x")
    )
    .rule(
        when=[("parent", "?x", "?y"), ("parent", "?y", "?z")],
        then=("grandparent", "?x", "?z")
    )
    .rule(
        when=[("parent", "?p", "?x"), ("parent", "?p", "?y")],
        then=("sibling", "?x", "?y"),
        where=lambda bindings: bindings["?x"] != bindings["?y"]
    )
    .infer()
)

# Query relationships
print(kb.query("grandparent"))  # alice is grandparent of charlie
print(kb.query("sibling"))      # bob and diana are siblings

Streaming Inference

Process continuous streams of facts:

from fuzzy_infer import stream_session

# Create streaming session
async with stream_session() as stream:
    # Add rules
    stream.rule(
        when=("temperature", "?sensor", "?temp"),
        then=("alert", "?sensor", "high"),
        where=lambda b: b["?temp"] > 30
    )

    # Stream facts from sensor
    async for fact in sensor_stream():
        await stream.add_fact(fact)
        alerts = await stream.query("alert")
        if alerts:
            print(f"ALERT: {alerts}")

Working with Degrees

FuzzyInfer supports various fuzzy operations:

from fuzzy_infer import fuzzy

kb = fuzzy()

# Fuzzy AND (minimum)
kb.rule(
    when=[
        ("condition-a", "?x", degree=">0.7"),
        ("condition-b", "?x", degree=">0.6")
    ],
    then=("result", "?x"),
    fuzzy_and="min"  # Uses minimum of degrees
)

# Fuzzy OR (maximum)
kb.rule(
    when_any=[  # OR conditions
        ("option-a", "?x"),
        ("option-b", "?x")
    ],
    then=("selected", "?x"),
    fuzzy_or="max"  # Uses maximum of degrees
)

# Degree modifiers
kb.rule(
    when=("likely", "?x", degree="?d"),
    then=("very-likely", "?x", degree="?d * ?d")  # Square for "very"
)

Next Steps

Now that you've got the basics:

  1. Core Concepts - Understand fuzzy logic fundamentals
  2. Fluent API Guide - Master the Python API
  3. Unix CLI Guide - Build inference pipelines
  4. Examples - See more real-world use cases

Getting Help