Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

destructuring-match - the name of the game

32 views
Skip to first unread message

Madhu

unread,
Aug 23, 2022, 11:53:26 AM8/23/22
to
It looks like tim bradshaw has published a macro called
destructring-match which sounds like destructuring bind but looks more
like elisp's pcase with additional multiple match clauses and additional
(:when ..) syntax. I would disagree with the name, because it doesn't
seem to be analogous to lisp's destructuring

I wanted to post my own destructuring-match macro (which I wrote when
pithy-xml came out.) This has the same surface-syntax as
destructuring-bind i.e. (lambda-list value-form &body body) and it just
expands to a destrucuring-bind form. The difference is it matches
literal elements of value-form directly, (and so it can process
"parameter lists" which are usually passed as the rhs expression to
destructuring-bind)

```
(destructuring-match (:head first-arg 2 &rest rest &key foo bar &allow-other-keys)
;; :head and 2 are matched literally
'(:head 1 2 :foo 10 :bar 20 :xyz 30)
(list foo bar first-arg )) => (10 20 1)
```

I've found it useful in processing sexps (from json or xml sources)
after flattening them, but maybe destructring-match isn't the best name.
Perhaps someone can understand what its doing can suggest a name or
confirm the name.


```
(defmacro destructuring-match (macro-lambda-list value-form &body body)
(let (renamed-bindings declares)
(labels ((remove-declares (code)
(cond ((and (consp (car code)) (eql (caar code) 'declare))
(push (car code) declares)
(remove-declares (cdr code)))
(t code)))
(snarf-bindings (x)
(typecase x
(null)
(cons (case (car x)
((&aux &key &optional &rest &allow-other-keys) x)
(t (cons (snarf-bindings (car x))
(snarf-bindings (cdr x))))))
((and symbol (not keyword)) x)
(t (let ((name (gensym)))
(push (list name x) renamed-bindings)
name)))))
(let ((real-body (remove-declares body)))
`(destructuring-bind ,(snarf-bindings macro-lambda-list) ,value-form
,@(nreverse declares)
,@(loop for (k x) in (nreverse renamed-bindings) collect
`(assert (equal ,k ,x) nil
"Failed to match ~S against ~S." ,k ,x))
,@real-body)))))

#+elisp
(put 'destructuring-match 'common-lisp-indent-function '((&whole 6 &rest 1) 4 &body))
```

Zyni Moë

unread,
Aug 23, 2022, 2:32:27 PM8/23/22
to
Madhu <eno...@net.meer> wrote:
> It looks like tim bradshaw has published a macro called
> destructring-match which sounds like destructuring bind but looks
> more like elisp's pcase with additional multiple match clauses and
> additional (:when ..) syntax. I would disagree with the name,
> because it doesn't seem to be analogous to lisp's destructuring

Each lambda list in

(destructuring-match <form>
(<lambda-list> ...)
(<lambda-list> ...)
...
(otherwise ...))

is full destructuring-bind lambda list (so, macro lambda list without
environment etc). In that sense yes, it is exactly common lisp's
destructuring.

In fact is easy:

(defmacro destructuring-bind (ll form &body forms)
`(destructuring-match ,form
(,ll ,@forms)
(otherwise (error "fleașcă"))))

Think you may be confuse dsm with spam which is in its implementation (I do
not know pcase you mention). In fact destructuring-bind os more safely
implement using both dsm for the work and spam for toxin prevention

(defmacro destructuring-bind (ll form &body forms)
(matching forms
((head-matches (repeating-list-of (some-of (is ':when)
(is ':unless))
(any)))
(error "futu-i"))
(otherwise
`(destructuring-match ,form
(,ll ,@forms)
(otherwise (error "fleașcă"))))))

Even this is not quite right: really you need to parse forms a little, so:

(defmacro destructuring-bind (ll form &body decls/forms)
(multiple-value-bind (decls forms) (parse-simple-body decls/forms)
`(destructuring-match ,form
(,ll
,@decls
(progn ,@forms)))
(otherwise (error "fleașcă"))))

This still may be wrong but is close.

--
the small snake

Madhu

unread,
Aug 23, 2022, 10:39:50 PM8/23/22
to

* Zyni Moë <te36fl$329vk$1...@dont-email.me> :
Wrote on Tue, 23 Aug 2022 18:32:22 -0000 (UTC):

> Madhu <eno...@net.meer> wrote:
>> It looks like tim bradshaw has published a macro called
>> destructring-match which sounds like destructuring bind but looks
>> more like elisp's pcase with additional multiple match clauses and
>> additional (:when ..) syntax. I would disagree with the name,
>> because it doesn't seem to be analogous to lisp's destructuring
>
> Each lambda list in
>
> (destructuring-match <form>
> (<lambda-list> ...)
> (<lambda-list> ...)
> ...
> (otherwise ...))
>
> is full destructuring-bind lambda list (so, macro lambda list without
> environment etc). In that sense yes, it is exactly common lisp's
> destructuring.

No the top level surface syntax is analogous to CL's CASE (or COND).

The name of the macro should reflect that it is a bunch of case clauses
or at least call it match-destructuring.

I think each clause which you have denoted as (<lambda-list> ...) also
has additional syntax (a cl-loopish :when in the ... forms, and loop is
apparently abhorred by the author)

Only the clauses involve destructuring.

> In fact is easy:
>
> (defmacro destructuring-bind (ll form &body forms)
> `(destructuring-match ,form
> (,ll ,@forms)
> (otherwise (error "fleașcă"))))
>
> Think you may be confuse dsm with spam which is in its implementation (I do
> not know pcase you mention). In fact destructuring-bind os more safely
> implement using both dsm for the work and spam for toxin prevention

Thanks, I'll get around to looking at it at some point.

Zyni Moë

unread,
Aug 24, 2022, 4:23:04 AM8/24/22
to
Madhu <eno...@net.meer> wrote:

> The name of the macro should reflect that it is a bunch of case clauses
> or at least call it match-destructuring.

Oh I had not realised your problem was so petty and stupid, would not have
wasted time replying if I knew that. But, well, you are old lisp person I
think: only thing lisp people really do well is petty and stupid. It's a
'*-match' macro, it should work the way match macros do, which is like
case.

Fairly sure his problem with loop is that it is a nightmare to parse, I
will ask next time I see him.


--
the small snake

Madhu

unread,
Aug 24, 2022, 5:18:54 AM8/24/22
to
* Zyni Moë <te4n52$397of$1...@dont-email.me> :
Wrote on Wed, 24 Aug 2022 08:22:58 -0000 (UTC):
> Oh I had not realised your problem was so petty and stupid, would not
> have wasted time replying if I knew that.

Your initial response was easily identifiable as a knee-jerk defence of
a (probably unworthy) hero's design decisions, and your follow up
confirms that

> But, well, you are old lisp person I think: only thing lisp people
> really do well is petty and stupid. It's a '*-match' macro, it should
> work the way match macros do, which is like case.

There is a unifying principle in common lisp design, not statable ,
which can nevertheles be grasped by practioners - similar to how
biblical texts can identified to be canonical because they "fit". TFEBs
macro does not "fit" either in the naming or in the syntax. I suspect
Much of tfeb's canards against trivia would also apply to hiw work.

> Fairly sure his problem with loop is that it is a nightmare to parse,
> I will ask next time I see him.

Don't bother - I'm sure there is some nuanced dissonance behind that
that you are sure miss or will be unable to grasp even if you saw it.





0 new messages