Site icon R-bloggers

"Inside" Functors — Evaluating things more than once

[This article was first published on Struggling Through Problems, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< !-- begin{Schunk} !--> < !--\end{Schunk}!-->

(The examples here work with the version of insidefunctor tagged as “v1”)

I ran into an interesting problem using “inside” functors.

Something is wrong in the following code (well, depending on what you thought it should do).

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> library(insidefunctor)

< !-- ccc -->

> `%+.%` = fmap(`+`)

< !-- ccc -->

> x = seq(0, 10, len = 50)

< !-- ccc -->

> plot(x, collect(each(x) %+.% runif(1)))

< !-- ccc --> < !--\end{Sinput}!--> < !--\end{Schunk}!-->

It’s clear that in constructions like each(x) + y, y is only going to be evaluated once. Of course, the preceding example could have been written

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> plot(x, collect(each(x) %+.% each(runif(length(x)))))

< !-- ccc --> < !--\end{Sinput}!--> < !--\end{Schunk}!-->

but I think that that is not as grammatically pretty.

But, since we solved the last grammatical problem with a hacky use of inside-functors, why not try the same trick? Say we define an inside functor meval (for multiple-evaluations) that behaves like this:

That is, the unevaluated chain keeps growing until it is finally collected, at which point a value results.

So let’s define that.

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> meval = function(expr, level=1) {

> expr = substitute(expr)

> callback = function () {

> eval(expr)

> }

> make.meval(callback, level=level, depth=1)

> }

< !-- ccc -->

> make.meval = function(callback, level, depth) {

> functor = inside.functor(level, depth)

> functor$callback = callback

>

> class(functor) = c(‘meval’, class(functor))

>

> functor

> }

< !-- ccc -->

> apply.functor.meval = function(

> inside,

> func,

> args,

> caller

> )

> {

> our.level = level(inside)

>

> args.boxed = args

> for (i in seq_along(args.boxed)) {

> arg = args.boxed[[i]]

>

> if (is.inside.functor(arg) && level(arg)>=our.level) {

> }

> else {

> args.boxed[[i]] = list(

> callback = function() {

> arg

> }

> )

> }

> }

> max.depth = max(sapply(args.boxed, depth))

>

> callback = function() {

> piece.args = lapply(args.boxed, function (arg) {

> arg$callback()

> })

> caller(func, piece.args)

> }

>

> make.meval(

> callback,

> level = our.level,

> depth = max.depth

> )

> }

< !-- ccc -->

> collect.end.meval = function(inside) {

> inside$callback()

> }

< !-- ccc --> < !--\end{Sinput}!--> < !--\end{Schunk}!-->

And test it.

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> promise = meval(runif(1))

< !-- ccc -->

> collect(promise)

< !-- ccc --> < !-- end{Sinput} !-->

0.633877807762474

< !-- begin{Sinput} !-->

> collect(promise)

< !-- ccc --> < !-- end{Sinput} !-->

0.236430999357253

< !--\end{Schunk}!-->

Works so far. Now try the motivating example:

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> plot(x, collect.all(each(x, l = 2) %+.% meval(runif(1))))

< !-- ccc --> < !--\end{Sinput}!--> < !--\end{Schunk}!-->

Oh god no it’s this problem again.

arg isn’t being remembered in

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> args.boxed[[i]] = list(

> callback = function() {

> arg

> }

> )

< !-- ccc --> < !--\end{Sinput}!--> < !--\end{Schunk}!-->

so the fix is to

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> apply.functor.meval = function(

> inside,

> func,

> args,

> caller

> )

> {

> our.level = level(inside)

>

> args.boxed = args

> for (i in seq_along(args.boxed)) {

> arg = args.boxed[[i]]

>

> if (is.inside.functor(arg) && level(arg)>=our.level) {

> }

> else {

> args.boxed[[i]] = (function(arg) {

> force(arg)

> list(

> callback = function() {

> arg

> }

> )

> })(arg)

> }

> }

> max.depth = max(sapply(args.boxed, depth))

>

> callback = function() {

> piece.args = lapply(args.boxed, function (arg) {

> arg$callback()

> })

> caller(func, piece.args)

> }

>

> make.meval(

> callback,

> level = our.level,

> depth = max.depth

> )

> }

< !-- ccc --> < !--\end{Sinput}!--> < !--\end{Schunk}!-->

Which is ugly but works. Then:

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> plot(x, collect.all(each(x, l = 2) %+.% meval(runif(1))))

< !-- ccc --> < !--\end{Sinput}!--> < !--\end{Schunk}!-->

Now the real challenge is to understand why the above code works, but interchanging the levels (ie making the each() happen before the meval()) does not:

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> plot(x, collect.all(each(x) %+.% meval(runif(1), l = 2)))

< !-- ccc --> < !--\end{Sinput}!--> < !--\end{Schunk}!-->

And, given that you obviously wanted it to go the first way or why would you have used meval(), is there any way to modify the semantics so that only the first way makes sense (and is that a good idea?), which brings us to…

About those levels…

They’re yucky. Also note that the call to collect.all in the preceding example is really doing 2 collects, even though the functors are only ever written 1 deep.

The reason is that expressions like

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> x = c(1, 2, 3)

< !-- ccc -->

> y = c(4, 5)

< !-- ccc -->

> collect.all(each(x, l = 2) %+.% each(y))

< !-- ccc --> < !-- end{Sinput} !-->




< !--\end{Schunk}!-->

behave like (inserting for the xs)

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> collect.all(

> each(x)

> %+.%

> each(

> lapply(x, function(x.) each(y))

> )

> )

< !-- ccc --> < !-- end{Sinput} !-->




< !--\end{Schunk}!-->

which behaves like (inserting for the ys)

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> collect.all(

> each(

> lapply(x, function(x.)

> each(

> lapply(y, function(y) x.)

> )

> )

> )

> %+.%

> each(

> lapply(x, function(x) each(y))

> )

> )

< !-- ccc --> < !-- end{Sinput} !-->




< !--\end{Schunk}!-->

ie only when the levels are the same do the eaches “line up” and remain a single each. When the levels are different they “miss” each other and become two nested eaches. This is by design but it still feels messy.

Suppose we were to bring back the suggestion of the name “corresponding” that we mentioned earlier:

< !-- begin{Schunk} !--> < !-- begin{Sinput} !-->

> each(x) %+.% corresponding(y)

< !-- ccc --> < !--\end{Sinput}!--> < !--\end{Schunk}!-->

would stand for when the levels are identical; in any other case the levels would be assumed to be different and the functors would “overlap”.

The advantage to this notation is that only when the word “each” is actually used is another level introduced. Plus it aligns more closely with English.

To leave a comment for the author, please follow the link and comment on their blog: Struggling Through Problems.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.