all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Some improvements for cl-flet
@ 2021-09-11 12:51 akater
  2021-09-11 23:32 ` Michael Heerdegen
  2021-09-23 22:37 ` [PATCH] " akater
  0 siblings, 2 replies; 42+ messages in thread
From: akater @ 2021-09-11 12:51 UTC (permalink / raw)
  To: emacs-devel; +Cc: Stefan Monnier

[-- Attachment #1: Type: text/plain, Size: 3758 bytes --]

I've implemented some fixes for ~cl-flet~ necessary to address an issue
with ~cl-generic~ but also improving ~cl-flet~ proper.  Before I post
the patch, I'd like to clarify one issue.

~cl-flet~'s ~(func exp)~ syntax, as described and implemented, is
incompatible with ~flet~ syntax as specified in Common Lisp.  Namely,
~(func exp)~ syntax does not allow to distinguish between a form ~exp~
returning a function and an arglist ~exp~ for a function ~func~ that
always returns ~nil~.

Consider the following valid (but stylistically poor) Common Lisp code:
#+begin_src lisp :wrap example lisp
(flet ((f #'g))
  (f t t))
#+end_src

#+RESULTS:
#+begin_example lisp
NIL
#+end_example

If I understand the purpose of ~cl-lib~ correctly, corresponding
~cl-flet~ form should return ~nil~ as well.  However, it errors:
#+begin_src emacs-lisp :results code :wrap example emacs-lisp
(condition-case err (cl-flet ((f #'g))
                      (f t t))
  (t err))
#+end_src

#+RESULTS:
#+begin_example emacs-lisp
(void-function g)
#+end_example

In case it's not clear what's going on: the previous example is
equivalent to the following one, only with ~function~, ~g~ replaced with
~x~, ~y~ correspondingly:
#+begin_src lisp :wrap example lisp
(flet ((f (x y)))
  (f t t))
#+end_src

#+RESULTS:
#+begin_example lisp
NIL
#+end_example

but due to ~(func exp)~ syntax, ~(x y)~ is presumed to be a form meant
to return a function, rather than an arglist, and so
#+begin_src emacs-lisp :results code :wrap example emacs-lisp
(condition-case err (cl-flet ((f (x y)))
                      (f t t))
  (t err))
#+end_src

#+RESULTS:
#+begin_example emacs-lisp
(void-function x)
#+end_example

The syntax of ~flet~ could only be compatible with ~cl-flet~'s ~(func
exp)~ syntax when ~exp~ is presumed to be a non-list.  For example,
~exp~ could be a symbol.

The following expression could also be unambiguously interpreted in
style of ~(func exp)~ because ~nil~ is not a valid function argument:
#+begin_src emacs-lisp :results code :wrap example emacs-lisp
(cl-flet ((f (lambda nil nil)))
  (f))
#+end_src

#+RESULTS:
#+begin_example emacs-lisp
nil
#+end_example

However I don't think it's worth it to use complicated rules to
distinguish such cases.  ~flet~ just has its own syntax, incompatible
with the (Scheme-inspired, likely) idea of binding a function to a
variable.  Such complications would keep ~cl-flet~ unfit for use in
macroexpanded (and otherwise generated) code.  Last but not least,
~(flet ((f (lambda nil nil))) (f))~, etc., is not a valid Common Lisp
code.

Note also that the following is valid (and stylistically fine) Common
Lisp code:
#+begin_src lisp :wrap example lisp
(flet ((f ()))
  (f))
#+end_src

#+RESULTS:
#+begin_example lisp
NIL
#+end_example

However, evaluating corresponding ~cl-flet~ form errors:
#+begin_src emacs-lisp :results code :wrap example emacs-lisp
(condition-case err (cl-flet ((f ()))
                      (f))
  (t err))
#+end_src

#+RESULTS:
#+begin_example emacs-lisp
(void-function nil)
#+end_example

Finally, note that (an equivalent) ~(func exp)~ syntax could not be
adopted by at all by ~cl-macrolet~, as macrolet forms support
destructuring lambda lists.  For example, the following is a valid (but
stylistically poor) Common Lisp code:
#+begin_src lisp :wrap example lisp
(macrolet ((f (lambda () 'x)))
  (f t nil (t t)))
#+end_src

#+RESULTS:
#+begin_example lisp
NIL
#+end_example

Given all this, I think ~(func exp)~ should be dropped from ~cl-flet~.
My patch (already discussed with Stefan Monnier to some extent)
introduces function ~cl--expand-flet~ which retains the functionality
currently provided by ~(func exp)~, in an unambiguous way.  I suggest to
move it there, away from ~cl-flet~.

Do you agree?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 865 bytes --]

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

end of thread, other threads:[~2021-11-09 20:37 UTC | newest]

Thread overview: 42+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-11 12:51 Some improvements for cl-flet akater
2021-09-11 23:32 ` Michael Heerdegen
2021-09-12  3:35   ` akater
2021-09-12 15:38     ` Stefan Monnier
2021-09-13  0:14     ` Michael Heerdegen
2021-09-13  2:26       ` Stefan Monnier
2021-10-07  2:32       ` akater
2021-10-07 18:03         ` Stefan Monnier
2021-10-08 21:57           ` Richard Stallman
2021-10-09  5:23             ` akater
2021-10-09  6:01               ` Michael Heerdegen
2021-10-09 23:37                 ` Richard Stallman
2021-10-10 10:41                   ` Po Lu
2021-10-10 20:27                     ` João Távora
2021-10-10 21:57                       ` Stefan Monnier
2021-10-11  0:45                       ` [External] : " Drew Adams
2021-10-11 21:16                     ` Richard Stallman
2021-10-11 21:26                       ` João Távora
2021-10-12 22:42                         ` Richard Stallman
2021-10-12  0:05                       ` Po Lu
2021-10-12  0:29                       ` Robin Tarsiger
2021-10-12 22:43                         ` Richard Stallman
2021-10-09 23:33               ` Richard Stallman
2021-10-09 23:33               ` Richard Stallman
2021-10-14  4:00               ` Michael Heerdegen
2021-09-23 22:37 ` [PATCH] " akater
2021-09-23 22:41   ` akater
2021-09-24  7:11     ` João Távora
2021-09-24 15:20       ` [PATCH] Some improvements for cl-flet, and some more akater
2021-09-24 16:22         ` João Távora
2021-09-25  1:28         ` Lars Ingebrigtsen
2021-09-25  8:37           ` João Távora
2021-09-24 20:30     ` [PATCH] Some improvements for cl-flet akater
2021-09-26  6:54     ` Lars Ingebrigtsen
2021-09-26 12:04       ` akater
2021-09-26 12:36         ` Lars Ingebrigtsen
2021-10-03  3:51     ` Stefan Monnier
2021-10-07  5:02       ` akater
2021-10-07 18:23         ` Stefan Monnier
2021-11-03 12:59           ` akater
2021-11-09 20:37             ` Stefan Monnier
2021-10-09  5:33       ` akater

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.