JSON-Lang/ir.py

117 lines
3 KiB
Python
Raw Normal View History

2023-03-05 21:44:30 +00:00
from dataclasses import dataclass
from typing import Sequence, TypeAlias
2023-03-05 22:18:11 +00:00
@dataclass(frozen=True)
class Subst:
"""
A substitution which may be performed on a term.
Only variables may be substituted, but they may be substituted with any term. That
is, `x1/Chris` is valid, but `Chris/x1` is not.
It is strongly recommended to avoid recursive substitutions, that is, substitutions
which contain the variable they are replacing
"""
variable: str
replacement: 'IRTerm'
def __str__(self) -> str:
return repr(self)
def __repr__(self) -> str:
2023-03-06 00:15:24 +00:00
return f'{self.replacement}/{self.variable}'
2023-03-05 22:18:11 +00:00
2023-03-05 21:44:30 +00:00
@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:18:11 +00:00
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
"""
2023-03-05 22:18:11 +00:00
arguments: 'Sequence[IRTerm]' = tuple()
def subst(self, subst: Subst) -> 'IRTerm':
"""
Perform substitution on a proposition
Returns the same proposition, but with any instances of the variable named in the
substitution replaced with the contents of the substitution.
>>> original = IRProp('angry', (IRVar('x1'),))
>>> original
angry(*x1)
>>> original.subst(Subst('x1', IRProp('Alex')))
angry(Alex())
"""
return IRProp(self.name, [arg.subst(subst) for arg in self.arguments])
2023-03-05 21:51:19 +00:00
def __str__(self) -> str:
2023-03-05 22:18:11 +00:00
return repr(self)
def __repr__(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 22:18:11 +00:00
def subst(self, subst: Subst) -> 'IRTerm':
"""
Perform substitution on a proposition
Returns the same proposition, but with any instances of the variable named in the
substitution replaced with the contents of the substitution.
>>> IRVar('x1').subst(Subst('x1', IRProp('Alex')))
Alex()
>>> IRVar('x1').subst(Subst('x2', IRProp('Alex')))
*x1
"""
if self.name == subst.variable:
return subst.replacement
else:
return self
2023-03-05 21:51:19 +00:00
def __str__(self) -> str:
2023-03-05 22:18:11 +00:00
return repr(self)
def __repr__(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'
2023-03-05 22:18:11 +00:00
def subst(self, subst: Subst) -> 'IRTerm':
"""
Perform substitution on a proposition
Returns the same proposition, but with any instances of the variable named in the
substitution replaced with the contents of the substitution.
>>> original = IRNeg(IRProp('happy', [IRVar('x1')]))
>>> original
¬happy(*x1)
>>> original.subst(Subst('x1', IRProp('parent', [IRProp('Susie')])))
¬happy(parent(Susie()))
"""
return IRNeg(self.inner.subst(subst))
2023-03-05 21:51:19 +00:00
def __str__(self) -> str:
2023-03-05 22:18:11 +00:00
return repr(self)
def __repr__(self) -> str:
2023-03-05 21:51:19 +00:00
return f'¬{self.inner}'
2023-03-05 21:44:30 +00:00
2023-03-05 22:18:11 +00:00
IRTerm: TypeAlias = IRVar | IRProp | IRNeg
if __name__ == '__main__':
import doctest
doctest.testmod()