[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: new function

From: Damien Mattei
Subject: Re: new function
Date: Thu, 23 Sep 2021 19:27:22 +0200

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:


(define-syntax <$
  (lambda (s)
    (syntax-case s ()
      ((_ var value)
       (case (syntax-local-binding #'var)
         ((lexical) #'(begin
			(display "<$ : lexical scope : ")
			(display (quote var))
			(set! var value)))
	 ((displaced-lexical) #'(begin
				  (display "<$ : displaced-lexical scope : ")
				  (display (quote var))
				  (set! var value)))
         ((global) #'(begin
		       (display "<$ : global scope : ")
		       (display (quote var))
		       (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!):


(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.....

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:
(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)
(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 ?
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 .


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.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]