From maechler at stat.math.ethz.ch  Mon Sep  4 11:57:15 2006
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Mon, 4 Sep 2006 11:57:15 +0200
Subject: [Rd] setMethod("Logic", ...)
In-Reply-To: <7F7440CD-18B2-4AB6-B0BA-6DB0B0491727@soc.soton.ac.uk>
References: <91AD8AFC-E5A3-4F79-821D-CACC2705333D@soc.soton.ac.uk>
	<Pine.LNX.4.64.0609011642240.16133@gannet.stats.ox.ac.uk>
	<7F7440CD-18B2-4AB6-B0BA-6DB0B0491727@soc.soton.ac.uk>
Message-ID: <17659.63611.323669.893701@stat.math.ethz.ch>

>>>>> "Robin" == Robin Hankin <r.hankin at noc.soton.ac.uk>
>>>>>     on Mon, 4 Sep 2006 09:41:46 +0100 writes:

    Robin> Professor Ripley
    Robin> thank you for this (and indeed thank you for solving my earlier
    Robin> problem on octonions).

    Robin> I still don't know how  to force logic operations on brob objects to  
    Robin> give an error.

You "just" {as we will see below, your question is more interesting
	    than I first thought !}
define methods for all three of

           '"&"', '"|"', '"!"'

e.g.

  > setClass("brob", contains="numeric")
  [1] "brob"
  > setMethod("!", "brob", function(e1) {cat("not a brob\n"); invisible(e1)})
  [1] "!"
  > !new("brob")
  not a brob
  > 



To keep things consistent, I would use helper functions {that
I'd keep hidden in the namespace}.

## Something like

logic.brob.error <- function(name) {
    stop("logic operator '", name, "' not applicable to brobs")
}

logic2 <- function(e1,e2) {
       logic.brob.error(.Generic)
}

setMethod("!", "brob",
          function(e1) { logic.brob.error("!") })

setMethod("&", signature("brob", "ANY"),  logic2) # -- okay --
setMethod("&", signature("ANY", "brob"),  logic2) # ERR  ----> bug in R (?)
## Error in ...
## the method for function '&' and signature e1="ANY", e2="brob" is sealed and cannot be re-defined

setMethod("|", signature("brob", "ANY"),  logic2) # -- okay --
setMethod("|", signature("ANY", "brob"),  logic2) # ERR  ----> bug in R (?!)

new("brob", 3) & new("brob", 0) #-> proper error
new("brob") & 1                 #-> proper error

TRUE & new("brob", pi) # -> TRUE  -- oops!

##--------------------------------------------------------------

I think this reveals a bug in R (still present in R-devel as of
this morning):

signature("ANY", "brob")  should not be sealed for method
definition but it is.


Martin Maechler




[Rd] setMethod("Logic", ...)
Martin Maechler