Open wenkokke opened 7 months ago
@pokey wrote here that the patterns should be addressed by "name", "key", or "condition".
I've argued above that "condition" should refer to the "subject" and that its use for patterns is confusing.
The complexity of patterns makes it rather odd to refer to them by "name" or "key". That suggestion may have been due to the overly simplistic nature of the examples. For instance, see the following Haskell program, where I've annotated the top-most patterns. At very least, I hope that shows that "name" is inadequate?
someFunction x (y1 : y2 : ys) (a, b, (c, [d])) = rhs
-- ^ ^^ ^^ ^^ ^ ^ ^ ^ <-- names
-- ^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ <-- top-level patterns
-- ^^ ^^^^^^^ ^ ^^^^^^^^^^^ <-\
-- ^^ ^^ ^ ^^^^^^^^ <-| sub-patterns
-- ^ ^^^ <-|
-- ^ <-/
Note: The program looks contrived, due to the fact I've copied it from a parser test, but patterns like these are extremely common in Haskell. Sometimes they use better names.
my thinking would be
someFunction x (y1 : y2 : ys) (a, b, (c, [d])) = rhs
-- ^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ <-- "arg"
-- ^^ ^^ ^^ ^ ^ ^^^^^^^^ <-\
-- ^ ^^^ <-| "item"
-- ^ <-/
I'm not sure if item makes sense in the general case?
either :: (a -> c) -> (b -> c) -> Either a b -> c
either f _ (Left x) = f x
either _ g (Right y) = g y
Are the x
and y
on the left hand side still item?
@pokey What do you think should be used in the JavaScript, Python and Rust examples?
These aren't value
because they're binding occurrences rather than values, so the use cases, domain and iteration scopes are very different.
These aren't key
or item
because they're not always in a list or map like context, and furthermore because they're binders rather than items in a list.
I'm not sure if item makes sense in the general case?
either :: (a -> c) -> (b -> c) -> Either a b -> c either f _ (Left x) = f x either _ g (Right y) = g y
Are the
x
andy
on the left hand side still item?
no I think I'd call those "arg"
?
we tend to use the same scope name whether it's constructing or deconstructing. makes it easier for the ol' lizard brain that just sees shapes
@pokey What do you think should be used in the JavaScript, Python and Rust examples?
I believe they're as follows, but easiest to paste them into a file, pull up the "bar cursorless" sidebar and click through the scopes to see what hits. Tho I'm guessing we don't support the tc39 one
// TypeScript
function fst<A, B>(tup: [A, B]) {
const { x, y } = tup;
// ^^^^^^^^ <~~ name
return x;
}
// Using tc39
function fst<A, B>(tup: [A, B]) {
match (tup) {
when ({ x, y }): return x;
// ^^^^^^^^ <~~ condition
}
}
# Python
def fst(tup):
match tup:
case (x, y): return x
# ^^^^^^ <~~ condition
I'm not sure if item makes sense in the general case?
either :: (a -> c) -> (b -> c) -> Either a b -> c either f _ (Left x) = f x either _ g (Right y) = g y
Are the
x
andy
on the left hand side still item?no I think I'd call those
"arg"
?
So it's nested args all the way down? Because (Left x)
is also "arg".
So it's nested args all the way down? Because
(Left x)
is also "arg".
In this case, yes, but note that in https://github.com/cursorless-dev/cursorless/issues/2177#issuecomment-1898749525 there's no "arg" nesting
True, but item is inappropriate there, so those cases would also be "arg".
item is inappropriate there
can you elaborate?
item is inappropriate there
can you elaborate?
If
foo x = undefined
-- ^ arg
and
foo (Left x) = undefined
-- ^ arg
then
foo (x : xs) = undefined
-- ^ arg
and
foo [x] = undefined
-- ^ arg
because they're all the same kind of thing.
One major use case for this, the overlapping iteration scopes between formal and actual arguments, might be fixed by separating @argument.formal
and @argument.actual
:
-- vvv argument.actual.iteration
-- v argument.actual
-- v argument.actual
-- v argument.actual
foo f x y = f x y
-- ^ argument.formal
-- ^ argument.formal
-- ^ argument.formal
-- ^^^^^^^^^^^^^ argument.formal.iteration
interesting. maybe let's discuss this one in meet-up?
I propose we add a scope type for the patterns in pattern matches, which are very important in functional languages like Haskell, Scala, and Rust, and increasingly important in languages such as JavaScript (see tc39) and Python (see pep 636).
Patterns are different from conditions. The easiest way to show this is by implementing the if then else construct using patterns.
Patterns are not adequately covered by branches, since "branch" selects the entire branch, which may match on multiple patterns.
This makes it impossible to use "branch" to select, e.g., the first pattern in the second branch.
See related issues #579 and #1174.