unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: "Jose A. Ortega Ruiz" <jao@gnu.org>
Subject: Re: srfi-39 implementation
Date: Mon, 10 May 2004 02:21:06 +0200	[thread overview]
Message-ID: <87brkxf7yl.fsf@holmes.localdomain> (raw)
In-Reply-To: <87brky5su3.fsf@zip.com.au> (Kevin Ryde's message of "Sun, 09 May 2004 10:48:04 +1000")

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


Hi,

Please find attached a new version of SRFI-39.

Kevin Ryde <user42@zip.com.au> writes:

>
> Paperwork will be required if adopted.
>

Fine.

>
> Tests are vital, you'll never know if it works if you don't exercise
> all features.
>

Indeed.

>>   #:use-module (ice-9 syncase)
>
> I believe syncase is pretty big and not very fast.  define-macro or
> similar is probably a better idea.  (And with enough quasiquote might
> hide all helpers :).
>

OK. I've used define-macro this time. Nonetheless, I'm still exporting
the additional procedure with-parameters* because i've found it useful
in other contexts :).

>> (define make-parameter
>>   (case-lambda
>>     ((val) (make-parameter/helper val (lambda (x) x)))
>                                          ^^^^
> `identity' in boot-9.
>

OK. Of course.

>> (define get-fluid-tag (lambda () 'get-fluid)) ;; arbitrary unique (as per eq?) value
>
> make-symbol perhaps.
>

It seems 1.6.4 does not defines `make-symbol'. Since I haven`t
installed the cvs version yet, I've followed Paul's suggestion and
used (list 0).

>> (define (make-parameter/helper val conv)
>
> How does this stand in comparison to make-mutable-parameter in boot-9?
> Similar but not quite the same?
>

Yup. Basically the same, except that make-parameter/helper
(substituted by make-parameter in the new code) adds support for
'parameterize' and (in its current version) makes addtional error
checking.

>>          ((eq? (car new-value) get-fluid-tag) value)
>>          ((eq? (car new-value) get-conv-tag) conv)
>
> Perhaps primitive-make-property or something to associate these,
> instead of a function call.  (If I understand what it's trying to do.)
>

Although you are right in that primitive-make-property & co. can be
used, i think the case-based thing is slightly lighter and maybe even
faster, since no hashtable creation/lookup is involved. Or, at any
rate, the difference should be minimal, and the case-based version is
shorter and, IMHO, simpler (unless I'm not using the property
primitives correctly). I'm attaching a version using case
(srfi-39.scm) and a version using properties (srfi-39-alt.scm), so you
can pick the one you like best :)

>> (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)))
>
> No, you should use the test-suite/lib.scm framework.
>

OK. The attached srfi-39.test contains a test suite using this
framework.


Thanks a lot for your time and help.

Regards,
jao

-- 
"People sometimes ask me if it is a sin in the Church of Emacs to use vi.
 Using a free version of vi is not a sin; it's a penance". - Richard Stallman


[-- Attachment #2: srfi-39 --]
[-- Type: application/octet-stream, Size: 4077 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 procedure 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)
  #:export (make-parameter)
  #:export-syntax (parameterize)

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

(define fluid-prop (list 0))
(define conv-prop (list 0))

(define (make-parameter val . conv)
  (let ((value (make-fluid))
        (conv (if (null? conv)
                  identity
                  (if (null? (cdr conv))
                      (car conv)
                      (scm-error 'wrong-number-of-args
                                 "make-parameter"
                                 "Wrong number of arguments")))))
    (fluid-set! value (conv val))
    (lambda new-value
      (cond
       ((null? new-value) (fluid-ref value))
       ((null? (cdr new-value))
        (fluid-set! value (conv (car new-value))))
       ((eq? (cadr new-value) fluid-prop) value) ; used by parameterize
       ((eq? (cadr new-value) conv-prop) conv)   ; ditto
       (else (error "A parameter expects 0 or 1 arguments" new-value))))))

(define-macro (parameterize params . body)
  `(with-parameters* (list ,@(map car params))
                     (list ,@(map cadr params))
                     (lambda () ,@body)))

(define (with-parameters* params values thunk)
  (with-fluids* (map (lambda (p) (p #f fluid-prop)) params)
                (map (lambda (p v) ((p #f conv-prop) v))
                     params values)
                thunk))

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

[-- Attachment #3: test suite --]
[-- Type: application/octet-stream, Size: 3967 bytes --]

;; srfi-39.test -- Test suite for SRFI-39's functions. -*- scheme -*-

;; Copyright (C) 2004 Free Software Foundation, Inc.
;; Author: Jose A Ortega Ruiz <jao@gnu.org>
;; Date: Mon May 10, 2004 00:19

;;
;; 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 of the License, 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 program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


(use-modules (srfi srfi-39)
             (test-suite lib))

(with-test-prefix
 "make-parameter"

 (pass-if-exception "too few args" exception:wrong-num-args (make-parameter))

 (pass-if-exception "too many args" exception:wrong-num-args
                    (make-parameter 'a 'b 'c))

 (pass-if-exception "wrong converter type" (cons 'misc-error
                                                 "^Wrong type to apply:.*")
                    (make-parameter 'a 'b))

 (pass-if-exception "wrong converter type" exception:wrong-num-args
                     (make-parameter 'a (lambda (a x) a)))

 (pass-if "numerical value"
          (let ((a (make-parameter 3))) (eqv? (a) 3)))

 (pass-if "string value"
          (let ((b (make-parameter "foo"))) (string=? (b) "foo")))

 (pass-if "change value 1"
          (let ((a (make-parameter 3)))
            (a 34)
            (eqv? (a) 34)))

 (pass-if "change value 2"
          (let ((a (make-parameter 23)))
            (a "foo")
            (string=? (a) "foo")))


 (let ((conv (lambda (x) (if (x > 10) 10 x))))
   (pass-if "converter 1" (let ((a (make-parameter 32))) (eqv? (a) 32)))
   (pass-if "converter 2" (let ((a (make-parameter 3))) (eqv? (a) 3)))
   (pass-if "converter 3" (let ((a (make-parameter 3))) (a 89) (eqv? (a) 89)))))

(with-test-prefix
 "parameterize"

 (let* ((a (make-parameter 10))
        (b (make-parameter 20))
        (c (make-parameter 2 (lambda (x) (if (< x 10) x 10))))
        (d (make-parameter 1 (lambda (x) (if (< x 10) x 10))))
        (test-values (lambda (x y z w)
                       (and (eqv? (a) x)
                            (eqv? (b) y)
                            (eqv? (c) z)
                            (eqv? (d) w)))))

   (pass-if "simple"
            (and
             (test-values 10 20 2 1)
             (parameterize ((a 2) (b 1)) (test-values 2 1 2 1))
             (test-values 10 20 2 1)))

   (pass-if "nested"
            (and
             (test-values 10 20 2 1)
             (parameterize ((a 2) (b 1))
               (and (test-values 2 1 2 1)
                    (parameterize ((d 8)) (test-values 2 1 2 8))
                    (test-values 2 1 2 1)))
             (test-values 10 20 2 1)))

   (pass-if "simple with converter"
            (and
             (test-values 10 20 2 1)
             (parameterize ((c 43) (d 1)) (test-values 10 20 10 1))
             (test-values 10 20 2 1)
             (parameterize ((a 1) (c 100) (d 21)) (test-values 1 20 10 10))
             (test-values 10 20 2 1)))

   (pass-if "nested with converter"
            (and
             (test-values 10 20 2 1)
             (parameterize ((c 24) (b 1))
               (and (test-values 10 1 10 1)
                    (parameterize ((d 88))
                      (a 15)
                      (and
                       (test-values 15 1 10 10)
                       (parameterize ((d 3)) (test-values 15 1 10 3))
                       (test-values 15 1 10 10)))
                    (test-values 15 1 10 1)))
             (test-values 15 20 2 1)))))

[-- Attachment #4: Alternative impl using properties --]
[-- Type: application/octet-stream, Size: 1426 bytes --]

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

;;; Code:

(define-module (srfi srfi-39)
  #:export (make-parameter)
  #:export-syntax (parameterize)

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

(define fluid-prop (primitive-make-property #f))
(define conv-prop (primitive-make-property #f))

(define (make-parameter val . conv)
  (let ((value (make-fluid))
        (conv (if (null? conv) identity (car conv)))
    (let ((param (lambda new-value
                   (cond
                    ((null? new-value) (fluid-ref value))
                    ((null? (cdr new-value))
                     (fluid-set! value (conv (car new-value))))
                    (else (error "a parameter expects 0 or 1 arguments"
                                 new-value))))))
      (fluid-set! value (conv val))
      (primitive-property-set! fluid-prop param value)
      (primitive-property-set! conv-prop param conv)
      param)))

(define-macro (parameterize params . body)
  `(with-parameters* (list ,@(map car params))
                     (list ,@(map cadr params))
                     (lambda () ,@body)))

(define (with-parameters* params values thunk)
  (with-fluids* (map (lambda (p) (primitive-property-ref fluid-prop p)) params)
                (map (lambda (p v) ((primitive-property-ref conv-prop p) v))
                     params values)
                thunk))

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

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

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

  parent reply	other threads:[~2004-05-10  0:21 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 ` srfi-39 implementation -- Bug fix Jose A Ortega Ruiz
2004-08-15 20:43   ` 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 [this message]
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=87brkxf7yl.fsf@holmes.localdomain \
    --to=jao@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).