Skip to content

Commit

Permalink
Trac #32148: change '__len__' method of propositional formula to 'len…
Browse files Browse the repository at this point in the history
…gth'

#28053 added a `__len__` method to propositional formulas, so that
`len(f)` would return the length of the formula. However, it was pointed
out in #29738 that only containers should have a `len`. So the method
should be renamed to `length`.
{{{
sage: f = propcalc.formula("a -> b")
sage: f.length()
3
}}}
For now, `__len__` is being retained as an alias.

URL: https://trac.sagemath.org/32148
Reported by: gh-DaveWitteMorris
Ticket author(s): Dave Morris
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Jul 23, 2021
2 parents c19df7f + 59cc054 commit 74ca56d
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/sage/logic/boolformula.py
Expand Up @@ -1508,9 +1508,9 @@ def get_next_op(self, str):
i += 1
return str[i]

def __len__(self):
def length(self):
r"""
Return the length of a Boolean formula.
Return the length of ``self``.
OUTPUT:
Expand All @@ -1521,37 +1521,41 @@ def __len__(self):
sage: import sage.logic.propcalc as propcalc
sage: s = propcalc.formula("a")
sage: len(s)
sage: s.length()
1
sage: s = propcalc.formula("(a)")
sage: len(s)
sage: s.length()
1
sage: s = propcalc.formula("~a")
sage: len(s)
sage: s.length()
2
sage: s = propcalc.formula("a -> b")
sage: len(s)
sage: s.length()
3
sage: s = propcalc.formula("alpha -> beta")
sage: len(s)
sage: s.length()
3
sage: s = propcalc.formula("a -> a")
sage: len(s)
sage: s.length()
3
sage: s = propcalc.formula("~(a -> b)")
sage: len(s)
sage: s.length()
4
sage: s = propcalc.formula("((a&b)|(a&c))->~d")
sage: len(s)
sage: s.length()
10
TESTS::
sage: s = propcalc.formula("(((alpha) -> ((beta))))")
sage: len(s)
sage: s.length()
3
"""
return len(flatten(self.full_tree()))

# For backward compatibility, we allow `self.length()` to be called as
# `len(self)`, but this may be deprecated in the future (see :trac:`32148`):
__len__ = length

# allow is_consequence to be called as a function (not only as a method of BooleanFormula)
is_consequence = BooleanFormula.is_consequence

0 comments on commit 74ca56d

Please sign in to comment.