283: (Type-)Check Introspection

by Artyom Bologov

Status

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

Abstract

This SRFI focuses on the introspection and type inference sides of typing / checking. It provides procedures deriving types / checks for arbitrary data. procedure-checks-of and check-of work with checks, as in SRFI 253. procedure-types-of and type-of work with implementation-specific types. type->check allows to turn implementation-specific types into portable checks, while check->sexp provides a way to serialize opaque checks into more readable s-expressions.

Issues

Table of Contents

Rationale

Introspection of requirements for procedure arguments and guarantees of return types enables programming support like as-you-type validity checks. They help in setting the expectations, e.g., for what the procedure accepts and what it returns. Or understanding the implementation-specific effective type for a value.

This SRFI provides a way to fetch types et al. for procedures and values. It augments the declarative side of SRFI 253 and SRFI 273 with more explanatory power, and allows peeking behind the implementation scaffolds for types.

The idea of checks and check-level introspection is (un)fortunately inherited from SRFI 253. The idea of type-level introspection was always on the surface in most Scheme implementations. Overall, this SRFI does nothing revolutionary (unless one is a static typing enthusiast thinking of types as a non-introspectable, purely compile-time artifact.)

Specification

There are two main concepts / levels in this specification: check-level and type-level. Check level focuses on checks, as per SRFI 253 and SRFI-273, while type level is based on an (inherently) implementation-specific notion of types, whatever they might mean.

Check-level API #

Checks, using the terminology of SRFI 253, might be the wrong basis for program checking. Types might be the right one. (One has yet to prove either.) However, checks capture the inherent dynamicity of Scheme values and are flexible enough for useful contract declaration. Introspecting the checks attached to the value / symbol / procedure is as useful as defining them. This section defines two procedures providing a complete set of introspection operations on procedures and values.

Given that SRFI 253 et al. serve as a portable façade for implementation-specific types, implementors are encouraged to infer checks even for things only having implementation-specific types. Thus making APIs in this section a superset of the next one.

There’s a peculiar choice of returning procedures instead of more introspectable s-expressions. It is motivated by the fact that s-expressions may be interpreted (eval-uated) in different ways depending on the context / environment. Returning procedures, on the other hand, ensures that there are no ambiguous interpretations. Thanks to Peter McGoron for bringing this up on the SRFI 273 mailing list. In case one does direly need s-expressions, one can use the check->sexp procedure to turn checks into parseable s-expressions.

(srfi 283) procedure-checks-of proc ⟹ <signatures>#

Procedure check introspection API. Accepts a procedure, possibly defined with SRFI 253 syntax or otherwise checked / typed. The (ad-hoc) format of the returned values is:

<signatures> :
   #f                          ;; Signatures unknown
   | ((<args> <returns>) ...)

<args> :
   <check>                     ;; Single rest argument
   | (<check> ...)             ;; Non-variadic signature
   | (<check> ... . <check>)   ;; Variadic / dotted / rest signature

<returns> :
   #f                          ;; The number and format of return values is unknown
   | (<check> ...)             ;; Exact number of return values conforming to provided checks

<check> :
   #f                          ;; Any value
   | <procedure>               ;; Value conforming to the procedure check, as per SRFI 253

While quite convoluted, it captures important facts about procedures and their checking:

(procedure-checks-of not-introspectable)
;; => #f
(procedure-checks-of identity)
;; => ([(#f) (#f)])
;; Square brackets are used as parentheses alternative, for readability
(procedure-checks-of procedure?)
;; => ([(#f) (#<procedure boolean?>)])
(procedure-checks-of map)
;; => ([(#<procedure procedure?> #<procedure list?> . #<procedure list?>) (#<procedure list?>)])
(procedure-checks-of +)
;; => ([#<procedure number?> (#<procedure number?>)])
(procedure-checks-of truncate/)
;; => ([(#<procedure integer?> #<procedure integer?>) (#<procedure integer?> #<procedure integer?>)])
(procedure-checks-of vector-copy)
;; => ([(#<procedure vector?>) (#<procedure vector?>)]
       [(#<procedure vector?> #<procedure integer?>) (#<procedure vector?>)]
       [(#<procedure vector?> #<procedure integer?> #<procedure integer?>) (#<procedure vector?>)])
;; or, depending on what vector-copy compiles to:
;; => ([(#<procedure vector?> . #<procedure integer?>) (#<procedure vector?>)])
;; or even:
;; => ([(#<procedure vector?> . #f) (#<procedure vector?>)])
(procedure-checks-of procedure-checks-of)
;; => ([(#<procedure procedure?>) (#<procedure (disjoin boolean? pair?)>)])

Such a return value format results in combinatorial explosion of cases to handle in the caller of procedure-checks-of. Especially compared to a much simpler format of “two list / boolean values for a single arity” this SRFI used to have in earlier drafts. But such is the cost of catering for multiple arities. Example code processing this format might look like:

;; Implying Wright-Cartwright-Shinn match macro
(map (lambda [signature]
       (match signature
         [[#f #f] #| no checks |#]
         [[#f (returns ...)] #| return checks only |#]
         [[(args ...) #f] #| arg checks only |#]
         [[(args ...) (returns ...)] #| required arglist|#]
         [[(args ... . rest) (returns ...)] #| required+rest arglist|#]
         [[rest (returns ...)] #| rest-only arglist|#]))
     (procedure-checks-of proc))

While intimidating, it’s unlikely to ever reach this level of complexity as most callers will only be interested in a subset of the returned signatures.

Note also that the argument signature for a variadic-only procedure with unknown variadic type and the signature for a procedure with unknown arguments and their types are the same—#f. This is a corner case that’s too rare, inconsequential, and complicated to be handled in this SRFI.

(srfi 283) check-of obj ⟹ procedure | #f #

Returns the check matching the obj, as a procedure that this obj must satisfy. The implementation can also return #f in case the check cannot be introspected — or at any time really.

(check-of 3)
;; => #<procedure exact-integer?>
(check-of check-of)
;; => #<procedure procedure?>

Type-level API

While checks might be a useful and portable way to reason about values, a more concrete / close-to-the-metal / flexible / implementation-specific introspection might be needed. Like… types. This section details on the forms working with types instead of checks.

(srfi 283) procedure-types-of proc ⟹ <signatures> (modified) #

Returns a procedure-checks-of-like value—list of signatures. But with types replacing <procedure> terminals. And with implementation-specific keyword / optional / guarded / other lambda arguments possibly represented.

Types are inherently implementation-specific objects and thus have little introspection potential. type->check allows one to turn them into more portable and manipulable checks.

;; Using Chibi Scheme syntax and square brackets as alternatives to parentheses for readability
(procedure-types-of +)
;; => ([#<type "Number"> (#<type "Number">)])
(procedure-types-of vector-ref)
;; => ([(#<type "Vector"> #<type "Integer">) (#f)])
(procedure-types-of string-copy)
;; => ([(#<type "String">) (#<type "String">)]
       [(#<type "String"> #<type "Integer">) (#<type "String">)]
       [(#<type "String"> #<type "Integer"> #<type "Integer">) (#<type "String">)])
(procedure-types-of procedure-types-of)
;; => ([(#<type "Procedure">) (#f)])

(srfi 283) type-of obj ⟹ implementation-specific #

Returns an implementation-specific object representing the type of obj. This is included by some implementations already.

;; Examples from Chibi Scheme
(type-of 3)
;; => #<type "Integer">
(type-of vector-copy!)
;; => #<type "Procedure">

Utilities #

Neither checks nor types are particularly easy to work with. They need some support to be introspectable and readable. Thus these utilities.

(srfi 283) check->sexp proc ⟹ list | symbol | #f #

Working with checking procedures is not entirely worry-free. Given that Scheme has no procedure introspection facilities in the standard, even getting a procedure name is an adventure in itself. Thus the need for a helper that translates the opaque procedures into something printable and graspable.

check->sexp converts the passed-in check procedure into a symbol (in case of simple predicates, like integer?), list (for more complex ones, like (cut eqv? <> 3)), or returns #f (in case the conversion is unsuccessful).

(check->sexp procedure?)
;; => procedure? ;; Symbol
(check->sexp (disjoin procedure? integer?))
;; => (disjoin procedure? integer?) ;; S-expression
(check->sexp some-unknown-or-non-inspectable-check)
;; => #f

(srfi 283) type->check type ⟹ procedure | #f #

Implementation-specific types are inherently… implementation-specific, so one needs a way to portably handle them. Converting these to checks is the easiest way to do so. type->check does exactly that—it takes an implementation-specific type object and returns a check matching it. Or #f in case this check cannot be inferred for some reason.

So, to get a human / machine readable representation of a type one may use something like:

(define (type->sexp type)
  (cond
   [(cond
     [type => type->check]
     [else #f])
    => check->sexp]
   [else #f]))
;; Or, using SRFI-2 and-let*
(define (type->sexp type)
  (and-let* ([type type]
             [check (type->check type)]
             [sexp (check->sexp check)])
    sexp))

Symbol Type Introspection

Early drafts of this SRFI included symbol-check-of and symbol-type-of procedures. These were supposed to be useful in case the domain of the symbol was wider than the domain of the value currently bound to it, say with a symbol restricted to reals, while its value was an exact integer. Both procedures were removed because interpretation of a symbol involves questions of environment management and evaluation. These are too expansive for a simple SRFI like this one, thus the removal.

Implementations might add these procedures if they wish to. But the complicated questions have to be answered for these.

Implementation

You can find a sample implementation (for Chibi Scheme and Teeny Scheme) in this SRFI’s repository.

Acknowledgements

Thanks to Peter McGoron for the discussion of an earlier design of this SRFI, then part of SRFI 273. Also thanks to him for suggestions on more detailed types from procedure-types-of and type->check.

Thanks to Arne Babenhauserheide for a splendid revision of the Rationale section. And for creating SRFI-284: define-typed with an explicit goal of supporting SRFI 283.

Thanks to Arthur Gleckler for shepherding the SRFI process all these years.

© 2026 Artyom Bologov.

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