From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.devel Subject: Rationalising c[ad]\{2,5\}r. Date: Wed, 11 Mar 2015 21:43:24 +0000 Message-ID: <20150311214324.GA2952@acm.fritz.box> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1426110251 14416 80.91.229.3 (11 Mar 2015 21:44:11 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 11 Mar 2015 21:44:11 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Mar 11 22:44:02 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YVoQ3-0007vr-FJ for ged-emacs-devel@m.gmane.org; Wed, 11 Mar 2015 22:43:59 +0100 Original-Received: from localhost ([::1]:57100 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YVoQ2-0005nv-Rt for ged-emacs-devel@m.gmane.org; Wed, 11 Mar 2015 17:43:58 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:35491) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YVoPz-0005ni-7S for emacs-devel@gnu.org; Wed, 11 Mar 2015 17:43:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YVoPu-0005FL-QG for emacs-devel@gnu.org; Wed, 11 Mar 2015 17:43:55 -0400 Original-Received: from colin.muc.de ([193.149.48.1]:57008 helo=mail.muc.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YVoPu-0005Eg-Fu for emacs-devel@gnu.org; Wed, 11 Mar 2015 17:43:50 -0400 Original-Received: (qmail 7052 invoked by uid 3782); 11 Mar 2015 21:43:48 -0000 Original-Received: from acm.muc.de (pD9518360.dip0.t-ipconnect.de [217.81.131.96]) by colin.muc.de (tmda-ofmipd) with ESMTP; Wed, 11 Mar 2015 22:43:47 +0100 Original-Received: (qmail 3110 invoked by uid 1000); 11 Mar 2015 21:43:24 -0000 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x X-Received-From: 193.149.48.1 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:183790 Archived-At: 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).