icyphy / ptII

Ptolemy II is an open-source software framework supporting experimentation with actor-oriented design.
https://ptolemy.eecs.berkeley.edu/ptolemyII
Other
99 stars 43 forks source link

Addition is not commutative #321

Closed cxbrooks closed 9 years ago

cxbrooks commented 11 years ago

Note: the issue was created automatically with bugzilla2github tool

Original bug ID: BZ#528 From: @lhstrh Reported version: 9.1.devel CC: pt-dev@chess.eecs.berkeley.edu

cxbrooks commented 11 years ago

From the expression evaluator:

{a=1} + 1 add method not supported between ptolemy.data.RecordToken '{a = 1}' and ptolemy.data.IntToken '1' because the tokens have different classes. 1 + {a=1} "1{a = 1}"

cxbrooks commented 11 years ago

Issue addressed by: 1) making record and string incomparable in the type lattice; 2) removing the overrides of add() and addSubtract() in StringToken that would forcefully convert any token into a StringToken using the toString() method, even if not permitted by the relations in the type order.

Change committed in revision: 67012

New behavior:

{a=1} + 1 add method not supported between ptolemy.data.RecordToken '{a = 1}' and ptolemy.data.IntToken '1' because the tokens have different classes. 1 + {a=1} add method not supported between ptolemy.data.IntToken '1' and ptolemy.data.RecordToken '{a = 1}' because the types are incomparable.

cxbrooks commented 11 years ago

The following cases make sense now:

{a = 1, b = 2} + 1 add method not supported between ptolemy.data.RecordToken '{a = 1, b = 2}' and ptolemy.data.IntToken '1' because the tokens have different classes. 1 + {a = 1, b = 2} add method not supported between ptolemy.data.IntToken '1' and ptolemy.data.RecordToken '{a = 1, b = 2}' because the types are incomparable.

{a = 1, b = 2} + "foo" add method not supported between ptolemy.data.RecordToken '{a = 1, b = 2}' and ptolemy.data.StringToken '"foo"' because the tokens have different classes. "foo" + {a = 1, b = 2} add method not supported between ptolemy.data.StringToken '"foo"' and ptolemy.data.RecordToken '{a = 1, b = 2}' because the types are incomparable.

But these cases are still need fixing:

{1, 2} + "foo" {"1foo", "2foo"} "foo" + {1, 2} add method not supported between ptolemy.data.StringToken '"foo"' and ptolemy.data.ArrayToken '{1, 2}' because the types are incomparable.

[1, 2] + "foo" "[1, 2]foo" "foo" + [1, 2] "foo[1, 2]"

Also, all of these cases must be captured in a regressions test.

cxbrooks commented 9 years ago

Ptolemy II supports something akin to what in MatLAB is referred to as scalar expansion; the addition of a token of base type to an array (or vice versa) will result in that token being added to each element of the array. Addition is overloaded as to mean concatenation when used between strings. String concatenation, however, is not commutative. Nevertheless, we should expect the add operation between arrays and different type tokens is not dependent on the order of its operands. Hence, we now have:

{1, 2} + "foo" {"1foo", "2foo"} "foo" + {1, 2} {"foo1", "foo2"}

The behavior between matrices and strings, however, is different because Matrix <= String in the type lattice. Therefore, adding a string to a matrix will cause the matrix to be converted into a string first. Therefore, the following remains:

[1, 2] + "foo" "[1, 2]foo" "foo" + [1, 2] "foo[1, 2]"

This may seem odd or inconsistent with the add operation for arrays, but it is directly implied by the design of the type lattice. Moreover, since there does not exist a StringMatrix, it seems unlikely that anyone would want to perform an add operation between the elements of a matrix and a string.