Re: Lexical scope of pattern variables is not strictly respected in the sample implementation Marc Nieper-WiÃkirchen 01 Sep 2020 10:02 UTC
The original paper by Wright (attached to this post, found in [1]) does actually a very good job of explaining the semantics of "match". In particular, it says (reasonably) (1) [An] identifier [...] matches anything, and binds a variable of this name to the matching value in the body [!]. (2) [...] The predicate expression is bound in the same scope as the match expression, ie, free variables in predicate are not bound by pattern variables. -- [1] https://legacy.cs.indiana.edu/scheme-repository/code.match.html Am Di., 1. Sept. 2020 um 11:25 Uhr schrieb Marc Nieper-Wißkirchen <xxxxxx@nieper-wisskirchen.de>: > > Sorry, typo on my side. > > My example (corrected) has meant to be > > (match '1 ((and odd? (? odd?)) odd?)) > > > Am Di., 1. Sept. 2020 um 11:21 Uhr schrieb Felix Thibault > <xxxxxx@gmail.com>: > > > > > > > > On Tue, Sep 1, 2020 at 4:35 AM Marc Nieper-Wißkirchen <xxxxxx@nieper-wisskirchen.de> wrote: > >> > >> In the specification section of SRFI 204, it says "identifiers will > >> match anything, and make the corresponding binding available in the > >> body", which I read as that the identifiers are lexically bound in an > >> extended environment, in which the body is evaluated. > >> > >> The sample implementation, however, fails to respect the correct > >> lexical scoping in some cases: > >> > >> (match '1 ((and odd (? odd)) odd)) > >> > >> The expected value of this expression (and according to how match is > >> specified in SRFI 204) is 1. The sample implementation, however, fails > >> with an error because the binding of odd is introduced too early, > >> namely in a scope surrounding the pattern `(? odd)` as well. > >> > >> To make the sample implementation do the correct thing, it has to > >> defer all actual bindings or bind to temporary variables first. > >> > >> Marc > > > > > > Unless I'm misunderstanding > > (match '1 ((and odd (? odd)) odd)) > > needs a predicate like either: > > (match '1 ((and odd (? odd?)) odd)) > > or > > (match '1 ((and odd (? odd? odd)) odd)) > > both of which evaluate to 1