TIP #194: Procedures as Values via '''apply'''

Title:Procedures as Values via '''apply'''
Version:$Revision: 1.6 $
Authors: Miguel Sofer <msofer at users dot sf dot net>
Joe Mistachkin <joe at mistachkin dot com>
Created:Friday, 30 April 2004
Keywords:Tcl, lambda, anonymous, command, function, functional programming


This TIP proposes a new command, tentatively named apply, to allow procedures to be first class values. It is an alternative to the approach of TIP #187, where attaining a similar goal requires a syntactic and semantic change.


Tcl is typeless, and every first class value is a string (or at least representable as such). Strings are managed by reference counting in the Tcl core, the required memory is freed automatically when the string is no longer referenced.

Tcl commands may interpret their arguments differently. Some commands interpret some of their arguments as scripts - eval, uplevel, and so on. But no current Tcl command is able to specify that a script is to be run in an isolated environment, free of unwanted side effects or memory leaks caused by name collisions.

In order to achieve this isolation, one has to define a new command via proc and assume the burden of name-collision avoidance and lifetime management, or else produce complicated scripts with ugly contorsions to avoid the name collisions.

Both TIP #187 and this one propose ways to provide this lacking functionality to Tcl. The approach here is to do so by creating a new apply command without any change to Tcl's syntax.


A reference manual page can be found at [1]. Summarizing, the syntax of the new command is:

The semantics of apply (with the exception of some of the fine details of error messages) can be described by:

 proc apply {fun args} {
     set len [llength $fun]
     if {($len < 2) || ($len > 3)} {
         error "can't interpret \"$fun\" as anonymous function"
     lassign $fun argList body ns
     set name ::$ns::[getGloballyUniqueName]
     set body0 {
         rename [lindex [info level 0] 0] {}
     proc $name $argList ${body0}$body
     set code [catch {uplevel 1 $name $args} res opt]
     return -options $opt $res

where the availability of a getGloballyUniqueName procedure was assumed.

Reference Implementation

There is a patch [2] that implements this TIP.

The patch defines a new tclLambdaType for Tcl_Objs that caches the internal structures necessary for efficient evaluation: a Proc struct, a pointer to namespace, and the bytecodes implementing body. It is a small patch that relies heavily on the implementation of proc, producing essentially a regular proc with no command attached to it: an anonymous function.

All cached internal structures are freed when func ceases to be referenced or when it loses its internal representation as a tcllambdaType through shimmering.

Note that a similar approach is likely for a definitive implementation of TIP #187.

Further Functional Programming Constructs

The availability of apply permits an easy and efficient access to other FP functions. For example one might define a constructor lambda and a curry command like this:

 proc lambda {arglist body {ns {}}} {
     list ::apply [list $arglist $body $ns]

 proc curry {lam args} {
     lappend lam {expand}$args

Function composition is also relatively easy to specify. Further examples may be seen in the Wiki, see for instance Neil Madden's map, filter, foldl, foldr [3] - note that the syntax is slightly different from the one proposed here.

Comparison to TIP 187 and outlook

In terms of usage, the main difference is that where TIP 187 does:

 set p [list lambda x {string length $x}]
 $p $foo

we would do here:

 set p [list ::apply [list x {string length $x}]]
 # or: set p [lambda x {string length $x}]
 {expand}$p $foo ;# or 'eval $p [list $foo]', or ...

or else:

 set p [list x {string length $x}]
 apply $p $foo

This TIP requires no changes to the rules in Tcl(n), whereas TIP #187 requires a change in rule [2].

If in the future Tcl evolves a rule for automatic expansion of leading words, apply will provide automatically the syntax of TIP #187.


This document has been placed in the public domain.

Powered by Tcl[Index] [History] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows