Le Fri, Feb 26, 2021 at 09:44:51PM -0500, Prakhar Goel a écrit :
> > Indeed, internally it rewrites the AST, of course. I think that's the
> > simplest way to implement it. But it doesn't mean the user that write
> > the compile-time code would have to be aware of the AST at all.
> > I mean, if you have a good use-case for it, then I'm ok with allowing
> > the compile-time code manipulating it. But I'd really prefer this not be
> > the main use of the compile-time execution feature.
> 
> Ah. I see where you are coming from. Wouldn't this be a ridiculous
> amount of work? At least if we insisted on something with a clean
> mental model that wouldn't just shred the user with a thousand corner
> cases? It would need a whole new sub-syntax in Cython with detailed
> semantics for all the weird things code lets people do (e.g. what if
> the input function f gets used like a first-class object and placed in
> a Python list that then gets passed to another function... at compile
> time...). You're basically asking for scheme's syntax-case but harder
> because Python/Cython are nowhere near as regular! And scheme's
> syntax-case is a beast (read up on phase separation if you don't
> believe me: https://docs.racket-lang.org/guide/phases.html).

Clearly, I think what I'm proposing has one of the simplest mental model
one could dream of. And if I'm not mistaken, it's not much work (I mean,
no fancy algorithms).
I mean, how could the mental model be any simpler than bringing in that
of python itself?

I don't know what example you had in mind, but I don't plan to have any
kind of static analysis in cython at all. All the burden is offloaded to
the python interpreter by running nothing but Pure Python Code. I'll
expand on that in a minute.

I mean, if you think you got an example that would be hard to analyze
with your list containing functions, then please expand on it. I'll
try my best to answer how my proposal would handle it. That's one of the
reasons I started this thread: to challenge the idea.

Here's how I see it working.
After some basic analysis, cython produces a python program that
basically rebuild the AST at that point and run the code itself at the
same time, with a few modifications.
1) cdef aren't run (of course, it's not python code), they're replaced
by the creation of a CdefFunction or CdefClass objects.
2) Functions marked ctfe_only aren't added to the AST.
3) Assignments maked with the `ctfe` keyword are handled specially to
turn whatever was returned to a new definition. (I can expand on that if
you want, especially on the "outlining" of functions.)

Running the code mostly means that all the def and assignment and
everything is evaluated. This has some obvious drawbacks, though.
Especially if it's a stadalone program, it would just... run. But this
is unavoidable. For instance the program could have conditional function
definition (a def in a if), that's used as compile time.
But I guess it's not much of an issue since a module would not have much
to run during load time, and a standalone program would be protected by
`if __name__ == "__main__":`.

> AST manipulation isn't _that_ hard (esp. with good helper libraries)
> and on the plus side, the basic idea is dead-simple to implement: just
> call some random Python function that the user points you towards.
> There's going to be some scaffolding but of an entirely manageable
> amount.
I'm not saying AST manipulation is hard. I'm rather saying it's unusual
in python. And it's so low level it's rarely easy to understand what's
going.
As someone who reads a lot more code than I write, I use grep and other
such tools a lot. And I have to say I'm not really a big fan of that
kind of trickery. If a coding pattern would fool grep + a brain, other
analysis tools were lost eons ago.

But if you have an actual use case for AST manipulation that would
belong to a preprocessing phase, then please explain it to me.
The only time I had to deal with python AST (beside cython itself,
obviously), was to write an external analysis tool to help me migrate a
program from threads to asyncio. Not a valid use-case for my proposal.


Best Regards,
Celelibi
_______________________________________________
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel

Reply via email to