>From a user's POV, I'm +1 on having overloadable boolean functions. In many cases I had to resort to overload add or neg instead of and & not, I foresee a lot of cases where the and overload could be used to join objects which represent constraints. Overloadable boolean operators could also be used to implement other types of logic (eg: fuzzy logic). Constraining them to just primitive binary operations in my view will be delimiting for a myriad of use cases.
<br><br>Sure, in some cases, one could overload the neg operator instead of the not but semantically they have different meanings.<br><br><div><span class="gmail_quote">On 5/25/07, <b class="gmail_sendername">Guido van Rossum
</b> &lt;<a href="mailto:guido@python.org">guido@python.org</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">On 5/24/07, Greg Ewing &lt;
<a href="mailto:greg.ewing@canterbury.ac.nz">greg.ewing@canterbury.ac.nz</a>&gt; wrote:<br>&gt; Guido van Rossum wrote:<br>&gt;<br>&gt; &gt; Last call for discussion! I&#39;m tempted to reject this -- the ability to<br>&gt; &gt; generate optimized code based on the shortcut semantics of and/or is
<br>&gt; &gt; pretty important to me.<br>&gt;<br>&gt; Please don&#39;t be hasty. I&#39;ve had to think about this issue<br>&gt; a bit.<br>&gt;<br>&gt; The conclusion I&#39;ve come to is that there may be a small loss<br>&gt; in the theoretical amount of optimization opportunity available,
<br>&gt; but not much. Furthermore, if you take into account some other<br>&gt; improvements that can be made (which I&#39;ll explain below) the<br>&gt; result is actually *better* than what 2.5 currently generates.<br>&gt;
<br>&gt; For example, Python 2.5 currently compiles<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;if a and b:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;stats&gt;<br>&gt;<br>&gt; into<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;evaluate a&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JUMP_IF_FALSE L1<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;POP_TOP
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;evaluate b&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JUMP_IF_FALSE L1<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;POP_TOP<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;stats&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JUMP_FORWARD L2<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;L1:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15 POP_TOP<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;L2:<br>&gt;<br>&gt; Under my PEP, without any other changes, this would become
<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;evaluate a&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOGICAL_AND_1 L1<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;evaluate b&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOGICAL_AND_2<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;L1:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JUMP_IF_FALSE L2<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;POP_TOP<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;stats&gt;
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JUMP_FORWARD L3<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;L2:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15 POP_TOP<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;L3:<br>&gt;<br>&gt; The fastest path through this involves executing one extra<br>&gt; bytecode. However, since we&#39;re not using JUMP_IF_FALSE to
<br>&gt; do the short-circuiting any more, there&#39;s no need for it<br>&gt; to leave its operand on the stack. So let&#39;s redefine it and<br>&gt; change its name to POP_JUMP_IF_FALSE. This allows us to<br>&gt; get rid of all the POP_TOPs, plus the jump at the end of
<br>&gt; the statement body. Now we have<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;evaluate a&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOGICAL_AND_1 L1<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;evaluate b&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOGICAL_AND_2<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;L1:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;POP_JUMP_IF_FALSE L2<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;stats&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;L2:<br>&gt;<br>&gt; The fastest path through this executes one *less* bytecode<br>&gt; than in the current 2.5-generated code. Also, any path that<br>&gt; ends up executing the body benefits from the lack of a
<br>&gt; jump at the end.<br>&gt;<br>&gt; The same benefits also result when the boolean expression is<br>&gt; more complex, e.g.<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;if a or b and c:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;stats&gt;<br>&gt;<br>&gt; becomes<br>&gt;
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;evaluate a&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOGICAL_OR_1 L1<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;evaluate b&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOGICAL_AND_1 L2<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;evaluate c&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOGICAL_AND_2<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;L2:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOGICAL_OR_2
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;L1:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;POP_JUMP_IF_FALSE L3<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;stats&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;L3:<br>&gt;<br>&gt; which contains 3 fewer instructions overall than the<br>&gt; corresponding 2.5-generated code.<br>&gt;<br>&gt; So I contend that optimization is not an argument for
<br>&gt; rejecting this PEP, and may even be one for accepting<br>&gt; it.<br><br>Do you have an implementation available to measure this? In most cases<br>the cost is not in the number of bytecode instructions executed but in
<br>the total amount of work. Two cheap bytecodes might well be cheaper<br>than one expensive one.<br><br>However, I&#39;m happy to keep your PEP open until you have code that we<br>can measure. (However, adding additional optimizations elsewhere to
<br>make up for the loss wouldn&#39;t be fair -- we would have to compare with<br>a 2.5 or trunk (2.6) interpreter with the same additional<br>optimizations added.)<br><br>--<br>--Guido van Rossum (home page: <a href="http://www.python.org/~guido/">
http://www.python.org/~guido/</a>)<br>_______________________________________________<br>Python-3000 mailing list<br><a href="mailto:Python-3000@python.org">Python-3000@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/python-3000">
http://mail.python.org/mailman/listinfo/python-3000</a><br>Unsubscribe: <a href="http://mail.python.org/mailman/options/python-3000/nevillegrech%40gmail.com">http://mail.python.org/mailman/options/python-3000/nevillegrech%40gmail.com
</a><br></blockquote></div><br><br clear="all"><br>-- <br>Regards,<br>Neville Grech