#|[ Author: Juergen Lorenz ju (at) jugilo (dot) de Copyright (c) 2014, Juergen Lorenz All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ]|# ;;; The option datatype is inspired by the equally named type in ML. It ;;; simply stores one or no value at all. To be able to type options, we ;;; implement it as a functor, named option-functor, and supply an ;;; untyped version as a module named options, which simply applies the ;;; functor to a module with an any? type test, i.e. no check at all. ;;; This argument module is generated implicitly by instantiation of the ;;; functor and named _options. ;;; Unfortunately, Chicken's functor implementation contains a bug, ;;; which appears only when compiling the generated module: The functor ;;; parameter, M, must be replaced by the functor argument, _options, in ;;; the Chicken generated file options.import.scm. Hence we need to ;;; patch that latter file. ;;; (require-library datatype) (functor (option-functor (M (item?))) (options option? none some none? some-ref) (import scheme datatype M (only chicken error case-lambda)) (define-datatype option option? (none) (some (item item?))) (define (none? xpr) (cases option xpr (none () #t) (else #f))) (define (some-ref xpr) (cases option xpr (none () (error 'option "no value available")) (some (item) item))) (define options (let ( (signatures '( (none) (some item) (option? xpr) (none? xpr) (some-ref opt) )) ) (case-lambda (() (map car signatures)) ((sym) (assq sym signatures))))) ) ; option-functor (module options = option-functor (import scheme) (define (item? xpr) #t) ) ; options