unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Rationalising c[ad]\{2,5\}r.
@ 2015-03-11 21:43 Alan Mackenzie
  2015-03-11 22:00 ` Drew Adams
  2015-03-11 22:15 ` Thien-Thi Nguyen
  0 siblings, 2 replies; 24+ messages in thread
From: Alan Mackenzie @ 2015-03-11 21:43 UTC (permalink / raw)
  To: emacs-devel

Hello, Emacs.

Right at the moment, our functions caar, cadr, cdar, cddr, caaar, caadr,
....., cddddr are in a mess:

1. caar,  cadr, cdar, and cddr are defined in subr.el;
2. c[ad]\{3,4\}r are actually called cl-caaar, etc;
3. cl-c[ad]\{3,4\}r are defined in cl-lib.el;
4. The aliases for the names without the cl- are defined in  cl.el;
5. c[ad]\{3,4\}r each contain the form
    (declare (compiler-macro cl--compiler-macro-cXXr)
  , whereas caar, cadr, cdar, cddr don't.  At the moment, I don't know
  whether this is a bug, or whether the two cases are just handled
  differently;
6. All the defuns are written out individually, making it hard to verify
  that they are all correct, and making it very hard to extend to 5 or
  more [ad]s, and also taking up a lot of lines of code spread over
  several files.el.

I propose the following solution: all these defuns should be in subr.el,
the canonical names will be caaadr etc., and there will be compatibility
aliases for cl-caaadr etc..  All these functions and aliases will be
generated by macros, thus saving source code lines, making them more
reliable, and enabling extension to the scheme with a trivial amount of
effort.

At the moment, the number of source file lines occupied by these
functions (including blank lines) is:
subr.el:    16
cl-lib.el: 120
cl.el:      24
           ---
TOTAL      160

In my proposed change, the macros and macro invocations occupy a mere 88
lines, and even extends the scheme to 5-[ad] defuns (like cadadar).
(Yes, I do have a use for caadadr, caddadr, and cdddadr in fix-re.el.)

Here is the code I propose to put into subr.el in place of the
declarations of caar etc.:

*************************************************************************

;; Macros to generate caar ... cdddddr in subr.el.
(eval-and-compile
  (defun gen-cXXr--rawname (n bits)
    "Generate and return a string like \"adad\" corresponding to N.
BITS is the number of a's and d's.
The \"corresponding\" means each bit of N is converted to an \"a\" (for zero)
or a \"d\" (for one)."
    (let ((name (make-string bits ?a))
	  (mask (lsh 1 (1- bits)))
	  (elt 0))
      (while (< elt bits)
	(if (/= (logand n mask) 0)
	    (aset name elt ?d))
	(setq elt (1+ elt)
	      mask (lsh mask -1)))
      name))

  (defun gen-cXXr--doc-string (raw)
    "Generate a doc string for a name like \"cadadr\".
RAW is the \"inner\" part of the name, e.g. \"adad\"."
    (concat
     "Return the `c"
     (mapconcat (lambda (ad) (char-to-string ad)) raw "r' of the `c")
     "r' of X."))

  (defun gen-cXXr--code (raw bits)
    "Generate the code for a defun like \"cadadr\" in terms of `car' and `cdr'.
RAW is the \"inner\" part of the name, e.g. \"adad\", BITS is the
length of RAW."
    (let ((code 'x)
	  (elt bits))
      (while (> elt 0)
	(setq elt (1- elt))
	(setq code (list (if (eq (aref raw elt) ?a) 'car 'cdr) code)))
      code))

  (defun gen-cXXr--defun (n bits compiler-macro)
    "Generate a `defun' for a symbol like \"cadadr\".
N is a number representing the \"inner\" part of the
symbol (e.g. binary 0101 for cadadr), BITS is the length of that
part of the symbol (e.g. 4).  If COMPILER-MACRO is non-nil,
include a `compiler-macro' declaration in the defun."
    (let ((raw (gen-cXXr--rawname n bits)))
      `(defun ,(intern (concat "c" raw "r")) (x)
	 ,(gen-cXXr--doc-string raw)
	 ,@(when compiler-macro
	     '((declare (compiler-macro cl--compiler-macro-cXXr))))
	 ,(gen-cXXr--code raw bits))))

  (defun gen-cXXr--make-seq (bits)
    "Generate a list of all integers with BITS bits, in ascending order."
    (let ((x (lsh 1 bits))
	  acc)
      (while (> x 0)
	(setq x (1- x))
	(push x acc))
      acc)))

(defmacro gen-cXXr-all (bits compiler-macro)
  "Generate defuns for all `c[ad]+r's with BITS a's and d's.
If COMPILER-MACRO is non-nil, include `compiler-macro'
declarations in the defuns."
  (let ((seq (gen-cXXr--make-seq bits)))
     `(progn
	,@(mapcar
	   (lambda (n)
	     (gen-cXXr--defun n bits compiler-macro))
	   seq))))

(defmacro gen-cXXr-all-cl-aliases (bits)
  "Generate cl- aliases for all defuns `c[ad]+r' with BITS a's and d's."
  (let ((seq (gen-cXXr--make-seq bits))
	)
    `(progn
       ,@(mapcar
	  (lambda (n)
	    (let ((raw (gen-cXXr--rawname n bits)))
	      `(defalias ',(intern (concat "cl-c" raw "r"))
		 ',(intern (concat "c" raw "r")))))
	  seq))))

(gen-cXXr-all 2 nil)
(gen-cXXr-all 3 t)
(gen-cXXr-all-cl-aliases 3)
(gen-cXXr-all 4 t)
(gen-cXXr-all-cl-aliases 4)
(gen-cXXr-all 5 t)
*************************************************************************

Comments?

-- 
Alan Mackenzie (Nuremberg, Germany).



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

end of thread, other threads:[~2015-04-05 13:00 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-11 21:43 Rationalising c[ad]\{2,5\}r Alan Mackenzie
2015-03-11 22:00 ` Drew Adams
2015-03-11 22:54   ` Artur Malabarba
2015-03-12  1:51     ` Drew Adams
2015-03-12  9:54       ` Artur Malabarba
2015-03-12 14:01         ` Drew Adams
2015-03-12  4:34     ` Stephen J. Turnbull
2015-03-12 10:15       ` Artur Malabarba
2015-03-12 10:30   ` Alan Mackenzie
2015-03-12 14:03     ` Drew Adams
2015-03-11 22:15 ` Thien-Thi Nguyen
2015-03-11 23:00   ` Alan Mackenzie
2015-03-11 23:36     ` Thien-Thi Nguyen
2015-03-12  1:35       ` Samuel W. Flint
2015-03-12  0:52     ` Artur Malabarba
2015-03-12  8:56       ` Alan Mackenzie
2015-03-12 10:15         ` Artur Malabarba
2015-03-12 13:53     ` Stefan Monnier
2015-03-13 16:41       ` Alan Mackenzie
2015-03-13 16:47         ` Drew Adams
2015-03-13 20:38         ` Stefan Monnier
2015-03-13 21:36           ` Alan Mackenzie
2015-03-13 23:00             ` Stefan Monnier
2015-04-05 13:00               ` Alan Mackenzie

Code repositories for project(s) associated with this public inbox

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

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).