pytableaux.lang
Lexical Base Classes
- class Lexical[source]
Base Lexical class. This is the base class from which all concrete lexical classes inherit, including enum classes. It provides basic features of identitfying, comparing, and generating items.
The
ident
andspec
attributes provide canonical ways of specifying items with tuples of numbers, strings, and other tuples. This is useful for serialization and deserialization.The
first()
,next()
, andgen()
methods enable programatic creation of items.The
hash
andsort_tuple
attributes allow for rich comparison and ordering of items, with consistency for comparing across different types.The
TYPE
refers to the corresponding member of the specialLexType
enum class member, which holds meta information about each type.- spec: tuple
The arguments roughly needed to construct, given that we know the type, i.e. in intuitive order. A tuple, possibly nested, containing numbers or strings.
- ident: tuple[str, tuple]
Equality identifier able to compare across types. Equivalent to
(classname, spec)
.
- sort_tuple: tuple[int, ...]
Sorting identifier, to order tokens of the same type, consisting of non-negative integers. This is also used in hashing, so equal objects must have equal sort_tuple values.
- classmethod first()[source]
Get the canonically first item of the type. This can be called on both concrete and abstract classes. For example:
>>> Atomic.first() <Sentence: A> >>> Constant.first() <Parameter: a> >>> Constant.first() == Parameter.first() True >>> Operated.first() <Sentence: ⚬A> >>> Lexical.first() <Predicate: F> >>> LexicalEnum.first() <Quantifier: Existential>
- Return type:
- abstract next(**kw)[source]
Get the canonically next item of the type. Various subclasses may provide optional keyword arguments.
- Return type:
- classmethod gen(stop, /, first=None, **nextkw)[source]
Generate items of the type, using
first()
andnext()
methods.This is convenient for making sequential lexical items quickly, without going through parsing.
>>> Predicate(1,0,4)(Constant.gen(4)) <Sentence: Gabcd>
- static orderitems(lhs: Lexical, rhs: Lexical, /) int [source]
Pairwise ordering comparison based on type rank and
sort_tuple
. This is the base method used to support equality and rich comparison operators between any Lexical instance, regardless of subclass.- Parameters:
- Returns:
- The relative order of
lhs
andrhs
. The return value will be either:
Less than
0
iflhs
precedesrhs
in order.Greater than
0
ifrhs
precedeslhs
in order.Equal to
0
if lhs is equal torhs
in order.
- The relative order of
- Return type:
- Raises:
TypeError -- if an argument is not an instance of
Lexical
with a validsort_tuple
attribute.
- class LexicalEnum[source]
Bases:
Lexical
,LangCommonEnum
Base class for Enum lexical classes. Subclassed by
Quantifier
andOperator
.- Parameters:
value (object) --
- Return type:
_EnumT
- class CoordsItem[source]
Common implementation for lexical types that are based on integer coordinates. For
Constant
,Variable
, andAtomic
, the coordinates are (index, subscript). ForPredicate
, the coordinates are (index, subscript, arity).
- class Parameter[source]
Bases:
CoordsItem
- class Sentence[source]
Bases:
LexicalAbc
Sentence base class. This provides common attributes and methods for the concrete sentence classes
Atomic
,Predicated
,Quantified
, andOperated
.Behaviors
Sentences support the built-in operators
~
,|
, and&
, for negation, disjunction, and conjunction, respectively.>>> a, b = Sentence.gen(2) >>> ~a == a.negate() True >>> a | b == a.disjoin(b) True >>> a & b == a.conjoin(b) True
Subclasses
- quantifiers: tuple[Quantifier, ...]
Sequence of quantifiers, recursive.
- negate()[source]
Negate this sentence, returning the new sentence. This can also be invoked using the
~
operator.- Returns:
The new sentence.
- asserted()[source]
Apply the
Assertion
operator. This can also be invoked using the+
operator.- Returns:
The new sentence.
- disjoin(rhs, /)[source]
Apply the
Disjunction
operator to the right-hand sentence. This can also be invoked using the|
operator.- Parameters:
rhs (Sentence) -- The right-hand disjunct.
- Returns:
The new sentence.
- conjoin(rhs, /)[source]
Apply the
Conjunction
operator to the right-hand sentence. This can also be invoked using the&
operator.- Parameters:
rhs (Sentence) -- The right-hand conjunct.
- Returns:
The new sentence.
- negative()[source]
Either negate this sentence, or, if this is already a negated sentence return its negatum, i.e., "un-negate" the sentence. This can also be invoked using the
-
operator.>>> a = Atomic.first() >>> a.negate() == a.negative() True >>> a.negate().negative() == a True
- Returns:
The new sentence.
- Return type:
Concrete Lexical Classes
Predicate
- class Predicate[source]
A predicate is specified by an integer 3-tuple of
(index, subscript, arity)
. The index can range from 0-3, and the subscript and arity can be any positive integer.To create a predicate, the spec can be passed either as separate arguments, or as a single
tuple
:>>> p = Predicate(0, 0, 1) >>> p == Predicate((0, 0, 1)) True
System Predicates
There are two built-in system predicates,
Identity
andExistence
. These are specified internal by the special indexes -1 and -2, respectively. They are defined in an enum classPredicate.System
, and can be accessed in several ways:>>> p = Predicate('Identity') >>> p is Predicate.Identity True >>> p is Predicate.System.Identity True
System predicates are considered equal to their name.
>>> Predicate.Identity == 'Identity' True
Behaviors
Calling a predicate will construct a
Predicated
sentence:>>> p = Predicate(0, 0, 2) >>> a, b = Constant.gen(2) >>> p((a, b)) == Predicated(p, (a, b)) True
- name: tuple[int, ...] | str
The name, for system predicates. For non-system predicates, this is the same as the
spec
.>>> Predicate.Identity.name 'Identity' >>> Predicate(0, 0, 1).name # For compatibility only (0, 0, 1)
Constant
- class Constant[source]
Bases:
Parameter
A constant is specified by an integer 2-tuple of
(index, subscript)
. The index can range from 0-3, and the subscript can be any positive integer.To create a constant, the spec can be passed either as separate arguments, or as a single
tuple
:>>> c = Constant(2, 9) >>> c == Constant((2, 9)) True
Behaviors
A constant can be right-shifted
>>
into aQuantified
sentence as a convenience for the sentence'sunquantify()
method:>>> c = Constant(0, 0) >>> s1 = Quantified.first() >>> s2 = c >> s1 >>> s2.constants == {c} True
Variable
Operator
- class Operator[source]
Bases:
LexicalEnum
Operator enum class.
Behaviors
Calling an operator constructs an
Operated
sentence
>>> s1 = Atomic(0, 0) >>> s2 = Operator.Conditional(s1, s1) >>> s2 == Operated(Operator.Conditional, (s1, s1)) True
Some operators map to Python operators, such as & or ~.
>>> s = Atomic(0, 0) >>> Operator.Conjunction(s, s) == s & s True >>> s.negate() == ~s True
See the
Sentence
class for more.Members
name
arity
Assertion
1
Negation
1
Conjunction
2
Disjunction
2
MaterialConditional
2
MaterialBiconditional
2
Conditional
2
Biconditional
2
Possibility
1
Necessity
1
- Parameters:
value (object) --
- Return type:
_EnumT
Quantifier
- class Quantifier[source]
Bases:
LexicalEnum
Quantifier enum class.
Behaviors
Calling a quantifier constructs a
Quantified
sentence>>> x = Variable(0, 0) >>> s1 = Predicated.first() >>> s2 = Quantifier.Existential(x, s1) >>> s2 == Quantified(Quantifier.Existential, x, s1) True
- Existential
The ∃ Existential quantifier
- Universal
The ∀ Universal quantifier
- Parameters:
value (object) --
- Return type:
_EnumT
Atomic
- class Atomic[source]
Bases:
CoordsItem
,Sentence
An atomic sentence sentence, like a
Parameter
, is specified by an integer 2-tuple of(index, subscript)
. The index can range from 0-4, and the subscript can be any positive integer.>>> Atomic(4, 0) == Atomic((4, 0)) True
Predicated
Quantified
- class Quantified[source]
-
Quantified sentence implementation.
- __init__(q, v, s, /)[source]
- Parameters:
q (
Quantifier
) -- TheQuantifier
or name, such as"Existential"
.v (
Variable
) -- TheVariable
to bind, or coords, such as(0, 0)
.
- quantifier: Quantifier
The quantifier.
- items: tuple[Quantifier, Variable, Sentence]
Quantifer
,Variable
,Sentence
.- Type:
The items sequence
- classmethod first(q=Quantifier.Existential, /)[source]
- Parameters:
q (
Quantifier
) -- The quantifier, default is the first quantifier.
Operated
Collection Classes
Argument
- class Argument[source]
Argument class.
A container of sentences (premises, conclusion) with sequence implementation, ordering and hashing.
Two arguments are considered equal just when their conclusions are equal, and their premises are equal (and in the same order). The title is not considered in equality.
- Parameters:
- Return type:
- predicates(**kw)[source]
Return the predicates occuring in the argument.
- Keyword Arguments:
**kw -- sort keywords to pass to
Predicates
constructor.- Returns:
The predicates.
- Return type:
- argstr()[source]
Get the canonical string representation for recreating with
from_argstr()
.- Returns:
The argument string.
- Return type:
Predicates
- class Predicates[source]
Bases:
PredicatesBase
,qset
[Predicate
]Predicate store. A sequenced set with a multi-keyed lookup index.
Predicates with the same symbol coordinates (index, subscript) may have different arities. This class ensures that conflicting predicates are not in the same set, which is necessary, for example, for determinate parsing.
- __init__(values=None, /, *, sort=False, key=None, reverse=False)[source]
Create a new store from an iterable of predicate objects or specs
- Parameters:
values -- Iterable of predicates or specs.
- Keyword Arguments:
sort -- Whether to sort. Default is
False
.key -- Optional sort key function.
reverse -- Whether to reverse sort.
- get(ref, /, default=<object object>)
Get a predicate by any reference. Also searches system predicates.
Parser Classes
Parser
- class Parser(notation: Notation | None = None, predicates: Predicates | None = None, table: ParseTable | str | None = None, **opts)[source]
Parser interface and coordinator.
To create a parser:
>>> from pytableaux.lang import Parser >>> Parser() <PolishParser:(<Notation.polish>, 'default')> >>> Parser('standard') <StandardParser:(<Notation.standard>, 'default')>
Or use the
Notation
enum class:>>> from pytableaux.lang import Notation >>> Parser(Notation.standard) <StandardParser:(<Notation.standard>, 'default')> >>> Notation.polish.Parser() <PolishParser:(<Notation.polish>, 'default')>
To parse
Predicated
sentences, the parser must know the predicate's arity. By default, it will deduce the arity from the first usage.>>> parser = Parser(notation='standard') >>> parser('Fa & Gab') <Sentence: Fa ∧ Gab> >>> parser.predicates Predicates({<Predicate: F>, <Predicate: G>}) >>> # parser('Fab') # raises ParseError
Alternatively, pass a
Predicates
object with the predicates:>>> from pytableaux.lang import Predicates >>> preds = Predicates(((0, 0, 1), (1, 0, 2))) >>> parser = Parser('standard', preds, auto_preds=False) >>> parser('Fa & Gab') <Sentence: Fa ∧ Gab>
- Return type:
- abstract __call__(input, /)[source]
Parse a sentence from an input string.
- Parameters:
input (str) -- The input string.
- Returns:
The parsed sentence.
- Return type:
- Raises:
ParseError --
- argument(conclusion, premises=None, *, title=None)[source]
Parse the input strings and create an argument.
- Parameters:
- Keyword Arguments:
title -- An optional title.
- Returns:
The argument.
- Raises:
ParseError -- if input cannot be parsed.
TypeError -- for bad argument types.
- Return type:
- class PolishParser[source]
Bases:
DefaultParser
Polish notation parser.
- Return type:
- class StandardParser[source]
Bases:
DefaultParser
Standard notation parser.
- Options:
drop_parens: Don't require parentheses on all binary sentences. Default
True
.
- Return type:
Writer Classes
LexWriter
- class LexWriter(*args, **kw)[source]
LexWriter interface and coordinator.
- class StandardLexWriter[source]
Bases:
LexWriter
Standard notation lexical writer implementation.
- Options:
drop_parens: Drop outer parens, default
True
. identity_infix: Write identity sentences in infix notation (e.g. a = b), defaultTrue
. max_infix: The max arity for writing predicate sentences in infix notation, default 0.
Utility Classes
- class LexType[source]
LexType metadata enum class for concrete types.
name
rank
cls
role
maxi
pcls
Predicate
10
3
Constant
20
3
None
Variable
30
3
None
Quantifier
40
None
Operator
50
None
Atomic
60
4
None
Predicated
70
None
Quantified
80
None
Operated
90
None
- Parameters:
value (object) --
- Return type:
_EnumT
- class ParseTable[source]
-
Parser table data class.
- Parameters:
data (Mapping) --
- class StringTable(data, /)[source]
-
Lexical writer strings table data class.
- Parameters:
data (Mapping) --
- class Notation(value, names=None, **kw)[source]
Notation (polish/standard) enum class.
name
polish
standard
- Parameters:
value (object) --
- Return type:
_EnumT
- class Marking(value, names=None, **kw)[source]
Miscellaneous marking/punctuation enum.
name
paren_open
paren_close
whitespace
digit
meta
subscript
subscript_open
subscript_close
tableau
- Parameters:
value (object) --
- Return type:
_EnumT
- class BiCoords(index, subscript)[source]
An (index, subscript) integer tuple.