Pattern matching formalization #13
Labels
feature
This issue is related to implementing a major feature of the compiler.
language feature
Adding a new feature into the language.
Right now pattern matching is not very clearly defined, so this issue attempts to resolve that.
Matching against existing variables, vs introducing new variables in patterns
One problem I have with Rust's pattern matching, is that there's no way of matching against an existing variable. Each identifier introduces a new variable, eg. in
Some(x)
,x
is a new variable, inThing { x: y }
,y
is also a new variable. Thus in tsuki I want to have a proper syntax for introducing variables into scope, vs matching against existing values.Since bringing values into scope is a more common use case than matching existing variables, the syntax
val x
can be used to match against an existing variable.The patterns
We should support a fairly limited, yet flexible set of patterns in the beginning.
_
1
,true
,:my_atom
,"Hello there."
==
.1..5
,1..<5
.val abc
abc
, using the equality operator==
.abc
,var abc
(x, var y)
.Some(x)
,MyUnion.MyVariant(var a, var b)
MyObject { some_field = x, another_field = var y }
Refutability
A pattern is irrefutable if it can be proved to always match, no matter the input. A pattern is refutable if it can be proved to not match sometimes, given a specific set of inputs. These are mutually exclusive, ie. a pattern that is not irrefutable is refutable, so only specifying one of these rules is enough.
A pattern is refutable if it contains any of the following patterns:
Any pattern that does not contain any of the aforementioned patterns is irrefutable.
Unifying assignment
With patterns introduced, assignment should be unified such that it uses pattern matching instead of some arbitrary keywords. Having assignment return the old value is quite a nice feature to have, so the existing
=
operator is not going anywhere. A newlet
statement could be introduced for matching. It would obsolete the existingval
andvar
statements in favor of taking a pattern to match against on its left hand side. This code:would instead be written as:
Although it looks a little verbose at first, it is a lot more flexible, as it allows for matching against patterns:
let
also becomes part of theif
statement andwhile
loop.As before, it should be possible to specify multiple patterns, all of which must match.
for
loops with patternsfor
loops should also use this pattern syntax, always expecting an irrefutable pattern before thein
keyword.As demonstrated before, more complex matches can be made.
The text was updated successfully, but these errors were encountered: