From maechler at stat.math.ethz.ch  Tue Apr 21 16:13:42 2009
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Tue, 21 Apr 2009 16:13:42 +0200
Subject: [Rd] print.closure at the R level
In-Reply-To: <18924.59339.873021.630427@cmath-5.math.ethz.ch>
References: <49E9DFE5.7060501@dbmail.com> <49E9E25D.9070106@stats.uwo.ca>
	<49EA39F1.2010709@dbmail.com> <49EAF99E.9070703@dbmail.com>
	<49ECDE2E.5060105@dbmail.com>
	<18924.59339.873021.630427@cmath-5.math.ethz.ch>
Message-ID: <18925.54422.329318.557524@lynne.math.ethz.ch>

>>>>> "MM" == Martin Maechler <maechler at stat.math.ethz.ch>
>>>>>     on Mon, 20 Apr 2009 23:23:23 +0200 writes:

>>>>> "RF" == Romain Francois <romain.francois at dbmail.com>
>>>>>     on Mon, 20 Apr 2009 22:42:22 +0200 writes:

    RF> Hello,

    RF> Sorry if I have waisted any time of people truing this
    RF> patch.

    MM> yes, you did waste (sic) ....  but thank you for the
    MM> code suggestions anyway.

    RF> There was an issue with debugging (use of debug and
    RF> browser that caused an infinite recursion). I think this
    RF> is now fixed.

    MM> [ actually I had found even simpler bugs in it, e.g.
    MM> you accidentally called C-level "print.function()" on
    MM> "language", some may have been fixed by your new patch
    MM> too, but others are not {e.g., print() *must* return its
    MM> argument !}  ]

    MM> However, I've already changed your old patch too much
    MM> (notably by using *our* C coding standards) to want to
    MM> look at your new patch in detail.

    MM> If you want we can communicate off-list about this,
    MM> tomorrow...
{and we did}

It turns out to be considerably more tricky than I had
anticipated...

Note that we also really want auto-printing and print()ing to be
equivalent,
and that is not even the case for .Primitives in current R :

  > c
  function (..., recursive = FALSE)  .Primitive("c")
  > print(c)
  function (..., recursive = FALSE)  
  > 

{for "obvious" reasons, but needing even more work ...}

Also, Romain was "right" insofar as he would have wanted to only
deal with closure, whereas of course Duncan was right that the
corresponding class is 'function' and that comprises other types
(in the 'typeof' sense).

[story to be continued ...]

Martin Maechler, ETH Zurich


    RF> At the R level, I have now this :

    >>> print.function
    RF> function (x, useSource = TRUE, ...)  {
    RF> invisible(.Internal(print.function(x, useSource, ...)))
    RF> } <environment: namespace:base>

    RF> and the PrintValueRec dispatches like this at the C
    RF> level:

    RF> case LANGSXP: PrintLanguage(s, FALSE) ; break; case
    RF> CLOSXP: { SEXP call; PROTECT( call =
    RF> lang2(install("print.function"), s)); eval(call,env);
    RF> UNPROTECT(1); break; }

    RF> so that LANGSXP are printed using the PrintLanguage
    RF> function and CLOSXP are printed using the R function
    RF> print.function which in turns calls the PrintClosure
    RF> function (unless it is masked in R)

    RF> Romain


    RF> Romain Francois wrote:
    >>> Yesterday's patch did not print the attributes. This one
    >>> seems fine:
    >>> 
    >>> > f <- function(){} > attr( f, "yada" ) <- function( )
    >>> "lobster bisk" > f function(){} attr(,"yada") function(
    >>> ) "lobster bisk"
    >>> 
    >>> Romain
    >>> 
    >>> Romain Francois wrote:
    >>>> Duncan Murdoch wrote:
    >>>>> On 18/04/2009 10:12 AM, Romain Francois wrote:
    >>>>>> Hello,
    >>>>>> 
    >>>>>> Could the code that auto prints a function/closure be
    >>>>>> extracted from print.c so that there would be a
    >>>>>> print.closure function.  I would like to be able to
    >>>>>> mask a print.closure function so that I have a custom
    >>>>>> auto-print. One reason for that is I plan to have
    >>>>>> syntax highlighting within the R console.
    >>>>> 
    >>>>> The class of a closure is "function", so you'd want
    >>>>> the method to be print.function.  Currently that
    >>>>> doesn't work for auto printing, so your suggestion is
    >>>>> still interesting.  (I'm not sure why auto printing is
    >>>>> special here...)
    >>>>> 
    >>>>> Duncan Murdoch
    >>>> The attached patch implements exposing the
    >>>> print.function at the R level.
    >>>> 
    >>>> Romain
    >>>> 
    >>>> ------------------------------------------------------------------------
    >>>> 
    >>>> ______________________________________________
    >>>> R-devel at r-project.org mailing list
    >>>> https://stat.ethz.ch/mailman/listinfo/r-devel
    >>> 
    >>> 
    >>> ------------------------------------------------------------------------
    >>> 
    >>> ______________________________________________
    >>> R-devel at r-project.org mailing list
    >>> https://stat.ethz.ch/mailman/listinfo/r-devel


    RF> -- Romain Francois Independent R Consultant +33(0) 6 28
    RF> 91 30 30 http://romainfrancois.blog.free.fr

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



[Rd] print.closure at the R level
Martin Maechler