gt22

Untitled

Oct 12th, 2020
2,323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. from __future__ import annotations
  2. from typing import List, Optional
  3.  
  4.  
  5. class Node:
  6.     pass
  7.  
  8.  
  9. class Atom(Node):
  10.  
  11.     name: str
  12.     args: List[Atom]
  13.  
  14.     def __init__(self, name: str, args: List[Atom] = []):
  15.         self.name = name
  16.         self.args = args
  17.  
  18.     def __str__(self):
  19.         return f"Atom({self.name}, [{', '.join(map(str, self.args))}])"
  20.  
  21.     def __eq__(self, other):
  22.         return isinstance(other, type(self))\
  23.                and self.name == other.name \
  24.                and len(self.args) == len(other.args) \
  25.                and all(x == y for (x, y) in zip(self.args, other.args))
  26.  
  27.  
  28. class Disjunction(Node):
  29.  
  30.     a: Node
  31.     b: Node
  32.  
  33.     def __init__(self, a: Node, b: Node):
  34.         self.a = a
  35.         self.b = b
  36.  
  37.     def __str__(self):
  38.         return f"OR({self.a}, {self.b})"
  39.  
  40.     def __eq__(self, other):
  41.         return isinstance(other, type(self)) and self.a == other.a and self.b == other.b
  42.  
  43.  
  44. class Conjunction(Node):
  45.  
  46.     a: Node
  47.     b: Node
  48.  
  49.     def __init__(self, a: Node, b: Node):
  50.         self.a = a
  51.         self.b = b
  52.  
  53.     def __str__(self):
  54.         return f"AND({self.a}, {self.b})"
  55.  
  56.     def __eq__(self, other):
  57.         return isinstance(other, type(self)) and self.a == other.a and self.b == other.b
  58.  
  59.  
  60. class Definition(Node):
  61.  
  62.     head: Atom
  63.     body: Optional[Node]
  64.  
  65.     def __init__(self, head: Atom, body: Optional[Node]):
  66.         self.head = head
  67.         self.body = body
  68.  
  69.     def __str__(self):
  70.         return f"{self.head} :- {self.body}"
  71.  
  72.     def __eq__(self, other):
  73.         return isinstance(other, type(self)) and self.head == other.head and self.body == other.body
  74.  
Advertisement
Add Comment
Please, Sign In to add comment