all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* About the macro expander
@ 2013-02-03 14:48 Xue Fuqiao
  0 siblings, 0 replies; 3+ messages in thread
From: Xue Fuqiao @ 2013-02-03 14:48 UTC (permalink / raw)
  To: help-gnu-emacs

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.

-- 
Best regards, Xue Fuqiao.
http://www.emacswiki.org/emacs/XueFuqiao



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

* Re: About the macro expander
       [not found] <mailman.18911.1359902924.855.help-gnu-emacs@gnu.org>
@ 2013-02-03 16:13 ` Pascal J. Bourguignon
  0 siblings, 0 replies; 3+ messages in thread
From: Pascal J. Bourguignon @ 2013-02-03 16:13 UTC (permalink / raw)
  To: help-gnu-emacs

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 {}.


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

* Re: About the macro expander
@ 2013-02-04  0:10 Xue Fuqiao
  0 siblings, 0 replies; 3+ messages in thread
From: Xue Fuqiao @ 2013-02-04  0:10 UTC (permalink / raw)
  To: help-gnu-emacs

> and again:
> 
>   (cons t (cons t (cons t (list-of-length 0))))
> 
> and again:
> 
>   (cons t (cons t (cons t nil)))

About this step, why does the 'null be expanded to nil?  I know that the type symbol `null' represents the symbol `nil' in `cl-typep', thus `(cl-typep OBJECT 'null)' is equivalent to `(null OBJECT)'.  But other than that, `null' should be a function, which returns t if OBJECT is nil.

> 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/

Thanks for your information, but I think CLHS and Practical Common Lisp is too long.  And in many ways, they are incompatible with `cl-lib' in Emacs, such as the `equal' predicate does not distinguish between IEEE floating-point plus and minus zero, and the second argument of `cl-check-type' is treated differently, etc.

-- 
Best regards, Xue Fuqiao.
http://www.emacswiki.org/emacs/XueFuqiao



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

end of thread, other threads:[~2013-02-04  0:10 UTC | newest]

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

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.