<div dir="ltr">You already know I want this for contracts etc.. Here some things that I consider important:<div><br></div><div>1. There should be some way to bind the names to function parameters, as in</div><div><br></div><div> @contract</div><div> def invert(x: `x != 0`) -> float: return 1<span class="inbox-inbox-Apple-converted-space"> / x</span></div><div><br></div><div><div> @contract</div><div> def invertdiff(x: int, y: `x != y`) -> float: return 1<span class="inbox-inbox-inbox-inbox-Apple-converted-space"> / (x-y)</span></div></div><div><br></div><div>2. For this and other reasons, the AST should be available. I think it can be a single AST per place in code, but it should be immutable.</div><div><br></div><div>3. Backticks are problematic because they cannot be nested. I suggest (name: <expression>) or ('name': expression). This name can be googled.</div><div><br class="inbox-inbox-Apple-interchange-newline"> def compose(f: `such_that: pure(f)`,<br> g: `such_that: pure(g)`):<br> return lambda x: f(g(x))</div><div> <br></div><div>4. I think it's a bad idea to use thunks as DSL (different semantics than standard expressions), except in annotations and for specification purposes.</div><div><br></div><div>In short, I want this thing. But for only annotations, assertions, and possibly default arguments as an ad-hoc fix.</div><div><br></div><div>Elazar</div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Sep 26, 2016 at 5:05 PM Joseph Jevnik <<a href="mailto:joejev@gmail.com">joejev@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>Hello everyone, this idea looks like something I have tried building already: <a href="https://github.com/llllllllll/lazy_python" target="_blank">https://github.com/llllllllll/lazy_python</a>. This project implements a `thunk` class which builds up a deferred computation which is evaluated only when needed. One use case I have had for this project is building up a larger expression so that it may be simplified and then computed concurrently with dask: <a href="http://daisy-python.readthedocs.io/en/latest/" target="_blank">http://daisy-python.readthedocs.io/en/latest/</a>. By building up a larger expression (and making the tree accessible) users have the ability to remove common subexpressions or remove intermediate objects. In numpy chained expressions often make lots of allocations which are quickly thrown away which is why projects like numexpr (<a href="https://github.com/pydata/numexpr" target="_blank">https://github.com/pydata/numexpr</a>) can be such a serious speed up. These intermediates are required because the whole expression isn't known at the start so it must be evaluated as written.<br><br></div>Things to consider about when to evaluate:<br><br></div><div>1. Functions which branch on their input need to know which branch to select.<br></div><div>2. Iteration is really hard to defer in a way that is efficient. lazy_python just eagerly evaluates at iteration time but builds thunks in the body.<br></div><div>3. Stateful operations like IO which normally have an implied order of operation now need some explicit ordering.<br><br></div><div>Regarding the `Py_TYPE` change: I don't think that is correct unless we made a thunk have the same binary representation as the underlying object. A lot of code does a type check and then calls macros that act on the actual type like `PyTuple_GET_ITEM` so we cannot fool C functions very easily.<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Sep 26, 2016 at 9:27 AM, Sjoerd Job Postmus <span dir="ltr"><<a href="mailto:sjoerdjob@sjoerdjob.com" target="_blank">sjoerdjob@sjoerdjob.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Mon, Sep 26, 2016 at 10:46:57PM +1000, Steven D'Aprano wrote:<br>
</span><div><div>> Let's talk about lazy evaluation in a broader sense that just function<br>
> annotations.<br>
><br>
> If we had syntax for lazy annotation -- let's call them thunks, after<br>
> Algol's thunks -- then we could use them in annotations as well as<br>
> elsewhere. But if we special case annotations only, the Zen has<br>
> something to say about special cases.<br>
><br>
><br>
> On Mon, Sep 26, 2016 at 02:57:36PM +1000, Nick Coghlan wrote:<br>
> [...]<br>
> > OK, that does indeed make more sense, and significantly reduces the<br>
> > scope for potential runtime compatibility breaks related to<br>
> > __annotations__ access. Instead, it changes the discussion to focus on<br>
> > the following main challenges:<br>
> ><br>
> > - the inconsistency introduced between annotations (lazily evaluated)<br>
> > and default arguments (eagerly evaluated)<br>
> > - the remaining compatibility breaks (depending on implementation details)<br>
> > - the runtime overhead of lazy evaluation<br>
> > - the debugging challenges of lazy evaluation<br>
><br>
><br>
> Default arguments are a good use-case for thunks. One of the most common<br>
> gotchas in Python is early binding of function defaults:<br>
><br>
> def func(arg=[]):<br>
> ...<br>
><br>
> Nine times out of ten, that's probably not what you want. Now, to avoid<br>
> all doubt, I do not want to change function defaults to late binding.<br>
> I've argued repeatedly on comp.lang.python and elsewhere that if a<br>
> language only offers one of early binding or late binding, it should<br>
> offer early binding as Python does. The reason is, given early binding,<br>
> it it trivial to simulate something like late binding:<br>
><br>
> def func(arg=None):<br>
> if arg is None:<br>
> arg = []<br>
> ...<br>
><br>
> but given late binding, it is ugly and inconvenient to get a poor<br>
> substitute for early binding when that's what you want. So, please,<br>
> let's not have a debate over the behaviour of function defaults.<br>
><br>
> But what if we could have both? Suppose we use backticks `...` to make a<br>
> thunk, then we could write:<br>
><br>
> def func(arg=`[]`):<br>
> ...<br>
><br>
> to get the late binding result wanted.<br>
><br>
> Are there other uses for thunks? Potentially, they could be used for<br>
> Ruby-like code blocks:<br>
><br>
> result = function(arg1, arg2, block=```# triple backticks<br>
> do_this()<br>
> do_that()<br>
> while condition:<br>
> do_something_else()<br>
> print('Done')<br>
> ```,<br>
> another_arg=1)<br>
><br>
><br>
> but then I'm not really sure what advantage code blocks have over<br>
> functions.<br>
><br>
><br>
> > The inconsistency argument is simply that people will be even more<br>
> > confused than they are today if default arguments are evaluated at<br>
> > definition time while annotations aren't. There is a lot of code out<br>
> > there that actively relies on eager evaluation of default arguments,<br>
> > so changing that is out of the question, which then provides a strong<br>
> > consistency argument in favour of keeping annotations eagerly<br>
> > evaluated as well.<br>
><br>
> Indeed. There are only (to my knowledge) only two places where Python<br>
> delays evaluation of code:<br>
><br>
> - functions (def statements and lambda expressions);<br>
> - generator expressions;<br>
><br>
> where the second can be considered to be syntactic sugar for a generator<br>
> function (def with yield). Have I missed anything?<br>
><br>
> In the same way that Haskell is fundamentally built on lazy evaluation,<br>
> Python is fundamentally built on eager evaluation, and I don't think we<br>
> should change that.<br>
><br>
> Until now, the only way to delay the evaluation of code (other than the<br>
> body of a function, of course) is to write it as a string, then pass it<br>
> to eval/exec. Thunks offer an alternative for delayed evaluation that<br>
> makes it easier for editors to apply syntax highlighting: don't apply it<br>
> to ordinary strings, but do apply it to thunks.<br>
><br>
> I must admit that I've loved the concept of thunks for years now, but<br>
> I'm still looking for the killer use-case for them, the one clear<br>
> justification for why Python should include them.<br>
><br>
> - Late-bound function default arguments? Nice to have, but we already<br>
> have a perfectly serviceable way to get the equivalent behaviour.<br>
><br>
> - Code blocks? Maybe a Ruby programmer can explain why they're so<br>
> important, but we have functions, including lambda.<br>
><br>
> - Function annotations? I'm not convinced thunks are needed or desirable<br>
> for annotations.<br>
><br>
> - A better way to write code intended for delayed execution? Sounds<br>
> interesting, but not critical.<br>
><br>
> Maybe somebody else can think of the elusive killer use-case for thunks,<br>
> because I've been pondering this question for many years now and I'm no<br>
> closer to an answer.<br>
<br>
</div></div>Well, there's a use-case I have been pondering for a long while now<br>
which could be satisfied by this: enumerated generator displays.<br>
<br>
So suppose you have a composite boolean value, composed by the 'and' of<br>
many conditions (which all take long to compute), and you want to<br>
short-circuit. Let's take the following example.<br>
<br>
valid = True<br>
valid &= looks_like_emailaddress(username)<br>
valid &= more_than_8_characters(password)<br>
valid &= does_not_exist_in_database(username)<br>
valid &= domain_name_of_emailaddress_has_mx_record(username)<br>
... some more options ...<br>
<br>
(I forgot the exact use-case, but I still remember the functionality I<br>
wanted, so bear with me).<br>
<br>
Of course, the above is not short-circuiting, so it would be replaced by<br>
<br>
def check_valid(username, password):<br>
if not looks_like_emailaddress(username): return False<br>
if not more_than_8_characters(password): return False<br>
if not does_not_exist_in_database(username): return False<br>
if not domain_name_of_emailaddress_has_mx_record(username): return False<br>
...<br>
return True<br>
<br>
<br>
valid = check_valid()<br>
<br>
or<br>
<br>
valid = True\<br>
and looks_like_emailaddress(username)\<br>
and more_than_8_characters(password)\<br>
and does_not_exist_in_database(username)\<br>
and domain_name_of_emailaddress_has_mx_record(username)<br>
<br>
But in all reality, I want to write something like:<br>
<br>
valid = all(@@@<br>
looks_like_emailaddress(username),<br>
more_than_8_characters(password),<br>
does_not_exist_in_database(username),<br>
domain_name_of_emailaddress_has_mx_record(username),<br>
@@@)<br>
<br>
With `@@@` designating the beginning/ending of the enumerated generator<br>
display.<br>
<br>
Now, this is currently not possible, but if we had some kind of thunk<br>
syntax that would become possible, without needing an enumerated<br>
generator display.<br>
<br>
However the problem I see with the concept of `thunk` is: When does it<br>
get un-thunked? In which of the following cases?<br>
<br>
1. When getting an attribute on it?<br>
2. When calling it? --> See 1. with `__call__`.<br>
3. When subindexing it? --> See 1. with `__getitem__`.<br>
4. When assigning it to a name? It shouldn't have to be un-thunked, I<br>
think.<br>
5. When adding it to a list? No un-thunking should be necessary, I<br>
think.<br>
<br>
However, the problem with thunks is (I think) that to make that happen<br>
either<br>
<br>
- *all* objects need to include yet another level of redirection,<br>
or<br>
- a thunk needs to get allocated the maximum size of the value it could<br>
possibly store. (But a `unicode` object could have an arbitrary size)<br>
or<br>
- there needs to be some way to 'notify' objects holding the thunk that<br>
its value got updated. For a dict/list/tuple this could readily grow<br>
into O(n) behaviour when un-thunking a thunk.<br>
or<br>
- any C-level functionality needs to learn how to deal with thunks. For<br>
instance, `Py_TYPE` would have to *resolve* the thunk, and then return<br>
the type of the value.<br>
or<br>
- I'm running out of ideas here, but maybe creating a custom type object<br>
for each thunk that does pass-through to a wrapped item? Thunked<br>
objects would work *exactly* the same as normal objects, but at a<br>
(small) indirection for any action taken. Still, somehow `Py_TYPE` and<br>
`Py_SIZE` and any other macros would still have to force evaluation.<br>
<br>
Kind regards,<br>
Sjoerd Job<br>
<div><div>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br></div>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a></blockquote></div>