unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* cond clause does not allow definitions
@ 2024-05-22 19:26 Damien Mattei
  2024-05-22 20:07 ` Jeronimo Pellegrini
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Damien Mattei @ 2024-05-22 19:26 UTC (permalink / raw)
  To: guile-user

scheme@(guile-user)> (cond (#t (define x 7) x))
While compiling expression:
Syntax error:
unknown file:7:10: definition in expression context, where definitions are
not allowed, in form (define x 7)

but it is allowed in else clause:
scheme@(guile-user)> (cond (else (define x 7) x))
$4 = 7

not really logic

it is allowed in Kawa and Racket but they are not scheme references
the R5RS and R7RS talk about 'clause' without more information

is it normal for Guile to consider clause for cond to be an expression
context and not a definition context?

should not be better to consider definition context? that would allow more
possibilities.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 19:26 cond clause does not allow definitions Damien Mattei
@ 2024-05-22 20:07 ` Jeronimo Pellegrini
  2024-05-22 20:33   ` Damien Mattei
  2024-05-22 21:07   ` Pierpaolo Bernardi
  2024-05-22 22:12 ` Maxime Devos
  2024-05-24 14:27 ` Linus Björnstam
  2 siblings, 2 replies; 14+ messages in thread
From: Jeronimo Pellegrini @ 2024-05-22 20:07 UTC (permalink / raw)
  To: Damien Mattei; +Cc: guile-user

Hello!

On 2024-05-22 16:26, Damien Mattei wrote:
> scheme@(guile-user)> (cond (#t (define x 7) x))
> While compiling expression:
> Syntax error:
> unknown file:7:10: definition in expression context, where definitions 
> are
> not allowed, in form (define x 7)

I get no error here (Guile 3.0.9.164-e1690, compiled locally):

scheme@(guile-user)> (cond (else (define x 7) x))
$1 = 7
scheme@(guile-user)> (cond (#t (define x 7) x))
$2 = 7

A: (cond (#t (define x 7) x))
B: (cond (else (define x 7) x))

| system      | A     | B     |
|-------------|-------|-------|
| Bigloo      | 7     | 7     |
| Biwa        | 7     | 7     |
| Chez        | error | 7     |
| Chibi       | error | 7     |
| Chicken     | 7     | 7     |
| Cyclone     | 7     | 7     |
| Gambit      | error | error |
| Gauche      | 7     | 7     |
| Guile       | 7     | 7     |
| Kawa        | 7     | 7     |
| LIPS        | 7     | error |
| Loko        | error | error |
| MIT         | error | 7     |
| Racket      | 7     | 7     |
| Sagittarius | 7     | 7     |
| Scheme48    | error | 7     |
| Scheme 9    | error | 7     |
| SCM         | 7     | 7     |
| STklos      | 7     | 7     |
| Tinyscheme  | 7     | 7     |

* LIPS does not recognise the 'else' clause.

J.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 20:07 ` Jeronimo Pellegrini
@ 2024-05-22 20:33   ` Damien Mattei
  2024-05-22 20:34     ` Damien Mattei
  2024-05-22 20:37     ` lloda
  2024-05-22 21:07   ` Pierpaolo Bernardi
  1 sibling, 2 replies; 14+ messages in thread
From: Damien Mattei @ 2024-05-22 20:33 UTC (permalink / raw)
  To: Jeronimo Pellegrini; +Cc: guile-user

Bonjour,

so it is with the version i currently use which is older than you:
(base) mattei@mbp-touch-bar library-FunctProg % guile
GNU Guile 3.0.8.99-f3ea8
Copyright (C) 1995-2022 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guile-user)> (cond (#t (define x 7) x))
While compiling expression:
Syntax error:
unknown file:1:10: definition in expression context, where definitions are
not allowed, in form (define x 7)

thanks

On Wed, May 22, 2024 at 10:07 PM Jeronimo Pellegrini <j_p@aleph0.info>
wrote:

> Hello!
>
> On 2024-05-22 16:26, Damien Mattei wrote:
> > scheme@(guile-user)> (cond (#t (define x 7) x))
> > While compiling expression:
> > Syntax error:
> > unknown file:7:10: definition in expression context, where definitions
> > are
> > not allowed, in form (define x 7)
>
> I get no error here (Guile 3.0.9.164-e1690, compiled locally):
>
> scheme@(guile-user)> (cond (else (define x 7) x))
> $1 = 7
> scheme@(guile-user)> (cond (#t (define x 7) x))
> $2 = 7
>
> A: (cond (#t (define x 7) x))
> B: (cond (else (define x 7) x))
>
> | system      | A     | B     |
> |-------------|-------|-------|
> | Bigloo      | 7     | 7     |
> | Biwa        | 7     | 7     |
> | Chez        | error | 7     |
> | Chibi       | error | 7     |
> | Chicken     | 7     | 7     |
> | Cyclone     | 7     | 7     |
> | Gambit      | error | error |
> | Gauche      | 7     | 7     |
> | Guile       | 7     | 7     |
> | Kawa        | 7     | 7     |
> | LIPS        | 7     | error |
> | Loko        | error | error |
> | MIT         | error | 7     |
> | Racket      | 7     | 7     |
> | Sagittarius | 7     | 7     |
> | Scheme48    | error | 7     |
> | Scheme 9    | error | 7     |
> | SCM         | 7     | 7     |
> | STklos      | 7     | 7     |
> | Tinyscheme  | 7     | 7     |
>
> * LIPS does not recognise the 'else' clause.
>
> J.
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 20:33   ` Damien Mattei
@ 2024-05-22 20:34     ` Damien Mattei
  2024-05-22 20:37     ` lloda
  1 sibling, 0 replies; 14+ messages in thread
From: Damien Mattei @ 2024-05-22 20:34 UTC (permalink / raw)
  To: Jeronimo Pellegrini; +Cc: guile-user

i wanted to write  'than your' not 'than you' :-/

On Wed, May 22, 2024 at 10:33 PM Damien Mattei <damien.mattei@gmail.com>
wrote:

> Bonjour,
>
> so it is with the version i currently use which is older than you:
> (base) mattei@mbp-touch-bar library-FunctProg % guile
> GNU Guile 3.0.8.99-f3ea8
> Copyright (C) 1995-2022 Free Software Foundation, Inc.
>
> Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
> This program is free software, and you are welcome to redistribute it
> under certain conditions; type `,show c' for details.
>
> Enter `,help' for help.
> scheme@(guile-user)> (cond (#t (define x 7) x))
> While compiling expression:
> Syntax error:
> unknown file:1:10: definition in expression context, where definitions are
> not allowed, in form (define x 7)
>
> thanks
>
> On Wed, May 22, 2024 at 10:07 PM Jeronimo Pellegrini <j_p@aleph0.info>
> wrote:
>
>> Hello!
>>
>> On 2024-05-22 16:26, Damien Mattei wrote:
>> > scheme@(guile-user)> (cond (#t (define x 7) x))
>> > While compiling expression:
>> > Syntax error:
>> > unknown file:7:10: definition in expression context, where definitions
>> > are
>> > not allowed, in form (define x 7)
>>
>> I get no error here (Guile 3.0.9.164-e1690, compiled locally):
>>
>> scheme@(guile-user)> (cond (else (define x 7) x))
>> $1 = 7
>> scheme@(guile-user)> (cond (#t (define x 7) x))
>> $2 = 7
>>
>> A: (cond (#t (define x 7) x))
>> B: (cond (else (define x 7) x))
>>
>> | system      | A     | B     |
>> |-------------|-------|-------|
>> | Bigloo      | 7     | 7     |
>> | Biwa        | 7     | 7     |
>> | Chez        | error | 7     |
>> | Chibi       | error | 7     |
>> | Chicken     | 7     | 7     |
>> | Cyclone     | 7     | 7     |
>> | Gambit      | error | error |
>> | Gauche      | 7     | 7     |
>> | Guile       | 7     | 7     |
>> | Kawa        | 7     | 7     |
>> | LIPS        | 7     | error |
>> | Loko        | error | error |
>> | MIT         | error | 7     |
>> | Racket      | 7     | 7     |
>> | Sagittarius | 7     | 7     |
>> | Scheme48    | error | 7     |
>> | Scheme 9    | error | 7     |
>> | SCM         | 7     | 7     |
>> | STklos      | 7     | 7     |
>> | Tinyscheme  | 7     | 7     |
>>
>> * LIPS does not recognise the 'else' clause.
>>
>> J.
>>
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 20:33   ` Damien Mattei
  2024-05-22 20:34     ` Damien Mattei
@ 2024-05-22 20:37     ` lloda
  2024-05-22 20:40       ` lloda
  1 sibling, 1 reply; 14+ messages in thread
From: lloda @ 2024-05-22 20:37 UTC (permalink / raw)
  To: Damien Mattei; +Cc: Jeronimo Pellegrini, guile-user


This is probably http://git.savannah.gnu.org/gitweb/?p=guile.git;a=commitdiff;h=764e3614b8c13de604399572a67d071621e9ca21 <http://git.savannah.gnu.org/gitweb/?p=guile.git;a=commitdiff;h=764e3614b8c13de604399572a67d071621e9ca21> and the next commit that documents the change. It was after 3.0.9.

> On 22 May 2024, at 22:33, Damien Mattei <damien.mattei@gmail.com> wrote:
> 
> Bonjour,
> 
> so it is with the version i currently use which is older than you:
> (base) mattei@mbp-touch-bar library-FunctProg % guile
> GNU Guile 3.0.8.99-f3ea8
> Copyright (C) 1995-2022 Free Software Foundation, Inc.
> 
> Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
> This program is free software, and you are welcome to redistribute it
> under certain conditions; type `,show c' for details.
> 
> Enter `,help' for help.
> scheme@(guile-user)> (cond (#t (define x 7) x))
> While compiling expression:
> Syntax error:
> unknown file:1:10: definition in expression context, where definitions are
> not allowed, in form (define x 7)
> 
> thanks
> 
> On Wed, May 22, 2024 at 10:07 PM Jeronimo Pellegrini <j_p@aleph0.info>
> wrote:
> 
>> Hello!
>> 
>> On 2024-05-22 16:26, Damien Mattei wrote:
>>> scheme@(guile-user)> (cond (#t (define x 7) x))
>>> While compiling expression:
>>> Syntax error:
>>> unknown file:7:10: definition in expression context, where definitions
>>> are
>>> not allowed, in form (define x 7)
>> 
>> I get no error here (Guile 3.0.9.164-e1690, compiled locally):
>> 
>> scheme@(guile-user)> (cond (else (define x 7) x))
>> $1 = 7
>> scheme@(guile-user)> (cond (#t (define x 7) x))
>> $2 = 7
>> 
>> A: (cond (#t (define x 7) x))
>> B: (cond (else (define x 7) x))
>> 
>> | system      | A     | B     |
>> |-------------|-------|-------|
>> | Bigloo      | 7     | 7     |
>> | Biwa        | 7     | 7     |
>> | Chez        | error | 7     |
>> | Chibi       | error | 7     |
>> | Chicken     | 7     | 7     |
>> | Cyclone     | 7     | 7     |
>> | Gambit      | error | error |
>> | Gauche      | 7     | 7     |
>> | Guile       | 7     | 7     |
>> | Kawa        | 7     | 7     |
>> | LIPS        | 7     | error |
>> | Loko        | error | error |
>> | MIT         | error | 7     |
>> | Racket      | 7     | 7     |
>> | Sagittarius | 7     | 7     |
>> | Scheme48    | error | 7     |
>> | Scheme 9    | error | 7     |
>> | SCM         | 7     | 7     |
>> | STklos      | 7     | 7     |
>> | Tinyscheme  | 7     | 7     |
>> 
>> * LIPS does not recognise the 'else' clause.
>> 
>> J.
>> 



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 20:37     ` lloda
@ 2024-05-22 20:40       ` lloda
  0 siblings, 0 replies; 14+ messages in thread
From: lloda @ 2024-05-22 20:40 UTC (permalink / raw)
  To: Damien Mattei; +Cc: Jeronimo Pellegrini, guile-user


Fwiw Guile isn't 100% consistent about this, for example match forms don't allow definitions and maybe some other form that I don't recall atm.

> On 22 May 2024, at 22:37, lloda <lloda@sarc.name> wrote:
> 
> 
> This is probably http://git.savannah.gnu.org/gitweb/?p=guile.git;a=commitdiff;h=764e3614b8c13de604399572a67d071621e9ca21 <http://git.savannah.gnu.org/gitweb/?p=guile.git;a=commitdiff;h=764e3614b8c13de604399572a67d071621e9ca21> and the next commit that documents the change. It was after 3.0.9.
> 
>> On 22 May 2024, at 22:33, Damien Mattei <damien.mattei@gmail.com <mailto:damien.mattei@gmail.com>> wrote:
>> 
>> Bonjour,
>> 
>> so it is with the version i currently use which is older than you:
>> (base) mattei@mbp-touch-bar library-FunctProg % guile
>> GNU Guile 3.0.8.99-f3ea8
>> Copyright (C) 1995-2022 Free Software Foundation, Inc.
>> 
>> Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
>> This program is free software, and you are welcome to redistribute it
>> under certain conditions; type `,show c' for details.
>> 
>> Enter `,help' for help.
>> scheme@(guile-user)> (cond (#t (define x 7) x))
>> While compiling expression:
>> Syntax error:
>> unknown file:1:10: definition in expression context, where definitions are
>> not allowed, in form (define x 7)
>> 
>> thanks
>> 
>> On Wed, May 22, 2024 at 10:07 PM Jeronimo Pellegrini <j_p@aleph0.info <mailto:j_p@aleph0.info>>
>> wrote:
>> 
>>> Hello!
>>> 
>>> On 2024-05-22 16:26, Damien Mattei wrote:
>>>> scheme@(guile-user)> (cond (#t (define x 7) x))
>>>> While compiling expression:
>>>> Syntax error:
>>>> unknown file:7:10: definition in expression context, where definitions
>>>> are
>>>> not allowed, in form (define x 7)
>>> 
>>> I get no error here (Guile 3.0.9.164-e1690, compiled locally):
>>> 
>>> scheme@(guile-user)> (cond (else (define x 7) x))
>>> $1 = 7
>>> scheme@(guile-user)> (cond (#t (define x 7) x))
>>> $2 = 7
>>> 
>>> A: (cond (#t (define x 7) x))
>>> B: (cond (else (define x 7) x))
>>> 
>>> | system      | A     | B     |
>>> |-------------|-------|-------|
>>> | Bigloo      | 7     | 7     |
>>> | Biwa        | 7     | 7     |
>>> | Chez        | error | 7     |
>>> | Chibi       | error | 7     |
>>> | Chicken     | 7     | 7     |
>>> | Cyclone     | 7     | 7     |
>>> | Gambit      | error | error |
>>> | Gauche      | 7     | 7     |
>>> | Guile       | 7     | 7     |
>>> | Kawa        | 7     | 7     |
>>> | LIPS        | 7     | error |
>>> | Loko        | error | error |
>>> | MIT         | error | 7     |
>>> | Racket      | 7     | 7     |
>>> | Sagittarius | 7     | 7     |
>>> | Scheme48    | error | 7     |
>>> | Scheme 9    | error | 7     |
>>> | SCM         | 7     | 7     |
>>> | STklos      | 7     | 7     |
>>> | Tinyscheme  | 7     | 7     |
>>> 
>>> * LIPS does not recognise the 'else' clause.
>>> 
>>> J.
>>> 
> 



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 20:07 ` Jeronimo Pellegrini
  2024-05-22 20:33   ` Damien Mattei
@ 2024-05-22 21:07   ` Pierpaolo Bernardi
  2024-05-22 22:09     ` Damien Mattei
  2024-05-22 22:37     ` Jeronimo Pellegrini
  1 sibling, 2 replies; 14+ messages in thread
From: Pierpaolo Bernardi @ 2024-05-22 21:07 UTC (permalink / raw)
  To: Jeronimo Pellegrini; +Cc: Damien Mattei, guile-user

On Wed, May 22, 2024 at 10:08 PM Jeronimo Pellegrini <j_p@aleph0.info> wrote:

> A: (cond (#t (define x 7) x))
> B: (cond (else (define x 7) x))
>
> | system      | A     | B     |
> |-------------|-------|-------|
> | Bigloo      | 7     | 7     |
> | Biwa        | 7     | 7     |
> | Chez        | error | 7     |
> | Chibi       | error | 7     |

In chez:

> (cond (else (define x 7) x))
7
> x
7

which looks like a bug to me. You may check if x is defined outside of
the cond expression in the other implementations which do not raise an
error too?



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 21:07   ` Pierpaolo Bernardi
@ 2024-05-22 22:09     ` Damien Mattei
  2024-05-22 22:37     ` Jeronimo Pellegrini
  1 sibling, 0 replies; 14+ messages in thread
From: Damien Mattei @ 2024-05-22 22:09 UTC (permalink / raw)
  To: Pierpaolo Bernardi; +Cc: Jeronimo Pellegrini, guile-user

in the version below it seems that else in cond with definition works alone
but not when there is other conditions before:

GNU Guile 3.0.8.99-f3ea8
Copyright (C) 1995-2022 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guile-user)> (cond (else (define x 7) x))
$1 = 7
scheme@(guile-user)> (cond (#f 'never) (else (define x 7) x))
While compiling expression:
Syntax error:
unknown file:2:24: definition in expression context, where definitions are
not allowed, in form (define x 7)

i understand that because  in my code, that i can not write all here,  i
had  to change:
#`(cond  (#,test (let ()
     #,@L-then))
   (else
      #,@L-else)))

#`(cond  (#,test (let ()
     #,@L-then))
   (else
    (let ()
      #,@L-else))))

add a (let ()  in the else of cond too to encapsulate the possible
definitions in #,@L-else

now i understand that and it is late here...
good night

On Wed, May 22, 2024 at 11:08 PM Pierpaolo Bernardi <olopierpa@gmail.com>
wrote:

> On Wed, May 22, 2024 at 10:08 PM Jeronimo Pellegrini <j_p@aleph0.info>
> wrote:
>
> > A: (cond (#t (define x 7) x))
> > B: (cond (else (define x 7) x))
> >
> > | system      | A     | B     |
> > |-------------|-------|-------|
> > | Bigloo      | 7     | 7     |
> > | Biwa        | 7     | 7     |
> > | Chez        | error | 7     |
> > | Chibi       | error | 7     |
>
> In chez:
>
> > (cond (else (define x 7) x))
> 7
> > x
> 7
>
> which looks like a bug to me. You may check if x is defined outside of
> the cond expression in the other implementations which do not raise an
> error too?
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* RE: cond clause does not allow definitions
  2024-05-22 19:26 cond clause does not allow definitions Damien Mattei
  2024-05-22 20:07 ` Jeronimo Pellegrini
@ 2024-05-22 22:12 ` Maxime Devos
  2024-05-22 22:34   ` Damien Mattei
  2024-05-24 14:27 ` Linus Björnstam
  2 siblings, 1 reply; 14+ messages in thread
From: Maxime Devos @ 2024-05-22 22:12 UTC (permalink / raw)
  To: Damien Mattei, guile-user

>but it is allowed in else clause:
>scheme@(guile-user)> (cond (else (define x 7) x))
>$4 = 7
>
>not really logic

Probably in the implementation of ‘cond’, (cond (else X)) reduces to ‘X’.

It’s tricky to ensure the same behaviour here as is the case when there is more than only the ‘else’ clause – without tail-calling, you could use ‘call-with-values’ in the macro to artificially make an expression context etc., but ‘cond’ needs to tail-call for X.

If someone wishes to implement this, I would propose defining some new syntax ‘begin-expression’ (like ‘begin’, but instead of splicing, it interprets things as a bunch of expressions to evaluate in-order, and with the last expression in tail-position). Would probably be needed to be implemented as a primitive.

Then, ‘(cond (else X))’ could be implemented as (begin-expression X).

The documentation complains a bit about how ‘begin’ does both splicing and this ‘begin-expression’, but 'begin’ can be split! (At least, I think so, it has been a while since I read that part.)

>It is allowed in Kawa and Racket but they are not scheme references
the R5RS and R7RS talk about 'clause' without more information

I don’t know what the standard says about this.

>is it normal for Guile to consider clause for cond to be an expression
context and not a definition context?

Personally, I don’t care whether something is ‘normal’, whatever that’s supposed to mean. What I would care about, is whether the implementation matches the documentation, whether the documentation is complete (even saying “It is unspecified whether [this] or [that].” would suffice) and whether the (documented) semantics is desired, useful, convenient and consistent with relevant standards and conventions.

In this case, not all of these are satisfied.

>should not be better to consider definition context? that would allow more
possibilities.

That would be possible – (cond (a x) ...) could be interpreted as (cond (a (let () x)) ...) instead.

However, I don’t think it has the semantics you desire. Some time ago, you asked questions  along the lines of making a macro ‘define-block’ that allows doings things like

(define (f)
  (if blah
      (define-block foo 1)
      (define-block foo 2))
  [do things with foo here])

but that’s not how scoping, macros and in particular ‘let’ works in Scheme, as mentioned there.

Personally, I think implementing ‘cond’ as the cond+let stuff mentioned above is a bit risky w.r.t. misunderstanding scoping, consider for example:

(define (f x)
  (define y x)
  (cond ((square? x) (define y (sqrt x)) (pk “took a square root”))
             [more branches here])
 y)

which would silently do the wrong thing (unless you enable warnings for shadowing, but you can do neat things with shadowing – you can write pure code in a somewhat imperative style (*)).

Best regards,
Maxime Devos

(*)  See, e.g., https://git.gnunet.org/gnunet-scheme.git/tree/gnu/gnunet/config/parser.scm, where ‘let^’ can do (if X (return Y)) and ‘continue’ without let/ec (not an use of shadowing but sounds like something you may be interested in). On line https://git.gnunet.org/gnunet-scheme.git/tree/gnu/gnunet/config/parser.scm#n481 and 485, you can see how shadowing is used as a replacement of mutation.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 22:12 ` Maxime Devos
@ 2024-05-22 22:34   ` Damien Mattei
  0 siblings, 0 replies; 14+ messages in thread
From: Damien Mattei @ 2024-05-22 22:34 UTC (permalink / raw)
  To: Maxime Devos; +Cc: guile-user

hello Maxime,

On Thu, May 23, 2024 at 12:12 AM Maxime Devos <maximedevos@telenet.be>
wrote:

> >but it is allowed in else clause:
>
> >scheme@(guile-user)> (cond (else (define x 7) x))
>
> >$4 = 7
>
> >
>
> >not really logic
>
>
>
> Probably in the implementation of ‘cond’, (cond (else X)) reduces to ‘X’.
>
>
>
> It’s tricky to ensure the same behaviour here as is the case when there is
> more than only the ‘else’ clause – without tail-calling, you could use
> ‘call-with-values’ in the macro to artificially make an expression context
> etc., but ‘cond’ needs to tail-call for X.
>
>
>
> If someone wishes to implement this, I would propose defining some new
> syntax ‘begin-expression’ (like ‘begin’, but instead of splicing, it
> interprets things as a bunch of expressions to evaluate in-order, and with
> the last expression in tail-position). Would probably be needed to be
> implemented as a primitive.
>
>
>
> Then, ‘(cond (else X))’ could be implemented as (begin-expression X).
>
>
>
> The documentation complains a bit about how ‘begin’ does both splicing and
> this ‘begin-expression’, but 'begin’ can be split! (At least, I think so,
> it has been a while since I read that part.)
>
>
>
> >It is allowed in Kawa and Racket but they are not scheme references
>
> the R5RS and R7RS talk about 'clause' without more information
>
>
>
> I don’t know what the standard says about this.
>
>
>
> >is it normal for Guile to consider clause for cond to be an expression
>
> context and not a definition context?
>
>
>
> Personally, I don’t care whether something is ‘normal’, whatever that’s
> supposed to mean. What I would care about, is whether the implementation
> matches the documentation, whether the documentation is complete (even
> saying “It is unspecified whether [this] or [that].” would suffice) and
> whether the (documented) semantics is desired, useful, convenient and
> consistent with relevant standards and conventions.
>
>
>
> In this case, not all of these are satisfied.
>
>
>
> >should not be better to consider definition context? that would allow more
>
> possibilities.
>
>
>
> That would be possible – (cond (a x) ...) could be interpreted as (cond (a
> (let () x)) ...) instead.
>
>
>
> However, I don’t think it has the semantics you desire. Some time ago, you
> asked questions  along the lines of making a macro ‘define-block’ that
> allows doings things like
>
yes...

i had written a special form condx that allow both inner definition and
inter-definition between conditionals LOL (do not scream please... :-) :

(define-syntax condx
  (syntax-rules (exec else)
    ((_) '()) ;; allow no else clause
    ((_ (else e ...))
     (let () e ...))
    ((_ (exec s ...) d1 ...)
     (let () s ... (condx d1 ...)))
    ((_ (t e ...) tail ...)
     (cond (t (let () e ...))
  (else (condx tail ...))))))


>
> (define (f)
>
>   (if blah
>
>       (define-block foo 1)
>
>       (define-block foo 2))
>
>   [do things with foo here])
>
>
>
but that’s not how scoping, macros and in particular ‘let’ works in Scheme,
> as mentioned there.
>
>
>
> Personally, I think implementing ‘cond’ as the cond+let stuff mentioned
> above is a bit risky w.r.t. misunderstanding scoping, consider for example:
>
>
>
> (define (f x)
>
>   (define y x)
>
>   (cond ((square? x) (define y (sqrt x)) (pk “took a square root”))
>
>              [more branches here])
>
> y)
>
>
>

if i understand , one could misuse y , but it is clear for me that the y in
cond is different than the one more global in f and the last y and result
of f should be x , it is to the programmer to take care of that.

consider Python , it is incredible :
if True:
    x=7

x
7

and now x is defined out of the branch of  x

not a problem for me with that.

Regards,
Damien



> which would silently do the wrong thing (unless you enable warnings for
> shadowing, but you can do neat things with shadowing – you can write pure
> code in a somewhat imperative style (*)).
>
>
>
> Best regards,
> Maxime Devos
>
>
>
> (*)  See, e.g.,
> https://git.gnunet.org/gnunet-scheme.git/tree/gnu/gnunet/config/parser.scm,
> where ‘let^’ can do (if X (return Y)) and ‘continue’ without let/ec (not an
> use of shadowing but sounds like something you may be interested in). On
> line
> https://git.gnunet.org/gnunet-scheme.git/tree/gnu/gnunet/config/parser.scm#n481
> and 485, you can see how shadowing is used as a replacement of mutation.
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 21:07   ` Pierpaolo Bernardi
  2024-05-22 22:09     ` Damien Mattei
@ 2024-05-22 22:37     ` Jeronimo Pellegrini
  2024-05-23 14:25       ` Damien Mattei
  1 sibling, 1 reply; 14+ messages in thread
From: Jeronimo Pellegrini @ 2024-05-22 22:37 UTC (permalink / raw)
  To: Pierpaolo Bernardi; +Cc: Damien Mattei, guile-user

On 2024-05-22 18:07, Pierpaolo Bernardi wrote:
> In chez:
> 
>> (cond (else (define x 7) x))
> 7
>> x
> 7
> 
> which looks like a bug to me. You may check if x is defined outside of
> the cond expression in the other implementations which do not raise an
> error too?

Sure! And the result varies a lot!

(cond (#t (define x 7) x))
(cond (else (define y 7) y))

| System      | A     | x     | B         | y         |
|-------------|-------|-------|-----------|-----------|
| Bigloo      | 7     | x     | 7         | 7         |
| Biwa        | 7     | 7     | 7         | 7         |
| Chez        | error | error | 7         | 7         |
| Chibi       | error | error | 7         | 7         |
| Chicken     | 7     | 7     | 7         | 7         |
| Cyclone     | 7     | error | 7         | error     |
| Gambit      | error | error | error     | error     |
| Gauche      | 7     | 7     | 7         | 7         |
| Guile       | 7     | error | 7         | error     |
| Kawa        | 7     | error | 7         | 7         |
| LIPS        | 7     | error | error (*) | error (*) |
| Loko        | error | error | error     | error     |
| MIT         | error | error | error     | error     |
| Racket      | 7     | error | 7         | error     |
| Sagittarius | 7     | 7     | 7         | 7         |
| Scheme48    | error | error | 7         | 7         |
| Scheme 9    | error | error | 7         | 7         |
| SCM         | 7     | 7     | 7         | 7         |
| STklos      | 7     | 7     | 7         | 7         |
| Tinyscheme  | 7     | 7     | 7         | 7         |

* LIPS does not recognise the `else` clause when it's the only 
expression in `cond`.

J.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 22:37     ` Jeronimo Pellegrini
@ 2024-05-23 14:25       ` Damien Mattei
  2024-05-23 14:59         ` Pierpaolo Bernardi
  0 siblings, 1 reply; 14+ messages in thread
From: Damien Mattei @ 2024-05-23 14:25 UTC (permalink / raw)
  To: Jeronimo Pellegrini; +Cc: Pierpaolo Bernardi, guile-user

On Thu, May 23, 2024 at 12:37 AM Jeronimo Pellegrini <j_p@aleph0.info>
wrote:

> On 2024-05-22 18:07, Pierpaolo Bernardi wrote:
> > In chez:
> >
> >> (cond (else (define x 7) x))
> > 7
> >> x
> > 7
> >
> > which looks like a bug to me.


yes ,unless they wanted Chez scheme to behave as Python :-)   :
if True:
    x=7
x
7

You may check if x is defined outside of
> > the cond expression in the other implementations which do not raise an
> > error too?
>
> Sure! And the result varies a lot!
>

yes i spent many hours debugging to understand why
(cond (else (define y 7) y)) worked and not (cond (#t (define x 7) x))


just for info in Racket there is 'identifier-binding' to know if an
identifier is already bind, this is only useful in a macro syntax context
but it works great.
I'm able to remove all 'define' (and i think 'let') of the code, the code
testing itself if a variable is already binded and define it when necessary.
In summary this works with this macro:

#lang racket

(define-syntax (if-defined stx)
  (syntax-case stx ()
    [(_ id iftrue iffalse)
     (let ([exist-id (identifier-binding #'id)])
       (if exist-id #'iftrue #'iffalse))]))


(define-syntax :=

  (lambda (stx)

    (syntax-case stx ()

      ((_ var expr)

       #'(if-defined var
           (set! var expr)
           (define var expr))))))


it works in any situation, and the checking of the binding of the
identifier seems to be done before the run-time (!) when in a program.

example at REPL but works in all my programs:

Welcome to DrRacket, version 8.13 [cs].
Language: racket, with debugging; memory limit: 8192 MB.
> (:= x 7)
> x
7

Welcome to DrRacket, version 8.13 [cs].
Language: racket, with debugging; memory limit: 8192 MB.
> (define x 7)
> (:= x 8)
> x
8

original code from:
https://stackoverflow.com/questions/20076868/how-to-know-whether-a-racket-variable-is-defined-or-not




> J.
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-23 14:25       ` Damien Mattei
@ 2024-05-23 14:59         ` Pierpaolo Bernardi
  0 siblings, 0 replies; 14+ messages in thread
From: Pierpaolo Bernardi @ 2024-05-23 14:59 UTC (permalink / raw)
  To: Damien Mattei; +Cc: Jeronimo Pellegrini, guile-user

On Thu, May 23, 2024 at 4:25 PM Damien Mattei <damien.mattei@gmail.com> wrote:
> On Thu, May 23, 2024 at 12:37 AM Jeronimo Pellegrini <j_p@aleph0.info> wrote:
>> On 2024-05-22 18:07, Pierpaolo Bernardi wrote:
>> > In chez:
>> >
>> >> (cond (else (define x 7) x))
>> > 7
>> >> x
>> > 7
>> >
>> > which looks like a bug to me.

> yes i spent many hours debugging to understand why
> (cond (else (define y 7) y)) worked and not (cond (#t (define x 7) x))

I had and explanation from Jamie Taylor.

The implementations which behave like Chez, are probably using the
example definition of the cond syntax given in appendix B of the R6
report, which expands (cond (else result1 result2 ...)) into (begin
result1 result2 ...). Given the splicing nature of begin, when the
cond is in a context valid for definitions we have the weird result
above.

https://github.com/cisco/ChezScheme/issues/835



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: cond clause does not allow definitions
  2024-05-22 19:26 cond clause does not allow definitions Damien Mattei
  2024-05-22 20:07 ` Jeronimo Pellegrini
  2024-05-22 22:12 ` Maxime Devos
@ 2024-05-24 14:27 ` Linus Björnstam
  2 siblings, 0 replies; 14+ messages in thread
From: Linus Björnstam @ 2024-05-24 14:27 UTC (permalink / raw)
  To: Damien Mattei, guile-user

I submitted a patch that expanded definition context to more places (cond, case, when, unless etc). I dont know what happened to that. I submitted it while moving and promptly forgot about it after some discussion.

-- Linus Björnstam

On Wed, 22 May 2024, at 21:26, Damien Mattei wrote:
> scheme@(guile-user)> (cond (#t (define x 7) x))
> While compiling expression:
> Syntax error:
> unknown file:7:10: definition in expression context, where definitions are
> not allowed, in form (define x 7)
>
> but it is allowed in else clause:
> scheme@(guile-user)> (cond (else (define x 7) x))
> $4 = 7
>
> not really logic
>
> it is allowed in Kawa and Racket but they are not scheme references
> the R5RS and R7RS talk about 'clause' without more information
>
> is it normal for Guile to consider clause for cond to be an expression
> context and not a definition context?
>
> should not be better to consider definition context? that would allow more
> possibilities.



^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-05-24 14:27 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-22 19:26 cond clause does not allow definitions Damien Mattei
2024-05-22 20:07 ` Jeronimo Pellegrini
2024-05-22 20:33   ` Damien Mattei
2024-05-22 20:34     ` Damien Mattei
2024-05-22 20:37     ` lloda
2024-05-22 20:40       ` lloda
2024-05-22 21:07   ` Pierpaolo Bernardi
2024-05-22 22:09     ` Damien Mattei
2024-05-22 22:37     ` Jeronimo Pellegrini
2024-05-23 14:25       ` Damien Mattei
2024-05-23 14:59         ` Pierpaolo Bernardi
2024-05-22 22:12 ` Maxime Devos
2024-05-22 22:34   ` Damien Mattei
2024-05-24 14:27 ` Linus Björnstam

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