The R language defines two different types of functions: primitive functions, which are low-level, and closures, which are the regular kind of functions.

is_function(x)

is_closure(x)

is_primitive(x)

is_primitive_eager(x)

is_primitive_lazy(x)

Arguments

x

Object to be tested.

Details

Closures are functions written in R, named after the way their arguments are scoped within nested environments (see https://en.wikipedia.org/wiki/Closure_(computer_programming)). The root environment of the closure is called the closure environment. When closures are evaluated, a new environment called the evaluation frame is created with the closure environment as parent. This is where the body of the closure is evaluated. These closure frames appear on the evaluation stack (see ctxt_stack()), as opposed to primitive functions which do not necessarily have their own evaluation frame and never appear on the stack.

Primitive functions are more efficient than closures for two reasons. First, they are written entirely in fast low-level code. Secondly, the mechanism by which they are passed arguments is more efficient because they often do not need the full procedure of argument matching (dealing with positional versus named arguments, partial matching, etc). One practical consequence of the special way in which primitives are passed arguments this is that they technically do not have formal arguments, and formals() will return NULL if called on a primitive function. See fn_fmls() for a function that returns a representation of formal arguments for primitive functions. Finally, primitive functions can either take arguments lazily, like R closures do, or evaluate them eagerly before being passed on to the C code. The former kind of primitives are called "special" in R terminology, while the latter is referred to as "builtin". is_primitive_eager() and is_primitive_lazy() allow you to check whether a primitive function evaluates arguments eagerly or lazily.

You will also encounter the distinction between primitive and internal functions in technical documentation. Like primitive functions, internal functions are defined at a low level and written in C. However, internal functions have no representation in the R language. Instead, they are called via a call to base::.Internal() within a regular closure. This ensures that they appear as normal R function objects: they obey all the usual rules of argument passing, and they appear on the evaluation stack as any other closures. As a result, fn_fmls() does not need to look in the .ArgsEnv environment to obtain a representation of their arguments, and there is no way of querying from R whether they are lazy ('special' in R terminology) or eager ('builtin').

You can call primitive functions with .Primitive() and internal functions with .Internal(). However, calling internal functions in a package is forbidden by CRAN's policy because they are considered part of the private API. They often assume that they have been called with correctly formed arguments, and may cause R to crash if you call them with unexpected objects.

Examples

# Primitive functions are not closures: is_closure(base::c)
#> [1] FALSE
is_primitive(base::c)
#> [1] TRUE
# On the other hand, internal functions are wrapped in a closure # and appear as such from the R side: is_closure(base::eval)
#> [1] TRUE
# Both closures and primitives are functions: is_function(base::c)
#> [1] TRUE
is_function(base::eval)
#> [1] TRUE
# Primitive functions never appear in evaluation stacks: is_primitive(base::`[[`)
#> [1] TRUE
is_primitive(base::list)
#> [1] TRUE
list(ctxt_stack())[[1]]
#> [[1]] #> <frame 31> (30) #> expr: eval(expr, envir, enclos) #> env: [local 0x7f979d57d9c8] #> #> [[2]] #> <frame 30> (20) #> expr: eval(expr, envir, enclos) #> env: [local 0x7f97a28244a0] #> #> [[3]] #> <frame 29> (20) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x7f97a2824708] #> #> [[4]] #> <frame 28> (20) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x7f979c4e4948] #> #> [[5]] #> <frame 27> (26) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x7f979d226350] #> #> [[6]] #> <frame 26> (25) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x7f979d226698] #> #> [[7]] #> <frame 25> (24) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x7f979c647a38] #> #> [[8]] #> <frame 24> (23) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x7f979d45a558] #> #> [[9]] #> <frame 23> (22) #> expr: try(f, silent = TRUE) #> env: [local 0x7f979d45a750] #> #> [[10]] #> <frame 22> (20) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x7f979d45a910] #> #> [[11]] #> <frame 21> (20) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x7f979d45aa28] #> #> [[12]] #> <frame 20> (19) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x7f979f888200] #> #> [[13]] #> <frame 19> (18) #> expr: evaluate::evaluate(text, code_env, new_device = TRUE) #> env: [frame 0x7f979d578ca0] #> #> [[14]] #> <frame 18> (16) #> expr: as_data.tag_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x7f979d69abc0] #> #> [[15]] #> <frame 17> (16) #> expr: as_data(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x7f979d6a0c68] #> #> [[16]] #> <frame 16> (12) #> expr: data_reference_topic(topic, pkg, path = path, examples = examples, <...> #> env: [frame 0x7f979d09b3c0] #> #> [[17]] #> <frame 15> (14) #> expr: stopifnot(is.list(x), is.list(val)) #> env: [frame 0x7f979d0b7e28] #> #> [[18]] #> <frame 14> (13) #> expr: utils::modifyList(data, data_template(pkg, depth = depth)) #> env: [frame 0x7f979d0b7bf8] #> #> [[19]] #> <frame 13> (12) #> expr: render_page(pkg, "reference-topic", data = data_reference_topic(topic, <...> #> env: [frame 0x7f979a49cdf0] #> #> [[20]] #> <frame 12> (11) #> expr: .f(.x[[i]], ...) #> env: [frame 0x7f97a0852540] #> #> [[21]] #> <frame 11> (10) #> expr: purrr::map(., build_reference_topic, path, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x7f979cbee238] #> #> [[22]] #> <frame 10> (8) #> expr: function_list[[k]](value) #> env: [frame 0x7f97a0d3fd10] #> #> [[23]] #> <frame 9> (8) #> expr: withVisible(function_list[[k]](value)) #> env: [frame 0x7f97a0d3fdf0] #> #> [[24]] #> <frame 8> (7) #> expr: freduce(value, `_function_list`) #> env: [frame 0x7f979f468918] #> #> [[25]] #> <frame 7> (6) #> expr: `_fseq`(`_lhs`) #> env: [frame 0x7f979f468ad8] #> #> [[26]] #> <frame 6> (5) #> expr: eval(expr, envir, enclos) #> env: [frame 0x7f97a0936408] #> #> [[27]] #> <frame 5> (3) #> expr: eval(quote(`_fseq`(`_lhs`)), env, env) #> env: [frame 0x7f979f468c98] #> #> [[28]] #> <frame 4> (3) #> expr: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) #> env: [frame 0x7f97a0e1ff20] #> #> [[29]] #> <frame 3> (2) #> expr: pkg$topics %>% purrr::transpose() %>% purrr::map(build_reference_topic, <...> #> env: [frame 0x7f97a09367c0] #> #> [[30]] #> <frame 2> (1) #> expr: build_reference(pkg, lazy = FALSE, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x7f97a07138e8] #> #> [[31]] #> <frame 1> (0) #> expr: pkgdown::build_site("/Users/lionel/Dropbox/R/hadley/rlang") #> env: [frame 0x7f97a457e3c8] #> #> [[32]] #> <frame 0> [global] #> expr: NULL #> env: [global] #> #> attr(,"class") #> [1] "ctxt_stack" "stack"
# While closures do: identity(identity(ctxt_stack()))
#> [[1]] #> <frame 33> (31) #> expr: identity(ctxt_stack()) #> env: [local 0x7f979f299670] #> #> [[2]] #> <frame 32> (31) #> expr: identity(identity(ctxt_stack())) #> env: [local 0x7f979f299b08] #> #> [[3]] #> <frame 31> (30) #> expr: eval(expr, envir, enclos) #> env: [local 0x7f979d57d9c8] #> #> [[4]] #> <frame 30> (20) #> expr: eval(expr, envir, enclos) #> env: [local 0x7f97a0a16c30] #> #> [[5]] #> <frame 29> (20) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x7f979e38a578] #> #> [[6]] #> <frame 28> (20) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x7f97a08cc908] #> #> [[7]] #> <frame 27> (26) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x7f97a0ef9aa0] #> #> [[8]] #> <frame 26> (25) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x7f97a0ddb6e8] #> #> [[9]] #> <frame 25> (24) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x7f97a0ef9b48] #> #> [[10]] #> <frame 24> (23) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x7f97a08cc828] #> #> [[11]] #> <frame 23> (22) #> expr: try(f, silent = TRUE) #> env: [local 0x7f97a0a16920] #> #> [[12]] #> <frame 22> (20) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x7f979f68f860] #> #> [[13]] #> <frame 21> (20) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x7f979f299478] #> #> [[14]] #> <frame 20> (19) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x7f97a016b660] #> #> [[15]] #> <frame 19> (18) #> expr: evaluate::evaluate(text, code_env, new_device = TRUE) #> env: [frame 0x7f979d578ca0] #> #> [[16]] #> <frame 18> (16) #> expr: as_data.tag_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x7f979d69abc0] #> #> [[17]] #> <frame 17> (16) #> expr: as_data(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x7f979d6a0c68] #> #> [[18]] #> <frame 16> (12) #> expr: data_reference_topic(topic, pkg, path = path, examples = examples, <...> #> env: [frame 0x7f979d09b3c0] #> #> [[19]] #> <frame 15> (14) #> expr: stopifnot(is.list(x), is.list(val)) #> env: [frame 0x7f979d0b7e28] #> #> [[20]] #> <frame 14> (13) #> expr: utils::modifyList(data, data_template(pkg, depth = depth)) #> env: [frame 0x7f979d0b7bf8] #> #> [[21]] #> <frame 13> (12) #> expr: render_page(pkg, "reference-topic", data = data_reference_topic(topic, <...> #> env: [frame 0x7f979a49cdf0] #> #> [[22]] #> <frame 12> (11) #> expr: .f(.x[[i]], ...) #> env: [frame 0x7f97a0852540] #> #> [[23]] #> <frame 11> (10) #> expr: purrr::map(., build_reference_topic, path, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x7f979cbee238] #> #> [[24]] #> <frame 10> (8) #> expr: function_list[[k]](value) #> env: [frame 0x7f97a0d3fd10] #> #> [[25]] #> <frame 9> (8) #> expr: withVisible(function_list[[k]](value)) #> env: [frame 0x7f97a0d3fdf0] #> #> [[26]] #> <frame 8> (7) #> expr: freduce(value, `_function_list`) #> env: [frame 0x7f979f468918] #> #> [[27]] #> <frame 7> (6) #> expr: `_fseq`(`_lhs`) #> env: [frame 0x7f979f468ad8] #> #> [[28]] #> <frame 6> (5) #> expr: eval(expr, envir, enclos) #> env: [frame 0x7f97a0936408] #> #> [[29]] #> <frame 5> (3) #> expr: eval(quote(`_fseq`(`_lhs`)), env, env) #> env: [frame 0x7f979f468c98] #> #> [[30]] #> <frame 4> (3) #> expr: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) #> env: [frame 0x7f97a0e1ff20] #> #> [[31]] #> <frame 3> (2) #> expr: pkg$topics %>% purrr::transpose() %>% purrr::map(build_reference_topic, <...> #> env: [frame 0x7f97a09367c0] #> #> [[32]] #> <frame 2> (1) #> expr: build_reference(pkg, lazy = FALSE, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x7f97a07138e8] #> #> [[33]] #> <frame 1> (0) #> expr: pkgdown::build_site("/Users/lionel/Dropbox/R/hadley/rlang") #> env: [frame 0x7f97a457e3c8] #> #> [[34]] #> <frame 0> [global] #> expr: NULL #> env: [global] #> #> attr(,"class") #> [1] "ctxt_stack" "stack"
# Many primitive functions evaluate arguments eagerly: is_primitive_eager(base::c)
#> [1] TRUE
is_primitive_eager(base::list)
#> [1] TRUE
is_primitive_eager(base::`+`)
#> [1] TRUE
# However, primitives that operate on expressions, like quote() or # substitute(), are lazy: is_primitive_lazy(base::quote)
#> [1] TRUE
is_primitive_lazy(base::substitute)
#> [1] TRUE