Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbolic manipulation over non-numeric types

Tags:

python

sympy

I'm interested in a python library that permits symbolic manipulation where the symbols and can be unknowns of an arbitrary type.

This is the code that I want to write:

>>> myexpression = symbol("foo") == "bar"
>>> print myexpression
foo == "bar"
>>> print myexpression(foo="quux")
False
>>> myexpression.or_(True)
True

Or some rough approximation of that. It doesn't actually even need to be that clever, I'd be happy enough having to call a lot of extra introspection methods to get something like the above (for instance, even if the logical tautology is not directly simplified)

My first instinct was to look at sympy, but it seems that library makes the strong assumption that symbolic variables must be numbers; and I'd like to at least operate on sequences and sets:

>>> myexpression = sympy.Eq(sympy.Symbol("foo"), 5)
>>> myexpression
foo == 5
>>> myexpression = sympy.Eq(sympy.Symbol("foo"), "bar")
Traceback (most recent call last):
  ...
sympy.core.sympify.SympifyError: SympifyError: 'bar'

Is there a way to get sympy to understand non-numeric variables, or another library that can do similar things?

like image 285
SingleNegationElimination Avatar asked Sep 26 '11 00:09

SingleNegationElimination


1 Answers

Not sure how well it fits the uses you have in mind, but the nltk (Natural Language Toolkit) has modules for symbolic manipulation, including first order logic, typed lambda calculus, and a theorem prover. Take a look at this howto.

like image 131
alexis Avatar answered Nov 20 '22 06:11

alexis