Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19967

Re: !0 versus true

Message-ID <2788472.6QvGpzPONO@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-05-27 12:07 +0200
Subject Re: !0 versus true
Newsgroups comp.lang.javascript
References (3 earlier) <knooc3$qpi$1@dont-email.me> <1639100.PjNAD8PV0k@PointedEars.de> <8f4997a5-cb1c-4d4c-961e-c88226531c6d@cl9g2000vbb.googlegroups.com> <2307387.pZyV4EZjM8@PointedEars.de> <7bbd3b78-c201-4ac4-98b6-77a3d270c807@fv8g2000vbb.googlegroups.com>

Show all headers | View raw


Scott Sauyet wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Scott Sauyet wrote:
>>> Thomas 'PointedEars' Lahn wrote:
>>>> An *expression* is
>>>> /equivalent/ to another *expression* if both expressions are evaluated
>>>> to the same value under the same conditions; one expression can be
>>>> exchanged with the other without affecting the outcome of expressions
>>>> in which they are used. [ ... ]
>>> 
>>> But this is less clear to me, at least as to its practical
>>> implications.  Because of the ability to retrieve (at least an
>>> approximation of) the source of a function as a string from within
>>> running code, expressions that are not character-for-character
>>> identical can cause different behaviors in a program, even though
>>> meeting your definition above.  This makes me suspect that your
>>> definition doesn't really capture what one would want it to capture:
>>> 
>>> var f=function(){return 0;};
>>> alert(("" + f).substring(19, 20));
>>> 
>>> versus:
>>> 
>>> var f=function(){return +!1;};
>>> alert(("" + f).substring(19, 20));
>>> 
>>> Although the expressions `0` and `+!1` should be considered equivalent
>>> by such a definition, these two short programs have different
>>> behaviors in many modern ES implementation, the first alerting "0",
>>> the second alerting "+".
>>
>> If that.  The string representation of functions, that is, the return
>> value of Function.prototype.toString() for a Function instance as its
>> “this” value, is *implementation-dependent* (ES 5.1, §15.3.4.2).
> 
> Correct, although it's scheduled to become more standardized in ES 6:
> 
>     <http://wiki.ecmascript.org/doku.php?
> id=harmony:function_to_string>

This is interesting, but does not justify the statement “is scheduled to 
become more standardized”.
 
>> Your confusion stems from the fact that you are proceeding from a false
>> assumption.  It is _not_ the function that is used in your example, but a
>> string representation of the function.
> 
> I believe I said as much.

No, you did not.
 
> I was not expressing confusion.

But evidently you *are* confused.

> I was noting that your definition of equivalence sounds as though it would
> imply that two programs that differed only by a difference of two
> expressions that were equivalent would in fact have identical behavior,
> but that this is not actually true.  I realize you didn't claim this.
> But the fact that it's not true means that the definition has less utility
> than it seems.

No, it means that you are trying (and failing) to use a strawman argument.
 
>> If you were to consider the function instead, you would find that
>>
>>   f() === 0
>>
>> in both cases.  That is, the expressions “0” and “+!1” *are* *equivalent*
>> because they are evaluated to the same (*identical*) value, (+)0 under
>> the same (here: all) conditions.  As a result, the functions returning
>> that value are equivalent, i.e. one can be replaced with the other in the
>> same expression without affecting the outcome (“true”) of evaluating the
>> expression in which it is used.  (It is that equivalence why refactoring
>> and minimizers work.)
> 
> And my example shows one limitations of minimizers (and in fact in all
> refactorings) in languages that allow programs access to their own
> source code.

No, it does not.  Minimizing and all refactoring are operations on the 
original *source code*.  You are confusing the expressions with the values 
created from them.
 
> Here you simply restate your definition.

I have used your example to show that “my” definition is correct.

> It's a reasonable-seeming one.

It is a/the correct one.

> But it has a serious flaw, which might be stated like this: two programs
> which differ only in that a single expression in one has been replaced by
> an equivalent expression in the other do not necessarily exhibit the same
> behavior under the same conditions.

No, you have just learned the difference between an expression and its 
string representation, and the difference between functional equivalence and 
value-equivalence.

BTW, this is beyond the scope of this newsgroup.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

!0 versus true Andrew Poulos <ap_prog@hotmail.com> - 2013-05-23 23:31 +1000
  Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-23 07:58 -0700
  Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-23 17:53 +0200
    Re: !0 versus true Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-24 12:17 -0700
      Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-24 21:56 +0200
        Re: !0 versus true Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-24 13:13 -0700
          Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-24 22:23 +0200
      Re: !0 versus true Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-24 22:08 +0000
        Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-25 07:00 +0200
          Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-25 20:04 -0700
            Re: !0 versus true "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-05-26 11:07 +0200
            Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-26 13:11 +0200
              Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-26 13:28 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-27 12:07 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-27 11:27 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-27 23:30 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-27 17:12 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-28 02:40 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-27 20:29 -0700
                Re: !0 versus true John Harris <niam@jghnorth.org.uk.invalid> - 2013-05-28 15:38 +0100
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-28 08:32 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-28 21:14 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-28 15:38 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-29 13:20 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-29 06:37 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-29 16:28 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-29 13:32 -0700
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-29 23:34 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-30 06:30 -0700
                Re: !0 versus true John Harris <niam@jghnorth.org.uk.invalid> - 2013-05-29 17:33 +0100
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-29 11:34 -0700
                Re: !0 versus true John Harris <niam@jghnorth.org.uk.invalid> - 2013-05-31 10:22 +0100
                Re: !0 versus true Christoph Becker <cmbecker69@gmx.de> - 2013-05-31 13:04 +0200
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-31 18:13 +0200
                Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-31 18:16 +0200
                Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-31 07:25 -0700
  Re: !0 versus true imma harper <immaterial_uk@yahoo.co.uk> - 2013-05-31 09:46 -0700
    Re: !0 versus true Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-31 21:38 +0200
    Re: !0 versus true John Harris <niam@jghnorth.org.uk.invalid> - 2013-06-01 17:37 +0100
  Re: !0 versus true Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-06-02 21:03 +0100
    Re: !0 versus true Scott Sauyet <scott.sauyet@gmail.com> - 2013-06-03 04:45 -0700
      Re: !0 versus true Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-06-04 17:23 +0100

csiph-web