Skip to content
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

Handle Equal/Unequal for Strings better. #1128

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions mathics/builtin/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


import itertools
from typing import Optional, Union

import sympy

Expand All @@ -26,6 +27,9 @@
)
from mathics.core.numbers import dps

def cmp(a, b) -> int:
"Returns 0 if a == b, -1 if a < b and 1 if a > b"
return (a > b) - (a < b)

class SameQ(BinaryOperator):
"""
Expand Down Expand Up @@ -208,7 +212,7 @@ def numerify_args(items, evaluation):
class _EqualityOperator(_InequalityOperator):
"Compares all pairs e.g. a == b == c compares a == b, b == c, and a == c."

def do_compare(self, l1, l2):
def do_compare(self, l1, l2) -> Union[bool, None]:
if l1.same(l2):
return True
elif l1 == SymbolTrue and l2 == SymbolFalse:
Expand Down Expand Up @@ -254,7 +258,13 @@ def apply(self, items, evaluation):
args = self.numerify_args(items, evaluation)
wanted = operators[self.get_name()]
for x, y in itertools.combinations(args, 2):
c = do_cmp(x, y)
if isinstance(x, String) or isinstance(y, String):
if not (isinstance(x, String) and isinstance(y, String)):
c = 1
else:
c = cmp(x.get_string_value(), y.get_string_value())
else:
c = do_cmp(x, y)
if c is None:
return
elif c not in wanted:
Expand Down Expand Up @@ -345,7 +355,7 @@ def apply(self, items, evaluation):
return Expression("And", *groups)


def do_cmp(x1, x2):
def do_cmp(x1, x2) -> Optional[int]:

# don't attempt to compare complex numbers
for x in (x1, x2):
Expand Down Expand Up @@ -399,8 +409,8 @@ class Equal(_EqualityOperator, SympyComparison):
"""
<dl>
<dt>'Equal[$x$, $y$]'
<dt>'$x$ == $y$'
<dd>yields 'True' if $x$ and $y$ are known to be equal, or
<dt>'$x$ == $y$'
<dd>yields 'True' if $x$ and $y$ are known to be equal, or
'False' if $x$ and $y$ are known to be unequal.
<dt>'$lhs$ == $rhs$'
<dd>represents the equation $lhs$ = $rhs$.
Expand All @@ -413,6 +423,17 @@ class Equal(_EqualityOperator, SympyComparison):
>> 1==1.
= True

Strings are allowed:
Equal["11", "11"]
= True

Equal["121", "11"]
= False

Comparision to mismatched types is False:
Equal[11, "11"]
= False

Lists are compared based on their elements:
>> {{1}, {2}} == {{1}, {2}}
= True
Expand Down Expand Up @@ -500,6 +521,17 @@ class Unequal(_EqualityOperator, SympyComparison):
>> 1 != 1.
= False

Strings are allowed:
Unequal["11", "11"]
= False

Equal["121", "11"]
= True

Comparision to mismatched types is True:
Equal[11, "11"]
= True

Lists are compared based on their elements:
>> {1} != {2}
= True
Expand Down