all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Why is booleanp defined this way?
Date: Fri, 17 Apr 2015 22:55:34 +0200	[thread overview]
Message-ID: <87wq1amq49.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.946.1429302909.904.help-gnu-emacs@gnu.org

Marcin Borkowski <mbork@mbork.pl> writes:

> Hi all,
>
> this is what I found in subr.el:
>
> ,----
> | (defun booleanp (object)
> |   "Return t if OBJECT is one of the two canonical boolean values: t or nil.
> | Otherwise, return nil."
> |   (and (memq object '(nil t)) t))
> `----
>
> Seemingly, it doesn't make much sense: what is the purpose of saying
>
> (and (whatever) t)
>
> instead of just
>
> (whatever)
>
> for a predicate?  Of course, this "normalizes" any "truthy" value to
> "t", but is it really needed for anything (except perhaps being
> elegant)?

There's a difference between a boolean and a generalized boolean.
Also notice how the docstrings give the SPECIFICATION of the function:

    (defun booleanp (object)
       "Return t if OBJECT is one of the two canonical boolean values: t or nil.
    Otherwise, return nil."
       (and (memq object '(nil t)) t))

    (defun generalized-booleanp (object)
       "Return t if OBJECT is a generalized boolean, otherwise return nil"
       t) ; all the lisp objects are generalized booleans, by definition!

    (defun truep (generalized-boolean)
       "Return t if GENERALIZED-BOOLEAN is true, nil otherwise."
       (and generalized-boolean t))

    (defun falsep (generalized-boolean)
       "Return t if GENERALIZED-BOOLEAN is false, nil otherwise."
       (null generalized-boolean))

Obviously, the last three functions are idiotic, given that almost all the
boolean lisp operators actually take generalized boolean, and that there
is already NULL and NOT doing the same job as the last.


You don't write:

   (if (truep (member fruit '(apple banana)))
      'fruit
      'vegetable)

you write:

   (if (member fruit '(apple banana))
      'fruit
      'vegetable)




Also a little trick to obtain a boolean from a generalized boolean,
instead of (and … t), is to use the double negation: (not (not …)).
Arguably, not being a function it could be slower, but  a sufficiently
smart compiler should be able to generate the same code, or in the case
of emacs lisp virtual machine, produce actually shorter byte code, since
not is a virtual machine instruction:

(defun f (x) (and x t))
(disassemble (byte-compile 'f))
byte code:
  args: (x)
0       varref    x
1       goto-if-nil-else-pop 1
4       constant  t
5:1     return    

(defun g (x) (not (not x)))
(disassemble (byte-compile 'g))
byte code:
  args: (x)
0       varref    x
1       not       
2       not       
3       return    

But this is really nit-picking.

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


       reply	other threads:[~2015-04-17 20:55 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.946.1429302909.904.help-gnu-emacs@gnu.org>
2015-04-17 20:55 ` Pascal J. Bourguignon [this message]
2015-04-17 23:20 ` Why is booleanp defined this way? Barry Margolin
2015-04-17 23:30   ` Emanuel Berg
2015-04-18  0:43     ` Pascal J. Bourguignon
2015-04-18  3:13       ` Barry Margolin
2015-04-18  3:12     ` Barry Margolin
2015-04-18  2:01 ` Rusi
2015-04-18  2:23   ` Emanuel Berg
2015-04-18  2:33     ` Rusi
2015-04-18  2:55       ` Emanuel Berg
2015-04-18  3:11         ` Barry Margolin
2015-04-18  3:35           ` Rusi
2015-04-18  4:56             ` Barry Margolin
2015-04-19 23:08               ` Emanuel Berg
2015-04-19 23:00             ` Emanuel Berg
2015-04-18 21:24           ` Emanuel Berg
2015-04-18  7:52         ` Marcin Borkowski
     [not found]         ` <mailman.997.1429343558.904.help-gnu-emacs@gnu.org>
2015-04-18 12:43           ` Rusi
2015-04-18  4:09       ` Pascal J. Bourguignon
2015-04-18  5:00         ` Rusi
2015-04-18  3:50   ` Pascal J. Bourguignon
2015-04-18  5:03     ` Stefan Monnier
2015-04-17 20:34 Marcin Borkowski
2015-04-17 20:49 ` Jorge A. Alfaro-Murillo
     [not found] ` <mailman.962.1429303822.904.help-gnu-emacs@gnu.org>
2015-04-17 23:06   ` Emanuel Berg
2015-04-18  0:41     ` Pascal J. Bourguignon
2015-04-18  1:06       ` Emanuel Berg
2015-04-18  1:23         ` Pascal J. Bourguignon
2015-04-18  7:44         ` Marcin Borkowski
2015-04-18  8:37         ` Stefan Nobis
2015-04-19 23:15           ` Emanuel Berg
2015-04-18  2:37 ` Drew Adams
2015-04-18  6:13 ` Tassilo Horn

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=87wq1amq49.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /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.