Opened 12 years ago

Closed 12 years ago

#1782 closed defect (invalid)

`or` is not strict in both operands, should it be?

Reported by: adabe588@… 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 Martin Sjölund, 12 years ago

Cc: peter.fritzson@… added

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.

comment:2 by Martin Sjölund, 12 years ago

Resolution: invalid
Status: newclosed

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.