Opened 13 years ago
Closed 13 years ago
#1782 closed defect (invalid)
`or` is not strict in both operands, should it be?
| Reported by: | Owned by: | somebody | |
|---|---|---|---|
| Priority: | high | Milestone: | 1.9.0 |
| Component: | Frontend | Version: | |
| Keywords: | Cc: | peter.fritzson@… |
Description
The or boolean operator evaluates both operands, meaning true or fail() will fail. I could not find any information on the expected behavior in the MM 2.0 spec, but I think it makes more sense for it to not evaluate both operands unnecessarily.
The following code fail()s since first is called, even though isEmpty returns true.
StrictOr.mo:
package StrictOr
public function isEmpty<A>
input List<A> list;
output Boolean result;
algorithm
result := match(list)
case {} then true;
else false;
end match;
end isEmpty;
public function first<A>
input List<A> list;
output A element;
algorithm
element := match list
local A x;
case x::_ then x;
end match;
end first;
function f
output Boolean e;
protected
List<List<Integer>> l := {};
algorithm
e := isEmpty(l) or isEmpty(first(l));
end f;
end StrictOr;
StrictoOr.mos:
setCommandLineOptions({"+d=rml,noevalfunc,failtrace","+g=MetaModelica","+showAnnotations","+showErrorMessages"});
loadFile("StrictOr.mo");
getErrorString();
StrictOr.f();
getErrorString();
// Expected
// Result:
// true
// ""
// endResult
Change History (2)
comment:1 by , 13 years ago
| Cc: | added |
|---|
comment:2 by , 13 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
Spec 3.3:
3.3.1 Example: Guarding Expressions Against Incorrect Evaluation x=v[I] and (I>=1 and I<=n); // Invalid x=if (I>=1 and I<=n) then v[I] else false; // Correct
Note:
See TracTickets
for help on using tickets.

Modelica semantics leave this undefined because no expression is allowed to have side-effects. Maybe we need to have a defined evaluation order in MM.