unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* srfi-39 implementation
@ 2004-05-06  1:02 Jose A Ortega Ruiz
  2004-05-06  1:24 ` srfi-39 implementation -- Bug fix Jose A Ortega Ruiz
  2004-05-09  0:48 ` srfi-39 implementation Kevin Ryde
  0 siblings, 2 replies; 9+ messages in thread
From: Jose A Ortega Ruiz @ 2004-05-06  1:02 UTC (permalink / raw)
  Cc: guile-sources

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

Hi.

Please find attached an implementation of SRFI-39 (parameter objects)
for Guile. Following hints by dsmith and rlb in #guile, i've implemented
it from scratch using Guile's fluids, which are already very close to
parameters (unlike SRFI-39's reference implementation, this is thus a
thread-safe implementation).

It's nothing fancy (we already have fluids after all!), but including
it would make Guile more buzzword-compliant ;-). So, I'd be glad to
transfer the copyright to the FSF (if needed; it's barely 30 loc without
comments) in case you deem convenient including it in future releases.

Thanks for your time.

Regards,

jao


[-- Attachment #2: srfi-39.scm --]
[-- Type: text/plain, Size: 3924 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 (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)
         ((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) values thunk))

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

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

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* srfi-39 implementation -- Bug fix
  2004-05-06  1:02 srfi-39 implementation Jose A Ortega Ruiz
@ 2004-05-06  1:24 ` Jose A Ortega Ruiz
  2004-08-15 20:43   ` Marius Vollmer
  2004-05-09  0:48 ` srfi-39 implementation Kevin Ryde
  1 sibling, 1 reply; 9+ messages in thread
From: Jose A Ortega Ruiz @ 2004-05-06  1:24 UTC (permalink / raw)
  Cc: guile-sources

[-- 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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: srfi-39 implementation
  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-05-09  0:48 ` Kevin Ryde
  2004-05-09  1:09   ` Paul Jarc
  2004-05-10  0:21   ` Jose A. Ortega Ruiz
  1 sibling, 2 replies; 9+ messages in thread
From: Kevin Ryde @ 2004-05-09  0:48 UTC (permalink / raw)
  Cc: guile-devel

Jose A Ortega Ruiz <jao@gnu.org> writes:
>
> I'd be glad to transfer the copyright to the FSF (if needed; it's
> barely 30 loc without comments)

Paperwork will be required if adopted.

> together with a little test program.

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

>   #: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 :).

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

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

make-symbol perhaps.

> (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?

>          ((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.)

> (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.


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: srfi-39 implementation
  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
  1 sibling, 0 replies; 9+ messages in thread
From: Paul Jarc @ 2004-05-09  1:09 UTC (permalink / raw)
  Cc: guile-devel

Kevin Ryde <user42@zip.com.au> wrote:
> Jose A Ortega Ruiz <jao@gnu.org> writes:
>> (define get-fluid-tag (lambda () 'get-fluid)) ;; arbitrary unique (as per eq?) value
>
> make-symbol perhaps.

Or (list 0), for minimal memory use (I think).


paul


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: srfi-39 implementation
  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
  1 sibling, 1 reply; 9+ messages in thread
From: Jose A. Ortega Ruiz @ 2004-05-10  0:21 UTC (permalink / raw)


[-- 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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: srfi-39 implementation
  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
  0 siblings, 1 reply; 9+ messages in thread
From: Kevin Ryde @ 2004-05-13 21:10 UTC (permalink / raw)
  Cc: guile-devel

"Jose A. Ortega Ruiz" <jao@gnu.org> writes:
>
> 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,

Ok.  Another possibility I guess (for both styles) would be a single
call or lookup giving back both fluid and converter.  Doesn't quite
suit the way with-parameters* does its `map's though.

> (define-module (srfi srfi-39)
>   #:export (make-parameter)
>   #:export-syntax (parameterize)
>
>   ;; procedure not in srfi-39.
>   #:export (with-parameters*))

One #:export (with whatever comment) is enough I expect.

> (with-test-prefix
>  "make-parameter"

In emacs

	(put 'with-test-prefix 'scheme-indent-function 1)

noted in test-suite/lib.scm gives good indentation.  I've also been
using

	(put 'expect-fail       'scheme-indent-function 1)
	(put 'pass-if           'scheme-indent-function 1)
	(put 'pass-if-exception 'scheme-indent-function 2)

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

A leftover from something?  conv seems unused, and "(x > 10)" looks
pretty doubtful.

Otherwise looks good to me.


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: srfi-39 implementation
  2004-05-13 21:10     ` Kevin Ryde
@ 2004-05-13 23:23       ` Jose A. Ortega Ruiz
  2004-07-19 20:17         ` Marius Vollmer
  0 siblings, 1 reply; 9+ messages in thread
From: Jose A. Ortega Ruiz @ 2004-05-13 23:23 UTC (permalink / raw)


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


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

> "Jose A. Ortega Ruiz" <jao@gnu.org> writes:
>>
>> 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,
>
> Ok.  Another possibility I guess (for both styles) would be a single
> call or lookup giving back both fluid and converter.  Doesn't quite
> suit the way with-parameters* does its `map's though.
>

Yes. I cannot think at the moment of a simple way of using a single
call---all that comes to mind involves call-with-values, fold or the
like, and makes the code more complex.

>> (define-module (srfi srfi-39)
>>   #:export (make-parameter)
>>   #:export-syntax (parameterize)
>>
>>   ;; procedure not in srfi-39.
>>   #:export (with-parameters*))
>
> One #:export (with whatever comment) is enough I expect.
>

OK.

>
> In emacs
>
> 	(put 'with-test-prefix 'scheme-indent-function 1)
> [...]

Ah. Thanks for the tip. I had overlooked it.

>
>>  (let ((conv (lambda (x) (if (x > 10) 10 x))))
>>    (pass-if "converter 1" (let ((a (make-parameter 32))) (eqv? (a) 32)))
>
> A leftover from something?  conv seems unused, and "(x > 10)" looks
> pretty doubtful.
>

Ooops. An error. 'conv', or rather, a correct version of it, was meant
to be used in the tests within the 'let'. I've corrected it.

> Otherwise looks good to me.

Great. Please find attached a new version of both files that,
hopefully, addresses the above issues (except the single lookup).

Best regards,
jao
-- 
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare. - Blair Houghton.


[-- Attachment #2: srfi-39.scm --]
[-- Type: application/octet-stream, Size: 4052 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
            with-parameters*) ;; procedure not in srfi-39.
  #:export-syntax (parameterize))

(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: srfi-39.test --]
[-- Type: application/octet-stream, Size: 3707 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 conv))) (eqv? (a) 10)))
    (pass-if "converter 2"
      (let ((a (make-parameter 3 conv))) (eqv? (a) 3)))
    (pass-if "converter 3"
      (let ((a (make-parameter 3 conv))) (a 89) (eqv? (a) 10)))))

(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: Type: text/plain, Size: 142 bytes --]

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: srfi-39 implementation
  2004-05-13 23:23       ` Jose A. Ortega Ruiz
@ 2004-07-19 20:17         ` Marius Vollmer
  0 siblings, 0 replies; 9+ messages in thread
From: Marius Vollmer @ 2004-07-19 20:17 UTC (permalink / raw)
  Cc: guile-devel

"Jose A. Ortega Ruiz" <jao@gnu.org> writes:

> [...] Please find attached a new version of both files that,
> hopefully, addresses the above issues (except the single lookup).

Excellent, thanks!  As promised on IRC, I will include this in Guile.
I will contact you in private mail about the paper work.

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: srfi-39 implementation -- Bug fix
  2004-05-06  1:24 ` srfi-39 implementation -- Bug fix Jose A Ortega Ruiz
@ 2004-08-15 20:43   ` Marius Vollmer
  0 siblings, 0 replies; 9+ messages in thread
From: Marius Vollmer @ 2004-08-15 20:43 UTC (permalink / raw)
  Cc: guile-sources, guile-devel

Jose A Ortega Ruiz <jao@gnu.org> writes:

> I'm attaching a (hopefully) corrected
> version, together with a little test program.

Excellent, thanks.  I have installed this in both the 1.6 and 1.7
series.

To make this perfect, could you write documentation for this that fits
into doc/ref/guile.info?

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2004-08-15 20:43 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2004-05-13 21:10     ` Kevin Ryde
2004-05-13 23:23       ` Jose A. Ortega Ruiz
2004-07-19 20:17         ` Marius Vollmer

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).