From: Damien Mattei <damien.mattei@gmail.com>
To: Per Bothner <per@bothner.com>
Cc: kawa@sourceware.org
Subject: Re: duplicate version reference - was #<syntax (scheme base) in #61>
Date: Sat, 4 Nov 2023 15:56:14 +0100 [thread overview]
Message-ID: <CADEOadeWy44Sn7is-bDgT_5cs+HzTS+TiU9iiLSyDc+GD9K2mQ@mail.gmail.com> (raw)
In-Reply-To: <CADEOadeeP1QYn6HXTSJY3P3xXaK_OXHm2H36_9-4XWGd27HdXQ@mail.gmail.com>
[-- 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))
prev parent reply other threads:[~2023-11-04 14:56 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-03 17:25 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 message]
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CADEOadeWy44Sn7is-bDgT_5cs+HzTS+TiU9iiLSyDc+GD9K2mQ@mail.gmail.com \
--to=damien.mattei@gmail.com \
--cc=kawa@sourceware.org \
--cc=per@bothner.com \
/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).