:wave::skin-tone-2: Welcome everyone! I'm excited ...
# pattern-matching
m
👋🏻 Welcome everyone! I'm excited to see what we can pull off with this! So I'm still catching up from yesterday's conversation, but so far I'm seeing a couple of high-level things: 1. Scala's pattern matching is nice to have for smaller parameter lists (I'm thinking of typical ADT matches returned by monads like
Some
or
None
. However, for constructor matches with lots of parameters, this can be confusing when there are similar types. Honestly, it's something in Scala that I've just dealt with but now that Raul has called attention to that, I agree. It would be nice to offer something a little bit easier to deal with. I like Raul's suggestion of making use of lenses for this. I feel we could offer both, as Nico pointed out. For starters, from a compiler plugin perspective, I'm thinking of starting with supporting at least a few basic Scala-style pattern matches using their constructor-styled wildcards, and type constraints (
Person(firstName = "Matt", lastName = String)
) or something similar to that. Of course you could just do it without named parameters
Person("Matt", String)
. For wildcard matching, if using
_
, what do you think about essentially translating that to
Any
? Or when using as a wildcard for a given field, would you expect that to match the type of the field? I'm thinking for a standalone wildcard (no class constructor) just rewrite
_
as
Any
but if it's in a class constructor, look up the type of the field and constrain the param to that type. 2. I like the use of guards. Regarding partial matches though, I don't yet have any ideas about how that would get implemented as a plugin. @raulraja correct me if I'm wrong, but I can still use Meta to at least get the
when
expression, but I'm not yet sure just how deeply I can check individual types on parameters. In my limited time looking into Meta, it appears most (all?) nodes are accessible, so I'm hoping I can check the type of a given value through the AST node? I'm also not sure I understand yet the structure of
KtWhenCondition
on a given
KtWhenEntry
. It's a collection, but there seems to only be a single condition attached even though I have multiple conditions...I'm probably not understanding what the breakdown is though.
n
Tiny note on (1.), you should make
_
a match on
Any?
rather than
Any
🙂
As fot
KtWhenCondition
. I really do not know what you are looking at, but that looks like the grammar names. So my guess is you are staring at the AST or more likely the parse tree. For an idea on what nodes have what children, I suggest you take a look at the official grammar. https://kotlinlang.org/docs/reference/grammar.html It's in antlr's flavour of BNF. Let me know if you need help reading it but there should be plenty of resources online
r
quotes won’t help us here
for a decent plugin we need to consider what I put on the channel
there are several phases
quotes or tree is the less important one in this case
as long as you can just produce valid syntax in the Kotlin Grammar
you would like to see type errors which is where the fun begins and we can create ad-hoc relations and alter smartcasting for the branches of the when
n
Right we can start by discarding the keep grammar then. It is not valid grammar in the parse tree
r
what would be the closest to something that does not type check but is grammatically correct?
for example in place of _ backticked as singleton object which we assign a special meaning to
or with a different symbol that is valid kotlin for ignoring
such as * which means in type projection DONT_CARE
n
Assuming we are sticking to
when
(otherwise anything is possible?) if we drop the keyword
is
we can do much more, because that's what causes a match with the
typecheck
node which is just a hardocded
inOperator type
(ie,
is Person
)
r
so if you change
is
for something
case
or similar
n
As long as the first word of the
when
entry is not
in
or
is
, we can match on expressions in general
r
ok, then it’s gonna be easier
n
case
should work yes I'll check
Yep
case(..)
works. We need the braces otherwise it's not an expression
r
it could work if it was nested in a receiver as an infix object with an invoke
n
we can do
Copy code
when(3) {
    case (Person(x,y)) where s -> 2
}
fine.
s
is also an expression so it could whatever too
r
but parents is good for now
n
I am using the antlr grammar checker, it does not like non parenthesis
Copy code
when(3) {
    case Person(x,y) where s -> 2
}
this is invalid
r
yes with where is ambiguous
what about without the where?
n
Doesn't work wither. The problem is Person, it's just not expecting an identifier
r
I see
thanks for trying that
n
No worries. I had the anltr grammar set up because I was planning to write a toy transpiler for the keep. So we can test the grammar and see how the stuff gets resolved to find it in the ast later
r
The arrow meta quotes give you already and ADT for kotlin and kastree also has one
we depend on a modified kastree
that keeps a connection the psi element
n
For some reason this is valid
Copy code
when(3) {
    case Person[y,3,Person[x,y]] where 2 -> 2
}
ie, square brackets apparently are a collection literal which is an atomicExpression
r
woooo
now we are talking
the semantics are also of operator access which is what is doing in the nested structure
it’s a polyadic version of the unsafe get but it wouldn’t be unsafe in matching since it’s a partial function
n
I agree semantically it kind of makes sense. This is also valid
Copy code
when(3) {
    case Person[y,3,Person[x,y]], case Person[2] where 2 -> 2
}
So we can make disjunctions a thing, which are the existing semantics of having a comma in a when condition
r
right that is excelent
can you use || as disjunction opoerator?
or | ?
also the the semantics of where as expression here are in sync with those of the lang. where is used for sub type upper bounds but here is a range of inhabitants of a type based on the match so it’s the same semantics but for the runtime
n
You can use
||
:)) but personally ngl I think a comma is good. Think of:
Copy code
when(alice) {
  is Person, Alien -> ...
  is Zombie, in 2..3 -> ...
}
a comma already represents a disjunction in the language
r
comma represents a product so both cases
in argument position always
so it represents the opposite of the coproduct
n
Is it not a sum in a
when
??
r
coproduct in kotlin is sealed hierarchy, enumeration or the upcoming union types
oh you mean for
is
?
n
wait did a miss something what do you mean upcoming are union types coming?? In the lang I mean
Yeah in the
is
case it's a disjunction
r
i didn’t know that thanks
in that case is consistent
yes, I have union types working in kotlin and other crazy stuff
refined types, SMT solving, structural polymorphism, cohercion functions and other stuff people have not seen yet that is comming to meta
most of them come with KEEP this year but not until we test them in Arrow and we cover all the edge cases.
n
so many types ahahah nice
r
do you mind sharing the error with the braces?
I want to see where it is chocking type checking or wherever
n
Yep 1min
It's an antlr error so it's a parse error I'm afraid
line 2:9 mismatched input 'Person' expecting {NL, ',', '->'}
that's the antlr error 🙂
For this:
Copy code
when(3) {
    case Person(2,3) -> 2
}
r
I mean for your case sorry meant to say square brackets
the one that finds it as a valid expression in grammar
n
oh ok
unresolved identifier for
case
obviously
Unsupported [Collection literals outside of annotations] for the square braces
And unresolved reference
x
that's all. It's for:
Copy code
val x = when(3) {
      case Person[x] -> 2
      else -> 23
    }
so it's a when expression
the reason for collection literals in the grammar makes more sense now ahahah
r
totally
Here is some code that gets to typechecking
n
What happened to the idea of using property accessing syntax?
It looks like it will be work but it looks preetty good
r
It's compatible too if we want to auto project the optics DSL
But if you want underscore and Scala like destructuring then it looks like we can make it work
Or you mean [ ]
That can work too
n
Underscore is great but I meant [ ] because it allows
case [...], case Person[...]
instead of
case((..)), case(Person(..))
, saving us from a pair of brackets. But i don't mind semantics are the same now.