-
Notifications
You must be signed in to change notification settings - Fork 256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dafny 4.0 suggestion: var
pattern matching
#2343
Comments
(Should we use the discussion feature for these discussions?) This would make code extremely heavy and verbose fr large matches, like this: function method MapChildren_Expr(e: Expr, tr: BottomUpTransformer) :
(e': Expr)
{
match e {
case Var(_) => e
case Literal(_) => e
case Abs(var vars, var body) =>
Expr.Abs(vars, Map_Expr(body, tr))
case Apply(var aop, var exprs) =>
var exprs' := Seq.Map(e requires e in exprs => Map_Expr(e, tr), exprs);
Expr.Apply(aop, exprs');
case Block(var exprs) =>
var exprs' := Seq.Map(e requires e in exprs => Map_Expr(e, tr), exprs);
Expr.Block(exprs');
case Bind(var vars, var vals, var body) =>
var vals' := Seq.Map(e requires e in vals => Map_Expr(e, tr), vals);
Expr.Bind(vars, vals', Map_Expr(body, tr));
case If(var cond, var thn, var els) =>
Expr.If(Map_Expr(cond, tr), Map_Expr(thn, tr), Map_Expr(els, tr));
}
} Additionally, it breaks the nice symmetry of constructors and destructors:
versus
Btw, C# doesn't require vars in all cases, right? |
I would have followed you for the assymetry, but we do require the "var" keyword to assign local variables, and this is an assymetry already. You're partially right for c#, it only requires We cannot do the same for dafny because type ascription cannot introduce local variables by itself, only the keyword match x with
case Plus(a: int, b: int) => Plus(a: int, b: int) |
To create local variables (not to assign them), but I'm not seeing the connection(?) |
Yes to create local variables. You are not seeing the connection? |
No, not really :/ I was talking about the symmetry between So I could see arguing for |
I don't think the @keyboardDrummer Do we have any other pending warning cases that would catch this? |
(I'm not sure about the exact error messages so I've just made these up) datatype C = LongName | OtherLongName
method f(c: C) {
var y := match c {
case LongName => assert c != OtherLongName; 1 // Error, constructors in case patterns must be followed by parentheses
};
} datatype C = LongName | OtherLongName
method f(c: C) {
var y := match c {
case Longname() => assert c != OtherLongName; 1 // Error, `Longname` is not a constructor
};
} |
The following is currently ambiguous
This fails because
Longname
is not the same asLongname
. I know we are working on a warning system, but what about the following syntax introduction: thevar
pattern in c#, which is necessary to introduce a variable.That way, the first one would yield something like "Longname" is not recognized (that's perfect) and the second would clearly indicate that Longname is a variable (no ambiguity !)
The only caveat though would be that it will lengthen cases when extracting all parameters of a datatype
Alternatively, we could also support the named syntax:
The text was updated successfully, but these errors were encountered: