Predicate core#

Classes and functions#

galactic.algebras.convex.predicates.base.core module

Predicates

This module defines basic predicates used in convex algebras, including:

  • Predicate: Base class for all predicates

  • Absurd: Represents an absurd predicate (always false)

  • Tautology: Represents a tautological predicate (always true)

  • Equality: Represents equality between two elements

  • Different: Represents inequality between two elements

  • GreaterThan: Represents the “greater than” relation

  • GreaterEqual: Represents the “greater than or equal to” relation

  • LessThan: Represents the “less than” relation

  • LessEqual: Represents the “less than or equal to” relation

  • Membership: Represents membership in a set

class Predicate(space, **parameters)#

Bases: Callable[[object], bool | None], ABC

Represents a predicate.

Parameters:

Notes

  • The exception handling in the __call__ method ensures that if one the characteristics returns [] or if the value cannot be processed, the predicate will return None, indicating that the truth value is unknown.

  • This allows for flexible and robust handling of various data types and structures. The __call__ method is designed to be used as a callable predicate, allowing instances of this class to be used directly in conditional statements or other contexts where a truth value is required.

  • The __call__ method iterates over the values returned by the characteristics, applying the _truth method to each tuple of values. If any values returns False, the predicate returns False. If all values return True, it returns True. If one of the characteristics returns [] or if an exception occurs during the truth evaluation, the predicate returns None, indicating that the truth value is unknown.

  • The __eq__ and __hash__ methods ensure that instances of this class can be compared and used in sets or as dictionary keys based on their characteristic and parameters, allowing for efficient storage and retrieval in collections.

  • The __str__ method provides a string representation of the predicate, formatted according to the specified template, space, and parameters. This makes it easy to visualize the predicate and understand its purpose when printed or logged. The template should contain {0} for the characteristic and {parameter_name} for each parameter used in the predicate. For example, if the template is “{0} = {parameter_name!r}”, it will be formatted with the characteristic and the parameter value.

  • The parameters are passed as keyword arguments and can be accessed using their names in the template.

__call__(datum)#

Make this predicate callable.

This method should not be overridden in sub-classes.

Parameters:

datum (None | bool | int | float | str | tuple[None | bool | int | float | str | tuple[Value, ...] | frozendict[str, Value], ...] | frozendict[str, None | bool | int | float | str | tuple[Value, ...] | frozendict[str, Value]]) – Any data associated with an item

Returns:

A bool if the truth is known, otherwise None

Return type:

bool | None

property parameters: frozendict[str, bool | int | float | str | tuple[bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter], ...] | frozendict[str, bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter]]]#

Get the parameters.

Returns:

The Parameters

Return type:

frozendict[str, Parameter]

property space: tuple[Characteristic, ...]#

Get the space.

Returns:

The space

Return type:

tuple[Characteristic, …]

abstract property template: str#

Get the template.

Returns:

The template

Return type:

str

Notes

The template should contain {0} for the characteristic and {parameter_name} for each parameter used in the predicate. For example, if the template is “{0} = {parameter_name!r}”, it will be formatted with the characteristic and the parameter value.

class Absurd(space, **parameters)#

Bases: Predicate

Represents the absurd predicate, which is always false.

Examples

>>> from galactic.algebras.convex.characteristics.base.core import String
>>> from galactic.algebras.convex.predicates.base.core import Absurd
>>> absurd = Absurd(space=(String(),))
>>> absurd
<Absurd object at 0x...>
>>> str(absurd)
'⊥'
>>> print(absurd("This is an example string."))
False
>>> print(absurd("This is a test string."))
False
>>> print(absurd(None))
False
>>> print(absurd(""))
False
>>> absurd(5)
False

Notes

  • The Absurd class is a concrete implementation of the Predicate class that represents a predicate that is always false.

  • It is used to represent a situation where the predicate cannot be satisfied, indicating that the condition is impossible or contradictory.

  • The __call__ method is overridden to always return False, regardless of the input datum. This means that any evaluation of this predicate will always return False, indicating that the condition is not met.

Hint

The Absurd class can be used in situations where a predicate is required, but the condition is inherently impossible or contradictory.

property template: str#

Get the template.

Returns:

The template

Return type:

str

Notes

The template for the Absurd predicate is simply ‘⊥’, representing falsehood.

class Tautology(space, **parameters)#

Bases: Predicate

Represents the tautology predicate, which is always true.

Examples

>>> from galactic.algebras.convex.characteristics.base.core import String
>>> from galactic.algebras.convex.predicates.base.core import Tautology
>>> tautology = Tautology(space=(String(),))
>>> tautology
<Tautology object at 0x...>
>>> str(tautology)
'⊤'
>>> tautology("This is an example string.")
True
>>> tautology("This is a test string.")
True
>>> tautology(None)
True
>>> tautology("")
True
>>> tautology(5)
True

Notes

  • The Tautology class is a concrete implementation of the Predicate class that represents a predicate that is always true.

  • It is used to represent a situation where the predicate is always satisfied, indicating that the condition is universally true.

  • The Tautology class can be used in situations where a predicate is required but the condition is always true, such as in logical proofs or when a condition is known to be universally valid.

  • It has been added in mirror to the Absurd class to provide a counterpart that represents a condition that is always true, allowing for a complete set of logical predicates.

property template: str#

Get the template.

Returns:

The template

Return type:

str

Notes

The template for the Tautology predicate is simply ‘⊤’, representing truth.

class Equality(space, value)#

Bases: Predicate

Represents an equality predicate.

Parameters:
Raises:
  • TypeError – If the value is not a Parameter instance

  • TypeError – If the space is not a tuple

  • TypeError – If the space does not contain a Characteristic instance

  • ValueError – If the space does not contain exactly one Characteristic

Examples

>>> from galactic.algebras.convex.characteristics.base.core import String
>>> from galactic.algebras.convex.predicates.base.core import Equality
>>> characteristic = String()
>>> value = "example"
>>> predicate = Equality(space=(characteristic,), value=value)
>>> predicate
<Equality object at 0x...>
>>> str(predicate)
"str($) = 'example'"
>>> predicate("This is an example string.")
False
>>> predicate("This is a test string.")
False
>>> predicate("example")
True
>>> predicate(5)
False
>>> print(predicate(None))
None
>>> predicate("")
False
property template: str#

Get the template.

Returns:

The template for the equality predicate

Return type:

str

Notes

The template should contain {0} for the characteristic and {value!r} for the value. For example, if the template is “{0} = {value!r}”, it will be formatted with the characteristic and the value.

property value: bool | int | float | str | tuple[bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter], ...] | frozendict[str, bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter]]#

Get the value.

Returns:

The value

Return type:

Parameter

class Different(space, value)#

Bases: Predicate

Represents a difference predicate.

Parameters:
Raises:
  • TypeError – If the value is not a Parameter instance

  • TypeError – If the space is not a tuple

  • TypeError – If the space does not contain a Characteristic instance

  • ValueError – If the space does not contain exactly one Characteristic

Examples

>>> from galactic.algebras.convex.characteristics.base.core import String
>>> from galactic.algebras.convex.predicates.base.core import Different
>>> characteristic = String()
>>> value = "example"
>>> predicate = Different(space=(characteristic,), value=value)
>>> predicate
<Different object at 0x...>
>>> str(predicate)
"str($) ≠ 'example'"
>>> predicate("This is an example string.")
True
>>> predicate("This is a test string.")
True
>>> predicate("example")
False
>>> print(predicate(None))
None
>>> predicate("")
True
>>> predicate(5)
True

Notes

The Different class is a concrete implementation of the Predicate class that represents a predicate that checks if a value is different from a specified value.

Hint

The Different class can be used in situations where a predicate is required to check for inequality, such as in filtering data or validating conditions where a specific value should not be present.

property template: str#

Get the template.

Returns:

The template for the difference predicate

Return type:

str

Notes

The template should contain {0} for the characteristic and {value!r} for the value. For example, if the template is “{0} ≠ {value!r}”, it will be formatted with the characteristic and the value.

property value: bool | int | float | str | tuple[bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter], ...] | frozendict[str, bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter]]#

Get the value.

Returns:

The value

Return type:

Parameter

class GreaterThan(space, value)#

Bases: Predicate

Represents a greater than predicate.

Parameters:
Raises:
  • TypeError – If the value is not a Parameter instance

  • TypeError – If the space is not a tuple

  • TypeError – If the space does not contain a Characteristic instance

  • ValueError – If the space does not contain exactly one Characteristic

Examples

>>> from galactic.algebras.convex.characteristics.base.core import Number
>>> from galactic.algebras.convex.predicates.base.core import GreaterThan
>>> characteristic = Number()
>>> value = 10
>>> predicate = GreaterThan(space=(characteristic,), value=value)
>>> predicate
<GreaterThan object at 0x...>
>>> str(predicate)
'float($) > 10'
>>> predicate(15.0)
True
>>> predicate(10.0)
False
>>> predicate(5.0)
False
>>> print(predicate(None))
None
>>> print(predicate(""))
None

Notes

The GreaterThan class is a concrete implementation of the Predicate class that represents a predicate that checks if a value is greater than a specified value.

Hint

The GreaterThan class can be used in situations where a predicate is required to check for greater than conditions, such as in filtering data or validating conditions where a value should exceed a certain threshold.

property template: str#

Get the template.

Returns:

The template for the greater than predicate

Return type:

str

Notes

The template should contain {0} for the characteristic and {value!r} for the value. For example, if the template is “{0} > {value!r}”, it will be formatted with the characteristic and the value.

property value: bool | int | float | str | tuple[bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter], ...] | frozendict[str, bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter]]#

Get the value.

Returns:

The value

Return type:

Parameter

class GreaterEqual(space, value)#

Bases: Predicate

Represents a greater than or equal predicate.

Parameters:
Raises:
  • TypeError – If the value is not a Parameter instance

  • TypeError – If the space is not a tuple

  • TypeError – If the space does not contain a Characteristic instance

  • ValueError – If the space does not contain exactly one Characteristic

Examples

>>> from galactic.algebras.convex.characteristics.base.core import Number
>>> from galactic.algebras.convex.predicates.base.core import GreaterEqual
>>> characteristic = Number()
>>> value = 10
>>> predicate = GreaterEqual(space=(characteristic,), value=value)
>>> predicate
<GreaterEqual object at 0x...>
>>> str(predicate)
'float($) ≥ 10'
>>> predicate(15.0)
True
>>> predicate(10.0)
True
>>> predicate(5.0)
False
>>> print(predicate(None))
None
>>> print(predicate(""))
None

Notes

The GreaterEqual class is a concrete implementation of the Predicate class that represents a predicate that checks if a value is greater than or equal to a specified value.

Hint

The GreaterEqual class can be used in situations where a predicate is required to check for greater than or equal conditions, such as in filtering data or validating conditions where a value should not fall below a certain threshold.

property template: str#

Get the template.

Returns:

The template for the greater than or equal predicate

Return type:

str

Notes

The template should contain {0} for the characteristic and {value!r} for the value. For example, if the template is “{0} ≥ {value!r}”, it will be formatted with the characteristic and the value.

property value: bool | int | float | str | tuple[bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter], ...] | frozendict[str, bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter]]#

Get the value.

Returns:

The value

Return type:

Parameter

class LessThan(space, value)#

Bases: Predicate

Represents a less than predicate.

Parameters:
Raises:
  • TypeError – If the value is not a Parameter instance

  • TypeError – If the space is not a tuple

  • TypeError – If the space does not contain a Characteristic instance

  • ValueError – If the space does not contain exactly one Characteristic

Examples

>>> from galactic.algebras.convex.characteristics.base.core import Number
>>> from galactic.algebras.convex.predicates.base.core import LessThan
>>> characteristic = Number()
>>> value = 10
>>> predicate = LessThan(space=(characteristic,), value=value)
>>> predicate
<LessThan object at 0x...>
>>> str(predicate)
'float($) < 10'
>>> predicate(5.0)
True
>>> predicate(10.0)
False
>>> predicate(15.0)
False
>>> print(predicate(None))
None
>>> print(predicate(""))
None

Notes

The LessThan class is a concrete implementation of the Predicate class that represents a predicate that checks if a value is less than a specified value.

Hint

The LessThan class can be used in situations where a predicate is required to check for less than conditions, such as in filtering data or validating conditions where a value should not exceed a certain threshold.

property template: str#

Get the template.

Returns:

The template for the less than predicate

Return type:

str

Notes

The template should contain {0} for the characteristic and {value!r} for the value. For example, if the template is “{0} < {value!r}”, it will be formatted with the characteristic and the value.

property value: bool | int | float | str | tuple[bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter], ...] | frozendict[str, bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter]]#

Get the value.

Returns:

The value

Return type:

Parameter

class LessEqual(space, value)#

Bases: Predicate

Represents a less than or equal predicate.

Parameters:
Raises:
  • TypeError – If the value is not a Parameter instance

  • TypeError – If the space is not a tuple

  • TypeError – If the space does not contain a Characteristic instance

  • ValueError – If the space does not contain exactly one Characteristic

Examples

>>> from galactic.algebras.convex.characteristics.base.core import Number
>>> from galactic.algebras.convex.predicates.base.core import LessEqual
>>> characteristic = Number()
>>> value = 10
>>> predicate = LessEqual(space=(characteristic,), value=value)
>>> predicate
<LessEqual object at 0x...>
>>> str(predicate)
'float($) ≤ 10'
>>> predicate(5.0)
True
>>> predicate(10.0)
True
>>> predicate(15.0)
False
>>> print(predicate(None))
None
>>> print(predicate(""))
None

Notes

The LessEqual class is a concrete implementation of the Predicate class that represents a predicate that checks if a value is less than or equal to a specified value.

Hint

The LessEqual class can be used in situations where a predicate is required to check for less than or equal conditions, such as in filtering data or validating conditions where a value should not exceed a certain threshold.

property template: str#

Get the template.

Returns:

The template for the less than or equal predicate

Return type:

str

Notes

The template should contain {0} for the characteristic and {value!r} for the value. For example, if the template is “{0} ≤ {value!r}”, it will be formatted with the characteristic and the value.

property value: bool | int | float | str | tuple[bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter], ...] | frozendict[str, bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter]]#

Get the value.

Returns:

The value

Return type:

Parameter

class Membership(space, member)#

Bases: Predicate

Represents a membership predicate.

Parameters:
Raises:
  • TypeError – If the member is not a Parameter instance

  • TypeError – If the space is not a tuple

  • TypeError – If the space does not contain a Characteristic instance

  • ValueError – If the space does not contain exactly one Characteristic

Examples

>>> from galactic.algebras.convex.characteristics.base.core import String
>>> from galactic.algebras.convex.predicates.base.core import Membership
>>> characteristic = String()
>>> member = "example"
>>> predicate = Membership(space=(characteristic,), member=member)
>>> predicate
<Membership object at 0x...>
>>> str(predicate)
"'example' ∈ str($)"
>>> predicate("This is an example string.")
True
>>> predicate("This is a test string.")
False
property member: bool | int | float | str | tuple[bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter], ...] | frozendict[str, bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter]]#

Get the member.

Returns:

The member

Return type:

Parameter

property template: str#

Get the template.

Returns:

The template for the membership predicate

Return type:

str

Notes

The template should contain {member!r} for the member and {0} for the characteristic. For example, if the template is “{member!r} ∈ {0}”, it will be formatted with the member and the characteristic.

Examples#

galactic.algebras.convex.predicates.examples.arithmetic.core module

Defines two predicates:

class Divisor(space, number=0)#

Bases: Predicate

Represents the predicate is it a divisor of a specific integer?.

Parameters:
  • space (tuple[Integer]) – The inner characteristic

  • number (int, default: 0) – The value to check for divisibility

Raises:
  • TypeError – If the number is not a int

  • TypeError – If the space is not a tuple

  • TypeError – If the space does not contain a Integer instance

  • ValueError – If the space does not contain exactly one Integer

Examples

>>> from galactic.algebras.convex.characteristics.base.core import Integer
>>> from galactic.algebras.convex.predicates.examples.arithmetic.core import (
...     Divisor,
... )
>>> characteristic = Integer()
>>> predicate = Divisor(space=(characteristic,), number=18)
>>> print(predicate)
D(int($),18)
>>> print(predicate(6))
True
>>> print(predicate(7))
False
>>> print(predicate(9))
True

Notes

  • The Divisor predicate checks if a value is a divisor of a specific integer.

  • It is typically used in conjunction with an Integer characteristic to determine if the integer value of the characteristic is a divisor of the specified number.

  • The number parameter specifies the integer to check against.

  • The characteristic parameter is the inner characteristic that provides the integer value to be checked.

Hint

The Divisor predicate can be used to filter or select items based on whether their integer characteristic is a divisor of a given number.

__call__(datum)#

Make this predicate callable.

This method should not be overridden in sub-classes.

Parameters:

datum (None | bool | int | float | str | tuple[None | bool | int | float | str | tuple[Value, ...] | frozendict[str, Value], ...] | frozendict[str, None | bool | int | float | str | tuple[Value, ...] | frozendict[str, Value]]) – Any data associated with an item

Returns:

A bool if the truth is known, otherwise None

Return type:

bool | None

property number: int#

Get the divisor value.

Returns:

The divisor value

Return type:

int

property parameters: frozendict[str, bool | int | float | str | tuple[bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter], ...] | frozendict[str, bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter]]]#

Get the parameters.

Returns:

The Parameters

Return type:

frozendict[str, Parameter]

property space: tuple[Characteristic, ...]#

Get the space.

Returns:

The space

Return type:

tuple[Characteristic, …]

property template: str#

Get the template.

Returns:

The template string for the predicate

Return type:

str

class Multiple(space, number=1)#

Bases: Predicate

Represents the predicate is it a multiple of a specific integer?.

Parameters:
  • space (tuple[Integer]) – The inner characteristic

  • number (int, default: 1) – The value to check for multiplicity

Raises:
  • TypeError – If the number is not a int

  • TypeError – If the space is not a tuple

  • TypeError – If the space does not contain a Integer instance

  • ValueError – If the space does not contain exactly one Integer

Examples

Notes

  • The Multiple predicate checks if a value is a multiple of a specific integer.

  • It is typically used in conjunction with an Integer characteristic to determine if the integer value of the characteristic is a multiple of the specified number.

  • The number parameter specifies the integer to check against.

  • The characteristic parameter is the inner characteristic that provides the integer value to be checked.

Hint

The Multiple predicate can be used to filter or select items based on whether their integer characteristic is a multiple of a given number.

__call__(datum)#

Make this predicate callable.

This method should not be overridden in sub-classes.

Parameters:

datum (None | bool | int | float | str | tuple[None | bool | int | float | str | tuple[Value, ...] | frozendict[str, Value], ...] | frozendict[str, None | bool | int | float | str | tuple[Value, ...] | frozendict[str, Value]]) – Any data associated with an item

Returns:

A bool if the truth is known, otherwise None

Return type:

bool | None

property number: int#

Get the multiple value.

Returns:

The multiple value

Return type:

int

property parameters: frozendict[str, bool | int | float | str | tuple[bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter], ...] | frozendict[str, bool | int | float | str | tuple[Parameter, ...] | frozendict[str, Parameter]]]#

Get the parameters.

Returns:

The Parameters

Return type:

frozendict[str, Parameter]

property space: tuple[Characteristic, ...]#

Get the space.

Returns:

The space

Return type:

tuple[Characteristic, …]

property template: str#

Get the template.

Returns:

The template string for the predicate

Return type:

str