From maechler at stat.math.ethz.ch  Sat Apr 28 15:35:29 2007
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Sat, 28 Apr 2007 15:35:29 +0200
Subject: [Rd] Signature of '!' (was Problem with R-2.5.0 patched and
	Matrix package)
In-Reply-To: <Pine.LNX.4.64.0704262211250.27404@gannet.stats.ox.ac.uk>
References: <m2bqhargo4.fsf@ziti.fhcrc.org>
	<Pine.LNX.4.64.0704262211250.27404@gannet.stats.ox.ac.uk>
Message-ID: <17971.19873.309790.77341@stat.math.ethz.ch>

>>>>> "BDR" == Prof Brian Ripley <ripley at stats.ox.ac.uk>
>>>>>     on Thu, 26 Apr 2007 22:57:11 +0100 (BST) writes:

    BDR> Yes, it works in the release.  The essence of the
    BDR> problem is that Matrix defines an S4 method for '!'
    BDR> with dispatch on 'e1', but the documentation for '!'
    BDR> (and many S3 methods) says the argument is 'x'.  As '!'
    BDR> is a primitive the argument matching of the base
    BDR> function might be expected to be positional only, but
    BDR> that is not what happens with methods.

    BDR> This needs some juggling, and clearly two patches in
    BDR> R-patched have conflicted.  I've reverted one of them
    BDR> and this seems to work again (but something else no
    BDR> longer works: see below).

    BDR> Longer term, I don't know what the right way forward
    BDR> is.  Consider

    >> setClass("foo", "logical")
    BDR> [1] "foo"
    >> setMethod("!", "foo", function(e1) NA)
    BDR> [1] "!"
    >> x <- new("foo", TRUE) !x
    BDR> [1] NA
    >> `!`(x)
    BDR> [1] NA
    >> `!`(x=x)
    BDR> Error in !x : unused argument(s) (x = TRUE)
    >> `!`(e1=x)
    BDR> [1] NA

    BDR> which is not what the reader of the help for '!' might
    BDR> expect.  But it gets worse:

    >> x2 <- structure(TRUE, class="bar") `!.bar` <- function(x)
    >> NA
    BDR> (and there are several S3 methods like that in CRAN
    BDR> packages)
    >> !x2
    BDR> [1] NA
    >> `!`(x2)
    BDR> [1] NA
    >> `!`(x=x2)
    BDR> Error in !x2 : unused argument(s) (x = TRUE)
    >> `!`(e1=x2)
    BDR> Error in `!.bar`(e1 = x2) : unused argument(s) (e1 =
    BDR> TRUE)

    BDR> whereas `!`(x=x2) does work if you do not set an S4
    BDR> method on '!'.  This may seem artificial, but this sort
    BDR> of thing can happen if you use operators as functions
    BDR> in lapply().

    BDR> Since there are in packages S3 methods on 'x' and S4
    BDR> methods on 'e1', we have a conflict.

    BDR> My gut feeling is that the inconsistency is the S4
    BDR> signature, and certainly that seems the easier one to
    BDR> change.  I knew about this because the
    BDR> behind-the-scenes S4 generics in R-devel are
    BDR> auto-generated from the descriptions of the primitives.
    BDR> Now in 2.5.0 you will see

    >> `!`
    BDR> function (x) .Primitive("!")

    BDR> and hence there needed to be an exclusion list (which
    BDR> now includes '!' and 'c') to have Matrix work in
    BDR> R-devel.  An alternative is to make the internal method
    BDR> dispatch on operators ignore the argument names.

    BDR> (I can guess where this comes from.  '!' is a member of
    BDR> the S3 Ops group, and as part of a group method you
    BDR> define Ops(e1, e2) and ignore e2.  But '!' is not part
    BDR> of the S4 group generic Logic in R.)

 Yes, indeed (and that was made so very consciously after
 discussion between John and me (and maybe more people)) in
 order to at least keep the signature precise for the (S4)Logic group.

I agree with you (Brian) that the inconsistency between 
'(x)' and '(e1)' is ugly and I agree that we 
make the signature into  '(x)' and we (I) have to change Matrix
subsequently.
If possible (easily enough), we should try to keep 'e1' working
in  setMethod("!", .)  with a deprecation warning

Martin


    BDR> On Thu, 26 Apr 2007, Seth Falcon wrote:

    >> Hi,
    >> 
    >> Using latest R 2.5.0 Patched, I'm unable to install the
    >> Matrix package from cran.fhcrc.org.
    >> 
    >> I get:
    >> 
    >> Creating a new generic function for "isSymmetric" in
    >> "Matrix" Creating a new generic function for "unname" in
    >> "Matrix" Error in conformMethod(signature, mnames,
    >> fnames, f) : In method for function "!": formal arguments
    >> omitted in the method definition cannot be in the
    >> signature (x = "Matrix") Error: unable to load R code in
    >> package 'Matrix' Execution halted ERROR: lazy loading
    >> failed for package 'Matrix' ** Removing
    >> '/home/sfalcon/RLIBS/2.5/Matrix' ** Restoring previous
    >> '/home/sfalcon/RLIBS/2.5/Matrix'
    >> 
    >> The downloaded packages are in
    >> /tmp/RtmpcEQJjw/downloaded_packages Warning message:
    >> installation of package 'Matrix' had non-zero exit status
    >> in: install.packages(pkgs = pkgs, repos = repos,
    >> dependencies = dependencies,
    >> 
    >> I don't have this problem when using a recent R-devel.
    >> And I'm guessing that Matrix works with the release, but
    >> haven't had time to check this.
    >> 
    >> + seth
    >> 
    >> 

    BDR> -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of
    BDR> Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
    BDR> University of Oxford, Tel: +44 1865 272861 (self) 1
    BDR> South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG,
    BDR> UK Fax: +44 1865 272595

    BDR> ______________________________________________
    BDR> R-devel at r-project.org mailing list
    BDR> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Signature of '!' (was Problem with R-2.5.0 patched and Matrix package)
Martin Maechler