foras 0.4.0

foras is a First-Order Reasoner which uses the principles of predicate logic to derive new facts, verify statements, and prove theorems from an existing knowledge base.
Documentation
<h1 align="center">
  <br>
  FORAS
  <br>
</h1>

<h4 align="center">
  A Rust library for automated reasoning in first-order logic.
</h4>

<p align="center">
  <a href="https://crates.io/crates/foras" target="_blank">
    <img src="https://img.shields.io/crates/v/foras" alt="Crates.io"/>
  </a>
  <a href="https://crates.io/crates/foras" target="_blank">
    <img src="https://img.shields.io/crates/d/foras" alt="Downloads"/>
  </a>
  <a href="https://docs.rs/foras" target="_blank">
    <img src="https://docs.rs/foras/badge.svg" alt="Documentation"/>
  </a>
  <a href="LICENSE" target="_blank">
    <img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License"/>
  </a>
</p>

<b>Author's bio:</b> πŸ‘‹πŸ˜€ Hi, I'm CryptoPatrick! I'm currently enrolled as an
Undergraduate student in Mathematics, at Chalmers & the University of Gothenburg, Sweden. <br>
If you have any questions or need more info, then please <a href="https://discord.gg/T8EWmJZpCB">join my Discord Channel: AiMath</a>

---

<p align="center">
  <a href="#-what-is-foras">What is Foras</a> β€’
  <a href="#-features">Features</a> β€’
  <a href="#-how-to-use">How To Use</a> β€’
  <a href="#-examples">Examples</a> β€’
  <a href="#-documentation">Documentation</a> β€’
  <a href="#-license">License</a>
</p>

## πŸ›Ž Important Notices
* **Logic System**: Implements first-order logic (FOL) with automated reasoning capabilities
* **Inference Engine**: Uses resolution and unification for theorem proving
* **Inspired by**: Prover9, Vampire, E Prover, Z3, and the TPTP library

<!-- TABLE OF CONTENTS -->
<h2 id="table-of-contents"> :pushpin: Table of Contents</h2>

<details open="open">
  <summary>Table of Contents</summary>
  <ol>
    <li><a href="#-what-is-foras">What is Foras</a></li>
    <li><a href="#-what-is-first-order-logic">What is First-Order Logic</a></li>
    <li><a href="#-features">Features</a></li>
      <ul>
        <li><a href="#-core-capabilities">Core Capabilities</a></li>
        <li><a href="#-reasoning-features">Reasoning Features</a></li>
        <li><a href="#-applications">Applications</a></li>
      </ul>
    <li><a href="#-architecture">Architecture</a></li>
    <li><a href="#-how-to-use">How to Use</a></li>
    <li><a href="#-examples">Examples</a></li>
    <li><a href="#-testing">Testing</a></li>
    <li><a href="#-documentation">Documentation</a></li>
    <li><a href="#-about-the-name">About the Name</a></li>
    <li><a href="#-license">License</a>
  </ol>
</details>

## πŸ€” What is Foras

`foras` (First-Order ReASoner) is a Rust library implementing a **first-order reasoner**, which automatically draws logical conclusions from statements expressed in **first-order logic (FOL)**. It provides automated theorem proving capabilities, enabling you to build knowledge bases, submit queries, and derive logical conclusions automatically.

Built for reliability and performance in Rust, Foras enables:
- Building knowledge bases from logical formulas (axioms, facts, and rules)
- Submitting queries to check logical entailment
- Using inference rules (resolution, unification) to generate proofs or find counterexamples
- Automated theorem proving for AI, verification, and logical computing tasks

### Use Cases

- **Automated Theorem Proving**: Verify mathematical theorems and logical propositions
- **AI Knowledge Representation**: Build intelligent systems with formal reasoning capabilities
- **Program Verification**: Prove properties about programs and systems
- **Ontology Reasoning**: Semantic web and knowledge graph reasoning
- **Model Checking**: Verify system properties and specifications

## πŸ“– What is First-Order Logic?

**First-order logic (FOL)** extends propositional logic by introducing:

- **Predicates**: Properties or relations about objects (e.g., `Loves(Alice, Bob)`)
- **Variables**: Placeholders for objects (e.g., `x`, `y`)
- **Quantifiers**: For expressing generality or existence:
  - Universal quantifier: βˆ€x ("for all x")
  - Existential quantifier: βˆƒx ("there exists an x")

**Example:**
```
βˆ€x (Human(x) β†’ Mortal(x))
Human(Socrates)
⟹ Mortal(Socrates)
```

Foras can automatically derive `Mortal(Socrates)` from these premises using formal inference rules.

## πŸ“· Features

`foras` provides a complete first-order logic reasoning engine with automated theorem proving:

### πŸ”§ Core Capabilities
- **Knowledge Base Management**: Build and maintain sets of logical formulas
- **Query Resolution**: Check if queries logically follow from the knowledge base
- **Automated Inference**: Apply resolution and unification automatically
- **Proof Generation**: Produce formal proofs for entailed queries

### 🧠 Reasoning Features
- **Resolution-Based Inference**: Core inference engine using resolution
- **Unification Algorithm**: Pattern matching for logical terms
- **Skolemization**: Handle existential quantifiers systematically
- **Clause Normal Form**: Automatic conversion to CNF for reasoning

### πŸ€– Applications
- **Theorem Proving**: Verify mathematical and logical propositions
- **Knowledge Representation**: Formal representation of domain knowledge
- **Program Verification**: Prove correctness properties
- **Semantic Reasoning**: Ontology and knowledge graph inference

## πŸ“ Architecture

### 1. πŸ› Overall Architecture
```diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              User Application (CLI/Library)              β”‚
β”‚              reasoner.add() / reasoner.entails()         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Reasoner Component                     β”‚
β”‚  β€’ Parse formulas from string/AST                        β”‚
β”‚  β€’ Convert to Clause Normal Form (CNF)                   β”‚
β”‚  β€’ Apply Skolemization for βˆƒ quantifiers                 β”‚
β”‚  β€’ Store in knowledge base                               β”‚
β”‚  β€’ Run resolution algorithm for queries                  β”‚
β”‚  β€’ Generate proofs or countermodels                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚                          β”‚
       β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚  Parser/AST    β”‚        β”‚  Inference Engineβ”‚
       β”‚  β€’ Formula     β”‚        β”‚  β€’ Resolution    β”‚
       β”‚  β€’ Term        β”‚        β”‚  β€’ Unification   β”‚
       β”‚  β€’ Predicate   β”‚        β”‚  β€’ Subsumption   β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### 2. πŸšƒ Data Flow Diagram

```diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           reasoner.entails("Mortal(Socrates)")           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚    1. Parse      β”‚
              β”‚    Query         β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚
                       β”‚ Formula AST
                       β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚   2. Negate        β”‚
              β”‚   Query            β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
                        β”‚ Β¬Mortal(Socrates)
                        β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚   3. Convert       β”‚
              β”‚   KB + Β¬Query      β”‚
              β”‚   to CNF           β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
                        β”‚ Clause set
                        β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚   4. Resolution    β”‚
              β”‚   Loop             β”‚
              β”‚   β€’ Unify          β”‚
              β”‚   β€’ Resolve        β”‚
              β”‚   β€’ Check βŠ₯        β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
                        β”‚ Empty clause found?
                        β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚   5. Return        β”‚
              β”‚   β€’ true (proved)  β”‚
              β”‚   β€’ false + model  β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### 3. πŸ’Ύ Knowledge Base Structure

```diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Knowledge Base                        β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚                   Formula Store                      β”‚  β”‚
β”‚  β”‚  β€’ Universal formulas: βˆ€x P(x)                       β”‚  β”‚
β”‚  β”‚  β€’ Existential formulas: βˆƒy Q(y)                     β”‚  β”‚
β”‚  β”‚  β€’ Ground facts: Human(Socrates)                     β”‚  β”‚
β”‚  β”‚  β€’ Rules: P(x) β†’ Q(x)                                β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                               β”‚                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚              CNF Conversion Layer                     β”‚ β”‚
β”‚  β”‚  β€’ Eliminate implications                             β”‚ β”‚
β”‚  β”‚  β€’ Move negations inward                              β”‚ β”‚
β”‚  β”‚  β€’ Skolemize existentials                             β”‚ β”‚
β”‚  β”‚  β€’ Distribute disjunctions                            β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                               β”‚                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚                   Clause Database                     β”‚ β”‚
β”‚  β”‚  β€’ Set of clauses (disjunctions of literals)          β”‚ β”‚
β”‚  β”‚  β€’ Indexed for efficient unification                  β”‚ β”‚
β”‚  β”‚  β€’ Subsumption checking                               β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸš™ How to Use

### Installation

Add `foras` to your `Cargo.toml`:

```toml
[dependencies]
foras = "0.1"
```

Or install with cargo:

```bash
cargo add foras
```

### Basic Example

```rust
use foras::{Formula, Reasoner};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut reasoner = Reasoner::new();

    // Add premises
    reasoner.add(Formula::parse("βˆ€x (Human(x) β†’ Mortal(x))").unwrap());
    reasoner.add(Formula::parse("Human(Socrates)").unwrap());

    // Query
    let query = Formula::parse("Mortal(Socrates)").unwrap();
    if reasoner.entails(&query) {
        println!("Query is entailed!");
        println!("Proof: {:?}", reasoner.get_proof());
    }

    Ok(())
}
```

### Advanced Usage

```rust
use foras::{Formula, Reasoner, Term, Predicate};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut reasoner = Reasoner::new();

    // Build knowledge base programmatically
    reasoner.add_axiom("transitivity",
        "βˆ€x βˆ€y βˆ€z ((Ancestor(x, y) ∧ Ancestor(y, z)) β†’ Ancestor(x, z))"
    )?;

    reasoner.add_fact("Parent(Alice, Bob)")?;
    reasoner.add_fact("Parent(Bob, Charlie)")?;
    reasoner.add_rule("βˆ€x βˆ€y (Parent(x, y) β†’ Ancestor(x, y))")?;

    // Query with variables
    let query = Formula::parse("βˆƒz Ancestor(Alice, z)").unwrap();
    if let Some(bindings) = reasoner.solve(&query) {
        println!("Found solutions: {:?}", bindings);
    }

    // Get proof trace
    if reasoner.entails(&query) {
        let proof = reasoner.get_proof_trace();
        for step in proof.steps {
            println!("{}: {} by {}", step.index, step.clause, step.rule);
        }
    }

    Ok(())
}
```

## πŸ§ͺ Examples

The repository includes several examples demonstrating different features:

```bash
# Basic first-order logic reasoning
cargo run --example basic

# Propositional logic with resolution
cargo run --example propositional_logic_answer

# Group theory commutator properties
cargo run --example group_theory_commutator

# Pigeonhole principle proof
cargo run --example pigeonhole_principle

# Robbins algebra verification
cargo run --example robbins_algebra

# Classic zebra puzzle solver
cargo run --example zebra_puzzle
```

## πŸ§ͺ Testing

Run the test suite:

```bash
# Run all tests
cargo test

# Run with output
cargo test -- --nocapture

# Run smoke tests
cargo test --test smoke_tests

# Run specific test
cargo test test_name
```

## πŸ“š Documentation

Comprehensive documentation is available at [docs.rs/foras](https://docs.rs/foras), including:
- API reference for all public types and functions
- Tutorial on building knowledge bases and querying
- Examples of theorem proving and automated reasoning
- Best practices for formula representation
- Guide to the resolution algorithm and unification

## πŸ“– About the Name

The name **Foras** comes from the demon president of Hell, who teaches logic and ethics to his twenty-nine legions according to demonology. The name is fitting for a tool that mechanically applies logical rules.

Learn more:
- [YouTube: Foras Demon]https://www.youtube.com/watch?v=c58qVmOkPZ4
- [Wikipedia: Foras]https://en.wikipedia.org/wiki/Foras

## πŸ–Š Author

<a href="https://x.com/cryptopatrick">CryptoPatrick</a>

Keybase Verification:
https://keybase.io/cryptopatrick/sigs/8epNh5h2FtIX1UNNmf8YQ-k33M8J-Md4LnAN

## 🐣 Support
Leave a ⭐ if you think this project is cool.

## πŸ—„ License

This project is licensed under MIT. See [LICENSE](LICENSE) for details.

This project is based on the Otter project. For questions regarding Otter and its license, please read: https://www.mcs.anl.gov/research/projects/AR/otter/legal.html