First Order Logic (FOL)


First Order Logic (FOL), also known as predicate logic, is a formal system used in mathematics, philosophy, and computer science for expressing statements and reasoning about objects, their properties, and their relationships. FOL is more expressive than propositional logic because it allows for the use of quantifiers (e.g., "for all" or "there exists") and variables to talk about objects.

Components of First Order Logic:

  1. Constants: These are fixed objects in the domain, such as specific students or courses (e.g., Alice, Math101).
  2. Variables: These represent arbitrary elements in the domain (e.g., x, y, z).
  3. Predicates (Relations): These represent properties or relationships between objects (e.g., Enrolled(Alice, Math101) to indicate that Alice is enrolled in Math101).
  4. Functions: These map objects to other objects (e.g., Grade(Alice, Math101) could return a grade for Alice in Math101).
  5. Quantifiers:
    • Universal Quantifier (∀): Means "for all" (e.g., ∀x, Enrolled(x, Math101) means "all students are enrolled in Math101").
    • Existential Quantifier (∃): Means "there exists" (e.g., ∃x, Enrolled(x, Math101) means "there exists at least one student enrolled in Math101").
  6. Logical Connectives: Used to form complex expressions:
    • ∧ (and): Both conditions must be true.
    • ∨ (or): At least one condition must be true.
    • ¬ (not): Negates a statement.
    • → (implies): If one statement is true, the other must also be true.

Example in FOL:

  • Domain: Students and courses.
  • Predicates:
    • Student(x): x is a student.
    • Course(y): y is a course.
    • Enrolled(x, y): Student x is enrolled in course y.
  • Statements:
    • "All students are enrolled in at least one course": ∀x (Student(x) → ∃y (Course(y) ∧ Enrolled(x, y))).
    • "There exists a student who is not enrolled in any course": ∃x (Student(x) ∧ ¬∃y (Enrolled(x, y))).

Python Program: Modeling the Domain

class Student: def __init__(self, name): self.name = name self.courses = [] def __repr__(self): return f"Student({self.name})" class Course: def __init__(self, name): self.name = name self.students = [] def __repr__(self): return f"Course({self.name})" def enroll_student(student, course): if student not in course.students: course.students.append(student) if course not in student.courses: student.courses.append(course) def is_enrolled(student, course): return student in course.students def list_enrolled_students(course): return course.students def list_student_courses(student): return student.courses # Example Usage # Create some students and courses alice = Student("Alice") bob = Student("Bob") math101 = Course("Math101") cs101 = Course("CS101") # Enroll students in courses enroll_student(alice, math101) enroll_student(bob, cs101) enroll_student(alice, cs101) # Check if a student is enrolled in a course print(f"Is Alice enrolled in Math101? {is_enrolled(alice, math101)}") print(f"Is Bob enrolled in Math101? {is_enrolled(bob, math101)}") # List all students enrolled in a course print(f"Students enrolled in CS101: {list_enrolled_students(cs101)}") # List all courses a student is enrolled in print(f"Courses Alice is enrolled in: {list_student_courses(alice)}")

 


Sample Output

Is Alice enrolled in Math101? True Is Bob enrolled in Math101? False Students enrolled in CS101: [Student(Alice), Student(Bob)] Courses Alice is enrolled in: [Course(Math101), Course(CS101)]

Explanation

  • Classes

    • Student: Represents a student with a name and a list of courses they are enrolled in.
    • Course: Represents a course with a name and a list of students enrolled in that course.
  • Functions

    • enroll_student(student, course): Adds the student to the course’s student list and the course to the student’s course list.
    • is_enrolled(student, course): Checks if a student is enrolled in a specific course.
    • list_enrolled_students(course): Lists all students enrolled in a course.
    • list_student_courses(student): Lists all courses a student is enrolled in.

Post a Comment

0 Comments

Close Menu