JSON-Lang/ir.py

39 lines
991 B
Python
Raw Normal View History

2023-03-05 21:44:30 +00:00
from dataclasses import dataclass
from typing import Sequence, TypeAlias
@dataclass(frozen=True)
class IRProp:
2023-03-05 21:51:19 +00:00
"""
Represents a proposition or object for resolution
Can have any number of arguments, each of which should be a `IRTerm`. Note that no
distinction is made between predicates (n-arity, logical statements), functions
(positive-arity functions over objects), and constants (stand-ins for objects)
"""
2023-03-05 22:01:10 +00:00
name: str
2023-03-05 21:51:19 +00:00
"""
The identifier of this thing, including its location in the source
"""
arguments: 'Sequence[IRTerm]'
def __str__(self) -> str:
2023-03-05 22:01:10 +00:00
return f'{self.name}({",".join(str(arg) for arg in self.arguments)})'
2023-03-05 21:44:30 +00:00
@dataclass(frozen=True)
class IRVar:
2023-03-05 21:51:19 +00:00
"""
A variable which may be substituted for any other term
"""
2023-03-05 22:01:10 +00:00
name: str
2023-03-05 21:51:19 +00:00
def __str__(self) -> str:
2023-03-05 22:01:10 +00:00
return f'*{self.name}'
2023-03-05 21:44:30 +00:00
@dataclass(frozen=True)
class IRNeg:
2023-03-05 21:51:19 +00:00
"""
A negated proposition
"""
inner: 'IRTerm'
def __str__(self) -> str:
return f'¬{self.inner}'
2023-03-05 21:44:30 +00:00
IRTerm: TypeAlias = IRVar | IRProp | IRNeg