On Fri, Dec 11, 2015 at 8:10 AM, David Winsemius <dwinsemius at comcast.net> wrote:> >> On Dec 11, 2015, at 5:38 AM, Dario Beraldi <dario.beraldi at gmail.com> wrote: >> >> Hi All, >> >> I'd like to understand the reason why stopifnot(logical(0) == x) doesn't >> (never?) throw an exception, at least in these cases: > > The usual way to test for a length-0 logical object is to use length(): > > x <- logical(0) > > stopifnot( !length(x) & mode(x)=="logical" )I found stopifnot(!length(x), mode(x) == "logical") more helpful when troubleshooting, because it will tell you whether it's !length(x) or mode(x) == "logical" that is FALSE. It's as if you wrote: stopifnot(!length(x)) stopifnot(mode(x) == "logical") /Henrik> > >> >> stopifnot(logical(0) == 1) >> stopifnot(logical(0) == TRUE) >> stopifnot(logical(0) == FALSE) >> >> My understanding is that logical(0) is an empty set, so I would expect the >> above tests to fail. >> >> (I got bitten by this in a piece of code where "x" happened to be >> logical(0) and stopifnot didn't catch it) >> >> Thanks! >> Dario > -- > > David Winsemius > Alameda, CA, USA > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
>>>>> Henrik Bengtsson <henrik.bengtsson at gmail.com> >>>>> on Fri, 11 Dec 2015 08:20:55 -0800 writes:> On Fri, Dec 11, 2015 at 8:10 AM, David Winsemius <dwinsemius at comcast.net> wrote: >> >>> On Dec 11, 2015, at 5:38 AM, Dario Beraldi <dario.beraldi at gmail.com> wrote: >>> >>> Hi All, >>> >>> I'd like to understand the reason why stopifnot(logical(0) == x) doesn't >>> (never?) throw an exception, at least in these cases: >> >> The usual way to test for a length-0 logical object is to use length(): >> >> x <- logical(0) >> >> stopifnot( !length(x) & mode(x)=="logical" ) > I found > stopifnot(!length(x), mode(x) == "logical") > more helpful when troubleshooting, because it will tell you whether > it's !length(x) or mode(x) == "logical" that is FALSE. It's as if you > wrote: > stopifnot(!length(x)) > stopifnot(mode(x) == "logical") > /Henrik Yes, indeed, thank you Henrik --- and Jeff Newmiller who's nice humorous reply added other relevant points. As author stopifnot(), I do agree with Dario's "gut feeling" that stopifnot() "somehow ought to do the right thing" in cases such as stopifnot(dim(x) == c(3,4)) which is really subtle version of his cases {But the gut feeling is wrong, as I argue from now on}. Someone writing the above would want stopifnot() to stop in the case where x is a simple vector instead of a matrix/data.frame/... with dimensions c(3,4) ... but it will not because, as Bill or Jeff explained, "the empty set is always true", and so yes indeed, you have to care about length-0 expressions in stopifnot(). Indeed, in the past, I had thought of "improving" stopifnot() by giving a warning or even stop() for logical(0) expressions, but I quickly dismissed that idea after some experiments. My conclusion: Breaking such a fundamental lemma of logic as "the empty set is always true" will lead to all kinds of illogical situations ... so don't do that! Martin Maechler, ETH Zurich
> On 12 Dec 2015, at 10:54 , Martin Maechler <maechler at stat.math.ethz.ch> wrote: > > My conclusion: Breaking such a fundamental lemma of logic as > "the empty set is always true"Umm, that doesn't make sense to me. Surely you mean that "an AND-operation over an empty index set is TRUE"? A similar OR operation is FALSE, i.e. they behave like empty products and sums, respectively. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
On Sat, Dec 12, 2015 at 3:54 AM, Martin Maechler <maechler at stat.math.ethz.ch> wrote:>>>>>> Henrik Bengtsson <henrik.bengtsson at gmail.com> >>>>>> on Fri, 11 Dec 2015 08:20:55 -0800 writes: > > > On Fri, Dec 11, 2015 at 8:10 AM, David Winsemius <dwinsemius at comcast.net> wrote: > >> > >>> On Dec 11, 2015, at 5:38 AM, Dario Beraldi <dario.beraldi at gmail.com> wrote: > >>> > >>> Hi All, > >>> > >>> I'd like to understand the reason why stopifnot(logical(0) == x) doesn't > >>> (never?) throw an exception, at least in these cases: > >> > >> The usual way to test for a length-0 logical object is to use length(): > >> > >> x <- logical(0) > >> > >> stopifnot( !length(x) & mode(x)=="logical" ) > > > I found > > > stopifnot(!length(x), mode(x) == "logical") > > > more helpful when troubleshooting, because it will tell you whether > > it's !length(x) or mode(x) == "logical" that is FALSE. It's as if you > > wrote: > > > stopifnot(!length(x)) > > stopifnot(mode(x) == "logical") > > > /Henrik > > Yes, indeed, thank you Henrik --- and Jeff Newmiller who's nice > humorous reply added other relevant points. > > As author stopifnot(), I do agree with Dario's "gut feeling" > that stopifnot() "somehow ought to do the right thing" > in cases such as > > stopifnot(dim(x) == c(3,4)) > > which is really subtle version of his cases > {But the gut feeling is wrong, as I argue from now on}.Personally, I think the problem there is that people forget that == is vectorised, and for a non-vectorised equality check you really should use identical: stopifnot(identical(dim(x), c(3,4))) Hadley -- http://had.co.nz/
> as Bill or Jeff explained, "the empty set is always true"My wording was that any(logical(0)) is FALSE because "there are no TRUEs in logical(0)". Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Dec 12, 2015 at 1:54 AM, Martin Maechler <maechler at stat.math.ethz.ch> wrote:>>>>>> Henrik Bengtsson <henrik.bengtsson at gmail.com> >>>>>> on Fri, 11 Dec 2015 08:20:55 -0800 writes: > > > On Fri, Dec 11, 2015 at 8:10 AM, David Winsemius <dwinsemius at comcast.net> wrote: > >> > >>> On Dec 11, 2015, at 5:38 AM, Dario Beraldi <dario.beraldi at gmail.com> wrote: > >>> > >>> Hi All, > >>> > >>> I'd like to understand the reason why stopifnot(logical(0) == x) doesn't > >>> (never?) throw an exception, at least in these cases: > >> > >> The usual way to test for a length-0 logical object is to use length(): > >> > >> x <- logical(0) > >> > >> stopifnot( !length(x) & mode(x)=="logical" ) > > > I found > > > stopifnot(!length(x), mode(x) == "logical") > > > more helpful when troubleshooting, because it will tell you whether > > it's !length(x) or mode(x) == "logical" that is FALSE. It's as if you > > wrote: > > > stopifnot(!length(x)) > > stopifnot(mode(x) == "logical") > > > /Henrik > > Yes, indeed, thank you Henrik --- and Jeff Newmiller who's nice > humorous reply added other relevant points. > > As author stopifnot(), I do agree with Dario's "gut feeling" > that stopifnot() "somehow ought to do the right thing" > in cases such as > > stopifnot(dim(x) == c(3,4)) > > which is really subtle version of his cases > {But the gut feeling is wrong, as I argue from now on}. > > Someone writing the above would want stopifnot() to stop in the > case where x is a simple vector instead of a > matrix/data.frame/... with dimensions c(3,4) ... but it will not > because, as Bill or Jeff explained, "the empty set is always > true", and so yes indeed, you have to care about length-0 > expressions in stopifnot(). > > Indeed, in the past, I had thought of "improving" stopifnot() > by giving a warning or even stop() for logical(0) expressions, > but I quickly dismissed that idea after some experiments. > > My conclusion: Breaking such a fundamental lemma of logic as > "the empty set is always true" > will lead to all kinds of illogical situations ... so don't do that! > > Martin Maechler, > ETH Zurich > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.