Skip to content

First Order Logic #7608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

First Order Logic #7608

wants to merge 20 commits into from

Conversation

sd-biswas
Copy link

  • Make sure tests pass
  • Use Symbol everywhere, not strings (or Constant or Variable or whatever)
  • Add docstring with doctest to every class and function (including and especially abstract classes, although the abstract classes do not need to have doctests if that does not make sense). Major methods of classes should have this as well.
  • Address First Order Logic #7608 (comment)
  • Address First Order Logic #7608 (comment)
  • Use a single to_cnf and to_dnf and use dispatching to distinguish the two.
  • Change name for Functions.
  • Improve FOL_KB to handle multiple recursive clauses.
  • Implement Answer Extraction with the new ask.
  • Add description to Exceptions
  • Fix error in rst doc and add KB to it

self._func = func
self._args = tuple(args)

def _sympystr(self, *args, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second argument should be the printer, I think. You should use it to recursively print the arguments.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the printers here are purely for my personal convenience at this moment. All of the printer code will go into the respective file soon.

@asmeurer
Copy link
Member

I think you'll need to override _hashable_content along with __eq__. Python 3 make also make you override __hash__, which you should just pass to the superclass using super.

>>> from sympy.logic.FOL import Predicate
>>> Knows = Predicate('Knows')
>>> Knows('John', 'Jack')
Knows(John, Jack)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are 'John' and 'Jack' implicitly converted to Symbol('John') and Symbol('Jack')?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. The are intended to be constants.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that SymPy objects don't really play with with non-SymPy objects such as strings as part of their expression tree. In this case, I would just only use Symbol everywhere, since that is the standard way to have a 'name' in SymPy.

@asmeurer
Copy link
Member

I think it would be cleaner to just use symbols in place of strings. Most actual usage will be with symbols, I think. It's just a matter of using symbols in the doctests.

self._name = name

def __call__(self, *args):
return self.apply()(self, *args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be some apply on this class, even if it just raises NotImplementedError.

@asmeurer
Copy link
Member

The class structure here is still a little confusing to me. Can you add a docstring to each class that just explains what it is for, especially the abstract base classes. It also should be made clear for each class if it is designed to be used on its own or if it is only a designed to be subclassed.

@asmeurer
Copy link
Member

I don't think it's a good idea to have multiple to_cnf functions. I would just have the one function, and use dispatching to have it do the right thing on different kinds of objects (i.e., have to_cnf call _eval_to_cnf on the object if it has that method, and then implement that on the logic and first order logic classes).

return expr.func(*args)


def standardize(expr):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably add a keyword argument to pass in a custom iterator of symbols.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the symbols argument to cse().

@asmeurer
Copy link
Member

asmeurer commented Aug 8, 2014

I am going to put a checklist at the top of the pull request, so that we can keep track of what needs to be done.

raise ValueError("Use a constant instead of %s()" % func)
args = [arg if isinstance(arg, (Boolean, Symbol))
else Constant(arg) for arg in args]
self._func = func
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this at all related to self.func (which will be the class, Applied)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with using a property is that it leads to confusion to people reading the code from here, as you can see from this comment of mine.

@asmeurer
Copy link
Member

I was hoping for more than just "abstract base class" in the docstrings of the abstract base classes. For someone coming to this module for the first time, it can be quite unclear what all the abstract classes are supposed to represent. It would be helpful to give a higher level idea of why they are there.

@asmeurer
Copy link
Member

I think the Python 3 test failures are coming from using strings instead of SymPy objects in the doctests.

@sd-biswas
Copy link
Author

  1. I'll work on better documentation.
  2. Simply calling to_snf followed by to_cnf/to_dnf (in boolalg) should give the user the required CNF/ DNF. to_snf returns an expression with no quantifiers (existentially bound variables are replaced by skolem functions and all other variables are considered to be universally quantified). Following this, any boolalg function can be called on this expression. The major requirement for CNF is for resolution, and the resolution method takes care of all this.
  3. I will fix it in some time.

@asmeurer
Copy link
Member

Simply calling to_snf followed by to_cnf/to_dnf (in boolalg) should give the user the required CNF/ DNF. to_snf returns an expression with no quantifiers (existentially bound variables are replaced by skolem functions and all other variables are considered to be universally quantified). Following this, any boolalg function can be called on this expression. The major requirement for CNF is for resolution, and the resolution method takes care of all this.

Good. I like this better. CNF and DNF should be only about propositional logic, and should treat quantifiers basic terms.

@peterwittek
Copy link

Is there any progress with this? Thanks.

@ghost
Copy link

ghost commented Jan 19, 2018

I'm also interested in this, it appears to be halted now. Is there currently any plans for first-order logic in the future?

@sd-biswas
Copy link
Author

@naiveaiguy Please speak to the maintainers. I have been away for a very long time and don't know the current state of things here. If there is still interest in having FOL then I can help you with modifying and/or rewriting this code.

skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Oct 29, 2018
Mechanistic port of sympy/sympy#7608 for Diofant
by @skirpichev.
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Oct 29, 2018
Mechanistic port of sympy/sympy#7608 for Diofant
by @skirpichev.
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Jan 13, 2019
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Jan 13, 2019
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Feb 28, 2019
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Aug 7, 2019
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Oct 8, 2019
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Jan 28, 2020
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Jan 29, 2020
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.
@czgdp1807
Copy link
Member

This should be there in SymPy.

skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Apr 12, 2022
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Apr 21, 2022
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Apr 23, 2022
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request May 3, 2022
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Jun 17, 2022
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Jun 17, 2022
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Jun 17, 2022
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Jun 27, 2022
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Jul 26, 2023
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Aug 4, 2023
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Aug 5, 2023
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>

- FOL class
skirpichev pushed a commit to skirpichev/diofant that referenced this pull request Sep 30, 2023
Mechanistic port of sympy/sympy#7608 for Diofant

// edited by @skirpichev

100% test coverage.

XXX cleanup FOL.py

XXX cleanup test_FOL.py

XXX adapt code

Function -> UndefinedBooleanFunction

AppliedFunction -> AppliedUndefinedBooleanFunction

Signed-off-by: Sergey B Kirpichev <skirpichev@gmail.com>

- FOL class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants