ModelDriven / Alf-Reference-Implementation

Open-source implementation of the Action Language for fUML (Alf) specification.
30 stars 2 forks source link

Non-Conformance for LocalNames defined in if Statements? #103

Closed neumantm closed 1 year ago

neumantm commented 1 year ago

If I understand the standard correctly (especially the Names subsection in the section 9.8 if Statement) the following code should compile and execute successfully and output a:

activity Test2() {
  if(true) {
    let a : String = "a";
  }
  WriteLine(a);
}

However when trying to run/compile that code with the reference implementation I get the following error:

Models/Test2.alf:

  WriteLine(a);
  ^----------^
[5:3] One or more arguments are not compatible (in type and/or multiplicity) with the behavior parameters. (behaviorInvocationExpressionArgumentCompatibility)

The same problem appears to exist for while statements (and maybe more, but I did not test that). The Block Statement, however, works as I'd expect:

activity Test1() {
  {
    let a : String = "a";
  }
  WriteLine(a);
}

outputs a.

Did I misunderstand the standard, or is this a bug in the reference implementation?

seidewitz commented 1 year ago

Since your if statement does not have an else clause, any names defined inside the "then" clause of the statement are considered to have multiplicity lower bound of 0 after the if statement. This is true regardless of the fact that, in this case, the if condition is "true" so the "then" clause is always evaluated.

The input parameter WriteLine activity has multiplicity 1..1, so, after the if statement, the multiplicity lower bound of 0 for a is incompatible. If you change WriteLine(a); to WriteLine(a ?? "");, then it will parse and execute as you would expect.

Alternatively, you can try something like

activity Test3() {
  if(true) {
    let a : String = "a";
  } else {
    let a : String = "b";
  }
  WriteLine(a);
}

As coded above, this will write "a". If you change true to false, then it will write "b".

neumantm commented 1 year ago

Ah I see, thanks and sorry for the noise.