Skip to content

rickxu0423/Automatic-Reasoning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Automatic-Reasoning

Brief Introduction

This project implements two inference methods (Model Checking and Resolution) for Propositional Logic and demonstrate them on some example problems. For extra credit, this project implements a parser for WFFs which means it is smart enough to operate ASK and TELL! In addition, it also implements CNF conversion for WFF. I wrote a module which automatically converts an underlying knowledge base into conjunctive normal form.

Key Data Structures:

1. Formula: Use Binary Tree to represent formula. For example, A=>(Bv~CvA) can be represented as:
binary-tree

2. Models: Models are represented as Dictionary which looks like: {“A”: True, “B”: False, “C”: True} and such dictionary provides the value to the leaf nodes which are Atoms when calling function Calculate during Model Checking which returns value from leaf to node level by level recursively.

3. Clauses: Clauses are represented as Frozenset which means (KB^~a) is a big set of frozensets. For example: (AvBv~C)^D is represented as: { frozenset({ “A”, “B”, “~C” }), frozenset({ “D” }) }

4. CNF Formula: The same data structure as Formula
Use function toCNF to transfer the original formula to CNF one

Operators

  1. Negation: ~
  2. Conjunction: ^
  3. Disjunction: v
  4. Implication: =>
  5. Biconditional: <=>
  6. Atom: A or P1-2 or Mythical is acceptable, but should not include any symbol includes: '~', ',', 'v', '^', '=', '<', '>' Should not include any space when you type into logic sentences
    Beacuse the project uses Binary Tree, please always add brackets!

Input Example:

(Av~BvC)=>D,A^B^C should be writen as: ((Av~B)vC)=>D,(A^B)^C

Exercises

1. Modus Ponens:

KB: P,P=>Q
Alpha: Q

2. Wumpus Word(Simple):

KB: ~P1-1,B1-1<=>(P1-2vP2-1),B2-1<=>((P1-1vP2-2)vP3-1),~B1-1,B2-1
Alpha: P1-2

3. Horn Clauses:

Description:

If the unicorn is mythical, then it is immortal, but if it is not mythical, then it is a mortal mammal. If the unicorn is either immortal or a mammal, then it is horned. The unicorn is magical if it is horned.
(a) Can we prove that the unicorn is mythical?
(b) Can we prove that the unicorn is magical?
(c) Can we prove that the unicorn is horned?

KB: Mythical=>~Mortal, ~Mythical=>(Mortal^Mammal), (~MortalvMammal)=>Horned, Horned=>Magical
Alpha: Mythical or Magical or Horned

4. The Doors of Enlightenment:

Description:

There are four doors X, Y , Z, and W leading out of the Middle Sanctum. At least one of them leads to the Inner Sanctum. If you enter a wrong door, you will be devoured by a fierce dragon. Well, there were eight priests A, B, C, D, E, F, G, and H, each of whom is either a knight or a knave. (Knights always tell the truth and knaves always lie.) They made the following statements to the philosopher:
• A: X isagooddoor.
• B: AtleastoneofthedoorsY orZ isgood.
• C: A and B are both knights.
• D: X and Y are both good doors.
• E: X and Z are both good doors.
• F: Either D or E is a knight.
• G: IfC isaknight,soisF.
• H: If G and I (meaning H) are knights, so is A.

(a) Smullyan's problem

Which door should the philosopher choose?

KB: A<=>X, B<=>(YvZ), C<=>(A^B), D<=>(X^Y), E<=>(X^Z), F<=>(DvE), G<=>(C=>F), H<=>((G^H)=>A)
Alpha:: X or Y or Z or W

(b) Liu's problem

Description:

The philosopher lacked concentration. All he heard was the first statement (A’s) and the last statement (H’s) plus two fragments:
• C: A and ...are both knights.
• G: IfC isaknight,...
Prove that he had heard enough to make a decision.

KB: A<=>X,H<=>((G^H)=>A),C<=>(A^M),G<=>(C=>N)
Alpha: X or Y or Z or W

How to Run Automatic-Reasoning

Use command: python3 run.py to run the program.
The first prompt asks you to input the Knowledge Base which should be separated by ,
The second prompt asks you to input the Alpha

Please have fun with it!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages