-
Notifications
You must be signed in to change notification settings - Fork 2
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
Jorge/get body callback #31
Conversation
fdaa5b9
to
1ca03c8
Compare
Now the interface looks a bit random. What about simplifying the signature: def get_body(
stm: AST,
predicate: Callable[[AST], bool] = None,
signs: Container[Sign] = (Sign.NoSign, Sign.Negation, Sign.DoubleNegation),
) -> List[AST]:
"""
Get body literals satisfying the (optional) predicate with the given signs.
""" You use case could then be written as: body = get_body(stm, lambda x: (
x.ast_type == ASTType.Literal
and x.atm.ast_type == ASTType.TheoryAtom
and x.elements
and x.elements[0].terms
and x.elements[0].terms[0].ast_type == ASTType.TheoryUnparsedTerm
and x.elements[0].terms[0].elements
and x.elements[0].terms[0].elements[0].ast_type == ASTType.TheoryUnparsedTermElement
and x.elements[0].terms[0].elements[0].operators
and x.elements[0].terms[0].elements[0].operators[0] == "not"
) Alternatively, we could also have a predicate for each type of body literal. Not sure what is the nicest solution. PS: In Python it is customary to test if a sequence is empty using |
I find it easier to use having a predicate for each type. Let me know, I can change it.
I know, and I really don't like it. It seems to be against the own Python principle of "explicit is better than implicit". Anything can be used in an if and good luck getting what it is without reading the whole code. If I read |
Sure, it would be a bit like a customizable visitor like this. I just don't think we need a union of a bool/callable then. Just a callable is enough (which can be None btw.).
It's the recommended way. Check https://peps.python.org/pep-0008/#programming-recommendations. You'll get used to it. |
I change the signature mostly as we discussed.
It seems to me that We may create a type aliases Predicate = Union[Callable[[AST], bool], bool] There are still a couple of things that I think deserve improvement.
self.helper_get_body(
"a(X) :- b(X), not c(Y), d(Z): e(X,Z); not d(Z): e(X,Z).",
["b(X)", "d(Z): e(X,Z)", "not d(Z): e(X,Z)"],
signs=(Sign.NoSign,),
) It seems to me that it would be natural that passing self.helper_get_body(
"a(X) :- b(X), not c(Y), d(Z): e(X,Z); not d(Z): e(X,Z).",
["b(X)", "d(Z): e(X,Z)"],
signs=(Sign.NoSign,),
conditional_literals_predicate=lambda x: x.literal.sign != Sign.Negation,
) but I think it would be more clear that it worked just with the former. Tought, slightly less configurable.
What do you think?
Ok, ok... done, I will stop thinking by myself :) It is good to know that I am not alone on this (https://youtu.be/S0No2zSJmks?t=788), though. |
We can do it as you suggest. Please decide whether you want to use singular or plural though. Maybe also use
Sure, but maybe call it
Yes, it is probably a good idea consider the sign of the head of a conditional literal, too.
Returning two lists would rather be a
Interesting that he compares with C/C++. Modern guidelines say one should write |
Done
I don't think so, I hope people can write a not by themselves.
Yeah, specially given that in C++ 0-overhead is a priority. Not in pyhton. |
I realized that
get_body()
is not general enough foreclingo
. For instance, with rulewe want to obtain the positive body
See test https://github.com/jorgefandinno/python-clingox/blob/1ca03c8e071192f3bf78995e70490afe8aa5e9b5/clingox/tests/test_ast.py#L2584
Obviously , this is
eclingo
specific. In general, the user of the library should be able to inspect the theory atom to decide if it should be excluded or not.This pull request achieves that by allowing to pass a callback as
exclude_theory_atoms