Search code examples
keywordrascal

Inferred types of constructor keyword parameters in Rascal pattern matches


I suppose I do not fully understand the concept of keywords in Rascal in relation to pattern matching (as the following notations are in fact supported by Rascal). Say I have defined a datatype Exp and a function demoFun1 (assuming that in this case z binds to y):

data Exp = a(int x, int y = 5) | b(int x);
Exp demoFun1(a(x, y = z)) = b(z);

And then I execute: demoFun1(a(2, y = 3)), Rascal returns:

|stdin:///|(25,4,<1,25>,<1,29>): The called signature: b(value),
does not match the declared signature:  Exp = b(int)

(Which is already quite a strange error message, since I cannot say something like int y = ... in the arguments, assuming that this would be the correct syntax). However, if I define another function where instead I assume that the value after the "="-sign is the default value (as is the case in the ADT-definition), and I can simply use the value of y instead:

Exp demoFun2(a(x, y = 3)) = b(y);

And I execute demoFun2(a(1, y=2))

Then Rascal returns:

|stdin:///|(0,19,<1,0>,<1,19>): The called signature: demoFun2(Exp),
does not match the declared signature:  Exp demoFun2(Exp); (abstract pattern);

Is pattern matching on keywords not (yet fully) supported, or am I doing something wrong?

Thank you!


Solution

  • First of all, yes, the error message needs improvement. Actually there is another unreported error which comes first. All introduced variables in patterns in function headers in Rascal must have types. The interpreter does not complain about this, and hence downstream there is an unexpected result.

    This fixes your problem, annotating the fresh variable z with int:

    Exp demoFun2(a(x, y = int z)) = b(z);
    

    Having said that, the following code triggers a similar issue, indicating that indeed something is amiss in the type inferencing during pattern matching keyword parameters:

    rascal>if (a(x, y = q) := xxx) q;
    value: 3
    

    The type of q should be nothing but int given the declaration of y.

    Thanks for the report, see https://github.com/cwi-swat/rascal/issues/843