From: Ruijie Yu via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: Jean Louis <bugs@gnu.support>
Cc: Nick Dokos <ndokos@gmail.com>, help-gnu-emacs@gnu.org
Subject: Re: [External] : Re: How to make M-x TAB not work on (interactive) declaration?
Date: Sat, 11 Feb 2023 12:38:30 +0800 [thread overview]
Message-ID: <sdvmt5kogt7.fsf@fw.net.yu> (raw)
In-Reply-To: <Y8bYsHiAsdKK8kLM@protected.localdomain>
Hello Jean,
It seems to me that you still have some lingering questions regarding
this topic. See below for my attempt to answer them.
Jean Louis <bugs@gnu.support> writes:
> * Nick Dokos <ndokos@gmail.com> [2023-01-17 19:27]:
>> The "empty sum" and "empty product" conventions are described in the
>> corresponding articles in Wikipedia too - they might help:
>>
>> https://en.wikipedia.org/wiki/Empty_sum
>> https://en.wikipedia.org/wiki/Empty_product
>
> Thanks much.
>
> I understand that "summation" is not equal to "addition" as summation
> is the addition of a sequence of any kind of numbers, called addends
> or summands; the result is their sum or total:
> https://en.wikipedia.org/wiki/Summation
>
> Then back to: https://en.wikipedia.org/wiki/Empty_sum
> where it says:
>
> In mathematics, an empty sum, or nullary sum,[1] is a summation where
> the number of terms is zero. The natural way to extend non-empty
> sums[2] is to let the empty sum be the additive identity.
>
> Basically function `+' deals with summation, not with addition. But
> that is conclusion I can draw alone with help of your references,
> without confirmation by docstring, manual or some references.
>
> Though I do not understand why it has to deal with summation, and not
> straight with addition? What is use in Lisp?
IIUC, are you asking why, within Emacs codebase, there is no 2-argument
"addition" function, in addition to the variadic summation function
known as `+'? And similarly, why there is no 2-argument
"multiplication" function, in addition to the variadic product function
known as `*'?
IMO, the answer is threefold. I believe most, if not all, of them have
been mentioned in previous messages of this thread, but hopefully by
listing them below side by side, it makes a bit more sense to you.
First, we can all agree that some sort of addition is needed, either
2-arg or variadic, in order to support the most basic functionalities
like motions within a buffer and making calculations.
Second, there should be plenty of cases (which I have not verified
within the codebase) where summing up a list of numbers is needed. If
the variadic "summation" function is not available, then each such
library (first or third party) have to either implement their own
summation function, or use something like `(cl-reduce #'add-two-numbers
input-list :initial-value 0)'.
Third, if it is determined that a "summation" function is needed, then
the "addition" function is a strict subset of the summation function and
no longer necessary. Suppose we have the two functions `add' (2-arg
only) and `sum' (variadic), I hope you can agree that these two
expressions are always equivalent in terms of their results:
(add 1 2) ;=> 3
(sum 1 2) ;=> 3
Because of the reasons above, IMO, the Emacs maintainers made a consious
decision that implementing the summation function as `+' is enough for
all its use cases, and it would be no longer necessary to have a
separate "addition" function. Similarly, they decided to implement the
product function as `*'.
Admittedly, they could have implemented the following summation function
and have made `+' only accept 2 arguments, but not doing so is their
decision to make and do not introduce real functional differences:
(defun sum (nums) (cl-reduce #'+ nums :initial-value 0))
The reason Emacs gets to make this decision (as opposed to, for example,
C) is because `+' and `*' are ordinary functions. Consider C:
arithmetic expressions are made with an operator, a left-hand-side
expression and a right-hand-side expression. There are no other
"sides". Therefore, without introducing more complexity to the language
(e.g. special-casing a "sum" function which takes generic, variadic
arguments), it is only feasible for C to have 2-arg arithmetic
operators.
> In this other reference: https://en.wikipedia.org/wiki/Empty_product
> the issue with Lisp is mentioned, and we can read, that in many
> programming languages it is so, like in Python, Lisp, but in Perl is
> not so, then it says:
>
> Multiplication is an infix operator and therefore a binary operator,
> complicating the notation of an empty product. Some programming
> languages handle this by implementing variadic functions. For example,
> the fully parenthesized prefix notation of Lisp languages gives rise
> to a natural notation for nullary functions:
>
> (* 2 2 2) ; evaluates to 8
> (* 2 2) ; evaluates to 4
> (* 2) ; evaluates to 2
> (*) ; evaluates to 1
>
> and:
>
> In mathematics and in computer programming, a variadic function is a
> function of indefinite arity, i.e., one which accepts a variable
> number of arguments. Support for variadic functions differs widely
> among programming languages.
>
> I may alone assume, while still needing confirmation, that Emacs Lisp,
> Common Lisp use those variadic function. Not that I have got stable
> feeling with it.
>
> Though that still does not tell me why?
>
> I have excluded the purpose for `apply' and similar functions as that
> is handled properly with PicoLisp where (*) ➜ NIL -- and maybe I am
> wrong, but with all references I came closer some reasoning. But all
> the reasoning is not confirmed in Lisp books.
I don't have an answer for why other lisp dialects have different
defaults.
> What I understand from C is thet if number of args nargs is 0 is that
> result shall be 1 -- that alone does not explain why and how is it
> useful in Lisp.
>
> DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
> doc: /* Return product of any number of arguments, which are numbers or markers.
> usage: (* &rest NUMBERS-OR-MARKERS) */)
> (ptrdiff_t nargs, Lisp_Object *args)
> {
> if (nargs == 0)
> return make_fixnum (1);
> Lisp_Object a = check_number_coerce_marker (args[0]);
> return nargs == 1 ? a : arith_driver (Amult, nargs, args, a);
> }
Regarding why the default values of summation and product are 0 and 1
respectively, here is a concrete example use case (with analysis) where
said default values are needed:
Consider you want to sum up *all values* inside a nested list to get
a single value:
(setq foo
'((1 2 3 4) ;=> 10
() ;=> 0
(1)) ;=> 1
To sum it manually, you see that this is essentially summing up the
list '(1 2 3 4 1) to get 11.
You can also write the following expression to calculate the sum:
(apply #'+ (mapcar (lambda (lst) (apply #'+ lst)) foo))
;=> 11
Notice how you have skipped the empty sublist. In a sense, because
this sublist is empty, you *don't add anything* to the accumulated
result. Mathematically, that is the same as adding 0. Therefore,
the only natural default value for summation is 0.
This analysis can also be made for the default value of product,
where when you skip an empty sublist, you *don't multiple anything*
to the accumulated result, which is mathematically equivalent to
multiplying by 1, which we can then conclude should be the natural
default value for product.
Hope that helps.
Best,
RY
next prev parent reply other threads:[~2023-02-11 4:38 UTC|newest]
Thread overview: 167+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-07 20:53 How to make M-x TAB not work on (interactive) declaration? Jean Louis
2023-01-07 21:11 ` Ruijie Yu via Users list for the GNU Emacs text editor
2023-01-07 23:40 ` Jean Louis
2023-01-08 6:06 ` Eli Zaretskii
2023-01-08 6:19 ` Emanuel Berg
2023-01-09 4:49 ` Jean Louis
2023-01-09 6:26 ` algorithmic Lisp language (was: Re: How to make M-x TAB not work on (interactive) declaration?) Emanuel Berg
2023-01-09 19:30 ` Jean Louis
2023-01-09 19:32 ` Jean Louis
2023-01-08 6:21 ` How to make M-x TAB not work on (interactive) declaration? Jean Louis
2023-01-08 6:32 ` Emanuel Berg
2023-01-08 10:38 ` Eli Zaretskii
2023-01-08 8:34 ` Tassilo Horn
2023-01-08 11:01 ` Eli Zaretskii
2023-01-09 13:29 ` Tassilo Horn
2023-01-08 21:35 ` Jean Louis
2023-01-08 22:35 ` [External] : " Drew Adams
2023-01-09 0:24 ` Emanuel Berg
2023-01-09 19:47 ` Jean Louis
2023-01-10 23:28 ` Emanuel Berg
2023-01-13 6:21 ` (*)->1 Jean Louis
2023-01-14 12:03 ` (*)->1 Michael Heerdegen
2023-01-14 12:33 ` (*)->1 Michael Heerdegen
2023-01-15 20:18 ` [External] : Re: How to make M-x TAB not work on (interactive) declaration? Rudolf Adamkovič
2023-01-15 20:57 ` Jean Louis
2023-01-15 22:33 ` Drew Adams
2023-01-15 23:10 ` Emanuel Berg
2023-01-16 15:28 ` Jean Louis
2023-01-16 17:07 ` Drew Adams
2023-01-16 18:25 ` Jean Louis
2023-01-17 2:20 ` Drew Adams
2023-01-17 5:28 ` tomas
2023-01-17 22:20 ` Drew Adams
2023-01-18 5:14 ` tomas
2023-01-18 5:26 ` Emanuel Berg
2023-01-19 11:38 ` tomas
2023-01-19 11:51 ` Emanuel Berg
2023-01-21 14:05 ` tomas
2023-01-23 10:14 ` Robert Pluim
2023-01-23 16:44 ` Michael Heerdegen
2023-01-23 19:28 ` tomas
2023-01-18 17:27 ` Drew Adams
2023-01-18 17:32 ` tomas
2023-01-18 14:32 ` Jean Louis
2023-01-18 20:36 ` Drew Adams
2023-01-19 9:05 ` (*) -> 1 Jean Louis
2023-01-19 9:41 ` Yuri Khan
2023-01-19 12:52 ` Anders Munch
2023-01-17 5:35 ` [External] : Re: How to make M-x TAB not work on (interactive) declaration? Jean Louis
2023-01-17 15:59 ` Yuri Khan
2023-01-17 16:42 ` Jean Louis
2023-01-17 16:05 ` Michael Heerdegen
2023-01-17 16:17 ` Yuri Khan
2023-01-17 16:25 ` tomas
2023-01-17 16:55 ` (*) -> 1 Jean Louis
2023-01-17 17:52 ` Michael Heerdegen
2023-01-17 18:11 ` Óscar Fuentes
2023-01-17 18:40 ` Jean Louis
2023-01-17 19:04 ` Óscar Fuentes
2023-01-18 13:15 ` Jean Louis
2023-01-18 14:37 ` Óscar Fuentes
2023-01-18 18:17 ` [External] : " Drew Adams
2023-01-17 19:35 ` Michael Heerdegen
2023-01-17 21:12 ` Michael Heerdegen
2023-01-17 22:01 ` Óscar Fuentes
2023-01-17 23:38 ` Michael Heerdegen
2023-01-18 7:50 ` Óscar Fuentes
2023-01-18 8:37 ` tomas
2023-01-18 12:46 ` Óscar Fuentes
2023-01-18 13:44 ` Michael Heerdegen
2023-01-18 14:07 ` Óscar Fuentes
2023-01-18 16:19 ` Andreas Eder
2023-01-18 17:14 ` Óscar Fuentes
2023-01-19 8:37 ` Jean Louis
2023-01-17 18:18 ` Jean Louis
2023-01-17 11:52 ` [External] : Re: How to make M-x TAB not work on (interactive) declaration? Michael Heerdegen
2023-01-15 21:08 ` Jean Louis
2023-01-16 5:02 ` Emanuel Berg
2023-01-16 5:38 ` tomas
2023-01-16 10:10 ` Jean Louis
2023-01-16 10:41 ` Yuri Khan
2023-01-16 15:26 ` Jean Louis
2023-01-17 4:06 ` Emanuel Berg
2023-01-17 14:00 ` tomas
2023-01-17 22:43 ` Emanuel Berg
2023-01-17 16:25 ` Nick Dokos
2023-01-17 17:19 ` Jean Louis
2023-02-11 4:38 ` Ruijie Yu via Users list for the GNU Emacs text editor [this message]
2023-02-11 10:54 ` Jean Louis
2023-01-17 17:41 ` Nick Dokos
2023-01-16 7:55 ` Yuri Khan
2023-01-16 10:16 ` Jean Louis
2023-01-16 10:37 ` Yuri Khan
2023-01-16 15:35 ` Jean Louis
2023-01-16 15:59 ` Yuri Khan
2023-01-16 16:14 ` Jean Louis
2023-01-16 16:47 ` tomas
2023-01-16 17:07 ` Drew Adams
2023-01-16 18:41 ` Jean Louis
2023-01-16 10:51 ` Anders Munch
2023-01-16 15:38 ` Jean Louis
2023-01-16 17:40 ` Andreas Eder
2023-01-16 18:17 ` tomas
2023-01-16 18:55 ` Jean Louis
2023-01-16 19:14 ` tomas
2023-01-16 18:46 ` Jean Louis
2023-01-17 2:37 ` Eduardo Ochs
2023-01-17 5:46 ` (*) -> 1 Jean Louis
2023-01-17 15:56 ` Michael Heerdegen
2023-01-17 16:29 ` Jean Louis
2023-01-17 16:43 ` tomas
2023-01-17 17:25 ` Jean Louis
2023-01-17 19:11 ` Nick Dokos
2023-01-17 17:17 ` Michael Heerdegen
2023-01-17 17:26 ` Jean Louis
2023-01-17 18:46 ` Michael Heerdegen
2023-01-17 18:51 ` Jean Louis
2023-01-17 18:04 ` Jean Louis
2023-01-17 18:28 ` Eduardo Ochs
2023-01-17 19:18 ` Michael Heerdegen
2023-01-18 12:27 ` Jean Louis
2023-01-18 13:37 ` Michael Heerdegen
2023-01-19 8:20 ` Jean Louis
2023-01-19 10:06 ` Tassilo Horn
2023-01-19 13:43 ` Michael Heerdegen
2023-01-19 14:42 ` Jean Louis
2023-01-19 15:27 ` tomas
2023-01-18 13:57 ` Óscar Fuentes
2023-01-19 8:32 ` Jean Louis
2023-01-19 16:51 ` Óscar Fuentes
2023-01-20 8:01 ` Jean Louis
2023-01-18 14:25 ` Michael Heerdegen
2023-01-19 8:34 ` Jean Louis
2023-01-19 13:54 ` Michael Heerdegen
2023-01-19 14:54 ` Jean Louis
2023-01-19 15:19 ` Tassilo Horn
2023-01-20 7:05 ` Jean Louis
2023-01-20 8:52 ` Tassilo Horn
2023-01-20 12:46 ` Jean Louis
2023-01-20 13:02 ` Tassilo Horn
2023-01-20 16:06 ` Jean Louis
2023-01-21 8:19 ` Tassilo Horn
2023-01-22 4:30 ` Emanuel Berg
2023-01-22 6:55 ` Jean Louis
2023-01-22 10:56 ` Emanuel Berg
2023-01-23 3:40 ` [External] : " Drew Adams
2023-01-22 14:34 ` Akib Azmain Turja
2023-01-23 2:23 ` Emanuel Berg
2023-01-23 5:37 ` Jean Louis
2023-01-23 5:55 ` Jean Louis
2023-01-24 2:33 ` Emanuel Berg
2023-01-19 15:46 ` Michael Heerdegen
2023-01-19 17:38 ` Dr Rainer Woitok
2023-01-20 7:31 ` Jean Louis
2023-01-20 11:49 ` Dr Rainer Woitok
2023-01-19 14:54 ` Jean Louis
2023-01-19 17:44 ` [External] : " Drew Adams
2023-01-19 21:29 ` Michael Heerdegen
2023-01-20 7:40 ` Jean Louis
2023-01-20 8:47 ` Emanuel Berg
2023-01-20 7:33 ` Jean Louis
2023-01-18 9:02 ` Anders Munch
2023-01-18 10:49 ` Michael Heerdegen
2023-01-18 11:10 ` Emanuel Berg
2023-01-18 12:48 ` Eli Zaretskii
2023-01-18 14:29 ` Michael Heerdegen
[not found] ` <87k01lica7.fsf@eder.anydns.info>
2023-01-17 16:04 ` Jean Louis
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sdvmt5kogt7.fsf@fw.net.yu \
--to=help-gnu-emacs@gnu.org \
--cc=bugs@gnu.support \
--cc=ndokos@gmail.com \
--cc=ruijie@netyu.xyz \
/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.
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.