unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Maxime Devos <maximedevos@telenet.be>
To: Tomas Volf <wolf@wolfsden.cz>, guile-devel@gnu.org
Subject: Re: [PATCH] doc: Extend documentation for (ice-9 match)
Date: Wed, 20 Sep 2023 00:57:33 +0200	[thread overview]
Message-ID: <61413d94-833e-ac1a-fa86-556c6b88d9d9@telenet.be> (raw)
In-Reply-To: <20230906142222.11673-1-wolf@wolfsden.cz>


[-- Attachment #1.1.1: Type: text/plain, Size: 7769 bytes --]



Op 06-09-2023 om 16:06 schreef Tomas Volf:
 > Extend the documentation for (ice-9 match) module with explanation of
 > some of the patterns and also provide more examples for them.  That
 > should make it more useful for people trying to use the module for the
 > first time just based on the documentation.
 >
 > * doc/ref/match.texi (Pattern Matching): Explain some patterns and
 > provide more examples
 > ---
 > When I tried to use (ice-9 match) for the first time, I was not able to,
 > except the most basic usage.  The documentation was not very helful, so
 > I went to the IRC.  I got help, but I was also asked to try to
 > contribute to the docs with examples that would have helped me in the
 > first place.
 >
 > I think even more additions would be useful, but I still do not
 > understand the pattern matching fully, so they are not a part of this
 > patch.  Examples would be: How and when I should use quasi-patterns?
 > What does `ooo' stand for?  Does `box' refer to SRFI-111 or something
 > else?
Note: there is a previous patch for improving (ice-9 match) 
documentation named ‘[PATCH v3] docs/match: pattern matcher example 
makeover’, though IIRC it needed some changes.  Maybe you can adapt some 
things from it.  As long as it doesn't descend in fake gatekeeping 
accusations again, it would be nice if it wasn't for nothing.

Smaller incremental improvements like this would also be nice, though.

 >
 > I was following the HACKING file, so I added myself into the THANKS
 > file, I am not sure if I was supposed to for this relatively trivial
 > change.
I think you aren't supposed to (whether it is trivial or not).  Instead, 
I think that others are supposed to add you to THANKS, otherwise you 
would be thanking yourself.  That is, however, a personal disagreement I 
have with HACKING.

I think that triviality or not is irrelevant; time spent on evaluating 
whether something is trivial or not is better spent on doing more 
contributors.  Besides, THANKS says ‘Contributors since the last 
release’, not ‘Contributors of non-trivial changes since the last release’.

 >
 >   THANKS             |  1 +
 >   doc/ref/match.texi | 75 ++++++++++++++++++++++++++++++++++++++++++++++
 >   2 files changed, 76 insertions(+)
 >
 > diff --git a/THANKS b/THANKS
 > index aa4877e95..bb07cda83 100644
 > --- a/THANKS
 > +++ b/THANKS
 > @@ -38,6 +38,7 @@ Contributors since the last release:
 >                BT Templeton
 >             David Thompson
 >              Bake Timmons
 > +	  Tomas Volf
 >            Mark H Weaver
 >             Göran Weinholt
 >              Ralf Wildenhues
 > diff --git a/doc/ref/match.texi b/doc/ref/match.texi
 > index f5ea43118..b5a0147fa 100644
 > --- a/doc/ref/match.texi
 > +++ b/doc/ref/match.texi
 > @@ -181,6 +181,81 @@ The names @code{quote}, @code{quasiquote}, 
@code{unquote},
 >   @code{or}, @code{not}, @code{set!}, @code{get!}, @code{...}, and
 >   @code{___} cannot be used as pattern variables.
 >
 > +@code{string}, @code{number}, and others refer to literal strings,
 > +numbers, and others.  Therefore pattern @code{string} binds the value to
 > +identifier @code{string}, pattern @code{"string"} matches if the value
 > +is @code{"string"}.
It would do pause after ‘therefore’, not ‘before’.  Might just be a 
personal thing, though.  Also, something about joining two sentences 
with a comma instead of ‘and’ bothers me.  Also², I find it difficult to 
tell whether adding ‘the’ before ‘pattern’ would be better or worse. 
What do you think?

‘Therefore, pattern @code{string} binds the value to

 > Therefore the pattern @code{string} binds the value to
 > the identifier @code{string} and the pattern [...].

 >  Example demonstrating this (by using very bad
 > +naming):
Maybe ‘An example’?  But again, dunno.

 > +
 > +@example
 > +(let ()
 > +  (match "foo"
 > +    (number number)))
 > +@result{} "foo"
 > +@end example
 > +
 > +The field operator (@code{(= field pat)}) has no relation to the fields
 > +of SRFI-9 records.  The @code{field} should be an expression evaluating
 > +to a procedure taking a single argument, and @code{pat} is matched
 > +against the return value.  Simple example:

s/SRFI-9 records/records

There's other methods for defining records to that it has no relation too!

 > +@example
 > +(let ()
 > +  (match '(1 2)
 > +    ((= cadr x)
 > +     x)))
 > +@result{} 2
 > +@end example
 > +
 > +The record operator(@code{($ record-name pat_1 ... pat_n)}) can be used
 > +for matching SRFI-9 and SRFI-99 records.  Patterns are matched against
 > +the slots in order, not all have to be present, and there is no way to
 > +skip a slot.  Example demonstrating the usage:
Probably also RNRS records, structs and (ice-9 records), though I 
haven't tested that.

Basically, all record types except perhaps GOOPS.

Though perhaps no slot order is guaranteed for RNRS and GOOPS, even if 
it happens to work currently?  Dunno.

I don't think it's _supposed_ to work with opaque RNRS records, but 
that's probably not yet implemented.

 > +@example
 > +(define-record-type <foo>
 > +  (make-foo bar baz zab)
 > +  foo?
 > +  (bar foo-bar)
 > +  (baz foo-baz)
 > +  (zab foo-zab))
 > +
 > +(let ((obj (make-foo 1 '2 "three")))
 > +  (match obj
 > +    ;; Make sure obj is a <foo> instance, with bar slot being a number
 > +    ;; and zab slot being a string.  We do not care about baz slot,
 > +    ;; therefore we use _ to match anything.
 > +    (($ <foo> (? number?) _ (? string?))
 > +     "ok")))
 > +@result{} "ok"
 > +@end example
You could inline 'obj' here -- unless there is a bug, match doesn't 
evaluate its ‘argument’ multiple times.

 > +
 > +If you need to ensure that a value is of a specific record type and at
 > +the same time bind it to a variable, the record operator will not be
 > +enough by itself, since you can only capture the fields.  You would need
 > +to combine it with other patterns, for example @code{(? foo? obj)}.
 > +
 > +When you need to apply multiple patterns, or a check and a pattern, you
 > +can use (@code{(? predicate pat_1 ... pat_n)}) for that.  The patterns
 > +are evaluated is if in the @code{(and ...)}.
That last sentence needs a grammar check.

 > If, for example, you want
 > +to check something is a symbol and at the same time bind the value to a
 > +variable, it could look like this:
s/something/if something

or

s/something/whether something

 > +
 > +@example
 > +(let ()
 > +  (match '(delete . some-id)
 > +    (('delete . (? symbol? id))
 > +     ;; We now could, for example, use the id to delete from some alist.
 > +     id)))
Slightly misleading: while in this particular example you don't need it, 
you can also use ‘and’ for that.  Especially useful if there is no 
predicate in play/if the predicate would be (const #true).

Whereas the previous 'let' might just be a matter of personal style, 
this let serves no purpose at all.  I would eliminate this distraction 
to eliminate the tiny risk that someone might be confused by it.

 > +@result{} some-id
 > +@end example
 > +
 > +@c FIXME: Remove this remark once everything is clearly described and
 > +@c consulting the comment is no longer necessary.
 > +If you are unclear about how something works, you can try consulting the
 > +large comment at the top of the @code{module/ice-9/match.upstream.scm}
 > +file in your guile distribution.
s/guile/Guile.

That's not supposed to be there, but given the current (lack of) (ice-9 
match) documentation, I suppose it's reasonable.

Best regards,
Maxime.

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

  reply	other threads:[~2023-09-19 22:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-06 14:06 [PATCH] doc: Extend documentation for (ice-9 match) Tomas Volf
2023-09-19 22:57 ` Maxime Devos [this message]
2023-10-15 17:40   ` Tomas Volf
2023-10-17 20:41     ` Maxime Devos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=61413d94-833e-ac1a-fa86-556c6b88d9d9@telenet.be \
    --to=maximedevos@telenet.be \
    --cc=guile-devel@gnu.org \
    --cc=wolf@wolfsden.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).