Outdated egg!
This is an egg for CHICKEN 3, the unsupported old release. You're almost certainly looking for the CHICKEN 4 version of this egg, if it exists.
If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.
Introduction
Chicken Scheme procedures have attributes, aspects of the procedure object that can be operated upon. Such an attribute is known as a "procedure decoration".
This extension provides a facility to perform procedure decoration operations.
Usage
(require-extension procedure-decoration)
Documentation
An interface for procedure decoration. Similar to the lolevel unit extend-procedure interface but allowing many decorations, not just one.
Lambda Decoration Interface
A lower level interface for procedure decoration.
A PREDICATE is a procedure of one argument, a procedure decoration. The predicate must return a boolean indicating whether the argument is a decoration of the proper kind.
A DECORATOR is a procedure of one argument, the current procedure decoration. The decorator must return an object to replace the current decoration.
decorate-lambda
[procedure] (decorate-lambda PROCEDURE PREDICATE DECORATOR)
Decorate the PROCEDURE per the PREDICATE with the result of the DECORATOR.
Returns the decorated procedure.
decorated-lambda?
[procedure] (decorated-lambda? PROCEDURE PREDICATE)
Is the PROCEDURE decorated per the PREDICATE?
lambda-decoration
[procedure] (lambda-decoration PROCEDURE PREDICATE)
set-lambda-decoration!
[procedure] (set-lambda-decoration! PROCEDURE PREDICATE DECORATOR)
Change the decoration of the PROCEDURE per the PREDICATE with the result of the DECORATOR. The PROCEDURE must be already decorated per the PREDICATE.
Returns the decorated procedure.
Procedure Decorator Interface
A more sophisticated interface for procedure decoration.
make-procedure-decorator
[procedure] (make-procedure-decorator PREDICATE DECORATOR RETRIEVER [#:INITIALIZER] [#:INITIAL-VALUE] [#:BECOME?])
Returns a new procedure-decorator.
A PREDICATE is a procedure of one argument, a procedure decoration. The predicate must return a boolean indicating whether the argument is a decoration of the proper kind.
A DECORATOR is a procedure at least one argument, a procedure decoration. The decorator must return an object to replace the existing decoration.
The RETRIEVER is a procedure of varying arity, returning the procedure decoration object. The first argument is the procedure decoration. Any following arguments are from the procedure-decoration call that will invoke the RETRIEVER.
The INITIAL-VALUE is used as the procedure decoration for the first invocation of decorate-procedure. If not supplied an INITIALIZER must be available.
The INITIALIZER is a procedure of varying arity, returning the procedure decoration initial object. The arguments are from the decorate-procedure call that invokes the INITIALIZER. The INITIALIZER is invoked only once per procedure to decorate.
The default INITIALIZER is built from the DECORATOR. When an INITIAL-VALUE is not supplied the default DECORATOR will be called with a first argument of (void), and any other arguments from the decorate-procedure call. Otherwise the default DECORATOR will be called with a first argument of (void) and the INITIAL-VALUE as the second argument; any arguments from decorate-procedure call are ignored.
The BECOME? flag determines whether the procedure object of a decorated procedure will be rewritten. The default is #f.
procedure-decorator?
[procedure] (procedure-decorator? OBJECT)
Is the OBJECT a procedure-decorator?
procedure-decorator-become?
[procedure] (procedure-decorator-become? PROCEDURE-DECORATOR)
Is the PROCEDURE-DECORATOR a BECOME? decorator?
decorate-procedure
[procedure] (decorate-procedure PROCEDURE PROCEDURE-DECORATOR [ARGUMENTS ...])
Decorate the PROCEDURE with the PROCEDURE-DECORATOR.
The PROCEDURE-DECORATOR INITIALIZER is called on the first invocation for PROCEDURE and the DECORATOR on all subsequent invocations.
The optional ARGUMENTS are passed to the invoked PROCEDURE-DECORATOR constructor, as above.
On the first invocation a GC will be performed.
Returns the decorated procedure.
decorated-procedure?
[procedure] (decorated-procedure? PROCEDURE PROCEDURE-DECORATOR)
Is the PROCEDURE a decorated by PROCEDURE-DECORATOR?
procedure-decoration
[procedure] (procedure-decoration PROCEDURE PROCEDURE-DECORATOR [ARGUMENTS ...])
Returns the procedure decoration of PROCEDURE-DECORATOR for the PROCEDURE.
The optional ARGUMENTS are passed to the PROCEDURE-DECORATOR RETRIEVER.
procedure-decorator-getter-and-setter
[procedure] (procedure-decorator-getter-and-setter PROCEDURE-DECORATOR)
Returns a single argument getter, and creates a two argument setter, for the PROCEDURE-DECORATOR.
The getter takes a PROCEDURE argument. The setter takes PROCEDURE and OBJECT arguments.
Procedure Extender
Not to be confused with the lolevel unit extend-procedure interface.
make-procedure-extender
[procedure] (make-procedure-extender TAG)
Returns a simple PROCEDURE-DECORATOR that recognizes its' decorations by the TAG and accepts any object as a decoration.
The TAG may be any object but is usually a symbol.
define-procedure-extender
[syntax] (define-procedure-extender TAG [GETTER-NAME] [PREDICATE-NAME])
Creates a functional interface for a PROCEDURE-EXTENDER that uses the {{TAG} symbol.
Defines the PROCEDURE-DECORATOR as TAG-decorator.
Defines a procedure named GETTER-NAME that takes one argument, a decorated procedure, and returns the procedure decoration. Creates a corresponding setter.
The default GETTER-NAME is TAG-decoration.
Defines a procedure named PREDICATE-NAME that takes one argument, a procedure, and returns whether the procedure is decorated.
The default PREDICATE-NAME is TAG-decorated?.
Miscellaneous
lambda->list
[procedure] (lambda->list PROCEDURE [START] [END])
Returns the procedure object items of PROCEDURE from START to END as a list.
The START default is 1.
The END default is the number of procedure object items.
Use of this procedure is potentially dangerous if any of the objects in the list are mutated. Do not mutate the objects! Even better, do not use this procedure!
Examples
Using a new define syntax (works for compiled or interpreted procedures).
(use procedure-decoration) (use (srfi 89)) ;; Procedure documentation string API (define-procedure-extender docstring procedure-documentation documented-procedure?) ;; Procedure documentation 'define' wrappers ; Common code helper (define-macro ($define/doc ?defknd ?head ?docstr ?body) (let ([nam (let loop ([hd ?head]) (if (pair? hd) (loop (car hd)) hd))]) `(begin (,?defknd ,?head ,@?body) (set! (procedure-documentation ,nam) ,?docstr) (begin) ) ) ) ; Chicken 'define' (define-macro (define/doc ?head ?docstr . ?body) `($define/doc define ,?head ,?docstr ,?body) ) ; Chicken SRFI-89 'define*' (define-macro (define*/doc ?head ?docstr . ?body) `($define/doc define* ,?head ,?docstr ,?body) ) ;; Try it out (define/doc (foo . args) "procedure is foo" #t ) (procedure-documentation foo) ;=> "procedure is foo" (define*/doc (h2 (key: k #f) a . r) "h2 is srfi-89 proc" (list a k r)) (procedure-documentation h2) ;=> "h2 is srfi-89 proc"
Using original define syntax, for compile-time only. Must be used as a compiler extension.
(use procedure-decoration) (let () (define-procedure-extender docstring lambda-documentation) (set! ##compiler#process-lambda-documentation (lambda (id doc proc) (set! (lambda-documentation proc) doc) proc)) )
Notes
The decorate-lambda interface decorated procedure is not the original procedure. Decorating procedure foo will not change the procedure object of foo, but return a new one.
The procedure-decorator interface can rewrite the procedure object. Decorating procedure foo with a BECOME? procedure-decorator changes the procedure object of foo.
Author
Kon Lovett
License
Copyright (c) 2008 Kon Lovett. 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 ASIS, 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.
Requirements
misc-extn
Version history
- 1.0