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: Thu, 23 Sep 2021 19:27:22 +0200 Message-ID: References: <147c2fb4-75d6-7cf6-4f4a-4edd996b0a4a@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000004177c405ccacf296" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38306"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Jean-Paul Roy , guile-devel To: Taylan Kammer Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Thu Sep 23 19:33:54 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 1mTSbZ-0009k6-Pn for guile-devel@m.gmane-mx.org; Thu, 23 Sep 2021 19:33:53 +0200 Original-Received: from localhost ([::1]:35988 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mTSbY-0007yo-OJ for guile-devel@m.gmane-mx.org; Thu, 23 Sep 2021 13:33:52 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46126) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mTSVa-0008JQ-EH for guile-devel@gnu.org; Thu, 23 Sep 2021 13:27:42 -0400 Original-Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]:45939) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mTSVV-0002Nx-CN for guile-devel@gnu.org; Thu, 23 Sep 2021 13:27:42 -0400 Original-Received: by mail-il1-x133.google.com with SMTP id b8so7386807ilh.12 for ; Thu, 23 Sep 2021 10:27:35 -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=DEvtos5Q/9KlmD00rK5gJMNr+T7NUH/6T08UjpgRKS8=; b=DHpWXok6o0RuAk8vBFWh5T/pwLVF22YKZ93/auGxjp7n4dxAFFKUzEcvshdBdqVoPl hxptTO3M0qBCJzJKMhaqpM+GFp0m2jvUBxd8RMaSdT/auWgSI0M5g6Kbb0UC9xR1hChq TVJOWl+AXUbhRFXenyYjDdd3U/D+IJ76MKw8eh+qCokdxoDEuyCEsBxuCOdb3GKeke8Z 38uZ2tF/RkGj/rW0rZM0ML5LVakzW67Iga8aZkA+XuFUheD2ARdfD7pI/tKwmLeQhnhC VtQwVfGJ97VmexUFPEt50K/nCKZAGqJU8hV8HIr1p/RLhEYb312iHjUocDro1HL1z9n2 bQ/w== 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=DEvtos5Q/9KlmD00rK5gJMNr+T7NUH/6T08UjpgRKS8=; b=Sng6oHU1Ub76JU/5X6KrrjHXNm5lnLuEoYsHxcUHogfJN3P8/YcHRx+G1RIEh/TeeX qDNshKBMJ5OlilTkaFJ0W+NRXqHH/OAVg3NU3nL9BpsqAEOHHCwKsrF4Tt+3AcgiO7Mo grybsmN0lN/yUNK3vyLl1Sw0aDJQSvxQ4JmVKSEkD9HANXUoueY0cS6fq+xZ5vYe4hIA jK9LoTkQfFHeRh5Gj17C+q0d5ttlFY0rDh181GJ4We8OeqPucqFJNkMfSz8ekAEyIsKz 1th0UwNUxRedFpcMfA47AQeaTcVXHKW3U82FQMq84TDqvdBJzTHnQzXlOfhy6yOF7GJQ bxjg== X-Gm-Message-State: AOAM532MHgdch7wqDDGR+OwnTR3RonA+szSEsAK0blgQ+7yXZD9V/QNs 5GqQcpq4PD8kS6nL4iGlOdtuunvX8Q93LyDMbBHb46JHEPQ= X-Google-Smtp-Source: ABdhPJzMStmowpSt+OUbUoAyYV6wBHf6l4wu8mBxR7LdVOSwrVNJImX+nP5hlgeT0QEEi2iBQ0DNAzu1WiWIaSaj2HE= X-Received: by 2002:a05:6e02:156a:: with SMTP id k10mr4732025ilu.317.1632418054369; Thu, 23 Sep 2021 10:27:34 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::133; envelope-from=damien.mattei@gmail.com; helo=mail-il1-x133.google.com X-Spam_score_int: -1 X-Spam_score: -0.2 X-Spam_bar: / X-Spam_report: (-0.2 / 5.0 requ) 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:20879 Archived-At: --0000000000004177c405ccacf296 Content-Type: text/plain; charset="UTF-8" yes i know parsing the whole code is the only portable solution, but it is slow,even on a few dozen of lines the slowing is visible ,so i can even think of that on one thousand lines... I finally succeed in Guile with simple piece of code to make my example run with a single assignment operator <- , here i define for variable the assignment operator <$ , <- is working with arrays too: *Preview:* (define-syntax <$ (lambda (s) (syntax-case s () ((_ var value) (case (syntax-local-binding #'var) ((lexical) #'(begin (display "<$ : lexical scope : ") (display (quote var)) (newline) (set! var value))) ((displaced-lexical) #'(begin (display "<$ : displaced-lexical scope : ") (display (quote var)) (newline) (set! var value))) ((global) #'(begin (display "<$ : global scope : ") (display (quote var)) (newline) (define var value))) (else #'(begin (display "<$ : unknow variable scope :") (display (quote var)) (error "<$ : unknow variable scope : ")))))))) it allows this Scheme+ code to run with a single assignment operator (note in some case the operator is also a definition of variable,but it is invisible for the programmer, it has the duality of define and set!): *Preview:* (define (subset-sum-guile L t) {ls <- (length L)} {dyn <- dyna[ls t]} ;; 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 some people were sceptic about the possibility to make it, but it works, i do not say it is portable code. When i run the program with debug i see that: scheme@(guile-user)> (subset-sum-guile L-init t-init) <$ : global scope : ls <$ : global scope : dyn <$ : global scope : c <$ : global scope : R <$ : global scope : s <$ : global scope : ls <$ : global scope : dyn <$ : global scope : c .... hundreds of lines..... #t all variable are global,but they are just global to the body of the function,not at toplevel,so there is no risk of breaking the code logic it is just that if we want to see lexical scope we need a more nested example,it is strange because i thought that the condx macro creates nestled code for each conditional clauses... to see the lexical scope we can use this example: scheme@(guile-user)> (condx [exec {k <- 1}] [{k = 1} {k <- {k + 1}} {k + 1}] [else 'never]) <$ : global scope : k <$ : lexical scope : k $3 = 3 here the lexical scope is well visible :-) but if k had existed at toplevel it is not modified :-( : scheme@(guile-user)> (define k 0) scheme@(guile-user)> (condx [exec {k <- 1}] [{k = 1} {k <- {k + 1}} {k + 1}] [else 'never]) <$ : global scope : k <$ : lexical scope : k $4 = 3 scheme@(guile-user)> k $5 = 0 :-( probably beause *syntax-local-binding* only works in the current lexical environment ? https://www.gnu.org/software/guile/docs/master/guile.html/Syntax-Transformer-Helpers.html but not at toplevel ??? Scheme Procedure: *syntax-local-binding* *id [#:resolve-syntax-parameters?=#t]* Resolve the identifer id, a syntax object, within the current lexical environment for this reason i still searching a solution that would be a mix of *syntax-local-binding* and Module System Reflection . Damien On Wed, Sep 22, 2021 at 8:51 PM Taylan Kammer wrote: > On 22.09.2021 11:53, Damien Mattei wrote: > > i already do it this way for internal defines ,using a recursive macro > that build a list of variable using an accumulator. It can works but macro > expansion seems slow, it was not immediate at compilation on a little > example (oh nothing more that 2 seconds) but i'm not sure it is easily > maintainable, it is at the limit what macro can do i think ,for speed > reasons. In fact i can not really understand in Guile as it is based on C > and compiled when macro expansion happens,what is the time cost... so for > all those ,perhaps not objective reason ,i prefer to avoid. > > I don't think there's any other way to achieve what you want, especially > using portable Scheme code. The lexical scoping semantics of Scheme are > a very fundamental part of the language, and cannot be worked around in > portable Scheme code without using a macro that rewrites whole bodies of > lambda expressions. > > Even using implementation-specific hacks, you won't get very far. Any > compiled Scheme implementation, and even most interpreted ones, won't > allow you to modify an outer scope's set of variable definitions from > within an inner scope. > > So if you really want to have Python's scoping semantics in Scheme, you > will probably have to write a complex 'def' macro that walks through the > body and "hoists" variable definitions to the outermost scope. > > If you're targeting R6RS implementations, you can use syntax-case to > write such a macro, but it won't be easy. > > If you're targeting R5RS or R7RS-small implementations, you will have to > rely on syntax-rules, which will probably be extremely difficult for this > kind of complex macro. > > Personally I don't even know how I would approach the problem using the > more capable syntax-case, let alone pure syntax-rules. > > -- > Taylan > --0000000000004177c405ccacf296 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
yes i know parsing the whole code is the only portable soluti= on, but it is slow,even on a few dozen of lines the slowing is visible ,so = i can even think of that on one thousand lines...

I finally succeed in Guile with simple piece of co= de to make my example run with a single assignment operator <-=C2=A0 , h= ere i define for variable the assignment operator <$ , <- is working = with arrays too:

Preview:

=20
(define-syntax <$
 =20
  (lambda (s)
   =20
    (syntax-case s ()
     =20
      ((_ var value)
      =20
       (case (syntax-local-binding #'var)
	=20
         ((lexical) #'(begin
			(display "<$ : lexical scope : ")
			(display (quote var=
))
			(newline)
			(set! var value)))
	=20
	 ((displaced-lexical) #'(begin
				  (display "<$ : displaced-lexical scope : ")
				  (display (quote var))
				  (newline)
				  (set! var value)))
	=20
         ((global) #'(begin
		       (display "<$ : global scope : ")
		       (display (quote var=
))
		       (newline)
		       (define var value)))
	=20
         (else #'(begin
		   (display "<$ : unknow variable scope :")
		   (display (quote var))
		   (error "<$ : unknow variable scope : "))))))))
=20


it allows this Scheme+ code to run= with a single assignment operator (note in some case the operator is also = a definition of variable,but it is invisible for the programmer, it has the= duality of define and set!):

Preview:

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

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

;; 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
=20

= some people were sceptic about the possibility to make it, but it works, i = do not say it is portable code.

When i run the program with debug i see that:
scheme@(guile-user)> (subset-sum= -guile =C2=A0L-init t-init)
<$ : global scope : ls
<$ : global = scope : dyn
=C2=A0
<$ : global scope : c
<$ : global scope := R
<$ : global scope : s
<$ : global scope : ls
<$ : glob= al scope : dyn
=C2=A0
<$ : global scope : c
.... hundreds of lines.....
=
#t

all variable are global,but they are jus= t global to the body of the function,not at toplevel,so there is no risk of= breaking the code logic it is just that if we want to see lexical scope we= need a more nested example,it is strange because i thought that the condx = macro creates nestled code for each conditional clauses...

to see the lexical scope we can use this= example:
schem= e@(guile-user)>
(condx [exec {k <- 1}]
=C2=A0 =C2=A0 [{k =3D 1} {k <- {k = + 1}} {k + 1}]
=C2=A0 =C2=A0 [else 'never])
<$ : global scope = : k
<$ : lexical scope : k
$3 =3D 3
<= /div>
here the lexica= l scope is well visible :-)
b= ut if k had existed at toplevel it is not modified :-( :
scheme@(guile-user)> (define k= 0)
scheme@(guile-user)>
(condx [exec {k <- 1}]
=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 [{k =3D 1} {k <- {k + 1}} {k + 1}]<= br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 [else '= never])
<$ : global scope : k
<$ : lexical scope : k
$4 =3D = 3
scheme@(guile-user)> k
$5 =3D 0

:-(
=C2=A0probably beause syntax-local-binding only works in the current lexical environment ?
but n= ot at toplevel ???
Scheme Procedure: syntax-local-binding id [#:resolve-syntax-parameters?=3D#t]=

Resolve the identifer id, a syntax object, with= in the current lexical environment

for this reason i still searching a solution that would be a mix o= f syntax-local-binding and Module System Reflection .

Damien


On Wed, Sep 22, 2021 at 8:51 PM Taylan Kammer <taylan.kammer@gmail.com> wro= te:
On 22.09.202= 1 11:53, Damien Mattei wrote:
> i already do it this way for internal defines ,using a recursive macro= that build a list of variable using an accumulator. It can works but macro= expansion seems slow, it was not immediate at compilation on a little exam= ple (oh nothing more that 2 seconds) but i'm not sure it is easily main= tainable, it is at the limit what macro can do i think ,for speed reasons. = In fact i can not really understand in Guile as it is based on C and compil= ed when macro expansion happens,what is the time cost... so for all those ,= perhaps not objective reason ,i prefer to avoid.

I don't think there's any other way to achieve what you want, espec= ially
using portable Scheme code.=C2=A0 The lexical scoping semantics of Scheme a= re
a very fundamental part of the language, and cannot be worked around in
portable Scheme code without using a macro that rewrites whole bodies of lambda expressions.

Even using implementation-specific hacks, you won't get very far.=C2=A0= Any
compiled Scheme implementation, and even most interpreted ones, won't allow you to modify an outer scope's set of variable definitions from within an inner scope.

So if you really want to have Python's scoping semantics in Scheme, you=
will probably have to write a complex 'def' macro that walks throug= h the
body and "hoists" variable definitions to the outermost scope.
If you're targeting R6RS implementations, you can use syntax-case to write such a macro, but it won't be easy.

If you're targeting R5RS or R7RS-small implementations, you will have t= o
rely on syntax-rules, which will probably be extremely difficult for this kind of complex macro.

Personally I don't even know how I would approach the problem using the=
more capable syntax-case, let alone pure syntax-rules.

--
Taylan
--0000000000004177c405ccacf296--