unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Jose A Ortega Ruiz <jao@gnu.org>
Cc: guile-sources@gnu.org
Subject: srfi-39 implementation -- Bug fix
Date: Thu, 6 May 2004 03:24:48 +0200	[thread overview]
Message-ID: <20040506012448.GA3174@uab.es> (raw)
In-Reply-To: <20040506010251.GA3066@uab.es>

[-- Attachment #1: Type: text/plain, Size: 207 bytes --]

Hi again!

I'm very sorry: my previous code had a bug (converter procedures were
not used in parameterize). I'm attaching a (hopefully) corrected
version, together with a little test program.

Thanks!

jao


[-- Attachment #2: srfi-39.scm --]
[-- Type: text/plain, Size: 4139 bytes --]

;;; srfi-39.scm --- Parameter objects

;; 	Copyright (C) 2004 Free Software Foundation, Inc.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; (at your option) any later version.
;;
;; This program 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
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this software; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;; Boston, MA 02111-1307 USA
;;
;; As a special exception, the Free Software Foundation gives permission
;; for additional uses of the text contained in its release of GUILE.
;;
;; The exception is that, if you link the GUILE library with other files
;; to produce an executable, this does not by itself cause the
;; resulting executable to be covered by the GNU General Public License.
;; Your use of that executable is in no way restricted on account of
;; linking the GUILE library code into it.
;;
;; This exception does not however invalidate any other reasons why
;; the executable file might be covered by the GNU General Public License.
;;
;; This exception applies only to the code released by the
;; Free Software Foundation under the name GUILE.  If you copy
;; code from other Free Software Foundation releases into a copy of
;; GUILE, as the General Public License permits, the exception does
;; not apply to the code that you add in this way.  To avoid misleading
;; anyone as to the status of such modified files, you must delete
;; this exception notice from them.
;;
;; If you write modifications of your own for GUILE, it is your choice
;; whether to permit this exception to apply to your modifications.
;; If you do not wish that, delete this exception notice.

;;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
;;; Date: 2004-05-05

;;; Commentary:

;; This is an implementation of SRFI-39 (Parameter objects).
;;
;; The implementation is based on Guile's fluid objects, and is, therefore,
;; thread-safe (parameters are thread-local).
;;
;; In addition to the forms defined in SRFI-39 (`make-parameter',
;; `parameterize'), a new procedure `with-parameters*' is provided.
;; This procedures is analogous to `with-fluids*' but taking as first
;; argument a list of parameter objects instead of a list of fluids.
;;

;;; Code:

(define-module (srfi srfi-39)
  #:use-module (ice-9 syncase)
  #:use-module (srfi srfi-16)

  #:export (make-parameter)
  #:export-syntax (parameterize)

  ;; helper procedure not in srfi-39.
  #:export (with-parameters*))

(define make-parameter
  (case-lambda
    ((val) (make-parameter/helper val (lambda (x) x)))
    ((val conv) (make-parameter/helper val conv))))

(define get-fluid-tag (lambda () 'get-fluid)) ;; arbitrary unique (as per eq?) value
(define get-conv-tag (lambda () 'get-conv)) ;; arbitrary unique (as per eq?) value

(define (make-parameter/helper val conv)
  (let ((value (make-fluid))
        (conv conv))
    (begin
      (fluid-set! value (conv val))
      (lambda new-value
        (cond
         ((null? new-value) (fluid-ref value))
         ((eq? (car new-value) get-fluid-tag) value)
         ((eq? (car new-value) get-conv-tag) conv)
         ((null? (cdr new-value)) (fluid-set! value (conv (car new-value))))
         (else (error "make-parameter expects 0 or 1 arguments" new-value)))))))

(define-syntax parameterize
  (syntax-rules ()
    ((_ ((?param ?value) ...) ?body ...)
     (with-parameters* (list ?param ...)
                       (list ?value ...)
                       (lambda () ?body ...)))))

(define (with-parameters* params values thunk)
  (with-fluids* (map (lambda (p) (p get-fluid-tag)) params)
                (map (lambda (p v) ((p get-conv-tag) v)) params values)
                thunk))

\f
; arch-tag: 536b6766-9947-4ad0-a35e-2420000d8a72

[-- Attachment #3: srfi-39-test.scm --]
[-- Type: text/plain, Size: 678 bytes --]


(use-modules (srfi srfi-39))


(define (check a b a-val b-val)
  (if (not (eqv? (a) a-val)) (error "failure -- a" (a) a-val))
  (if (not (eqv? (b) b-val)) (error "failure -- b" (b) b-val)))

(define a (make-parameter 3))
(define b (make-parameter 4))

(check a b 3 4)

(parameterize ((a 2) (b 1))
  (check a b 2 1)
  (parameterize ((b 8))
    (check a b 2 8)))

(check a b 3 4)

(define c (make-parameter 2 (lambda (x) (if (< x 10) x 10))))
(define d (make-parameter 15 (lambda (x) (if (< x 10) x 10))))

(check c d 2 10)

(parameterize ((a 0) (b 1) (c 98) (d 9))
  (check a b 0 1)
  (check c d 10 9)
  (parameterize ((c (a)) (d (b)))
    (check a b 0 1)
    (check c d 0 1)))

[-- Attachment #4: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel

  reply	other threads:[~2004-05-06  1:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-06  1:02 srfi-39 implementation Jose A Ortega Ruiz
2004-05-06  1:24 ` Jose A Ortega Ruiz [this message]
2004-08-15 20:43   ` srfi-39 implementation -- Bug fix Marius Vollmer
2004-05-09  0:48 ` srfi-39 implementation Kevin Ryde
2004-05-09  1:09   ` Paul Jarc
2004-05-10  0:21   ` Jose A. Ortega Ruiz
2004-05-13 21:10     ` Kevin Ryde
2004-05-13 23:23       ` Jose A. Ortega Ruiz
2004-07-19 20:17         ` Marius Vollmer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040506012448.GA3174@uab.es \
    --to=jao@gnu.org \
    --cc=guile-sources@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).