public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* duplicate version reference - was #<syntax (scheme base) in #61>
@ 2023-11-03 17:25 Damien Mattei
  2023-11-03 17:35 ` Per Bothner
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Mattei @ 2023-11-03 17:25 UTC (permalink / raw)
  To: kawa mailing list

i just notice this works:
kawa -Dkawa.import.path="."
#|kawa:1|# (import (rename (scheme base) (+ orig+)))
#|kawa:2|# (define + *)
#|kawa:3|# (+ 2 3)
6
#|kawa:4|# (orig+ 2 3)
5

but if i put the statement in a macro, it no more works:
kawa -Dkawa.import.path="."
#|kawa:1|# (define-syntax define-overload-existing-operator
#|.....2|#
#|.....3|#   (syntax-rules ()
#|.....4|#
#|.....5|#     ((_ proc) (import (rename (scheme base) (proc orig-proc))))))
#|kawa:6|# (define-overload-existing-operator +)
/dev/tty:6:1: duplicate version reference - was #<syntax (scheme base) in #61>
/dev/tty:6:1: unknown library (#<syntax#3 rename in #61>)

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

* Re: duplicate version reference - was #<syntax (scheme base) in #61>
  2023-11-03 17:25 duplicate version reference - was #<syntax (scheme base) in #61> Damien Mattei
@ 2023-11-03 17:35 ` Per Bothner
  2023-11-03 17:57   ` Damien Mattei
  0 siblings, 1 reply; 6+ messages in thread
From: Per Bothner @ 2023-11-03 17:35 UTC (permalink / raw)
  To: kawa



On 11/3/23 10:25, Damien Mattei via Kawa wrote:
> but if i put the statement in a macro, it no more works:
> kawa -Dkawa.import.path="."
> #|kawa:1|# (define-syntax define-overload-existing-operator
> #|.....2|#
> #|.....3|#   (syntax-rules ()
> #|.....4|#
> #|.....5|#     ((_ proc) (import (rename (scheme base) (proc orig-proc))))))
> #|kawa:6|# (define-overload-existing-operator +)
> /dev/tty:6:1: duplicate version reference - was #<syntax (scheme base) in #61>
> /dev/tty:6:1: unknown library (#<syntax#3 rename in #61>)

To start with, orig-proc is undefined. proc and orig-proc are completely unrelated identifiers.

Generating a new identifier orig-proc based on an existing identifier proc
is possible (using syntax->datum and datum->syntax) but it's tricky.

Using import in a macro expansion may have some issues or bugs
I haven't really thought about, at least not in a long time.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

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

* Re: duplicate version reference - was #<syntax (scheme base) in #61>
  2023-11-03 17:35 ` Per Bothner
@ 2023-11-03 17:57   ` Damien Mattei
  2023-11-03 18:29     ` Per Bothner
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Mattei @ 2023-11-03 17:57 UTC (permalink / raw)
  To: Per Bothner; +Cc: kawa

i understand. For my overloading operator idea, do you think is it
possible in Kawa (like in guile: (define-method (+ (x <vector>) (y
<vector>)) (vector-append x y)) ),using the class and defining method
to overload some operators like + ,* ,etc ? and  even do it for basic
type like vectors? allowing the use of + instead of vector-append?

On Fri, Nov 3, 2023 at 6:35 PM Per Bothner <per@bothner.com> wrote:
>
>
>
> On 11/3/23 10:25, Damien Mattei via Kawa wrote:
> > but if i put the statement in a macro, it no more works:
> > kawa -Dkawa.import.path="."
> > #|kawa:1|# (define-syntax define-overload-existing-operator
> > #|.....2|#
> > #|.....3|#   (syntax-rules ()
> > #|.....4|#
> > #|.....5|#     ((_ proc) (import (rename (scheme base) (proc orig-proc))))))
> > #|kawa:6|# (define-overload-existing-operator +)
> > /dev/tty:6:1: duplicate version reference - was #<syntax (scheme base) in #61>
> > /dev/tty:6:1: unknown library (#<syntax#3 rename in #61>)
>
> To start with, orig-proc is undefined. proc and orig-proc are completely unrelated identifiers.
>
> Generating a new identifier orig-proc based on an existing identifier proc
> is possible (using syntax->datum and datum->syntax) but it's tricky.
>
> Using import in a macro expansion may have some issues or bugs
> I haven't really thought about, at least not in a long time.
> --
>         --Per Bothner
> per@bothner.com   http://per.bothner.com/

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

* Re: duplicate version reference - was #<syntax (scheme base) in #61>
  2023-11-03 17:57   ` Damien Mattei
@ 2023-11-03 18:29     ` Per Bothner
  2023-11-03 23:48       ` Damien Mattei
  0 siblings, 1 reply; 6+ messages in thread
From: Per Bothner @ 2023-11-03 18:29 UTC (permalink / raw)
  To: Damien Mattei; +Cc: kawa



On 11/3/23 10:57, Damien Mattei wrote:
> i understand. For my overloading operator idea, do you think is it
> possible in Kawa (like in guile: (define-method (+ (x <vector>) (y
> <vector>)) (vector-append x y)) ),using the class and defining method
> to overload some operators like + ,* ,etc ? and  even do it for basic
> type like vectors? allowing the use of + instead of vector-append?

First, I think using + for vector-append is a bad idea.
It's questionable for strings, but if + on vectors would be
better to mean vector addition - i.e. mapping + element-by-element.
(Of course my exposure to APL is influencing me here.)

Second, Kawa has some half-assed support for "multi-methods":
https://www.gnu.org/software/kawa/Generic-procedures.html
That could be enhanced.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

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

* Re: duplicate version reference - was #<syntax (scheme base) in #61>
  2023-11-03 18:29     ` Per Bothner
@ 2023-11-03 23:48       ` Damien Mattei
  2023-11-04 14:56         ` Damien Mattei
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Mattei @ 2023-11-03 23:48 UTC (permalink / raw)
  To: Per Bothner; +Cc: kawa

On Fri, Nov 3, 2023 at 7:29 PM Per Bothner <per@bothner.com> wrote:

> First, I think using + for vector-append is a bad idea.
not part to be always defined like that
> It's questionable for strings, but if + on vectors would be
> better to mean vector addition - i.e. mapping + element-by-element.
just to port from Python a test example as in python + is used to append vectors
> (Of course my exposure to APL is influencing me here.)
:-)
>
> Second, Kawa has some half-assed support for "multi-methods":
> https://www.gnu.org/software/kawa/Generic-procedures.html
> That could be enhanced.
sounds great, a fast test give good results:
#|kawa:1|# (import (rename (scheme base) (+ orig+)))
#|kawa:4|# (define + (make-procedure method: (lambda (x ::number y
::number) (orig+ x y))
#|.....5|# method: (lambda (x ::vector y ::vector) (vector-append x y))))
#|kawa:6|# (+ 2 3)
5
#|kawa:7|# (+ #(1 2) #(3 4 5))
#(1 2 3 4 5)
could be enhanced for associative n-arity operator of course, the way
i did it in overload procedures
Damien

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

* Re: duplicate version reference - was #<syntax (scheme base) in #61>
  2023-11-03 23:48       ` Damien Mattei
@ 2023-11-04 14:56         ` Damien Mattei
  0 siblings, 0 replies; 6+ messages in thread
From: Damien Mattei @ 2023-11-04 14:56 UTC (permalink / raw)
  To: Per Bothner; +Cc: kawa

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

finally i find a way to make the overload features of Scheme+ working in Kawa:
-just by putting the necessary import-rename at the toplevel or in the
used module as it does not works in macro
-and adding the renamed existing operator/procedure as an extra
parameter to the overloading definition macro:

(require test-define)

(import (rename (scheme base) (+ orig+)))

(define-overload-existing-operator + orig+)

(overload-existing-operator + vector-append (vector? vector?))


(define rv (+ #(1 2) #(3 4 5)))
(newline) (display "rv=") (display rv) (newline)

here is a running example with the updated source files in attachment:
 kawa -Dkawa.import.path=".:/Users/mattei/Library/Mobile
Documents/com~apple~CloudDocs/Scheme-PLUS-for-Kawa"

#|kawa:1|# (load "main-test-define.scm")
proc =#<procedure +>
orig-proc =#<procedure +>

rv=#(1 2 3 4 5)
#|kawa:2|# (+ 2 3)
5
#|kawa:3|# (+ 2 3 4)
9
#|kawa:4|# (+ #(1 2) #(3) #(4 5))
#(1 2 3 4 5)
you will notice that it works with + even as an n-arity operator
because operator are associative and overloaded procedure take care to
apply associativeness.

This also could be done with Kawa support for multi-method i suppose
by using my code in the defined method, this was my primary idea
before finding this little hack to adapt for Kawa my already written
Scheme+ algorithm for overloading.

Damien

On Sat, Nov 4, 2023 at 12:48 AM Damien Mattei <damien.mattei@gmail.com> wrote:
>
> On Fri, Nov 3, 2023 at 7:29 PM Per Bothner <per@bothner.com> wrote:
>
> > First, I think using + for vector-append is a bad idea.
> not part to be always defined like that
> > It's questionable for strings, but if + on vectors would be
> > better to mean vector addition - i.e. mapping + element-by-element.
> just to port from Python a test example as in python + is used to append vectors
> > (Of course my exposure to APL is influencing me here.)
> :-)
> >
> > Second, Kawa has some half-assed support for "multi-methods":
> > https://www.gnu.org/software/kawa/Generic-procedures.html
> > That could be enhanced.
> sounds great, a fast test give good results:
> #|kawa:1|# (import (rename (scheme base) (+ orig+)))
> #|kawa:4|# (define + (make-procedure method: (lambda (x ::number y
> ::number) (orig+ x y))
> #|.....5|# method: (lambda (x ::vector y ::vector) (vector-append x y))))
> #|kawa:6|# (+ 2 3)
> 5
> #|kawa:7|# (+ #(1 2) #(3 4 5))
> #(1 2 3 4 5)
> could be enhanced for associative n-arity operator of course, the way
> i did it in overload procedures
> Damien

[-- Attachment #2: main-test-define.scm --]
[-- Type: application/octet-stream, Size: 431 bytes --]

;; use: kawa -Dkawa.import.path=".:/Users/mattei/Library/Mobile Documents/com~apple~CloudDocs/Scheme-PLUS-for-Kawa"
;; (load "main-test-define.scm")



;;(require overload)
(require test-define)

(import (rename (scheme base) (+ orig+)))

(define-overload-existing-operator + orig+)

(overload-existing-operator + vector-append (vector? vector?))


(define rv (+ #(1 2) #(3 4 5)))
(newline) (display "rv=") (display rv) (newline)


[-- Attachment #3: test-define.scm --]
[-- Type: application/octet-stream, Size: 3458 bytes --]

(module-name test-define)

(require 'srfi-1)

(require 'srfi-69)

;;(require infix-operators)

(export define-overload-existing-operator
	overload-existing-operator)
	;;+)

(include "condx.scm")


(define $ovrld-ht$ (make-hash-table)) ;; for procedure and operators


(define (check-arguments pred-list args)
  (if (= (length pred-list) (length args))
      (let ((pred-arg-list (map cons pred-list args)))
	;;(andmap (lambda (p) ((car p) (cdr p)))
	;; replace andmap with every in Guile
	(every (lambda (p) ((car p) (cdr p)))
	       pred-arg-list))
      #f))



(define-syntax define-overload-existing-operator

  (syntax-rules ()

    ((_ proc orig-proc)

     (begin

       ;; (import (rename (only (kawa) proc) (proc orig-proc)))
       ;;(import (rename (only (scheme base)) (proc orig-proc)))
       ;;(import (rename (test-define) (proc orig-proc)))
       ;;(import (rename (gnu kawa) (proc orig-proc)))
       ;;(require (rename-in racket/base (proc
       	;;			        orig-proc)))

       (display "proc =") (display proc) (newline)
       (display "orig-proc =") (display orig-proc) (newline)
       
       (define qproc (quote proc)) 
       
       (define (proc . args-lst)

	 ;;(display "proc=") (display proc) (newline)
	 ;;(define ht (hash-table->alist $ovrld-ht$))
	 ;;(display ht) (newline)


	 (define proc-lst (hash-table-ref $ovrld-ht$ qproc)) ;;  example: ((number? string?) (lambda (n s) (display n) (display s) (newline)))
	 ;;(display "proc-lst=") (display proc-lst)
	 ;;(newline)
	 
	 (define (check-args-lst pred-list) ; check arguments list match predicates
	   ;;(display "pred-list=") (display pred-list) (newline)
	   ;;(display "args-lst=") (display args-lst) (newline)
	   (check-arguments pred-list args-lst))



	 (define (test-proc pred-proc-list) ; test the procedure if it matches with arguments
	   ;;(display "pred-proc-list=") (display pred-proc-list) (newline)
	   (if (check-args-lst (car pred-proc-list)) ;; check args
	       (car (cdr  pred-proc-list)) ;; return procedure
	       #f))

	 
	 (define proc-search-result (any test-proc proc-lst)) ; search for a procedure matching arguments

	 
	 ;;(display "proc-search-result=") (display proc-search-result) (newline)
	 
	 (condx (proc-search-result (apply proc-search-result args-lst))
		(exec
		 (define nb-args (length args-lst)))
		((> nb-args 2)   ;;(display ">2 args") (newline)
		 (proc (car args-lst) (apply proc (cdr args-lst))))
		(else
		 ;;(display "else") (newline)
		 (apply orig-proc args-lst))))
       
       ;;(hash-table-set! $ovrld-ht$ qproc (list (list (list number? number?) orig-proc)))
       (hash-table-set! $ovrld-ht$ qproc '())

       ;;(replace-operator! orig-proc proc)
       
       )))) 




(define-syntax overload-existing-operator
  
  (syntax-rules ()

    ((_ orig-funct funct (pred-arg1 ...))

     (overload orig-funct funct (pred-arg1 ...)))))


(define-syntax overload

  (syntax-rules ()

    ;; arguments are function to be overloaded, procedure that do the overloading, list of predicate to check the arguments

    ((_ orig-funct funct (pred-arg1 ...))

     (let* ((qorig-funct (quote orig-funct))
	    (ovrld-lst (hash-table-ref $ovrld-ht$ qorig-funct)))
       ;;(display qorig-funct) (newline)
       (hash-table-set! $ovrld-ht$ qorig-funct
			(cons (list (list pred-arg1 ...) ;; example: ((number? string?) (lambda (n s) (display n) (display s) (newline)))
				    funct)
			      ovrld-lst))))))
 	 


[-- Attachment #4: condx.scm --]
[-- Type: application/octet-stream, Size: 3038 bytes --]

;; condx: cond(itionals) with optional execution of statements before
					;

; This file is part of Scheme+

;; Copyright 2021 Damien MATTEI

;; 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 3 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, see <https://www.gnu.org/licenses/>.


; example:
;(define x 1)
;(condx ((= x 7) 'never)
;        (exec
;          (define y 3)
;          (set! x 7))
;        ((= y 1) 'definitely_not)
;        (exec
;          (set! y 10)
;          (define z 2))
;        ((= x 7) (+ x y z))
;        (else 'you_should_not_be_here))
;
; 19

(define-syntax condx
  (syntax-rules (exec else)
    ((_) '()) ;; allow no else clause
    ((_ (else e ...))
     (let () e ...))
    ((_ (exec s ...) d1 ...)
     (let () s ... (condx d1 ...)))
    ((_ (t e ...) tail ...)
     (if t
         (let () e ...)
         (condx tail ...)))))

;; (define-syntax condx
;;  (syntax-rules (exec else)
;;    ((_)
 ;;     (error 'condx "No else clause"))
;;    ((_ (else e ...))
;;     (let () e ...))
;;    ((_ (exec s ...) d1 ...)
;;     (let () s ... (condx d1 ...)))
;;    ((_ (t e ...) tail ...)
;;     (if t
;;         (let () e ...)
;;         (condx tail ...)))))

;; warning this ones behaves differently (can not remember the problem)
(define-syntax condx-begin
  (syntax-rules (exec else)
    ((_) '()) ;; allow no else clause
    ((_ (else e ...))
     (begin e ...))
    ((_ (exec s ...) d1 ...)
     (begin s ... (condx-begin d1 ...)))
    ((_ (t e ...) tail ...)
     (if t
         (begin e ...)
         (condx-begin tail ...)))))


;; (define-syntax condx-begin
;;   (syntax-rules (exec else)
;;     ((_)
;;      (error 'condx-begin "No else clause"))
;;     ((_ (else e ...))
;;      (begin e ...))
;;     ((_ (exec s ...) d1 ...)
;;      (begin s ... (condx-begin d1 ...)))
;;     ((_ (t e ...) tail ...)
;;      (if t
;;          (begin e ...)
;;          (condx-begin tail ...)))))


;; (define x 1)
;; (condx ((= x 7) 'never)
;;         (exec
;;           (define y 3)
;;           (set! x 7))
;;         ((= y 1) 'definitely_not)
;;         (exec
;;           (set! y 10)
;;           (define z 2))
;;         ((= x 7) (+ x y z))
;;         (else 'you_should_not_be_here))

;; (define y 0)
;; (define z 0)
;; (set! x 1)
;; (condx-begin ((= x 7) 'never)
;;         (exec
;;           (set! y 3)
;;           (set! x 7))
;;         ((= y 1) 'definitely_not)
;;         (exec
;;           (set! y 10)
;;           (set! z 2))
;;         ((= x 7) (+ x y z))
;;         (else 'you_should_not_be_here))


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

end of thread, other threads:[~2023-11-04 14:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-03 17:25 duplicate version reference - was #<syntax (scheme base) in #61> Damien Mattei
2023-11-03 17:35 ` Per Bothner
2023-11-03 17:57   ` Damien Mattei
2023-11-03 18:29     ` Per Bothner
2023-11-03 23:48       ` Damien Mattei
2023-11-04 14:56         ` Damien Mattei

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