* equality of syntax objects
@ 2024-05-20 11:42 Damien Mattei
2024-05-20 14:53 ` Damien Mattei
0 siblings, 1 reply; 5+ messages in thread
From: Damien Mattei @ 2024-05-20 11:42 UTC (permalink / raw)
To: guile-user
hello,
i want to check the equality of two syntax objects.
i wrote this macro:
(define (check-syntax=? obj1 obj2)
(display "check-syntax=? : obj1 =") (display obj1) (newline)
(display "check-syntax=? : obj2 =") (display obj2) (newline)
(define rv
(and (identifier? obj1) ;(syntax? obj1)
(identifier? obj2) ;(syntax? obj2)
(free-identifier=? obj1 obj2)))
(display "check-syntax=? : rv =") (display rv) (newline)(newline)
rv)
What works:
if i have expt (exponentiation) , the same procedure used and checked from
to different files, i have this output in debug when doing some processing,
note i think it is important to focus on the filenames of the syntax
object also:
when computing {1 + 2 expt 3}
gives right result:
9
note that this computation requires operator precedence analysis doing on
the syntax by the process...
debug output include:
check-syntax=? : obj1 =#<syntax:unknown file:2:7 expt>
check-syntax=? : obj2 =#<syntax:operation-redux.scm:88:12 expt>
check-syntax=? : rv =#t
now i change expt by overloading itself by one of my macro:
(overload-existing-operator expt expt (number? number?))
i will not explain what is the overloading algorithm here and why i
overload expt.(in summary to allow n-arity with exponential such as {2 ** 3
** 4} or {2 expt 3 expt 4}
But note that expt is still working,even in infix guile mode:
scheme@(guile-user)> {2 expt 3}
$2 = 8
and is now an n-arity operator:
scheme@(guile-user)> {2 expt 3 expt 4}
$6 = 2417851639229258349412352
scheme@(guile-user)> (expt 2 3 4)
$7 = 2417851639229258349412352
(and with evaluation from right to left as by convention for expt ,which is
different for operator such as + or - or * and / that are evaluated from
left to right)
but that is of little importance with the current problem.
expt looks now like that:
scheme@(guile-user)> expt
$3 = #<procedure new-funct args>
new-funct is an inner function created by overloading process.
and :
scheme@(guile-user)> #'expt
$4 = #<syntax:unknown file:7:2 expt>
syntax looks like the original expt , no problem with that.
now the ouput in debug,after overloading expt:
on the calculus:
{1 + 2 expt 3}
note that this computation again requires operator precedence analysis
doing on the syntax by the process...
check-syntax=? : obj1 =#<syntax:unknown file:8:7 expt>
check-syntax=? : obj2 =#<syntax:operation-redux.scm:88:12 expt>
check-syntax=? : rv =#f
and the result 3 is wrong .(calculus has been truncated to first value 3
at rightmost as computation with expt goes from right to left but that is
of no concern with the problem again)
What i understand is even if obj1 and obj2 display the same in the output
debug,the expt definition has changed and the checking is not only about
litteral syntax but also with an hidden environment or context.
That seems normal about syntax in all scheme.
But i want ,for this point , only to manipulate "mathematic" syntax here.
I have a few simple or complex idea for solution , some not really pretty
and i ask opinion for solving this problem?
just what i need is a way to find equal the expt even if it has been
modified, only from a litteral point of view here.
regards,
damien
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: equality of syntax objects
2024-05-20 11:42 equality of syntax objects Damien Mattei
@ 2024-05-20 14:53 ` Damien Mattei
2024-05-20 21:21 ` Damien Mattei
2024-05-22 22:12 ` Maxime Devos
0 siblings, 2 replies; 5+ messages in thread
From: Damien Mattei @ 2024-05-20 14:53 UTC (permalink / raw)
To: guile-user
asked more simply:
how to know ,for two syntax objects now different but that represents two
different procedures created with the same name at two different moment,how
to know that the names are still the same?
i see two solutions:
-parse the representation string to know for example expt that expt is in
both string:
"#<syntax:unknown file:2:7 expt>"
"#<syntax:operation-redux.scm:88:12 expt>"
not really pretty but will work.I test it in Racket and kawa too...
-keep an up to date value of #'expt to use it for compare to avoid falling
on an old #'expt that match an old procedure ,i'm not sure it is clear but
i already do it last year for the same problem when parsing not syntax but
sexpr composed of real procedures (that were changing in time due to
overloading) or quoted ones (which is the worse because you have to
re-evaluate later and it again depends of context....and that not reliable,
but quoting (with syntax) is the only solution to manipulate 'and' / 'or'.
So i think the best solution is the simple and not pretty that rely on
parsing the string representation.
On Mon, May 20, 2024 at 1:42 PM Damien Mattei <damien.mattei@gmail.com>
wrote:
> hello,
>
> i want to check the equality of two syntax objects.
>
> i wrote this macro:
>
> (define (check-syntax=? obj1 obj2)
> (display "check-syntax=? : obj1 =") (display obj1) (newline)
> (display "check-syntax=? : obj2 =") (display obj2) (newline)
> (define rv
> (and (identifier? obj1) ;(syntax? obj1)
> (identifier? obj2) ;(syntax? obj2)
> (free-identifier=? obj1 obj2)))
> (display "check-syntax=? : rv =") (display rv) (newline)(newline)
> rv)
>
> What works:
>
> if i have expt (exponentiation) , the same procedure used and checked from
> to different files, i have this output in debug when doing some processing,
> note i think it is important to focus on the filenames of the syntax
> object also:
>
> when computing {1 + 2 expt 3}
> gives right result:
> 9
>
> note that this computation requires operator precedence analysis doing on
> the syntax by the process...
>
> debug output include:
>
> check-syntax=? : obj1 =#<syntax:unknown file:2:7 expt>
> check-syntax=? : obj2 =#<syntax:operation-redux.scm:88:12 expt>
> check-syntax=? : rv =#t
>
> now i change expt by overloading itself by one of my macro:
>
> (overload-existing-operator expt expt (number? number?))
>
> i will not explain what is the overloading algorithm here and why i
> overload expt.(in summary to allow n-arity with exponential such as {2 ** 3
> ** 4} or {2 expt 3 expt 4}
> But note that expt is still working,even in infix guile mode:
> scheme@(guile-user)> {2 expt 3}
> $2 = 8
> and is now an n-arity operator:
> scheme@(guile-user)> {2 expt 3 expt 4}
> $6 = 2417851639229258349412352
> scheme@(guile-user)> (expt 2 3 4)
> $7 = 2417851639229258349412352
> (and with evaluation from right to left as by convention for expt ,which
> is different for operator such as + or - or * and / that are evaluated from
> left to right)
> but that is of little importance with the current problem.
>
> expt looks now like that:
> scheme@(guile-user)> expt
> $3 = #<procedure new-funct args>
> new-funct is an inner function created by overloading process.
>
> and :
> scheme@(guile-user)> #'expt
> $4 = #<syntax:unknown file:7:2 expt>
>
> syntax looks like the original expt , no problem with that.
>
> now the ouput in debug,after overloading expt:
>
> on the calculus:
> {1 + 2 expt 3}
> note that this computation again requires operator precedence analysis
> doing on the syntax by the process...
>
> check-syntax=? : obj1 =#<syntax:unknown file:8:7 expt>
> check-syntax=? : obj2 =#<syntax:operation-redux.scm:88:12 expt>
> check-syntax=? : rv =#f
>
>
> and the result 3 is wrong .(calculus has been truncated to first value 3
> at rightmost as computation with expt goes from right to left but that is
> of no concern with the problem again)
>
> What i understand is even if obj1 and obj2 display the same in the output
> debug,the expt definition has changed and the checking is not only about
> litteral syntax but also with an hidden environment or context.
>
> That seems normal about syntax in all scheme.
>
> But i want ,for this point , only to manipulate "mathematic" syntax here.
>
> I have a few simple or complex idea for solution , some not really pretty
> and i ask opinion for solving this problem?
>
> just what i need is a way to find equal the expt even if it has been
> modified, only from a litteral point of view here.
>
> regards,
>
> damien
>
>
>
>
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: equality of syntax objects
2024-05-20 14:53 ` Damien Mattei
@ 2024-05-20 21:21 ` Damien Mattei
2024-05-22 22:12 ` Maxime Devos
1 sibling, 0 replies; 5+ messages in thread
From: Damien Mattei @ 2024-05-20 21:21 UTC (permalink / raw)
To: guile-user
it works :-)
(define (syntax-string=? obj1 obj2)
(define str-obj1 (format #f "~s" obj1))
(define str-obj2 (format #f "~s" obj2))
(define lst-split-str-obj1 (string-split str-obj1 #\space))
(define lst-split-str-obj2 (string-split str-obj2 #\space))
(string=? (car (reverse lst-split-str-obj1)) (car (reverse
lst-split-str-obj2))))
(define (check-syntax=? obj1 obj2)
;; (display "check-syntax=? : obj1 =") (display obj1) (newline)
;; (display "check-syntax=? : obj2 =") (display obj2) (newline)
(define rv
(and (identifier? obj1) ;(syntax? obj1)
(identifier? obj2) ;(syntax? obj2)
(or (free-identifier=? obj1 obj2) ; a procedure can be overloaded
(syntax-string=? obj1 obj2)))) ; this solve the overloaded problem
;;(display "check-syntax=? : rv =") (display rv) (newline)(newline)
rv)
scheme@(guile-user)> (define x 3)
scheme@(guile-user)> {2.0 / 3.0 * 4.0 + 2.0 ** x ** 4.0 - 1.0}
= 2.4178516392292583e24
Python check:
Python 3.11.2 (v3.11.2:878ead1ac1, Feb 7 2023, 10:02:41) [Clang 13.0.0
(clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
x=3
2.0 / 3.0 * 4.0 + 2.0 ** x ** 4.0 - 1.0
2.4178516392292583e+24
same result :-)
On Mon, May 20, 2024 at 4:53 PM Damien Mattei <damien.mattei@gmail.com>
wrote:
> asked more simply:
>
> how to know ,for two syntax objects now different but that represents two
> different procedures created with the same name at two different moment,how
> to know that the names are still the same?
>
> i see two solutions:
> -parse the representation string to know for example expt that expt is in
> both string:
> "#<syntax:unknown file:2:7 expt>"
> "#<syntax:operation-redux.scm:88:12 expt>"
> not really pretty but will work.I test it in Racket and kawa too...
>
> -keep an up to date value of #'expt to use it for compare to avoid falling
> on an old #'expt that match an old procedure ,i'm not sure it is clear but
> i already do it last year for the same problem when parsing not syntax but
> sexpr composed of real procedures (that were changing in time due to
> overloading) or quoted ones (which is the worse because you have to
> re-evaluate later and it again depends of context....and that not reliable,
> but quoting (with syntax) is the only solution to manipulate 'and' / 'or'.
>
> So i think the best solution is the simple and not pretty that rely on
> parsing the string representation.
>
> On Mon, May 20, 2024 at 1:42 PM Damien Mattei <damien.mattei@gmail.com>
> wrote:
>
>> hello,
>>
>> i want to check the equality of two syntax objects.
>>
>> i wrote this macro:
>>
>> (define (check-syntax=? obj1 obj2)
>> (display "check-syntax=? : obj1 =") (display obj1) (newline)
>> (display "check-syntax=? : obj2 =") (display obj2) (newline)
>> (define rv
>> (and (identifier? obj1) ;(syntax? obj1)
>> (identifier? obj2) ;(syntax? obj2)
>> (free-identifier=? obj1 obj2)))
>> (display "check-syntax=? : rv =") (display rv) (newline)(newline)
>> rv)
>>
>> What works:
>>
>> if i have expt (exponentiation) , the same procedure used and checked
>> from to different files, i have this output in debug when doing some
>> processing, note i think it is important to focus on the filenames of the
>> syntax object also:
>>
>> when computing {1 + 2 expt 3}
>> gives right result:
>> 9
>>
>> note that this computation requires operator precedence analysis doing on
>> the syntax by the process...
>>
>> debug output include:
>>
>> check-syntax=? : obj1 =#<syntax:unknown file:2:7 expt>
>> check-syntax=? : obj2 =#<syntax:operation-redux.scm:88:12 expt>
>> check-syntax=? : rv =#t
>>
>> now i change expt by overloading itself by one of my macro:
>>
>> (overload-existing-operator expt expt (number? number?))
>>
>> i will not explain what is the overloading algorithm here and why i
>> overload expt.(in summary to allow n-arity with exponential such as {2 ** 3
>> ** 4} or {2 expt 3 expt 4}
>> But note that expt is still working,even in infix guile mode:
>> scheme@(guile-user)> {2 expt 3}
>> $2 = 8
>> and is now an n-arity operator:
>> scheme@(guile-user)> {2 expt 3 expt 4}
>> $6 = 2417851639229258349412352
>> scheme@(guile-user)> (expt 2 3 4)
>> $7 = 2417851639229258349412352
>> (and with evaluation from right to left as by convention for expt ,which
>> is different for operator such as + or - or * and / that are evaluated from
>> left to right)
>> but that is of little importance with the current problem.
>>
>> expt looks now like that:
>> scheme@(guile-user)> expt
>> $3 = #<procedure new-funct args>
>> new-funct is an inner function created by overloading process.
>>
>> and :
>> scheme@(guile-user)> #'expt
>> $4 = #<syntax:unknown file:7:2 expt>
>>
>> syntax looks like the original expt , no problem with that.
>>
>> now the ouput in debug,after overloading expt:
>>
>> on the calculus:
>> {1 + 2 expt 3}
>> note that this computation again requires operator precedence analysis
>> doing on the syntax by the process...
>>
>> check-syntax=? : obj1 =#<syntax:unknown file:8:7 expt>
>> check-syntax=? : obj2 =#<syntax:operation-redux.scm:88:12 expt>
>> check-syntax=? : rv =#f
>>
>>
>> and the result 3 is wrong .(calculus has been truncated to first value 3
>> at rightmost as computation with expt goes from right to left but that is
>> of no concern with the problem again)
>>
>> What i understand is even if obj1 and obj2 display the same in the output
>> debug,the expt definition has changed and the checking is not only about
>> litteral syntax but also with an hidden environment or context.
>>
>> That seems normal about syntax in all scheme.
>>
>> But i want ,for this point , only to manipulate "mathematic" syntax here.
>>
>> I have a few simple or complex idea for solution , some not really pretty
>> and i ask opinion for solving this problem?
>>
>> just what i need is a way to find equal the expt even if it has been
>> modified, only from a litteral point of view here.
>>
>> regards,
>>
>> damien
>>
>>
>>
>>
>>
>>
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: equality of syntax objects
2024-05-20 14:53 ` Damien Mattei
2024-05-20 21:21 ` Damien Mattei
@ 2024-05-22 22:12 ` Maxime Devos
2024-05-23 13:55 ` Damien Mattei
1 sibling, 1 reply; 5+ messages in thread
From: Maxime Devos @ 2024-05-22 22:12 UTC (permalink / raw)
To: Damien Mattei, guile-user
>asked more simply:
>how to know ,for two syntax objects now different but that represents two
different procedures created with the same name at two different moment,how
to know that the names are still the same?
>i see two solutions:
-parse the representation string to know for example expt that expt is in
both string:
"#<syntax:unknown file:2:7 expt>"
"#<syntax:operation-redux.scm:88:12 expt>"
not really pretty but will work.I test it in Racket and kawa too...
It appears you want to know equality as _symbols_, instead of equality of _identifiers_.
> (and (identifier? obj1) ;(syntax? obj1)
> (identifier? obj2) ;(syntax? obj2)
> (free-identifier=? obj1 obj2)))
So, instead of checking identifiers with free-identifier=?, instead try ‘syntax->datum’ to extract the name (with lexical information removed) + ‘eq?’ (to compare two symbols).
Best regards,
Maxime Devos
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: equality of syntax objects
2024-05-22 22:12 ` Maxime Devos
@ 2024-05-23 13:55 ` Damien Mattei
0 siblings, 0 replies; 5+ messages in thread
From: Damien Mattei @ 2024-05-23 13:55 UTC (permalink / raw)
To: Maxime Devos; +Cc: guile-user
On Thu, May 23, 2024 at 12:12 AM Maxime Devos <maximedevos@telenet.be>
wrote:
>
> So, instead of checking identifiers with free-identifier=?, instead try
> ‘syntax->datum’ to extract the name (with lexical information removed) +
> ‘eq?’ (to compare two symbols).
>
:-O yes.
i did not think it.it is the first year i use 'syntax' and co .
i modified all the code to use the procedure below and all always works:
(define (datum=? obj1 obj2)
(eq? (syntax->datum obj1)
(syntax->datum obj2)))
thanks
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-05-23 13:55 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-20 11:42 equality of syntax objects Damien Mattei
2024-05-20 14:53 ` Damien Mattei
2024-05-20 21:21 ` Damien Mattei
2024-05-22 22:12 ` Maxime Devos
2024-05-23 13:55 ` Damien Mattei
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).