nimble-dev / nimble

The base NIMBLE package for R
http://R-nimble.org
BSD 3-Clause "New" or "Revised" License
160 stars 24 forks source link

problematic lookup/scoping for nested nimbleFunctions #1497

Closed paciorek closed 2 days ago

paciorek commented 1 month ago

If a user defines a nimbleFunction whose name is the same as the name of a function in the nimble DSL, in compiled code we use the DSL function and not the user's nimbleFunction. E.g., exp, log1p, gamma, etc.

This is a problem for (at least) two reasons:

log1p <- nimbleFunction(
    run=function(x=double(0)) {
        returnType(double(0))
        return(9999)
    })

nf <- nimbleFunction(
    run=function(x=double(0)) {
        out = log1p(x)
        print(out)
    })

cnf <- compileNimble(nf)
cnf(1)  # user would expect 9999
# 0.693147

Some options:

gamma() is a more complicated variation on this given we translate gamma -> gammafn earlier on...

@perrydv this is not urgent, but we'll want to discuss for 1.3.0.

paciorek commented 1 month ago

Followup: given nCompiler, I think I will propose:

  1. We use the minimal approach and warn users.
  2. We keep this in mind for nCompiler lookup and handle things more robustly there.
paciorek commented 1 month ago

See related issue #1469, including the possibility of using getAnywhere rather than get.

perrydv commented 1 month ago

@paciorek I agree with both instincts: leave as is because we'll have a revamped system coming into place; and if using getAnywhere and checking what results are actually nimbleFunctions is not too hard to do, I'm not opposed to it. There may be multiple places where this would need to be done. All of that said, I would vote for leaving as is for now.

paciorek commented 2 days ago

I'm error-trapping this via #1517 .