any() and all() on empty list?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Adam

    #31
    Re: any() and all() on empty list?

    Ron Adam wrote:[color=blue]
    >
    > hasall(S, True, lambda n: n=42)
    >[/color]

    That was suppose to be:

    hasall(S, True, lambda n: n==42)

    Comment

    • Marcin Ciura

      #32
      Re: any() and all() on empty list?

      Georg Brandl wrote:[color=blue]
      > Steven D'Aprano wrote:[color=green]
      >>all(flying elephants which are pink) => true
      >>all(flying elephants which are not pink) => true
      >>
      >>So, these flying elephants -- are they pink or not?[/color]
      >
      > No, you ask two different sets whether they are true.[/color]

      No, there is only one empty set.
      Relevant to this discussion is stuff
      about syllogisms concerning non-existent objects
      and what C.S. Peirce did to them in the 19th century.
      See e.g. http://www.math.fau.edu/schonbek/mfl....html#tth_sEc5
      Marcin

      Comment

      • Fredrik Lundh

        #33
        Re: any() and all() on empty list?

        Marcin Ciura wrote:
        [color=blue][color=green][color=darkred]
        >>>all(flying elephants which are not pink) => true
        >>>
        >>>So, these flying elephants -- are they pink or not?[/color]
        >>
        >> No, you ask two different sets whether they are true.[/color]
        >
        > No, there is only one empty set.[/color]

        who said anything about empty sets ?

        </F>



        Comment

        • Antoon Pardon

          #34
          Re: any() and all() on empty list?

          Op 2006-03-30, Steven D'Aprano schreef <steve@REMOVEME cyber.com.au>:[color=blue]
          > Paul Rubin wrote:
          >[color=green]
          >> Steven D'Aprano <steve@REMOVEME cyber.com.au> writes:
          >>[color=darkred]
          >>>Think of it this way: if all(seq) is true, shouldn't it be the case
          >>>that you can point to a specific element in seq that is true?[/color]
          >>
          >>
          >> No, all(seq) is true if you can't point to a specific element in seq
          >> that's false.[/color]
          >
          > No, all(seq) is true if every element in seq is true.
          > Surely that's a more intuitive definition than your
          > definition by what you can't do.
          >
          > The question that needs to be answered is, what if
          > there are no elements at all? That's an arbitrary
          > decision. Like the question "what is 0**0?" in
          > mathematics, some answers are more useful than others.
          > I can respect that practical answer -- but it isn't the
          > *only* answer.
          >
          > (For those who don't see why 0**0 is problematic, 0**x
          > is equal to 0 for all x, and x**0 is equal to 1 for all
          > x, so what do you do for 0**0?)
          >
          > Here's another way of looking at the problem:
          >
          > all(flying elephants which are pink) => true
          > all(flying elephants which are not pink) => true
          >
          > So, these flying elephants -- are they pink or not?[/color]

          They are both.

          --
          Antoon Pardon

          Comment

          • Paul McGuire

            #35
            Re: any() and all() on empty list?


            "Antoon Pardon" <apardon@forel. vub.ac.be> wrote in message
            news:slrne2nogp .e68.apardon@rc pc42.vub.ac.be. ..[color=blue]
            > Op 2006-03-30, Steven D'Aprano schreef <steve@REMOVEME cyber.com.au>:
            >[color=green]
            > > So, these flying elephants -- are they pink or not?[/color]
            >
            > They are both.
            >[/color]

            That would make them Schrödinger elephants!

            -- Paul


            Comment

            • Paul Rubin

              #36
              Re: any() and all() on empty list?

              Steven D'Aprano <steve@REMOVEME cyber.com.au> writes:[color=blue][color=green]
              > > No, all(seq) is true if you can't point to a specific element in seq
              > > that's false.[/color]
              >
              > No, all(seq) is true if every element in seq is true. Surely that's a
              > more intuitive definition than your definition by what you can't do.[/color]

              They are different? I'd say every element in seq is true, unless
              there's an element that's false. Do you want to pick a different word
              than "all" and suggest renaming the function?
              [color=blue]
              > Here's another way of looking at the problem:
              >
              > all(flying elephants which are pink) => true
              > all(flying elephants which are not pink) => true
              >
              > So, these flying elephants -- are they pink or not?[/color]

              By the definition, "all flying elephants are pink" and "all flying
              elephants are non-pink" are both true statements, if that's what
              you're asking. There is no contradiction. It's one of those
              questions like "have you stopped beating your wife".

              I'd say:

              def boolify(s): return map(bool, s)

              then:

              all(S) = reduce(operator .and_, boolify(S), True)
              any(S) = reduce(operator .or_, boolify(S), False)

              You can see that changing True to False in the above definition of all
              would make the result always false.

              FWIW, I threw all my TV sets off the roof of my building this morning.
              But nobody on the sidewalk needed to worry about getting hit by one ;-).

              Comment

              • Steven D'Aprano

                #37
                Re: any() and all() on empty list?

                On Thu, 30 Mar 2006 11:28:54 -0800, Paul Rubin wrote:
                [color=blue]
                > Steven D'Aprano <steve@REMOVEME cyber.com.au> writes:[color=green][color=darkred]
                >> > No, all(seq) is true if you can't point to a specific element in seq
                >> > that's false.[/color]
                >>
                >> No, all(seq) is true if every element in seq is true. Surely that's a
                >> more intuitive definition than your definition by what you can't do.[/color]
                >
                > They are different?[/color]

                Of course they are different -- they differ in the case of an empty
                sequence.
                [color=blue]
                > I'd say every element in seq is true, unless
                > there's an element that's false. Do you want to pick a different word
                > than "all" and suggest renaming the function?[/color]

                I've already pointed out that I'm satisfied with Tim Peters' explanation
                for why the defined behaviour for any() is *most often* the Right Way for
                it to be implemented in Python, *but* there are circumstances that this
                behaviour is incorrect, therefore the programmer needs to actually
                consider carefully what should happen for the empty sequence case. Was I
                not clear enough?
                [color=blue][color=green]
                >> Here's another way of looking at the problem:
                >>
                >> all(flying elephants which are pink) => true
                >> all(flying elephants which are not pink) => true
                >>
                >> So, these flying elephants -- are they pink or not?[/color]
                >
                > By the definition, "all flying elephants are pink" and "all flying
                > elephants are non-pink" are both true statements, if that's what
                > you're asking. There is no contradiction.[/color]

                Of course there is a contradiction. The contradiction is that flying
                elephants are simultaneously pink and not pink.

                If you don't understand why "Foo is Bar" and "Foo is not Bar" can't both
                be true simultaneously, I suggest you spend some time googling on
                "noncontradicti on logic". To get you started, here's the Wikipedia entry:



                [color=blue]
                > It's one of those
                > questions like "have you stopped beating your wife".[/color]

                Think about it: what is the logical value of the boolean "I have stopped
                beating my wife" in the case of somebody who never started beating
                their wife?

                if husband.stopped _beating_wife() : # returns True or False
                pay_fine()
                else:
                go_to_jail()

                Pretty hard on the innocent husbands who never even beat their wife at all.

                What we're doing is running up to the limitations of Aristotelian
                two-value logic. We're trying to pigeon-hole answers into True/False that
                really don't fit, so of course there will be the occasional case where
                neither True nor False is correct. In hacker culture, the Chinese word
                "mu" (literally "without") is sometimes used to mean "I cannot answer that
                question because your assumptions are not correct".

                In the case of all(seq), the correct answer is "mu". But since we're
                stuck with binary logic, the more commonly useful behaviour is True, but
                sometimes that leads to problems, such as in my first example of Guido
                being punished because he was found guilty of all the terrorist crimes he
                committed -- which is an empty list.


                --
                Steven.

                Comment

                • Paul Rubin

                  #38
                  Re: any() and all() on empty list?

                  Steven D'Aprano <steve@REMOVETH IScyber.com.au> writes:[color=blue][color=green][color=darkred]
                  > >> No, all(seq) is true if every element in seq is true. Surely that's a
                  > >> more intuitive definition than your definition by what you can't do.[/color]
                  > > They are different?[/color]
                  > Of course they are different -- they differ in the case of an empty
                  > sequence.[/color]

                  I don't think they differ in the case of an empty sequence. If the
                  sequence is empty, both statements are true.
                  [color=blue][color=green]
                  > > By the definition, "all flying elephants are pink" and "all flying
                  > > elephants are non-pink" are both true statements, if that's what
                  > > you're asking. There is no contradiction.[/color]
                  >
                  > Of course there is a contradiction. The contradiction is that flying
                  > elephants are simultaneously pink and not pink.[/color]

                  Neither statement asserts the existence of any flying elephants
                  regardless of color, so neither statement contradicts the other
                  statement.
                  [color=blue]
                  > If you don't understand why "Foo is Bar" and "Foo is not Bar" can't both
                  > be true simultaneously, I suggest you spend some time googling on
                  > "noncontradicti on logic". To get you started, here's the Wikipedia entry:
                  >
                  > http://en.wikipedia.org/wiki/Law_of_noncontradiction[/color]

                  "All flying elephants are pink" is not a statement of the form "Foo is
                  Bar". See <http://en.wikipedia.or g/wiki/For_all>, as I've cited
                  several times. "All flying elephants are pink" simply means "there
                  are no non-pink flying elephants". "All flying elephants are
                  non-pink" similarly means "there are no pink flying elephants". The
                  statements don't contradict, and in fact both statements are true.
                  [color=blue]
                  > if husband.stopped _beating_wife() : # returns True or False
                  > pay_fine()
                  > else:
                  > go_to_jail()
                  >
                  > Pretty hard on the innocent husbands who never even beat their wife at all.[/color]

                  Correct. The code should not be written that way.
                  [color=blue]
                  > In hacker culture, the Chinese word
                  > "mu" (literally "without") is sometimes used to mean "I cannot answer that
                  > question because your assumptions are not correct".
                  >
                  > In the case of all(seq), the correct answer is "mu".[/color]

                  I don't think it's that bad. We just have to spell out precisely what
                  the assumptions are, and we've done so.

                  Comment

                  • Carl Banks

                    #39
                    Re: any() and all() on empty list?

                    Steven D'Aprano wrote:[color=blue]
                    > On Thu, 30 Mar 2006 11:28:54 -0800, Paul Rubin wrote:
                    >[color=green]
                    > > Steven D'Aprano <steve@REMOVEME cyber.com.au> writes:[color=darkred]
                    > >> > No, all(seq) is true if you can't point to a specific element in seq
                    > >> > that's false.
                    > >>
                    > >> No, all(seq) is true if every element in seq is true. Surely that's a
                    > >> more intuitive definition than your definition by what you can't do.[/color]
                    > >
                    > > They are different?[/color]
                    >
                    > Of course they are different -- they differ in the case of an empty
                    > sequence.[/color]

                    No, they're not.

                    Look, there are significant differences between natural and computer
                    languages, and in this case something is happening in the natural
                    language that isn't happening in this computer language.

                    In English, if I were to ask you a question like "Have you put all your
                    books in the truck?" when you have no books, a valid and reasonable
                    answer is "I don't have any books." I.e., the answer is neither yes
                    nor no. In fact, yes and no aren't valid answers at all (unless you're
                    being snarky**), because, in English, the word "all" carries an
                    assumption of existence. (Or maybe it doesn't for you guys in
                    Australia; it does in the USA.)

                    In Python, yes and no are the only possible answers. Probably the only
                    analogous thing you could do in Python would be for all() to raise
                    ValueError when passed an empty sequence.


                    Carl Banks

                    ** - and note that, if you are being snarky, you would say "yes".

                    Comment

                    • Alex Martelli

                      #40
                      Re: any() and all() on empty list?

                      Fredrik Lundh <fredrik@python ware.com> wrote:
                      [color=blue]
                      > Marcin Ciura wrote:
                      >[color=green][color=darkred]
                      > >>>all(flying elephants which are not pink) => true
                      > >>>
                      > >>>So, these flying elephants -- are they pink or not?
                      > >>
                      > >> No, you ask two different sets whether they are true.[/color]
                      > >
                      > > No, there is only one empty set.[/color]
                      >
                      > who said anything about empty sets ?[/color]

                      Universally-false predicate <--> empty set

                      ....in Cantor's/Frege's world, which is commonly accepted as equivalent
                      to Aristotle's Logic. Modal logic is a different kettle of fish (and,
                      in retrospect, what Dodgson [aka Carroll] was groping towards)... but I
                      know of no programming paradigm based on it (even Turing's and Church's
                      map more easily to naive set theory//logic, give or take a Zermelo or
                      so;-). I would in fact challenge the assertion that a useful programming
                      paradigm COULD be based on modal logic, hoping for proof of the
                      contrary;-)


                      Alex

                      Comment

                      • Ron Adam

                        #41
                        Re: any() and all() on empty list?

                        Carl Banks wrote:
                        [color=blue]
                        > In Python, yes and no are the only possible answers. Probably the only
                        > analogous thing you could do in Python would be for all() to raise
                        > ValueError when passed an empty sequence.[/color]

                        There is also 'None' which serves a similar purpose of indicating an
                        invalid value when passing arguments.

                        Cheers,
                        Ron

                        Comment

                        • Fredrik Lundh

                          #42
                          Re: any() and all() on empty list?

                          Alex Martelli wrote:
                          [color=blue][color=green][color=darkred]
                          > > >>>all(flying elephants which are not pink) => true
                          > > >>>
                          > > >>>So, these flying elephants -- are they pink or not?
                          > > >>
                          > > >> No, you ask two different sets whether they are true.
                          > > >
                          > > > No, there is only one empty set.[/color]
                          > >
                          > > who said anything about empty sets ?[/color]
                          >
                          > Universally-false predicate <--> empty set
                          >
                          > ...in Cantor's/Frege's world[/color]

                          I was more thinking of Disney.

                          </F>



                          Comment

                          • Antoon Pardon

                            #43
                            Re: any() and all() on empty list?

                            Op 2006-03-30, Paul McGuire schreef <ptmcg@austin.r r._bogus_.com>:[color=blue]
                            >
                            > "Antoon Pardon" <apardon@forel. vub.ac.be> wrote in message
                            > news:slrne2nogp .e68.apardon@rc pc42.vub.ac.be. ..[color=green]
                            >> Op 2006-03-30, Steven D'Aprano schreef <steve@REMOVEME cyber.com.au>:
                            >>[color=darkred]
                            >> > So, these flying elephants -- are they pink or not?[/color]
                            >>
                            >> They are both.
                            >>[/color]
                            >
                            > That would make them Schrödinger elephants![/color]

                            Every member of the empty set is a Schrödinger element.

                            --
                            Antoon Pardon

                            Comment

                            • Ant

                              #44
                              Re: any() and all() on empty list?

                              I don't think that there will be any valid examples.

                              all(list) simply means "every element of the list evaluates to True".
                              This is trivially true in the case of the empty list. This is logically
                              equivalent to "There are no elements in the list which evaluate to
                              False".

                              any(list) simply means "at least one element of the list evaluates to
                              true". This is trivially false for the empty list - there are no
                              elements to be true.

                              These are logical functions and should be mathematically sound. It's
                              possible to add all sorts of problems if we just arbitrarily decide
                              what "for all x" should mean. We may just as well decide that for
                              convenience: math.pi == 3.

                              --
                              Ant...

                              Comment

                              • Gerard Flanagan

                                #45
                                Re: any() and all() on empty list?

                                Steve R. Hastings wrote:
                                [color=blue]
                                > Therefore, I propose that all() should work as if it were written this way:[/color]
                                [color=blue]
                                > def all(S):
                                > ret_val = False
                                >
                                > for x in S:
                                > ret_val = True
                                > if not x:
                                > return False
                                >
                                > return ret_val
                                >
                                > Comments?[/color]

                                Ant wrote:
                                [color=blue]
                                > all(list) simply means "every element of the list evaluates to True".
                                > This is trivially true in the case of the empty list. This is logically
                                > equivalent to "There are no elements in the list which evaluate to
                                > False".
                                >
                                > any(list) simply means "at least one element of the list evaluates to
                                > true". This is trivially false for the empty list - there are no
                                > elements to be true.
                                >
                                > These are logical functions and should be mathematically sound. It's
                                > possible to add all sorts of problems if we just arbitrarily decide
                                > what "for all x" should mean. We may just as well decide that for
                                > convenience: math.pi == 3.
                                >[/color]

                                I agree.

                                Some amateur maths - applying the identities of a 'two-element Boolean
                                algebra' found here:



                                def any(S):
                                for x in S:
                                if x:
                                return True
                                return False

                                def all(S):
                                for x in S:
                                if not x:
                                return False
                                return True

                                #the identities don't hold if you use the alternative
                                ##def all(S):
                                ## ret_val = False
                                ##
                                ## for x in S:
                                ## ret_val = True
                                ## if not x:
                                ## return False
                                ##
                                ## return ret_val

                                empty = []
                                universe = [ 0, 1 ]

                                one = all(empty)
                                zero = any(empty)

                                assert (one or one) == one
                                assert (one or zero) == one
                                assert (zero or one) == one
                                assert (zero or zero) == zero
                                assert (zero and zero) == zero
                                assert (zero and one) == zero
                                assert (one and zero) == zero
                                assert (one and one) == one
                                assert (not one) == zero
                                assert (not zero) == one

                                #on the other hand
                                one = all(universe)
                                zero = any(universe)

                                #de Morgan - swap 'and' and 'or' and complement the result
                                assert not(one and one) != one
                                assert not(one and zero) != one
                                assert not(zero and one) != one
                                assert not(zero and zero) != zero
                                assert not(zero or zero) != zero
                                assert not(zero or one) != zero
                                assert not(one or zero) != zero
                                assert not(one or one) != one
                                assert not(not one) != zero
                                assert not(not zero) != one

                                ----------------------------------------------------------------------

                                Gerard

                                Comment

                                Working...