[Top][All Lists]

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

Re: "Like `let*' but ....."

From: Alan Mackenzie
Subject: Re: "Like `let*' but ....."
Date: Fri, 27 Jan 2017 20:31:13 +0000
User-agent: Mutt/1.7.2 (2016-11-26)

Hello, Stefan.

On Wed, Jan 25, 2017 at 00:08:01 -0500, Stefan Monnier wrote:

[ .... ]

> BTW, regarding the big pcase-let* in byte-compile-file-form-defalias,
> it was partly an experiment in the use of pcase patterns.

> OT1H I'm not convinced the result speaks very much in favor of pcase,
> OTOH if you try to rewrite this code without pcase it's not
> pretty either.

> I'll read it for you:

>            ;; `macro' is non-nil if it defines a macro.
>            ;; `fun' is the function part of `arg' (defaults to `arg').
>            (((or (and (or `(cons 'macro ,fun) `'(macro . ,fun)) (let macro t))
>                  (and (let fun arg) (let macro nil)))
>              arg)

> This part binds two vars: `fun` and `macro`.
> Basically, it binds `fun` to the value of `arg` except that if `arg` is
> a "macro value", `fun` gets the value of the macro's function (and
> `macro` gets value t rather than nil).

OK, I think I might have got the semantics now.  The `arg' at the end of
that thing is the object upon which the pcase pattern at the beginning
of it acts on.  The `or's and `and's are pcase ones, not the primitives.
The variables which are bound are precisely the cadrs of the three
element lists beginning `(let ...)', none of the variables bound in the
pcase form surviving after that form terminates.  Er, actually that
isn't true, looking at `lam' in the next bit.  Variables bound within
the pcase pattern remain bound over the entire pcase-let* form (unless,
of course, a subsequent binding rebinds the same symbol).

Am I right?

>             ;; `lam' is the lambda expression in `fun' (or nil if not
>             ;; recognized).
>             ((or `(,(or `quote `function) ,lam) (let lam nil))
>              fun)

> This only binds `lam`.  It takes the `fun` apart and extracts a lambda
> expression from it (or nil if it can't).

>             ;; `arglist' is the list of arguments (or t if not recognized).
>             ;; `body' is the body of `lam' (or t if not recognized).
>             ((or `(lambda ,arglist . ,body)
>                  ;; `(closure ,_ ,arglist . ,body)
>                  (and `(internal-make-closure ,arglist . ,_) (let body t))
>                  (and (let arglist t) (let body t)))
>              lam))

> This tries to take apart `lam` and extract two parts: `arglist` and `body`.

> The complexity here is that `arg` doesn't hold a "macro value"
> (i.e. something of the form (macro . FUN)), but instead it holds an
> expression that will return a macro value.  So `arg` can be of the form

>     (quote (macro . FUN))
>     (cons 'macro 'FUN)
>     (cons 'macro #'FUN)
>     (cons 'macro (internal-make-closure ...))
>     ...

> and of course FUN is usually of the form (lambda ...) but it can also be
> a plain symbol, or a #<subr ...> object or god knows what else.

>         Stefan

Alan Mackenzie (Nuremberg, Germany).

reply via email to

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