From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Maxime Devos Newsgroups: gmane.lisp.guile.devel Subject: Re: new function Date: Thu, 23 Sep 2021 20:00:15 +0200 Message-ID: <702b197719f098262a424608fb134f218af635a9.camel@telenet.be> References: <147c2fb4-75d6-7cf6-4f4a-4edd996b0a4a@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg="pgp-sha512"; protocol="application/pgp-signature"; boundary="=-905kWKf/c1Ul1bT8sxQJ" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13080"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Evolution 3.34.2 Cc: Jean-Paul Roy , guile-devel To: Damien Mattei , Taylan Kammer Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Thu Sep 23 20:01:23 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 1mTT29-0003Cz-U5 for guile-devel@m.gmane-mx.org; Thu, 23 Sep 2021 20:01:22 +0200 Original-Received: from localhost ([::1]:32986 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mTT28-0000sP-9m for guile-devel@m.gmane-mx.org; Thu, 23 Sep 2021 14:01:20 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53046) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mTT1K-0000pz-1u for guile-devel@gnu.org; Thu, 23 Sep 2021 14:00:32 -0400 Original-Received: from laurent.telenet-ops.be ([2a02:1800:110:4::f00:19]:39542) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mTT1G-0005XY-Gf for guile-devel@gnu.org; Thu, 23 Sep 2021 14:00:29 -0400 Original-Received: from ptr-bvsjgyjmffd7q9timvx.18120a2.ip6.access.telenet.be ([IPv6:2a02:1811:8c09:9d00:aaf1:9810:a0b8:a55d]) by laurent.telenet-ops.be with bizsmtp id xW0M250010mfAB401W0MQk; Thu, 23 Sep 2021 20:00:21 +0200 In-Reply-To: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telenet.be; s=r21; t=1632420021; bh=9C5hLLKSk0EEqDwpOThPyyPMIGfecP2y4lTsEAPLWoY=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=JsBc5F3wU0MiOCGi2OJqeRaz1ZciCJa9Ojbew3VbBeTkpCim3LrkYL4meingVLDUm jbdsVL3ifC7NJCpaewqyJ0A4ru5H2kLP/WLHz3xAmxy00m1tN3slppV0XgX0xV/tI9 ySk0bgTpBz6kTWB5b5Jv3ovBtFnyD+bzPn++39EfnNGlkvW3Ac7BHIOv5q0XC3mAvX cErKQFSYRLb64ATa1lNZitqV2Ndjo+qPiCJ9w4/NYzKjlg0lq7MEgrzbh7/VenZUXc d7/ZnTXaFcm8uRYL9vYDA3rogPpBhfyAjj1fPfVC3FaRo9x0ZnPcEY0EeqrYczlK1/ xK0n1EXOV7JRQ== Received-SPF: pass client-ip=2a02:1800:110:4::f00:19; envelope-from=maximedevos@telenet.be; helo=laurent.telenet-ops.be X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, 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:20880 Archived-At: --=-905kWKf/c1Ul1bT8sxQJ Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Damien Mattei schreef op do 23-09-2021 om 19:27 [+0200]: > yes i know parsing the whole code is the only portable solution, but it i= s slow,even on a few dozen of lines the slowing is visible ,so i can even t= hink of that on one thousand lines... >=20 > I finally succeed in Guile with simple piece of code to make my example r= un with a single assignment operator <- , here i define for variable the a= ssignment operator <$ , <- is working with arrays too: >=20 > 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 >=20 > it allows this Scheme+ code to run with a single assignment operator (not= e in some case the operator is also a definition of variable,but it is invi= sible for the programmer, it has the duality of define and set!): >=20 > Preview: >=20 > (define (subset-sum-guile L t) >=20 > {ls <- (length L)} > {dyn <- dyna[ls t]} >=20 > ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: no soluti= on > =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 <- (subset-sum-guile R t)} > {dyna[ls t] <- (one-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-guile R {t - c}) or (subset-sum-guile R t)}} > {dyna[ls t] <- (one-two s)} > s])) ;; return boolean value >=20 > =20 >=20 > some people were sceptic about the possibility to make it, but it works, = i do not say it is portable code. >=20 > 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 > =20 > <$ : global scope : c > <$ : global scope : R > <$ : global scope : s > <$ : global scope : ls > <$ : global scope : dyn > =20 > <$ : global scope : c > .... hundreds of lines..... > #t >=20 > all variable are global, No, they are local, even though syntax-local-binding returns 'global'. 'syntax-local-binding' doesn't know we will be defining a local variable with the same name later, so it says 'global' instead of 'lexical' or 'displaced-lexical'. There is no such thing as =E2=80=98global to the body of the function=E2=80= =99, what you are descrbing is local variables. The macro <$ you have defined won't work for the "hello world" example I se= nt you: (define (#{hello/won't-work}# language) (cond ((equal? language "dutch") (<$ message "Hallo wereld")) ((equal? language "english") (<$ message "Hello world"))) (display message) (newline)) While compiling expression: Syntax error: unknown file:70:9: definition in expression context, where definitions are = not allowed, in form (define message "Hallo wereld") The following does, however: (define (hello language) (<$ message #f) (cond ((equal? language "dutch") (<$ message "Hallo wereld")) ((equal? language "english") (<$ message "Hello world"))) (display message) (newline)) Possibly this limitation of <$ is acceptable to you though. > but they are just global to the body of the function,not at toplevel,so t= here is no risk of breaking the code logic it is just that if we want to se= e lexical scope we need a more nested example,it is strange because i thoug= ht that the condx macro creates nestled code for each conditional clauses..= . >=20 > to see the lexical scope we can use this example: > scheme@(guile-user)>=20 > (condx [exec {k <- 1}] > [{k =3D 1} {k <- {k + 1}} {k + 1}] > [else 'never]) > <$ : global scope : k > <$ : lexical scope : k > $3 =3D 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)>=20 > (condx [exec {k <- 1}] > [{k =3D 1} {k <- {k + 1}} {k + 1}] > [else 'never]) > <$ : global scope : k > <$ : lexical scope : k > $4 =3D 3 > scheme@(guile-user)> k > $5 =3D 0 >=20 > :-( > probably beause syntax-local-binding only works in the current lexical e= nvironment ? > https://www.gnu.org/software/guile/docs/master/guile.html/Syntax-Transfor= mer-Helpers.html > but not at toplevel ??? > Scheme Procedure: syntax-local-binding id [#:resolve-syntax-parameters?= =3D#t] > Resolve the identifer id, a syntax object, within the current lexical env= ironment >=20 > for this reason i still searching a solution that would be a mix of synta= x-local-binding and Module System Reflection . Use the second return value of syntax-local-binding. Or just use set! instead of <$ and <- to modify global variables, and use <- and <$ for local variables only. Greetings, Maxime. --=-905kWKf/c1Ul1bT8sxQJ Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- iI0EABYKADUWIQTB8z7iDFKP233XAR9J4+4iGRcl7gUCYUzArxccbWF4aW1lZGV2 b3NAdGVsZW5ldC5iZQAKCRBJ4+4iGRcl7hvuAP9TB7iVO1cJbLzoSrhWk2/iqLOg AUSqyW2pNVcxEeCcSwD/WKKDzW/aG84PuabTd2yDMxayvEV5xQox/FUe0TxYLAA= =w7iB -----END PGP SIGNATURE----- --=-905kWKf/c1Ul1bT8sxQJ--