Lazy evaluation question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Heathfield

    #16
    Re: Lazy evaluation question

    Boltar said:
    On 5 Jan, 16:38, Richard Heathfield <r...@see.sig.i nvalidwrote:
    >But bitwise operators are not used to establish truth or falsity. They
    >are used to calculate the result of a mathematical operation. Lazy
    >evaluation would be pointless and meaningless.
    >
    Why? If theres a zero on the LHS of a bitwise AND you know the result
    will be zero no matter what is on the RHS so why bother evaluating the
    RHS?
    That's the kind of thing a good optimising compiler will already do.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Thad Smith

      #17
      Re: Lazy evaluation question

      Boltar wrote:
      Why does C do lazy evaluation for logical boolean operations but not
      bitwise ones?
      >
      Ie: the following program prints "1 2" , not "1 1" under gcc
      >
      main()
      {
      int a = 1;
      int b = 1;
      0 && ++a;
      0 & ++b;
      printf("%d %d\n",a,b);
      }
      >
      Surely the bitwise boolean ops are just as appropriate for doing lazy
      evaluation with?
      No, they are not.

      An important goal of C was to generate efficient code. The operands of the
      logical operators &&, ||, !, and ? must usually be tested at run time for a
      true or false value, normally by a conditional branch. There is no extra
      cost to short circuit evaluation and it is a short-hand convenience to
      programming.

      The boolean operators normally map to bit-wise and, or, xor, and
      complement, which return a result value. In general, zero is not a special
      case for the operators and no zero test is needed. Performing short
      circuit evaluation at run time would be added code and execution time with
      little payoff.

      It would also lead to less useful results using operators with side
      effects. Defining
      a = b .& c++; /* where .& is defined as bitwise and with no
      evaluation of second operand when first operand is 0 */
      is less useful than the current
      a = b & c++;
      for me.

      Compilers can and do take advantage of special values when known at compile
      time. a += 0; will generate no code on many compilers if a is not
      volatile. But if you write
      a += 0 * y++;
      the compiler must increment y, even though it is not needed for the
      determination of a. 0 & ++b in your example code falls into the same category.

      --
      Thad

      Comment

      • Richard Tobin

        #18
        Re: Lazy evaluation question

        In article <5Pqdndi7rOzvKO LanZ2dnUVZ8rCdn Z2d@bt.com>,
        Richard Heathfield <rjh@see.sig.in validwrote:
        >Furthermore, there is no concept of 'lazy evaluation' in C. Perhaps in
        >Haskell or some other programming language but certainly not in C, not
        >only that, but I fail to see what lazy evaluation has to do with this.
        >>
        >(a && b) in this expression, b is evaluated ONLY if after 'a' gets
        >evaluated it is not equal to 0 (or NULL)
        >This is generally what people mean by "lazy evaluation".
        No, that's not what's usually meant by lazy evaluation. Lazy
        evaluation means delaying evaluation until the result is needed. So
        if you call f(a+b), a+b might not be evaluated until (and unless) the
        function needs the value of its argument. It's a very general idea.

        What's being referred to here is short-circuit evaluation. You could
        regard it as a very special case of lazy evaluation, in that the right
        operand is not evaluated until the right operand has been determined
        to be true.

        -- Richard
        --
        :wq

        Comment

        • Richard Heathfield

          #19
          Re: Lazy evaluation question

          Richard Tobin said:
          In article <5Pqdndi7rOzvKO LanZ2dnUVZ8rCdn Z2d@bt.com>,
          Richard Heathfield <rjh@see.sig.in validwrote:
          >
          >>Furthermore , there is no concept of 'lazy evaluation' in C. Perhaps in
          >>Haskell or some other programming language but certainly not in C, not
          >>only that, but I fail to see what lazy evaluation has to do with this.
          >>>
          >>(a && b) in this expression, b is evaluated ONLY if after 'a' gets
          >>evaluated it is not equal to 0 (or NULL)
          >
          >>This is generally what people mean by "lazy evaluation".
          >
          No, that's not what's usually meant by lazy evaluation.
          Yes, I accept the correction. I was of course thinking of short-circuiting.
          My mistake.

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • Ben Bacarisse

            #20
            Re: Lazy evaluation question

            Boltar <boltar2003@yah oo.co.ukwrites:
            On 5 Jan, 17:12, Willem <wil...@stack.n lwrote:
            >Now that we're talking about it anyway, here's a gripe I have about the
            >logical operators in C:
            >>
            >Why oh why is (a || b) not equivalent to (a ? a : b) [1]
            >The same way (less useful), (a && b) could be equivalent to (a ? b : 0)
            >
            And why isn't there a logical XOR operator? eg: a ^^ b
            >
            Logical XOR might not be required often but it is required and having
            to do
            (a && !b) || (!a && b) is hardly efficient or particularly readable.
            There are lots of simpler options: !a != !b is one.

            --
            Ben.

            Comment

            • Richard Harter

              #21
              Re: Lazy evaluation question

              On Sat, 05 Jan 2008 18:00:38 +0000, Richard Heathfield
              <rjh@see.sig.in validwrote:
              >Boltar said:
              >
              >On 5 Jan, 16:38, Richard Heathfield <r...@see.sig.i nvalidwrote:
              >>But bitwise operators are not used to establish truth or falsity. They
              >>are used to calculate the result of a mathematical operation. Lazy
              >>evaluation would be pointless and meaningless.
              >>
              >Why? If theres a zero on the LHS of a bitwise AND you know the result
              >will be zero no matter what is on the RHS so why bother evaluating the
              >RHS?
              >
              >That's the kind of thing a good optimising compiler will already do.
              Even though the RHS might have side effects?


              Comment

              • Richard Harter

                #22
                Re: Lazy evaluation question

                On Sat, 05 Jan 2008 11:04:10 -0700, Thad Smith
                <ThadSmith@acm. orgwrote:
                >Boltar wrote:
                >Why does C do lazy evaluation for logical boolean operations but not
                >bitwise ones?
                >>
                >Ie: the following program prints "1 2" , not "1 1" under gcc
                >>
                >main()
                >{
                > int a = 1;
                > int b = 1;
                > 0 && ++a;
                > 0 & ++b;
                > printf("%d %d\n",a,b);
                >}
                >>
                >Surely the bitwise boolean ops are just as appropriate for doing lazy
                >evaluation with?
                >
                >No, they are not.
                >
                >An important goal of C was to generate efficient code. The operands of the
                >logical operators &&, ||, !, and ? must usually be tested at run time for a
                >true or false value, normally by a conditional branch. There is no extra
                >cost to short circuit evaluation and it is a short-hand convenience to
                >programming.
                This isn't really right. The point of short circuit evaluation
                of && and || is to suppress evaluation of the RHS when the LHS
                establishes the truth of falsity of the boolean expression. This
                is a programming convenience in code patterns like

                if (ptr && foo(ptr)) {...}

                IF we don't want to call foo when ptr is null. However if we do
                want to call foo regardless of whether ptr is null, then short
                circuit evaluation forces us to do something like

                tmp = foo(ptr);
                if (ptr && tmp) {...}

                It turns out that short circuit evaluation is more convenient
                (IMHO) and that C made the right choice.
                >
                >The boolean operators normally map to bit-wise and, or, xor, and
                >complement, which return a result value. In general, zero is not a special
                >case for the operators and no zero test is needed. Performing short
                >circuit evaluation at run time would be added code and execution time with
                >little payoff.
                >
                >It would also lead to less useful results using operators with side
                >effects. Defining
                a = b .& c++; /* where .& is defined as bitwise and with no
                > evaluation of second operand when first operand is 0 */
                >is less useful than the current
                a = b & c++;
                >for me.
                >
                >Compilers can and do take advantage of special values when known at compile
                >time. a += 0; will generate no code on many compilers if a is not
                >volatile. But if you write
                a += 0 * y++;
                >the compiler must increment y, even though it is not needed for the
                >determinatio n of a. 0 & ++b in your example code falls into the same category.
                >
                >--
                >Thad

                Comment

                • Tor Rustad

                  #23
                  Re: Lazy evaluation question

                  Richard Heathfield wrote:
                  Boltar said:
                  >
                  >On 5 Jan, 16:38, Richard Heathfield <r...@see.sig.i nvalidwrote:
                  >>But bitwise operators are not used to establish truth or falsity. They
                  >>are used to calculate the result of a mathematical operation. Lazy
                  >>evaluation would be pointless and meaningless.
                  >Why? If theres a zero on the LHS of a bitwise AND you know the result
                  >will be zero no matter what is on the RHS so why bother evaluating the
                  >RHS?
                  >
                  That's the kind of thing a good optimising compiler will already do.
                  Really?

                  Methinks, & operator always evaluate both operands, just like the |
                  operator.


                  --
                  Tor <bwzcab@wvtqvm. vw | tr i-za-h a-z>

                  Comment

                  • Syren Baran

                    #24
                    Re: Lazy evaluation question

                    Boltar schrieb:
                    On 5 Jan, 17:21, Willem <wil...@stack.n lwrote:
                    >Boltar wrote:
                    >>
                    >) Yes , my mistake. Should just be AND.
                    >>
                    >Why not OR ?
                    >
                    Yes , if the LHS value is 0xFFFFFFF or whatever theres no point
                    evaluating the RHS of a bitwise OR.
                    Right. But theres no point in checking the LHS side first for a 2 in
                    2^32 chance of not having to do the operation.
                    In x86 assembly this relates to 2 instructions, a CMP ..,0 + a JE ..
                    or a CMP ..,0xFFFFFFFF + a JE .. just to save an AND or OR?
                    A preprocessor can replace such an instruction in case of a constant,
                    but with vars such a check is complete bullshit.
                    >
                    B2003
                    >

                    Comment

                    • christian.bau

                      #25
                      Re: Lazy evaluation question

                      Consider the loops

                      for (i = 0; i < n; ++i)
                      *p++ = *q++ & *r++;

                      and

                      for (i = 0; i < n; ++i)
                      *p++ = *r++ & *q++;

                      Would you want both loops to have the same or different behaviour?

                      Comment

                      • Tor Rustad

                        #26
                        Re: Lazy evaluation question

                        Boltar wrote:
                        Why does C do lazy evaluation for logical boolean operations but not
                        bitwise ones?
                        >
                        Ie: the following program prints "1 2" , not "1 1" under gcc
                        >
                        main()
                        {
                        int a = 1;
                        int b = 1;
                        0 && ++a;
                        0 & ++b;
                        printf("%d %d\n",a,b);
                        }
                        >
                        Surely the bitwise boolean ops are just as appropriate for doing lazy
                        evaluation with?
                        In C, 32 out of 34 binary operators always evaluate both their operands.

                        Many C coding standards prohibits side-effects on the second operand of
                        the && and the || operators, because there are some clueless
                        programmers, not aware of the conditional evaluation of the second operand.

                        --
                        Tor <bwzcab@wvtqvm. vw | tr i-za-h a-z>

                        Comment

                        • Richard Heathfield

                          #27
                          Re: Lazy evaluation question

                          Richard Harter said:
                          On Sat, 05 Jan 2008 18:00:38 +0000, Richard Heathfield
                          <rjh@see.sig.in validwrote:
                          >
                          >>Boltar said:
                          >>
                          >>On 5 Jan, 16:38, Richard Heathfield <r...@see.sig.i nvalidwrote:
                          >>>But bitwise operators are not used to establish truth or falsity. They
                          >>>are used to calculate the result of a mathematical operation. Lazy
                          >>>evaluation would be pointless and meaningless.
                          >>>
                          >>Why? If theres a zero on the LHS of a bitwise AND you know the result
                          >>will be zero no matter what is on the RHS so why bother evaluating the
                          >>RHS?
                          >>
                          >>That's the kind of thing a good optimising compiler will already do.
                          >
                          Even though the RHS might have side effects?
                          Oh dear, this isn't turning out to be my thread, is it? No, obviously the
                          compiler can't do that if there are side effects to process.

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • Richard Heathfield

                            #28
                            Re: Lazy evaluation question

                            Tor Rustad said:
                            Richard Heathfield wrote:
                            >Boltar said:
                            >>
                            >>On 5 Jan, 16:38, Richard Heathfield <r...@see.sig.i nvalidwrote:
                            >>>But bitwise operators are not used to establish truth or falsity. They
                            >>>are used to calculate the result of a mathematical operation. Lazy
                            >>>evaluation would be pointless and meaningless.
                            >>Why? If theres a zero on the LHS of a bitwise AND you know the result
                            >>will be zero no matter what is on the RHS so why bother evaluating the
                            >>RHS?
                            >>
                            >That's the kind of thing a good optimising compiler will already do.
                            >
                            Really?
                            >
                            Methinks, & operator always evaluate both operands, just like the |
                            operator.
                            I was thinking of cases such as 0 & 1, 0 & vanilla_int_obj ect, and so on.

                            --
                            Richard Heathfield <http://www.cpax.org.uk >
                            Email: -http://www. +rjh@
                            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                            "Usenet is a strange place" - dmr 29 July 1999

                            Comment

                            • Tor Rustad

                              #29
                              Re: Lazy evaluation question

                              Richard Heathfield wrote:
                              Tor Rustad said:
                              >
                              >Richard Heathfield wrote:
                              >>Boltar said:
                              >>>
                              >>>On 5 Jan, 16:38, Richard Heathfield <r...@see.sig.i nvalidwrote:
                              >>>>But bitwise operators are not used to establish truth or falsity. They
                              >>>>are used to calculate the result of a mathematical operation. Lazy
                              >>>>evaluatio n would be pointless and meaningless.
                              >>>Why? If theres a zero on the LHS of a bitwise AND you know the result
                              >>>will be zero no matter what is on the RHS so why bother evaluating the
                              >>>RHS?
                              >>That's the kind of thing a good optimising compiler will already do.
                              >Really?
                              >>
                              >Methinks, & operator always evaluate both operands, just like the |
                              >operator.
                              >
                              I was thinking of cases such as 0 & 1, 0 & vanilla_int_obj ect, and so on.
                              The context was:

                              0 && ++a;
                              0 & ++b;

                              Unlike && operator, the & operator has is no sequence point after
                              evaluating an operand, and there is no guarantees for a left-to-right
                              evaluation of the operands.

                              The role of result of the & operator, is a bit setting role, I see no
                              reason for the optimiser to affect this evaluation, unless the result is
                              a constant with no side-effects, that can be pre-computed at compile-time.

                              --
                              Tor <bwzcab@wvtqvm. vw | tr i-za-h a-z>

                              Comment

                              • Richard Heathfield

                                #30
                                Re: Lazy evaluation question

                                Tor Rustad said:
                                Richard Heathfield wrote:
                                >Tor Rustad said:
                                >>
                                <snip>
                                >>Methinks, & operator always evaluate both operands, just like the |
                                >>operator.
                                >>
                                >I was thinking of cases such as 0 & 1, 0 & vanilla_int_obj ect, and so
                                >on.
                                >
                                The context was:
                                >
                                0 && ++a;
                                0 & ++b;
                                In that context, of course, RHS evaluation is indeed required. I was guilty
                                of not reading the thread properly.

                                --
                                Richard Heathfield <http://www.cpax.org.uk >
                                Email: -http://www. +rjh@
                                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                                "Usenet is a strange place" - dmr 29 July 1999

                                Comment

                                Working...