Skip to content

some more typing of core#907

Open
tias wants to merge 9 commits intomasterfrom
mypy_core3
Open

some more typing of core#907
tias wants to merge 9 commits intomasterfrom
mypy_core3

Conversation

@tias
Copy link
Copy Markdown
Collaborator

@tias tias commented Apr 3, 2026

one notable change is about 'implies' and simplification...

one principle is that we typically don't simplify expressions at creation time, only at transformation time (subject to some exceptions).

I now make this more explicit, making 'simplify' an option to implies (which we don't activate anywhere at the moment). So for the user we wont delete expressions and variables unexpectedly, and for us, if we want it to activate we can.

@tias tias requested a review from IgnaceBleukx April 3, 2026 21:33
Copy link
Copy Markdown
Collaborator

@IgnaceBleukx IgnaceBleukx left a comment

Choose a reason for hiding this comment

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

Great, that should do away with a lot of weird cases in the transformation stack...
Maybe also add the optional argument to expressions.utils.implies? We use it when we're not sure the lhs is an expression

Comment thread cpmpy/expressions/core.py
Comment thread cpmpy/expressions/core.py Outdated
Comment thread cpmpy/expressions/core.py Outdated
@tias tias mentioned this pull request Apr 7, 2026
12 tasks
@tias tias requested a review from IgnaceBleukx April 15, 2026 09:20
Copy link
Copy Markdown
Collaborator

@IgnaceBleukx IgnaceBleukx left a comment

Choose a reason for hiding this comment

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

Minor comments on docs, and one potential simplification in implies that we can always do, regardless of the simplify arg

Comment thread cpmpy/expressions/core.py
def value(self):
return None # default
def value(self) -> Optional[int]:
return None # default
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this raise a NotImplemented error instead? It seems weird to have a default value for a method that every subclass absolutely should override...

Comment thread cpmpy/expressions/core.py
"""Implication constraint: ``self -> other``.

Python does not offer relevant syntax for implication, call this method instead.
For double reification (<->), use equivalence ``self == other``.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choco has expr.reify_with, which we could also add maybe? That would just redirect to ==
(Not for this PR, but smt to think about in the future)

Comment thread cpmpy/expressions/core.py

Args:
other (ExprLike): the right-hand-side of the implication
simplify (bool): if True, simplify True/False constants (might remove expressions & their variables from user-view)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

"might eliminate..." is more accurate?

Comment thread cpmpy/expressions/core.py

def __invert__(self):
if not (is_boolexpr(self)):
def __invert__(self) -> "Expression":
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

So this __invert__ is typed, because we call it in implies, right? And that one is also typed.
Next PR should probably type the other __xxx__ methods?

Comment thread cpmpy/expressions/core.py
Expression: the implication constraint or a BoolVal if simplified

Simplification rules:
- BoolVal(True) -> other :: other (BoolVal-ified if needed)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can always apply this one, it does not remove user vars

return math.floor(expr), math.ceil(expr)

def implies(expr, other):
# first to are declarations for typing purposes only
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

"two"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants