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: About the macro expander
Date: Sun, 03 Feb 2013 17:13:31 +0100	[thread overview]
Message-ID: <87ehgx2x5g.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.18911.1359902924.855.help-gnu-emacs@gnu.org

Xue Fuqiao <xfq.free@gmail.com> writes:

> In the trunk version of (info "(cl) Type Predicates"):
>
>  -- Macro: cl-deftype name arglist forms...
>      The type specifier `(NAME ARGS...)' is expanded by calling the
>      expander with those arguments; the type symbol `NAME' is expanded
>      by calling the expander with no arguments.  The ARGLIST is
>      processed the same as for `cl-defmacro' except that optional
>      arguments without explicit defaults use `*' instead of `nil' as the
>      "default" default.
>
> It says "calling the expander with no arguments", but I don't know what
> is the argument of expander (what the argument of expander should be).
> I have searched the Emacs FAQ, Emacs Lisp manual, cl-lib manual and the
> web, but I don't find anything useful.  Can anybody help (is there an
> argument list of the expander)?  Thanks a lot.

Assume we want to define a type for lists of specific length:

    (deftype list-of-length (n) 
       (if (zerop n)
           'null
           `(cons t (list-of-length ,(1- n)))))

Now we can specify a type as (list-of-length 3)
and test:  (typep '(1 2 3) '(list-of-length 3))

To test this, the type must be expanded.  So 3 is bound to n, and the
body of the deftype is evaluated, giving: 

  (cons t (list-of-length 2))

Of course, now to check the cdr of (1 2 3), we have to expand
(list-of-length 2), which gives:

  (cons t (cons t (list-of-length 1)))

and again:

  (cons t (cons t (cons t (list-of-length 0))))

and again:

  (cons t (cons t (cons t nil)))

which is the type of a list of length 3.


Now, if we used the type specifier: list-of-length as in:

  (typep '(1 2 3) 'list-of-length)

we would expand (list-of-length) ; <-- no arguments.

Of course, this would give an error, since list-of-length takes one
mandatory parameter (named n).


We could extend the definition of list-of-length saying that if no
argument is given then it reduces to list (ie. lists of any length):

    (deftype list-of-length (&optional (n '*))
       (case n
          ((*)       'list)
          ((0)       'null)
          (otherwise `(cons t (list-of-length ,(1- n))))))

then we can test:

  (typep '(1 2 3) 'list-of-length) --> T

which expands to (list-of-length) therefore n is bound to the default
value *, and the deftype returns the type LIST.  And since (1 2 3) is a
LIST, it's true.

Of course, we can still check:

  (typep '(1 2 3) '(list-of-length 3)) --> T
  (typep '(1 2 3) '(list-of-length 42)) --> NIL

that is, in Common Lisp.  Of course, in emacs lisp it doesn't work,
because it looks like it doesn't expand recursive types.


When trying to understand what (require 'cl) is about, what it aims for,
you can have a look at: 
http://www.lispworks.com/documentation/HyperSpec/Front/index.htm
and Practical Common Lisp http://www.gigamonkeys.com/book/
and http://cliki.net/

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


       reply	other threads:[~2013-02-03 16:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.18911.1359902924.855.help-gnu-emacs@gnu.org>
2013-02-03 16:13 ` Pascal J. Bourguignon [this message]
2013-02-04  0:10 About the macro expander Xue Fuqiao
  -- strict thread matches above, loose matches on Subject: below --
2013-02-03 14:48 Xue Fuqiao

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=87ehgx2x5g.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.