Comments (7)
My immediate impression is that this seems fine. I haven’t thought about the full implications, but it shouldn’t be too hard to implement some sort of dependency graph in the pattern compiler (something like this will be needed to do simpler optimizations like caching name lookups anyways).
However, while I have been doing some experimenting on the side, I’m not planning on implementing any optimizations until the “dumb” implementation is already merged. The PR review will already be wildly complex, and I think that knowledge transfer for other maintainers will be better if we keep things simple to start.
from patma.
That’s a good plan, and I would even say if you can simplify the code by making it slower that would be fine.
from patma.
Cool. Isn’t there a quote attributed to you about Python having the “dumbest compiler ever” (or something to that effect)? ;)
from patma.
We might have to be very, very careful with this. At the top of my mind, I am thinking of something along the lines of:
y = 0
def get_y():
return y
def foo(arg):
global y
match arg:
case (x, y) if x == get_y(): ...
With the proposed optimisation, we clearly end up getting the wrong result.
So, if the compiler can prove that the expression in the guard has no way of accessing any variables bound by subsequent patterns, it might be fine.
However, the intent of the question is probably more about the guarantees given by the specs, though. The specs would probably not want to explicitly name or propose such optimisations, i.e. allow them only to be applied if the runtime semantics does not change at all. That being said, at the same time, the specs should make it clear that there is no guarantee ever that the interpreter will or will not check any given pattern.
In a case like the following, the interpreter does IMHO not have to check the patterns P
and Q
applied to 1
and 2
, respectively, first. If it discovers that 3 == "three"
from the outset, it can just skip the entire pattern directly. At the same token: it does not have to skip P
and Q
, either, of course.
match (1, 2, 3):
case (P, Q, "three"): ...
from patma.
We'd have to trust (or not care) that there isn't a function call in a guard that implicitly references one of the later capture variables, so we'd have to have some explicit language in the PEP to allow this. (Similar to the existing language about caching certain outcomes.)
I think that if we're already fine breaking things like side-effecty attribute/item access, then breaking @Tobias-Kohn's example is a no-brainer. ;) Note that our handling of dotted names means that this pattern won't be an issue for assignments to attributes on self
, for example... I think that using global/nonlocal name bindings in unintuitive ways like this is the only trivial way of producing broken code.
Are there any non-toy examples that are broken by this, but are still clearly better written using pattern matching than other control-flow constructs like if
/else
? I'm struggling to come up with any.
from patma.
I think it's a purely theoretical concern, but nevertheless people could write code to find out how the implementation works in practice (or just use "dis"), and then write obfuscated code that depends on it just to show off. :-)
from patma.
I consider this fully pepped; PEP 635 has explicit language:
If a guard stipulates that a variable
x
must be positive, say (i.e.if x > 0
), the interpreter might check this directly after bindingx
and before any further subpatterns are considered.
from patma.
Related Issues (20)
- Phrasing update in abstract and overview HOT 2
- Disallow guards when using an "open pattern"? HOT 11
- JSON example HOT 5
- Add link to DLS paper to PEP 635 HOT 1
- Reach out to Pablo for code review HOT 1
- Use "as" for named patterns HOT 2
- Raise SyntaxErrors for anything following irrefutable patterns
- Finish AST validation for patterns
- Catch remaining errors in the compiler HOT 1
- Unify the PEP 634 and implementation grammars HOT 2
- Review progress for the reference implementation HOT 5
- Thomas Wouters' objections HOT 13
- Nick Coghlan's (revised) PEP 642 HOT 1
- Dedicated AST nodes for patterns? HOT 5
- PyCon 2021 HOT 10
- work error with zip param "strict=False" on py386 HOT 1
- Please document the design decisions behind PEP 634. HOT 7
- Case goto's? HOT 1
- PyCon JP 2021 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from patma.