contract-utils.ss
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;  Contract-Utils: general-purpose PLT contract utilities.
;;  Copyright (C) 2005  Richard Cobbe
;;  Version 2.1
;;
;;  This library is free software; you can redistribute it and/or modify it
;;  under the terms of the GNU Lesser General Public License as published by
;;  the Free Software Foundation; either version 2.1 of the License, or (at
;;  your option) any later version.
;;
;;  This library is distributed in the hope that it will be useful, but WITHOUT
;;  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;;  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
;;  License for more details.
;;
;;  You should have received a copy of the GNU Lesser General Public License
;;  along with this library; if not, write to the Free Software Foundation,
;;  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(module contract-utils mzscheme

  (require (lib "contract.ss"))

  ;; abstract types provided by contract.ss:
  ;;   Contract
  ;;   FlatContract

  ;; PredContract ::= (Union (-> ?a Bool) Contract)
  ;; PredFlatContract ::= (Union (-> ?a Bool) FlatContract)

  ;; nelistof/c :: PredFlatContract -> FlatContract
  ;; produces a contract that recognizes a non-empty list of elements
  ;; which satisfy the contract c.
  (define nelistof/c
    (lambda (c)
      (and/c (listof c) (not/c null?))))

  ;; sexp/c :: FlatContract
  ;; recognizes arbitrary s-expressions.
  (define sexp/c
    (flat-rec-contract sexp
                       (cons/c sexp sexp)
                       null?
                       number?
                       symbol?
                       string?
                       boolean?
                       char?))

  ;; predicate/c :: Contract
  ;; recognizes unary predicates
  (define predicate/c (any/c . -> . boolean?))

  ;; optional/c :: PredContract -> Contract
  ;; produces a contract that recognizes both #f and all values recognized
  ;; by the argument
  (define optional/c (lambda (contract) (union contract false/c)))

  ;; positive-int/c :: FlatContract
  ;; recognizes all positive integers
  (define positive-int/c
    (and/c natural-number/c (lambda (x) (> x 0))))

  ;; contract/c :: Contract
  ;; recognizes contracts and predicates
  (define contract/c (union contract? predicate/c))

  ;; flat-contract/c :: Contract
  ;; recognizes flat contracts and predicates
  (define flat-contract/c (union flat-contract? predicate/c))

  ;; contract-of :: PredContract -> Contract
  ;; wraps a predicate in a flat contract; idempotent
  (define contract-of
    (lambda (c/p)
      (if (contract? c/p) c/p (flat-contract c/p))))

  ;; predicate-of :: PredFlatContract -> Predicate
  ;; extracts a flat contract's predicate if necessary.  Idempotent.
  (define predicate-of
    (lambda (c/p)
      (if (flat-contract? c/p) (flat-contract-predicate c/p) c/p)))

  (define-syntax eta
    (syntax-rules ()
      [(_ f) (lambda args (apply f args))]))

  (provide/contract [sexp/c flat-contract?]
                    [predicate/c contract?]
                    [optional/c (-> contract/c contract?)]
                    [positive-int/c flat-contract?]
                    [nelistof/c (-> contract/c flat-contract?)]
                    [contract/c contract?]
                    [flat-contract/c contract?]
                    [contract-of (-> contract/c contract?)]
                    [predicate-of (-> flat-contract/c predicate/c)])

  (provide eta))