private/bindings.ss
;;; PLT Scheme Inference Collection
;;; bindings.ss
;;; Copyright (c) 2006 M. Douglas Williams
;;;
;;; 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 bindings mzscheme
  
  (provide (all-defined))
  
  (require (lib "list.ss" "srfi" "1"))
  
  ;; bindings?: any -> boolean?
  (define bindings? proper-list?)
  
  ;; make-bindings: -> bindings?
  (define (make-bindings)
    '())
  
  ;; bindings-bound? bindings? x any -> boolean?
  (define (bindings-bound? bindings key)
    (assq key bindings))
  
  ;; bindings-put!: bindings? x any x any -> list
  (define (bindings-put! bindings key datum)
    (alist-cons key datum bindings))
 
  ;; bindings-update!: bindings? x any x any -> list
  (define (bindings-update! bindings key datum)
    (let ((association (assq key bindings)))
      (if association
          (begin
            (set-cdr! association datum)
            bindings)
          (bindings-put! bindings key datum))))
  
  ;; bindings-get! bindings? x any -> any
  (define (bindings-get bindings key)
    (let ((association (assq key bindings)))
      (if association
          (cdr association)
          (error 'bindings-get
                 "No binding for ~a in ~a." key bindings))))
  
  ;; bindings-remove! bindings? x any
  (define (bindings-remove! bindings key)
    (alist-delete! key bindings eq?))
  
  ;; bindings-map: bindings? x procedure
  (define (bindings-map bindings proc)
    (map
     (lambda (binding)
       (proc (car binding) (cdr binding)))
     bindings))
  
  ;; bindings-for-each: bindings? x procedure
  (define (bindings-for-each bindings proc)
    (for-each
     (lambda (binding)
       (proc (car binding) (cdr binding)))
     bindings))
  
  ;; bindings-keys: bindings? -> list
  (define (bindings-keys bindings)
    (bindings-map bindings
     (lambda (key datum)
       key)))
  
  ;; bindings-data: bindings? -> list
  (define (bindings-data bindings)
    (bindings-map bindings
     (lambda (key datum)
       datum)))
  
  ;; bindings-keys-data: bindings? -> list x list
  (define (bindings-keys-data bindings)
    (let ((keys (bindings-keys bindings))
          (data (bindings-data bindings)))
      (values keys data)))
  
  )