LogicalReasoner<T>

Models & Types · AiDotNet.Reasoning.DomainSpecific

Specialized reasoner for formal logic and logical reasoning problems.

Implements: IDomainReasoner<T>

For Beginners

This reasoner solves logic puzzles and formal reasoning problems.

What is LogicalReasoner? A specialized system for solving problems requiring formal logic, including:

  • Propositional logic (AND, OR, NOT, IF-THEN)
  • Predicate logic (All, Some, None)
  • Logical inference and deduction
  • Puzzle solving with constraints

Example 1 - Propositional Logic:

Problem: "If it rains, the ground gets wet. It rained. What can we conclude?"

Logical reasoning:
Premise 1: Rain → Wet ground
Premise 2: Rain (happened)
Rule: Modus ponens (If P→Q and P, then Q)
Conclusion: The ground is wet

Example 2 - Predicate Logic:

Problem: "All cats are mammals. All mammals are animals. Therefore?"

Logical reasoning:
Premise 1: ∀x (Cat(x) → Mammal(x))
Premise 2: ∀x (Mammal(x) → Animal(x))
Rule: Transitivity (If A→B and B→C, then A→C)
Conclusion: ∀x (Cat(x) → Animal(x)) - All cats are animals

Example 3 - Logic Puzzle:

Problem: "There are 3 people: Alice, Bob, and Carol. One always tells truth,
one always lies, one alternates. Alice says 'Bob lies.' Bob says 'Carol alternates.'
Carol says 'Alice tells truth.' Who is who?"

Logical reasoning:

1. Assume Alice = truth-teller
- Bob lies (from Alice)
- If Bob lies, "Carol alternates" is false
- So Carol is truth-teller or liar
- But Alice is truth-teller, so Carol must be liar
- Carol says "Alice tells truth" (true statement from liar) - CONTRADICTION

2. Assume Alice = liar
- Bob doesn't lie (from Alice lying)
- So Bob is truth-teller or alternator
- Bob says "Carol alternates"
- If Bob is truth-teller, Carol alternates, Alice lies ✓
- Carol says "Alice tells truth" (false from alternator in lying mode) ✓
- Consistent!

Conclusion: Alice = liar, Bob = truth-teller, Carol = alternator

Example 4 - Contrapositive:

Problem: "If you study, you pass. You didn't pass. What can we conclude?"

Logical reasoning:
Original: Study → Pass
Contrapositive: ¬Pass → ¬Study
Given: ¬Pass (didn't pass)
Conclusion: ¬Study (didn't study)

Logical inference rules:

  • Modus ponens: P→Q, P ⊢ Q
  • Modus tollens: P→Q, ¬Q ⊢ ¬P
  • Hypothetical syllogism: P→Q, Q→R ⊢ P→R
  • Disjunctive syllogism: P∨Q, ¬P ⊢ Q
  • Conjunction: P, Q ⊢ P∧Q
  • Simplification: P∧Q ⊢ P
  • Addition: P ⊢ P∨Q

Types of problems:

  • Deductive reasoning (must be true)
  • Inductive reasoning (probably true)
  • Abductive reasoning (best explanation)
  • Puzzle solving (knights and knaves, etc.)
  • Argument validity assessment
  • Fallacy detection

Common logical fallacies to avoid:

  • Affirming the consequent: P→Q, Q ⊬ P
  • Denying the antecedent: P→Q, ¬P ⊬ ¬Q
  • False dilemma: Assuming only two options exist
  • Circular reasoning: Using conclusion as premise
  • Ad hominem: Attacking person, not argument

Usage example:

var chatModel = new OllamaChatClient<double>("llama3");
var reasoner = new LogicalReasoner<double>(chatModel);

// Deductive reasoning
var result = await reasoner.SolveAsync(
"All programmers are logical. John is a programmer. What can we conclude?",
logicType: "deductive"
);

// Logic puzzle
result = await reasoner.SolvePuzzleAsync(
"In a room are 3 switches and 3 light bulbs in another room. " +
"You can flip switches but only enter the bulb room once. " +
"How do you determine which switch controls which bulb?"
);

Key features:

  • Formal logic notation support
  • Step-by-step inference
  • Assumption tracking
  • Contradiction detection
  • Proof verification
  • Multiple solution paths (Tree-of-Thoughts)

Constructors

Constructor Summary
LogicalReasoner(IChatClient<T> chatModel, bool enableContradictionDetection) Initializes a new instance of the LogicalReasoner class.

Methods

Method Summary
Task<ReasoningResult<T>> AnalyzeRelationshipAsync(string statement1, string statement2, ReasoningConfig config, CancellationToken cancellationToken) Determines the logical relationship between statements.
Task<ReasoningResult<T>> DetectFallaciesAsync(string argument, ReasoningConfig config, CancellationToken cancellationToken) Identifies logical fallacies in reasoning.
Task<ReasoningResult<T>> EvaluateArgumentAsync(string argument, List<string> premises, string conclusion, ReasoningConfig config, CancellationToken cancellationToken) Evaluates the validity of a logical argument.
Task<ReasoningResult<T>> ProveAsync(string statement, List<string> axioms, ReasoningConfig config, CancellationToken cancellationToken) Constructs a formal proof for a logical statement.
Task<ReasoningResult<T>> SolveAsync(string problem, string logicType, ReasoningConfig config, bool useTreeSearch, CancellationToken cancellationToken) Solves a logical reasoning problem.
Task<ReasoningResult<T>> SolvePuzzleAsync(string puzzle, ReasoningConfig config, CancellationToken cancellationToken) Solves a logic puzzle with constraints.