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.devel Subject: Re: new function Date: Fri, 24 Sep 2021 06:41:18 +0200 Message-ID: References: <147c2fb4-75d6-7cf6-4f4a-4edd996b0a4a@gmail.com> <9c058f24-5b15-417a-17ff-10bc13fed12d@gmail.com> <8dbf9ce5-f01c-94ae-40a2-0fae688f856b@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="00000000000062f39005ccb65c7b" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13875"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Jean-Paul Roy , guile-devel To: Taylan Kammer , Matt Wette , Maxime Devos Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Fri Sep 24 06:42:16 2021 Return-path: Envelope-to: guile-devel@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 1mTd2M-0003Na-A3 for guile-devel@m.gmane-mx.org; Fri, 24 Sep 2021 06:42:14 +0200 Original-Received: from localhost ([::1]:46154 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mTd2K-0007IT-CA for guile-devel@m.gmane-mx.org; Fri, 24 Sep 2021 00:42:12 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42898) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mTd1h-0006vV-BQ for guile-devel@gnu.org; Fri, 24 Sep 2021 00:41:33 -0400 Original-Received: from mail-il1-x132.google.com ([2607:f8b0:4864:20::132]:38465) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mTd1e-0002H1-RW for guile-devel@gnu.org; Fri, 24 Sep 2021 00:41:33 -0400 Original-Received: by mail-il1-x132.google.com with SMTP id q14so9006408ils.5 for ; Thu, 23 Sep 2021 21:41:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=17K6al4HipVYFbFMjz0U0nJEM5S8DPMSjlXUkq14/nI=; b=ixGOVVPbS/iCsoFEoI1z/EiAeM5z8UgDvyfgpfNuG5nMn2pI+tOwZeqbEcDbJCiuVj lsU+Zsf06UX5F+CT3TFD/S0ycs7ckpirsnkPn0zCm6GSyXSzWGuloz5VVLkXUzNF4RA0 iktlqa65M39Kx4cgHF+JiaOzTMhNkhiIYIlPBGuJ8S/N4Lgf5iKJwA31hpidJKXhtsCQ xdZajyBszNcmzhO2vx2zajueYR9B3KLcAWoGjhhkNVYXvy7moFVaJpMllyntkEDwCM3i whWh4Y1RXrHWS5Rol3/4HgTQa2on1zDrbGT1HZ8MugbFQK5HaUNjQm5WE4JIKpJnOfcu NSRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=17K6al4HipVYFbFMjz0U0nJEM5S8DPMSjlXUkq14/nI=; b=VWJtt+3Qr6PL4abe1CZ9x8dkj28p7KsZX9p5c7tHX9h1PFoDf1ryk+hcX1ph4vv5VE ERr8hERJ5yfz/9L2GcJnejikAMAqRegovvlHkZVQQlqgfu4BWzgQOL3s12RNRxW91d6E KbeRSvUwwsEqJLEZbB2g71Iq0KFEznmyhoL1cF7rDwiyr/K5O1cD5YrwWgt78FmrUxpc 2IqsL7rAyRxrlkR43qlCqprjYW6S5VIxhQXLU6aInQQgIasnZwHRvGWc4cMhC+9LTQ/M 5LDV0ZVaz6VMZxc4RMWWy0INxgCLCCVLIVbxYBDVOewoINw2MBzp8mj1KZBtlIaYA7do 47Cw== X-Gm-Message-State: AOAM532R8togDxHInUpUK6jUVA9vC27EXzUsCKDFnv+Kzc7EBpd685NV sfbGqIU9ZdCUluAXyYohCXBNVGa8Lq+PpQVSYLw= X-Google-Smtp-Source: ABdhPJyUfykFmRUKrJuRmjv/WzO4YBoe1h3dxx1NRefE2G8NTwKQtKT3IQXQNUDJiai91bpQ3ANvh8eB9UpcWW41QDE= X-Received: by 2002:a05:6e02:156a:: with SMTP id k10mr6787436ilu.317.1632458489654; Thu, 23 Sep 2021 21:41:29 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::132; envelope-from=damien.mattei@gmail.com; helo=mail-il1-x132.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_FONT_LOW_CONTRAST=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-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.io gmane.lisp.guile.devel:20886 Archived-At: --00000000000062f39005ccb65c7b Content-Type: text/plain; charset="UTF-8" so i drop off the idea of a single assignment operator, there will be 2 ,<- for single assignment when the variable has been previously defined (at upper level), and another <+ that add a variable in the environment using define. Other possibility is to have a single assignment operator <- and to declare the variables not previously defined at upper level. All this was already done in Scheme+ : here is what looks like the module definition : (define-module (Scheme+Guile) #:export (def $bracket-apply$ <- -> <+ declare $ condx <> ) ) (include "def.scm") (include "Scheme+Guile/array-square-brackets.scm") (include "Scheme+Guile/assignment.scm") (include "declare.scm") (include "condx.scm") (include "block.scm") (include "not-equal.scm") and so, the code for the 2 assignment operator : *Preview:* (define (subset-sum-guile L t) {ls <+ (length L)} {dyn <+ dyna[ls t]} {cpt <- {cpt + 1}} ;; cpt has been already defined at toplevel ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: no solution (condx [{dyn <> 0} (one? dyn)] [(null? L) {dyna[ls t] <- 2} #f] ;; return #f [exec {c <+ (first L)}] ;; c is the solution [{c = t} {dyna[ls t] <- 1} #t] ;; return #t [exec {R <+ (rest L)}] ;; continue searching a solution in the rest [{c > t} {s <+ (subset-sum-guile R t)} {dyna[ls t] <- (one-two s)} s] ;; return boolean value ;; else : c < t at this point ;; c is part of a solution OR not part of a solution [else {s <+ {(subset-sum-guile R {t - c}) or (subset-sum-guile R t)}} {dyna[ls t] <- (one-two s)} s])) ;; return boolean value and the code which use a single operator and a *declare* (it is a define unspecified): *Preview:* (define (subset-sum-guile-dec L t) (declare ls dyn c R s) {ls <- (length L)} {dyn <- dyna[ls t]} {cpt <- {cpt + 1}} ;; cpt has been already defined at toplevel ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: no solution (condx [{dyn <> 0} (one? dyn)] [(null? L) {dyna[ls t] <- 2} #f] ;; return #f [exec {c <- (first L)}] ;; c is the solution [{c = t} {dyna[ls t] <- 1} #t] ;; return #t [exec {R <- (rest L)}] ;; continue searching a solution in the rest [{c > t} {s <- (subset-sum-guile-dec R t)} {dyna[ls t] <- (one-two s)} s] ;; return boolean value ;; else : c < t at this point ;; c is part of a solution OR not part of a solution [else {s <- {(subset-sum-guile-dec R {t - c}) or (subset-sum-guile-dec R t)}} {dyna[ls t] <- (one-two s)} s])) ;; return boolean value the two syntax can be mixed but i do not think it is a good idea to have 2 coding style in the same project, also the position of 'declare' or '<+' is depending of the possibility of the used Scheme implementation to have define placed everywhere more or less, in the worse case the 'declare' or '<+' have to placed at top of the body of the procedure. The way python declare them, note that i have no regret to not be able to define variables nested in a branch of code the way python do it that can be seen from the whole procedure.I do not think it is a good idea and useful. But i hoped to be able to avoid a 'declare' at the top or somewhere but declaration even in Scheme could be necessary with some typed Scheme or when using object oriented features of some Scheme perheaps that used typed variables (for overloading function? not sure it exist in Scheme also ,even in OOP).So this is not a great problem. Damien On Thu, Sep 23, 2021 at 11:53 PM Damien Mattei wrote: > yes i'm using GNU Guile 3.0.7, > https://www.gnu.org/software/guile/docs/master/guile.html/Syntax-Transformer-Helpers.html > > i have tested a lot , even define-once again and i choose to use to > assignment operators and portable code because the non-portable function do > not bring more, finally it was not a bad idea to ask for a new function > because we can do it with the actual toolbox... > Damien > > On Thu, Sep 23, 2021 at 10:48 PM Taylan Kammer > wrote: > >> Responding to myself: >> >> On 23.09.2021 22:27, Taylan Kammer wrote: >> >> > I can't seem to find syntax-local-binding in Guile 2.2 or 3.0. Did you >> > have to import some special module, or are you using another version? >> >> Worked when I imported (system syntax internal). >> >> > Either way, I suspect that the following will not work with your macro: >> > >> > (let () >> > (let () >> > (<$ x 1)) >> > (display x) >> > (newline)) >> >> Indeed it doesn't work, though for a different reason: >> >> While compiling expression: >> Syntax error: >> unknown file:43:8: body should end with an expression in form (let () >> (<$ x 1)) >> >> That's because indeed the inner let expands into: >> >> (let () >> (define x 1)) >> >> And there has to be at least one expression after the define. So I tried: >> >> (let () >> (let () >> (<$ x 1) >> (newline)) >> (display x) >> (newline)) >> >> And as I expected, it says 'x' is unbound: >> >> ;;; :44:45: warning: possibly unbound variable `x' >> <$ : global scope : x >> >> ice-9/boot-9.scm:1685:16: In procedure raise-exception: >> Unbound variable: x >> >> The only way it will work is if you never use nested scopes, but that will >> lead to very strange Scheme code, and there will probably be many cases >> where you accidentally use a nested scope without immediately noticing it. >> >> Note also that definitions aren't allowed everywhere. Consider this: >> >> (let () >> (if 'whatever >> (<$ x 1) >> (<$ x 2)) >> (display x) >> (newline)) >> >> It leads to: >> >> While compiling expression: >> Syntax error: >> unknown file:49:17: definition in expression context, where definitions >> are not allowed, in form (define x 1) >> >> Because the arms of 'if' aren't allowed to be definitions. >> >> -- >> Taylan >> > --00000000000062f39005ccb65c7b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
so i drop off the idea of a single assignment operator, there= will be 2 ,<- for single assignment when the variable has been previous= ly defined (at upper level), and another <+ that add a variable in the e= nvironment using define.
Other possibility is to have a single assignment operator <- a= nd to declare the variables not previously defined at upper level.
All this was already do= ne in Scheme+ :

here is w= hat looks like
=C2=A0the module definition :
(define-module (Scheme+Guile)

=C2=A0 #:export (= def $bracket-apply$ <- -> <+ declare $ condx <> )
=C2=A0 =
=C2=A0 )

(include "def.scm")
(include "Scheme+= Guile/array-square-brackets.scm")
(include "Scheme+Guile/assig= nment.scm")
(include "declare.scm")
(include "con= dx.scm")
(include "block.scm")
(include "not-equa= l.scm")

and so, = the code for the 2 assignment operator :

Preview:

=20
(define (subset-sum-guile L =
t)

  {ls <+ (length L)}
  {dyn <+ <=
span style=3D"color:rgb(251,102,10)">dyna[ls t]}

  {cpt <- <=
span style=3D"color:rgb(255,255,255)">{cpt + 1}} ;; cpt has been already defined at top=
level
 =20
  ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: =
no solution
 =20
  (condx [{dyn <> 0} (one? dyn)]
	 [(null? L) =
{dyna[ls t] <- 2}  #f] ;; return #f
	=20
	 [exec {c <+ (first L)}]	=20
	 ;; c is the solution
	 [{c =3D t} {dyna[ls t] <- 1}  #t]  ;; return #t
	=20
	 [exec {R <+ (rest L)}]	=20
	 ;; continue searching a solution in the rest
	 [{c > t} {s &=
lt;+ (subset-sum-guile R t=
)}
	          {dyna[ls t] <- (on=
e-two s)}
		  s] ;; return boolean value
		=09
	 ;; else : c < t at this point
	 ;; c is part of a solution OR not part of a solution
	 [else {s <+ =
{(subset-sum-g=
uile R {t - c}) or (subset-sum-guile R<=
/span> t)}}
	       {dyna[ls t] <- (one-t=
wo s)}
	       s])) ;; return boolean value
=20
=
and the co= de which use a single operator and a declare (it is a define unspeci= fied):

Preview:

=20
(define (subset-sum-guile-dec L t)

  (declare ls dyn c =
R s)
 =20
  {ls <- (length L)}
  {dyn <- <=
span style=3D"color:rgb(251,102,10)">dyna[ls t]}

  {cpt <- <=
span style=3D"color:rgb(255,255,255)">{cpt + 1}} ;; cpt has been already defined at top=
level
 =20
  ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: =
no solution
 =20
  (condx [{dyn <> 0} (one? dyn)]
	 [(null? L) =
{dyna[ls t] <- 2}  #f] ;; return #f
	=20
	 [exec {c <- (first L)}]	=20
	 ;; c is the solution
	 [{c =3D t} {dyna[ls t] <- 1}  #t]  ;; return #t
	=20
	 [exec {R <- (rest L)}]	=20
	 ;; continue searching a solution in the rest
	 [{c > t} {s &=
lt;- (subset-sum-guile-dec R t)}
	          {dyna[ls t] <- (on=
e-two s)}
		  s] ;; return boolean value
		=09
	 ;; else : c < t at this point
	 ;; c is part of a solution OR not part of a solution
	 [else {s <- =
{(subset-sum-g=
uile-dec R {t=
 - c}) =
or (subset-sum-guile-dec R t)}}
	       {dyna[ls t] <- (one-t=
wo s)}
	       s])) ;; return boolean value
=20

the two syntax can be mixed but i do not think it= is a good idea to have 2 coding style in the same project, also the positi= on of 'declare' or '<+' is depending of the possibility = of the used Scheme implementation to have define placed everywhere more or = less, in the worse case the 'declare' or '<+' have to pl= aced at top of the body of the procedure.
The way python declare them, note that i ha= ve no regret to not be able to define variables nested in a branch of code = the way python do it that can be seen from the whole procedure.I do not thi= nk it is a good idea and useful. But i hoped to be able to avoid a 'dec= lare' at the top or somewhere but declaration even in Scheme could be n= ecessary with some typed Scheme or when using object oriented features of s= ome Scheme perheaps that used typed variables (for overloading function? no= t sure it exist in Scheme also ,even in OOP).So this is not a great problem= .

Damien

On Thu, Sep 23, 2021 at 11:53 PM Damien Mattei <damien.mattei@gmail.com> wrote:

i have tested a lot , = even define-once again and i choose to use to assignment operators and port= able code because the non-portable function do not bring more, finally it w= as not a bad idea to ask for a new function because we can do it with the a= ctual toolbox...
Damien
=
On Thu= , Sep 23, 2021 at 10:48 PM Taylan Kammer <taylan.kammer@gmail.com> wrote:
Responding to myself:=

On 23.09.2021 22:27, Taylan Kammer wrote:

> I can't seem to find syntax-local-binding in Guile 2.2 or 3.0.=C2= =A0 Did you
> have to import some special module, or are you using another version?<= br>
Worked when I imported (system syntax internal).

> Either way, I suspect that the following will not work with your macro= :
>
>=C2=A0 =C2=A0(let ()
>=C2=A0 =C2=A0 =C2=A0(let ()
>=C2=A0 =C2=A0 =C2=A0 =C2=A0(<$ x 1))
>=C2=A0 =C2=A0 =C2=A0(display x)
>=C2=A0 =C2=A0 =C2=A0(newline))

Indeed it doesn't work, though for a different reason:

=C2=A0 While compiling expression:
=C2=A0 Syntax error:
=C2=A0 unknown file:43:8: body should end with an expression in form (let (= ) (<$ x 1))

That's because indeed the inner let expands into:

=C2=A0 (let ()
=C2=A0 =C2=A0 (define x 1))

And there has to be at least one expression after the define.=C2=A0 So I tr= ied:

=C2=A0 (let ()
=C2=A0 =C2=A0 (let ()
=C2=A0 =C2=A0 =C2=A0 (<$ x 1)
=C2=A0 =C2=A0 =C2=A0 (newline))
=C2=A0 =C2=A0 (display x)
=C2=A0 =C2=A0 (newline))

And as I expected, it says 'x' is unbound:

=C2=A0 ;;; <stdin>:44:45: warning: possibly unbound variable `x'<= br> =C2=A0 <$ : global scope : x

=C2=A0 ice-9/boot-9.scm:1685:16: In procedure raise-exception:
=C2=A0 Unbound variable: x

The only way it will work is if you never use nested scopes, but that will<= br> lead to very strange Scheme code, and there will probably be many cases
where you accidentally use a nested scope without immediately noticing it.<= br>
Note also that definitions aren't allowed everywhere.=C2=A0 Consider th= is:

=C2=A0 (let ()
=C2=A0 =C2=A0 (if 'whatever
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (<$ x 1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (<$ x 2))
=C2=A0 =C2=A0 (display x)
=C2=A0 =C2=A0 (newline))

It leads to:

=C2=A0 While compiling expression:
=C2=A0 Syntax error:
=C2=A0 unknown file:49:17: definition in expression context, where definiti= ons are not allowed, in form (define x 1)

Because the arms of 'if' aren't allowed to be definitions.

--
Taylan
--00000000000062f39005ccb65c7b--