# SRFI 7: Feature-based program configuration language

by Richard Kelsey

> > > This copy of the SRFI 7 specification document is distributed as part of the Racket package srfi-doc.

> > > The canonical source of this document is https://srfi.schemers.org/srfi-7/srfi-7.html.

## Status

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

- Received: 1999-05-12
- Draft: 1999-05-26--1999-07-26
- Final: 1999-08-19

## Abstract

This SRFI describes a configuration language to be used for specifying the set of Scheme features or extensions required to run a program. In addition to a list of required features, a program may also contain Scheme code to be used only when a particular feature or combination of features is available.

The configuration language is entirely distinct from Scheme; it is neither embedded in Scheme nor includes Scheme as a subset.

## Rationale

The use of a separate configuration language makes it easy for both human and machine readers to determine the requirements of a program. It also avoids ambiguities arising from having to expand macros to determine which features, and thus which macros, are used in the program.

See SRFI 0 for a rationale for the need for some kind of configuration control.

## Specification

### Syntax

     <program> --> (program <program clause>+)

     <program clause>
       --> (requires <feature identifier>+)
         | (files <filename>*)
         | (code <Scheme expression, definition, or syntax definition>*)
         | (feature-cond <feature-cond clause>+)
         | (feature-cond <feature-cond clause>* (else <program clause>+))

     <feature-cond clause>
       --> (<feature requirement> <program clause>+)

     <feature requirement>
       --> <feature identifier>
         | (and <feature requirement>*)
         | (or <feature requirement>*)
         | (not <feature requirement>)

     <feature identifier>
       --> a symbol which is the name of a SRFI

### Semantics

The configuration language is distinct from Scheme. Given a set of available features a `<program>` can be converted into a sequence of Scheme commands, definitions, and syntax definitions. This conversion does not require expanding macros or doing any other processing of Scheme code.

An implementation of the configuration language will need to provide some method for executing a program. For example, it might have a `(LOAD-PROGRAM <filename>)` function or a compiler that compiles a program into an executable file.

The meanings of the different `<program>` clauses are given below. The ordering of the clauses in a `<program>` determines the order of the forms in the resultant Scheme program.

In processing the `REQUIRES` and `FEATURE-COND` clauses in a `<program>`, an implementation should be consistent with some fixed set of present and absent features. An implementation may analyze a `<program>` before choosing a set of features to use in processing it, but it should not use different sets of features for different clauses in the same `<program>`.

`(requires <feature-identifier>+)`
  The listed features are required by the program. If one or more is not provided by the implementation the program cannot be run.
`(files <filename>*)`
  The listed files are read and the forms they contain added to the program.
`(code <body>)`
  The forms in <body> are added to the program.
`(feature-cond <feature-cond clause>+)`

  The meaning of a `FEATURE-COND` clause is that of the `<program-clause>`s in the first `<feature-cond clause>` whose `<implementation-requirement>` is satisfied by the implementation. If an `ELSE` clause is present it is used if and only if no preceding clause is satisfied; a `FEATURE-COND` with an `ELSE` clause is always satisfied.

  If no clause can be satisfied the `<program>` cannot be evaluated in the implementation.

  The meaning of the `<implementation requirement>`s is as follows:

  `<feature identifier>` | satisfied if the feature is present
  `(and)` | always satisfied
  `(and x ...)` | satisfied if every `X` is satisfied
  `(or)` | never satisfied
  `(or x ...)` | satisfied if any `X` is satisfied
  `(not x)` | satisfied if `X` is not satisfied

## Implementation

Two implementations are provided here. The first is a `PROCESS-PROGRAM` function that converts a `<program>`, represented as an S-expression, and a list of feature identifiers and returns the list expressions, definitions, and syntax definitions that are the source for the `<program>` in the presence of those features. The function returns `#F` if the program cannot be run using the features provided.

This is not a complete implementation of the configuration language; it needs an (implementation-dependent) method for evaluating the forms returned by `PROCESS-PROGRAM`.

    (define (process-program program features)
      (call-with-current-continuation
        (lambda (exit)	; We exit early when an unsatisfiable clause is found.

          ; Process each clause in turn

          (define (process-clauses clauses)
    	(if (null? clauses)
    	    '()
    	    (append (process-clause (car clauses))
    		    (process-clauses (cdr clauses)))))

          ; Dispatch on the type of the clause.

          (define (process-clause clause)
    	(case (car clause)
    	  ((requires)
    	   (if (all-satisfied? (cdr clause))
    	       '()
    	       (exit #f)))
    	  ((code)
    	   (cdr clause))
    	  ((files)
    	   (read-files (cdr clause)))
    	  ((feature-cond)
    	   (process-cond-clauses (cdr clause)))))

          ; Loop through CLAUSES finding the first that is satisfied.

          (define (process-cond-clauses clauses)
    	(cond ((null? clauses)
    	       (exit #f))
    	      ((or (and (eq? (caar clauses) 'else)
    			(null? (cdr clauses)))
    		   (satisfied? (caar clauses)))
    	       (process-clauses (cdar clauses)))
    	      (else
    	       (process-cond-clauses (cdr clauses)))))

          ; Compound requirements are handled recursively, simple ones are tested.

          (define (satisfied? requirement)
    	(if (pair? requirement)
    	    (case (car requirement)
    	      ((and)
    	       (all-satisfied? (cdr requirement)))
    	      ((or)
    	       (any-satisfied? (cdr requirement)))
    	      ((not)
    	       (not (satisfied? (cadr requirement)))))
    	    (memq requirement features)))

          ; True if every requirement in LIST is satisfied.

          (define (all-satisfied? list)
    	(if (null? list)
    	    #t
    	    (and (satisfied? (car list))
    		 (all-satisfied? (cdr list)))))

          ; True if any requirement in LIST is satisfied.

          (define (any-satisfied? list)
    	(if (null? list)
    	    #f
    	    (or (satisfied? (car list))
    		(any-satisfied? (cdr list)))))

          ; Start by doing the whole program.

          (process-clauses (cdr program)))))

    ; Returns a list of the forms in the named files.

    (define (read-files filenames)
      (if (null? filenames)
          '()
          (append (call-with-input-file (car filenames)
    		(lambda (in)
    		  (let label ()
    		    (let ((next (read in)))
    		      (if (eof-object? next)
    			  '()
    			  (cons next (label)))))))
    	      (read-files (cdr filenames)))))

The second implementation is a `PROGRAM` macro that implements the configuration language in terms of the `COND-EXPAND` syntax of SRFI 0. Note that this implementation requires that `LOAD` use the current evaluation environment.

    (define-syntax program
      (syntax-rules (requires files code feature-cond)
        ((program)
         (begin))
        ((program (requires feature-id ...)
                  more ...)
         (begin (cond-expand ((and feature-id ...) 'okay))
                (program more ...)))
        ((program (files filename ...)
                  more ...)
         (begin (load filename) ...
                (program more ...)))
        ((program (code stuff ...)
                  more ...)
         (begin stuff ...
                (program more ...)))
        ((program (feature-cond (requirement stuff ...) ...)
                  more ...)
         (begin (cond-expand (requirement (program stuff ...)) ...)
                (program more ...)))))

## Copyright

Copyright (C) Richard Kelsey (1999). All Rights Reserved.

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 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.