From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Damien Mattei Newsgroups: gmane.lisp.guile.user Subject: Re: define anywhere Date: Fri, 11 Jun 2021 10:15:19 +0200 Message-ID: References: <28842df5-d82b-4819-b7ad-2148265f7a83@www.fastmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38185"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Jean-Paul Roy , guile-user To: =?UTF-8?Q?Linus_Bj=C3=B6rnstam?= Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Fri Jun 11 10:16:38 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lrcLG-0009lV-B0 for guile-user@m.gmane-mx.org; Fri, 11 Jun 2021 10:16:38 +0200 Original-Received: from localhost ([::1]:50838 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lrcLF-0006WZ-9X for guile-user@m.gmane-mx.org; Fri, 11 Jun 2021 04:16:37 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60118) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lrcKG-0005lx-3G for guile-user@gnu.org; Fri, 11 Jun 2021 04:15:36 -0400 Original-Received: from mail-io1-xd2e.google.com ([2607:f8b0:4864:20::d2e]:33306) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lrcKC-0007uJ-76 for guile-user@gnu.org; Fri, 11 Jun 2021 04:15:35 -0400 Original-Received: by mail-io1-xd2e.google.com with SMTP id a6so30081752ioe.0 for ; Fri, 11 Jun 2021 01:15:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=nPFwGxGOjPC6zU2hlVGQO4qwaAh5LoyLbWB9xWvuTso=; b=FGI9FfBm9ohbPNaPBevGZzBx/sZFqenaSPEGF8EVA6kg5135PULlsyenH9xwQNpocV 7QIVf7V6sQx7FXWoVsaUPOw4mUmXEq9VrVAiMYJ1yDoDphrUneOLCxYwKCG+UR/KOZno nJoIIruWlFZmEUqBtLE1kElvV+T10tJOR/VzFBq92RwnboDlAByxYZbtipcBbtv18P7U cnHk3nvXcNnqfXwqJsS1ZGTdleei2pWiJEZxY7fm+pyZ/mOJZ+zvwdN4wMQuCHT8pYbj 99L0E8mrvf6BLsfZAOna/ZUyFfl6n+oSSSgsoCz+z5in7vzZgLZ1F+beINPrLDcQtati ED2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=nPFwGxGOjPC6zU2hlVGQO4qwaAh5LoyLbWB9xWvuTso=; b=GHNkZb0Lq/eEgrbHKmEkbzp8w4XciahE0dYN90uuBcnn6wR0kJtVvSGXv3o0qt1i84 M5aTQxJlsBMSh/gOmd5bLkCx5IioLB3UgrlLa6GcJCl8dHQdQwA7+IW0yNt/WqDQADn2 Vw51WYvPPMhFZjuPpJYIf6YbQ1BxDTGHN2D3qWu9AkTv9zkpY+Uk++V3CpmnOunsFWQs +cplIspB88LXOSgf1x/6zfEs98CurQrhz6kvtQD4N1yIELHQH98L6NOL8+Lhf4wHk+6Y Z3dlPJ8s99Sgi9R4UYtmHFkVhKHk0X6UteYZxmQLgEfPpKv7z7WF4OiX3jESBGqRuSeF QvGA== X-Gm-Message-State: AOAM532iRMYSRVyKnDE9qjJf8NVSEDiL5T9XYH0Zd1SCrbhHxfh+P7fX 9P73nGB/o/FFMqF8k4PcZjViIS4GHeWRQ86QEDE= X-Google-Smtp-Source: ABdhPJw5bJjO2Hs4ge1yHoxNevdnVes6qqRtpaAqRo/6bEE8UwAbjl98avKNJlTQ7oo2ThP/tLqkwvpjmwOWUJxN7Lg= X-Received: by 2002:a05:6638:2707:: with SMTP id m7mr2714228jav.66.1623399331059; Fri, 11 Jun 2021 01:15:31 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::d2e; envelope-from=damien.mattei@gmail.com; helo=mail-io1-xd2e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17594 Archived-At: about : https://www.gnu.org/software/guile/manual/html_node/Module-System-Reflectio= n.html i do not want to implement things that are not portable to others schemes using standart macros and SRFI, for now my Scheme+ is a set of macros portable to other scheme and use only SRFI if they are implemented in a particular Scheme I use Guile and not Racket ot Bigloo because Racket do not use the standart SRFI 105 infix notation, and Bigloo or Kawa have no implementation of infix SRFI 105. Damien On Wed, Jun 9, 2021 at 12:11 PM Linus Bj=C3=B6rnstam wrote: > Hi Damien! > > The problem that define only defines a variable in the current lexical > environment. Even if a definition in an if block would work from a > syntactical standpoint, it would be pointless. > > As per r6rs, internal (define ...) are akin to letrec, but are only > allowed in definition context. Guile 3 relaxes this for function bodies, > whereas the macros I linked above relaxes this for some additional forms. > It cannot however "export" bindings to any outer environment. > > What you can do is use module reflection to create top-level variables in > the current module, but that makes everyone flinch about 105% of the time= . > > -- > Linus Bj=C3=B6rnstam > > On Wed, 9 Jun 2021, at 11:29, Damien Mattei wrote: > > hello, i'm just answering now because my ten years old Mac book pro > > definitely died sunday evening RIP > > > > i was trying to make macro that set! variable if they exist or create > > it before if the variable is not defined (tested by the Guile procedure > > 'defined?' which is not R*RS in any version) > > > > but it seems that Guile 3.0 'define' can not create variable in local > > blocks, as in Python : > > (if test > > (begin > > (define x 7) > > (foo x)) > > (else-block)) > > will not work. > > > > This behavior force the definition of all variable at the upper level > > of the function, the only advantage of guile 3.0 is perhaps (i have not > > tested it fully) that you can put define not only at beginning of > > function. > > > > This would be interesting to be able to define local variables with > > 'define',defining variable only in the block they are usefull. > > > > With those limitation in mind i defined a few more macros: > > (define-syntax <+ > > (syntax-rules () > > ((_ var expr) (define var expr)))) > > > > { x <+ 7 } is equivalent to : (<- x 7) or (define x 7) > > > > :=3D is the same as <+ > > (define-syntax :=3D > > (syntax-rules () > > ((_ var expr) (define var expr)))) > > > > { x :=3D 7 } is equivalent to : (:=3D x 7) or (define x 7) > > > > ;; definition without a value assigned > > ;; (def x) > > (define-syntax def > > (syntax-rules () > > ((_ var) (define var '())))) > > > > i added all that to my previous macro system in my Scheme+ > > (unfortunately my github account is on my dead mac,so no commit for now= ) > > > > i do not think i can do more for now, define anywhere in code could be > > a good solution to let letrec let* that in my opinion should no longer > > exist in Scheme. > > > > Here is an example on the famous sub set sum problem algorithm in two > > versions one with define 'anywhere' and the other with my previous > > macro of Scheme+: > > scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723 889 > > 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027)) > > scheme@(guile-user)> (start-ssigma-sol-approx-pack-define-anywhere > > L-init 19836) > > $1 =3D (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285 5027= ) > > scheme@(guile-user)> (apply + '(1 3 4 16 17 256 275 723 1040 1041 1284 > > 1344 1520 3000 4285 5027)) > > $2 =3D 19836 > > > > code: > > *Preview:* > > > > (define (start-ssigma-sol-approx-pack-define-anywhere L t) ;; Sub Set > > Sum problem (find solution or approximation) > > ;; { } are for infix notation as defined in SRFI 105 > > ;; <+ and :=3D are equivalent to (define var value) > > { best-sol <+ (lambda (L1 L2) > > > > {s1 <+ (apply + L1)} > > {s2 <+ (apply + L2)} > > > > (if {(abs {t - s1}) <=3D (abs {t - s2})} > > L1 > > L2)) } > > > > ;; :=3D is the same macro as <+ > > { best-sol3 :=3D (lambda (L1 L2 L3) > > > > {L22 <+ (best-sol L2 L3)} > > (best-sol L1 L22)) } > > > > > > { ssigma-sol-approx <+ (lambda (L) > > ;; def is a macro for declared but unasigned > variable, it is same > > as (define var '()) > > (def c) > > (def R) > > > > (if (null? L) > > L > > (begin {c <- (first L)} > > {R <- (rest L)} > > > > (cond [ {c =3D t} (list c) ] ;; c i= s > the solution > > [ {c > t} (best-sol (list c) > (ssigma-sol-approx R)) ] ;; c is > > to big to be a solution but could be an approximation > > ;; c < t at this point, 3 > possibilities : > > ;; c is the best solution > > ;; c is part of the solution > or his approximation > > ;; or c is not part of > solution or his approximation > > [ else (best-sol3 (list c) ;; > c is the best solution > > > > ;; c part o= f > solution or is approximation > > (cons c > (start-ssigma-sol-approx-pack-define-anywhere R {t > > - c})) ;; we have to find a solution or an approximation for t-c now > > > > > ;; c is not > part of solution or his approximation > > > (ssigma-sol-approx R))])))) } > > > > > > ;; start the function > > (ssigma-sol-approx L)) > > * > > * > > > > with other macros of Scheme+ code looked like this:* > > * > > > > * > > * > > > > *Preview:* > > > > ;; scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723 889 > > 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027)) > > ;; scheme@(guile-user)> (start-ssigma-sol-approx-pack L-init 19836) > > ;; $1 =3D (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285 > > 5027) > > > > (define (start-ssigma-sol-approx-pack L t) ;; Sub Set Sum problem (find > > solution or approximation) > > > > > > (letrec-arrow* [ best-sol =E2=86=90 (lambda (L1 L2) > > > > (let-arrow* [ s1 =E2=86=90 (apply + L1) > > s2 =E2=86=90 (apply + L2) = ] > > > > (if {(abs {t - s1}) <=3D (ab= s > {t - s2})} > > L1 > > L2))) > > > > best-sol3 =E2=86=90 (lambda (L1 L2 L3) > > > > (let [(L22 (best-sol L2 L3))] > > (best-sol L1 L22))) > > > > ssigma-sol-approx =E2=86=90 (lambda (L) > > > > (if (null? L) > > > > L > > > > (let-arrow* [ c =E2=86=90 (firs= t L) > > R =E2=86=90 (rest= L) ] > > > > (cond [ {c =3D t} (list c) ] = ;; > c is the solution > > [ {c > t} (best-sol > (list c) (ssigma-sol-approx R)) ] ;; c is > > to big to be a solution but could be an approximation > > ;; c < t at this point, > 3 possibilities : > > ;; c is the best soluti= on > > ;; c is part of the > solution or his approximation > > ;; or c is not part of > solution or his approximation > > [ else (best-sol3 (list > c) ;; c is the best solution > > > > > ;;(begin > > ;; > (display "append c=3D") (display c) (newline) > > ;; c > part of solution or is approximation > > (cons > c (start-ssigma-sol-approx-pack R {t - c}));;) ;; we > > have to find a solution or an approximation for t-c now > > > > ;; c > is not part of solution or his approximation > > > (ssigma-sol-approx R))])))) > > > > > > ] > > > > ;; start the function > > (ssigma-sol-approx L))) > > > > > > the use of define anywhere will make code more readable, infixe > > notation and macros too... > > > > A last example in Scheme+ with arrays on the dynamic solution of subset > > sum problem (just answer if a solution exist or not): > > > > *Preview:* > > > > (include "../library-FunctProg/first-and-rest.scm") > > (include "../library-FunctProg/guile/array.scm") > > (include "../library-FunctProg/pair.scm") > > (include "../library-FunctProg/number.scm") > > (include "../library-FunctProg/list.scm") > > (include "../library-FunctProg/let.scm") > > > > > > (define L-init '(1 3 4 16 17 24 45 64 197 256 275 323 540 723 889 915 > > 1040 1041 1093 1099 1111 1284 1344 1520 2027 2500 2734 3000 3267 3610 > > 4285 5027)) > > > > (define t-init 35267) > > > > (define ls (length L-init)) > > > > (define dyna (make-array 0 {ls + 1} {t-init + 1})) > > > > (define (one-two b) > > (if b 1 2)) > > > > (define cpt 0) > > > > ;; scheme@(guile-user)> (ssigma-dyna-define-anywhere L-init t-init) > > ;; $1 =3D #t > > ;; scheme@(guile-user)> cpt > > ;; $2 =3D 147801 > > (define (ssigma-dyna-define-anywhere L t) > > > > {cpt <- {cpt + 1}} ;; cpt is defined at toplevel > > > > ;;(display L) (display " ") (display t) (newline) > > > > {ls <+ (length L)} > > {dyn <+ {dyna[ls t]}} > > > > (def c) > > (def R) > > > > ;; dyna[ls t] means 0: unknown solution, 1: solution found, 2: no > solution > > (one? > > (if (not (zero? dyn)) > > > > dyn > > > > ;; set the array but return the variable > > { dyna[ls t] <- (one-two > > (if (null? L) > > #f > > (begin > > {c <- (first L)} > > {R <- (rest L)} > > (cond [ {c =3D t} #t ] ;; c is the soluti= on > > [ {c > t} > (ssigma-dyna-define-anywhere R t) ] ;; c is to big > > to be a solution but can be an approximation > > ;; c < t at this point > > ;; c is part of the solution or his > approximation > > ;; or c is not part of solution or > his approximation > > [ else {(ssigma-dyna-define-anywher= e > R {t - c}) or > > (ssigma-dyna-define-anywhere R t)} ] )))) } ))) > > > > > > Damien > > > > > > On Sun, Jun 6, 2021 at 7:32 PM Linus Bj=C3=B6rnstam > > wrote: > > > Try > > > ,expand (new-set! X 7) at the repl. > > > > > > That will however not do what you want. define does not work like > that. Define adds the binding in what is called the current lexical > environment: a define inside an if will not be visible outside that if > (even if it would have been valid syntax). > > > > > > What are you trying to write? > > > > > > -- > > > Linus Bj=C3=B6rnstam > > > > > > On Sun, 6 Jun 2021, at 17:16, Damien Mattei wrote: > > > > macros... i can not figure out how to make this one works: > > > > (define-syntax new-set! > > > > (syntax-rules () > > > > ((_ var expr) (if (defined? (quote var)) > > > > (set! var expr) > > > > (define var expr))))) > > > > > > > > scheme@(guile-user)> (new-set! x 7) > > > > ;;; :24:0: warning: possibly unbound variable `x' > > > > ;;; :24:0: warning: possibly unbound variable `x' > > > > ice-9/boot-9.scm:1669:16: In procedure raise-exception: > > > > Unbound variable: x > > > > > > > > Entering a new prompt. Type `,bt' for a backtrace or `,q' to > continue. > > > > scheme@(guile-user) [1]> ,bt > > > > In current input: > > > > 24:0 1 (_) > > > > In ice-9/boot-9.scm: > > > > 1669:16 0 (raise-exception _ #:continuable? _) > > > > > > > > any idea? > > > > > > > > Damien > > > > > > > > On Sat, Jun 5, 2021 at 5:24 PM Linus Bj=C3=B6rnstam > > > > wrote: > > > > > I implemented this hack before guile 3 got defines in function > bodies: https://hg.sr.ht/~bjoli/guile-define > > > > > > > > > > Even I guile 3 it allows a more liberal placement of define, but > it won't work for things like bodies of imported macros (like match) > > > > > -- > > > > > Linus Bj=C3=B6rnstam > > > > > > > > > > On Sat, 5 Jun 2021, at 00:27, Damien Mattei wrote: > > > > > > hello, > > > > > > i'm was considering that i want to be able to define a variable > > > > > > anywhere in > > > > > > code, the way Python do. Few scheme can do that (Bigloo i know) > > > > > > ( the list here is not exact: > > > > > > > https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_insid= e_lambda_bodies_in/ > > > > > > ) > > > > > > is it possible in guile to do it? i do not know, so could it b= e > added > > > > > > to > > > > > > the language specification for future release? > > > > > > > > > > > > regards, > > > > > > Damien > > > > > > >