Title

Procedure Arity Inspection

Author

John Cowan (based on SRFI 102 by David van Horn)

Status

This SRFI is currently in withdrawn status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-191@nospamsrfi.schemers.org. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.

Table of contents

Abstract

Many Scheme systems provide mechanisms for inspecting the arity of a procedural value, making it a common feature, however there is no standard interface. As a result there is no portable way to observe the arity of a procedure without actually applying it. This SRFI proposes a simple interface that is consistent with existing Scheme systems' facilities and prior proposals.

Rationale

This proposal provides a single procedure that makes it possible to determine if a procedure applied to a particular number of arguments either may accept them or definitely does not accept them. This is the first step towards being able to determine safely whether a procedure can safely call another procedure passed to it as an argument. Equivalent functionality is already supported in several Scheme implementations and it should be easy to add support in many more systems. This particular interface is based on an email by Kent Dybvig sent to the SRFI-102 mailing list.

Many Scheme systems provide procedure inspection facilities for properties other than arity, such as source code, source location, and documentation. This SRFI focuses solely on the ability to retrieve arity information from procedural values; features for retrieving other kinds of information from procedures are considered beyond the scope of this proposal and left for future SRFIs to take up.

For the history of earlier proposals, see the Rationale of SRFI 102.

Specification

(procedure-arity-mask proc) → exact integer

Returns an exact integer n such that if the implementation can prove that proc definitely cannot be called with k arguments, then the kth bit of n is 0. Otherwise, the kth bit is 1. The 0th bit of k is the least significant bit; positive integers are left extended with zeros and negative integers with ones.

The implications of this are that if a procedure can only be called with k arguments, then n = 2k. If a procedure can be called with k or more arguments, then n = -2k - 1. In particular, if the procedure can be called with any number of arguments, then n = -1. If n = 0, the procedure cannot be called at all, which should never happen.

Most Schemes expand away case-lambda before compilation, and so such procedures will return -1. However, some implementations might preserve information from case-lambda. If so, a procedure that accepts exactly 1, 3, or 5 arguments would return n = 21 + 23 + 25 = 42, whereas a procedure that accepts 0, 2, 4, or 6 or more arguments (which is to say, a procedure that rejects 1, 3, or 5 arguments) would return n = (bitwise-not 42) = -43.

Implementation

A simple and portable sample implementation is:

(define (procedure-arity-mask n) -1)

Since a kth bit of 1 means that the procedure may be callable with k arguments, this is completely correct (and useless).

Racket provides a native implementation.

An implementation on top of SRFI 102's API can be found in the repository of this SRFI. It depends on the bitwise-ior procedure, which can be found in R6RS and in various SRFIs including SRFI 151, "Integers as Bits".

Acknowledgments

Here are the acknowledgements from SRFI 102:

I would like to thank Eli Barzilay, R. Kent Dybvig, Matthias Felleisen, Daniel P. Friedman, and Shriram Krishnamurthi for answering questions on the history of arity inspection in Scheme. In particular, I would like to further thank R. Kent Dybvig for providing the analysis of why not to support arity inspection. I would like to thank Aaron W. Hsu for providing information on Chez Scheme's procedure inspection facilities. I am grateful to Michael Sperber for serving as SRFI editor. Support was provided by the National Science Foundation under Grant #0937060 to the Computing Research Association for the CIFellow Project.

Copyright

Copyright © David Van Horn 2009, John Cowan 2020.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Editor: Arthur A. Gleckler