From: Hung Jung Lu
Subject: Language keywords or special forms in Lisp
Date: 
Message-ID: <8ef9bea6.0406091427.40e67ad8@posting.google.com>
Hi,

I am experimenting with Lisp a little bit. I tried to redefine the
"if" function (actually, the same is true for "setq", or "t"):

defun if (x y z) (+ x y z)

but the interpreter did not allow me to do so. So the question is:

(a) Does Lisp have "language keywords" (like "if", "while", "return",
etc. in other languages)?

(b) The fact that the "if" function cannot be overriden: is this
particular to specific implementation of Lisp? Does it happen only for
interpreters? Or this is a general limitation?

(c) What other functions are considered special and cannot be
overriden? Is there a document somewhere that specifies the set of
special forms?

My understanding is that there are truly keywordless languages like
Io, where every single function is overridable/interceptible. If
functions like "setq" are not overridable, what do Lisp programmers do
when they want to intercept assignments/bindings?

regards,

Hung Jung

From: Matthew Danish
Subject: Re: Language keywords or special forms in Lisp
Date: 
Message-ID: <Pine.LNX.4.58-035.0406091836060.8203@unix45.andrew.cmu.edu>
On Wed, 9 Jun 2004, Hung Jung Lu wrote:

> Hi,
>
> I am experimenting with Lisp a little bit. I tried to redefine the
> "if" function (actually, the same is true for "setq", or "t"):
>
> defun if (x y z) (+ x y z)
>
> but the interpreter did not allow me to do so. So the question is:
>
> (a) Does Lisp have "language keywords" (like "if", "while", "return",
> etc. in other languages)?
>
> (b) The fact that the "if" function cannot be overriden: is this
> particular to specific implementation of Lisp? Does it happen only for
> interpreters? Or this is a general limitation?
>
> (c) What other functions are considered special and cannot be
> overriden? Is there a document somewhere that specifies the set of
> special forms?
>
> My understanding is that there are truly keywordless languages like
> Io, where every single function is overridable/interceptible. If
> functions like "setq" are not overridable, what do Lisp programmers do
> when they want to intercept assignments/bindings?

[assuming Common Lisp]

Some of your fundamentals need fixing:

* Don't leave out parenthesis; each one is there for a reason--they
  are a concrete syntax showing the abstract structure of the code.
  Parenthesis are NOT precedence-resolving operators in Lisp.
* special operator -- an operator which is treated specially by the
  evaluator, not like a function or macro.  There is a list of standard
  special operators in the hyperspec, and the function SPECIAL-OPERATOR-P
  will return T when given the /name/ of such an operator.  This is
  the closest notion to what is called a "keyword" in other languages,
  except that "keywords" in other languages are built into the language
  lexer, while special operators in Lisp are actually symbols which have
  syntactic meaning only on a higher level.
* special form -- a form beginning with a special operator,
  such as (if ...).
* keyword -- a symbol in the "KEYWORD" package (with nickname ""),
  ie. KEYWORD:FOO and :FOO are both expressions evaluating to keywords
  (the same one, in this case).  This concept is not particularly
  important to the current discussion, and has nothing to do with the
  notion of "keyword" in most other languages.
* IF is not a function, it is a special operator in CL.  IF could not
  be a function in CL, because CL is a call-by-value language.  Functions
  evaluate all their arguments BEFORE invoking the function with the
  values.  Quite clearly, this would be disasterous for IF:
    (if t 1 (destroy-world))
  SETQ is not a function either.
* You are not allowed to define global bindings on the symbols in the
  CL packages, in general, and you are not allowed to shadow existing
  bindings in the CL package locally.
* The semantics of compiled Lisp code and interpreted Lisp code are
  supposed to remain the same (with a few unavoidable exceptions),
  and your so-called "interpreter" is probably compiling on-the-fly,
  since many lisps these days do that.

If you want to give different meanings to names in your program, it is
advised that you create your own package into which you can selectively
import CL symbols, and define the other ones yourself.

(cl:defpackage lu-lisp
  (:use)  ;; no USE-PACKAGE
  (:import-from cl
                defun defmacro))

(cl:in-package lu-lisp)

(defmacro if (test then else)
  ;; IF is backwards in Lu-lisp
  `(cl:if ,test ,else ,then))

(defun foo (t) ;; this is lu-lisp::t, not cl:t
  (if t 0 1))  ;; this uses lu-lisp::if
From: ·········@random-state.net
Subject: Re: Language keywords or special forms in Lisp
Date: 
Message-ID: <ca85a5$d0gjk$1@midnight.cs.hut.fi>
Hung Jung Lu <··········@yahoo.com> wrote:

> (a) Does Lisp have "language keywords" (like "if", "while", "return",
> etc. in other languages)?

Symbols in the COMMON-LISP package have restrictions related to
redefinition, among other things. Make a package of your own and
you have virtually free reign:

* (defpackage :foo (:use))

#<PACKAGE "FOO">
* (in-package :foo)

#<COMMON-LISP:PACKAGE "FOO">
* (common-lisp:defun if (test then else)
    (common-lisp:error "Surprise! Did you expect ~S?" 
                       (common-lisp:if test then else)))

IF
* (if common-lisp:t common-lisp:nil "Boing")

debugger invoked on a COMMON-LISP:SIMPLE-ERROR in thread 1813:
  Surprise! Did you expect COMMON-LISP:NIL?


 ...

The Common Lisp standard is here:

 http://www.lispworks.com/reference/HyperSpec/Front/index.htm

...but it's not an ideal starting point for learning. Try this
excellent piece of work by Peter Seibel (hi Peter!) for starters:

 http://www.gigamonkeys.com/book/

Cheers,

 -- Nikodemus                   "Not as clumsy or random as a C++ or Java. 
                             An elegant weapon for a more civilized time."
From: Edi Weitz
Subject: Re: Language keywords or special forms in Lisp
Date: 
Message-ID: <87oensfiui.fsf@bird.agharta.de>
On 9 Jun 2004 15:27:07 -0700, ··········@yahoo.com (Hung Jung Lu) wrote:

> I am experimenting with Lisp a little bit. I tried to redefine the
> "if" function (actually, the same is true for "setq", or "t"):
>
> defun if (x y z) (+ x y z)
>
> but the interpreter did not allow me to do so. So the question is:
>
> (a) Does Lisp have "language keywords" (like "if", "while",
> "return", etc. in other languages)?
>
> (b) The fact that the "if" function cannot be overriden: is this
> particular to specific implementation of Lisp? Does it happen only
> for interpreters? Or this is a general limitation?
>
> (c) What other functions are considered special and cannot be
> overriden? Is there a document somewhere that specifies the set of
> special forms?
>
> My understanding is that there are truly keywordless languages like
> Io, where every single function is overridable/interceptible. If
> functions like "setq" are not overridable, what do Lisp programmers
> do when they want to intercept assignments/bindings?

A couple of notes:

1. IF cannot be implemented as a function because it doesn't evaluate
   all of its arguments. If you want to mimic its behaviour you have
   to resort to macros. Neither is SETQ a function.

2. There's an ANSI standard for Common Lisp. Online versions of this
   standard are available at

     <http://www.lispworks.com/reference/HyperSpec/Front/index.htm>
     <http://www.franz.com/support/documentation/6.2/ansicl/ansicl.htm>

3. Lisp doesn't have "language keywords" but there are a couple of
   constraints on the standardized COMMON-LISP package:

     <http://www.lispworks.com/reference/HyperSpec/Body/11_aba.htm>

   This is why your Lisp didn't allow you to redefine IF. It doesn't
   matter if you're using an interpreter or a compiler. (Note that
   some Lisps like SBCL or MCL don't even have an interpreter.)

4. You can create your own package (read the standard section about
   packages) and "shadow" symbols like IF and SETQ to create the
   behaviour you want.

5. Some Lisps have an "advice" facility that'll allow you to
   "intercept" or "override" functions. See

     <http://www.lispworks.com/reference/lw43/LWUG/html/lwuser-39.htm>

   for an example. This is not covered by the ANSI standard, though.

6. You might also want to look at :AROUND, :BEFORE and :AFTER methods
   in CLOS. These are standardized and serve a similar purpose.

HTH,
Edi.
From: Tim Bradshaw
Subject: Re: Language keywords or special forms in Lisp
Date: 
Message-ID: <fbc0f5d1.0406100804.1fd68a9d@posting.google.com>
Edi Weitz <···@agharta.de> wrote in message news:<··············@bird.agharta.de>...

> 4. You can create your own package (read the standard section about
>    packages) and "shadow" symbols like IF and SETQ to create the
>    behaviour you want.

Just to blow my own trumpet, if you use conduits you can do:

(defpackage :cl/my-if
  (:use)
  (:extends/excluding :cl #:if)
  (:export #:if))


And now CL/MY-IF is just like CL, but CL/MY-IF:IF is not the same as
CL:IF.

Of course you can do this without conduits, but it's more typing (a
lot more typing  in fact).

--tim
From: Svein Ove Aas
Subject: Re: Language keywords or special forms in Lisp
Date: 
Message-ID: <ca84r3$mcu$1@services.kq.no>
Hung Jung Lu wrote:

> Hi,
> 
> I am experimenting with Lisp a little bit. I tried to redefine the
> "if" function (actually, the same is true for "setq", or "t"):
> 
> defun if (x y z) (+ x y z)
> 
> but the interpreter did not allow me to do so. So the question is:
> 
> (a) Does Lisp have "language keywords" (like "if", "while", "return",
> etc. in other languages)?
> 
Kind of.
Ye other poster already told you how to get around it; what Lisp does have
is "special forms", which are the forms (macro- or functionlike) that
can't possibly be implemented in Lisp, or that just won't be for
performance reasons.

Special forms are mostly macrolike; functionlike forms that should be
special, like "car", are often defined as themselves. This works because
you need a Lisp compiler to compile a Lisp compiler; it's circular
reasoning, but you'll live.

> (c) What other functions are considered special and cannot be
> overriden? Is there a document somewhere that specifies the set of
> special forms?
> 
The Common Lisp Hyperspec is a very nifty piece of work. Google is your
friend here, because I'm too tired to be.

> My understanding is that there are truly keywordless languages like
> Io, where every single function is overridable/interceptible. If
> functions like "setq" are not overridable, what do Lisp programmers do
> when they want to intercept assignments/bindings?
> 
"setq" is effectively deprecated; you should use setf instead, which is
indeed extensible using something like (defun (setf place) whatever). The
details are beyond me, as I haven't done it yet.

The upshot is that setf works on everything that setq does, but not vice
versa. Setq is best avoided, perhaps unless you're into macrology.
From: Kalle Olavi Niemitalo
Subject: Re: Language keywords or special forms in Lisp
Date: 
Message-ID: <87oenrykxp.fsf@Astalo.kon.iki.fi>
Svein Ove Aas <··············@brage.info> writes:

> "setq" is effectively deprecated; you should use setf instead, which is
> indeed extensible using something like (defun (setf place) whatever).

I think that's too strict.

If the place to be assigned is a compound form, e.g. (gethash key
category-table), you must use SETF.  SETQ would just signal an
error.  (Or is the behavior entirely undefined?  I don't know
and section 3.5 does not help as SETQ is not a function.)

If the place to be assigned is a symbol macro, you need SETF in
principle; but you can also use SETQ, which is treated as SETF in
this case.  Which one you choose it is a matter of style;
neither is more "extensible" than the other.

If the place to be assigned is a variable, you need SETQ in
principle; but you can also use SETF, which expands to SETQ in
this case.  Again, it is a matter of style.

> Setq is best avoided, perhaps unless you're into macrology.

I'm not sure what you mean by "macrology" here.
From: Svein Ove Aas
Subject: Re: Language keywords or special forms in Lisp
Date: 
Message-ID: <ca9b66$rlc$1@services.kq.no>
Kalle Olavi Niemitalo wrote:

> Svein Ove Aas <··············@brage.info> writes:
> 
>> "setq" is effectively deprecated; you should use setf instead, which is
>> indeed extensible using something like (defun (setf place) whatever).
> 
> I think that's too strict.
> 
> [details]
> 
> If the place to be assigned is a variable, you need SETQ in
> principle; but you can also use SETF, which expands to SETQ in
> this case.  Again, it is a matter of style.
> 
My style is that I don't want to need to rememeber more symbols than I
need to; thus, I just use setf everywhere.

>> Setq is best avoided, perhaps unless you're into macrology.
> 
> I'm not sure what you mean by "macrology" here.

As you mentioned, setq doesn't handle compund forms; in particular, if you
write a macro for which one of the arguments is a variable, using setq on
it is an additional sanity test to make sure it's actually a variable and
not a place.
From: Joe Marshall
Subject: Re: Language keywords or special forms in Lisp
Date: 
Message-ID: <pt87tq0a.fsf@ccs.neu.edu>
··········@yahoo.com (Hung Jung Lu) writes:

> Hi,
>
> I am experimenting with Lisp a little bit. I tried to redefine the
> "if" function (actually, the same is true for "setq", or "t"):
>
> defun if (x y z) (+ x y z)
>
> but the interpreter did not allow me to do so. So the question is:
>
> (a) Does Lisp have "language keywords" (like "if", "while", "return",
> etc. in other languages)?
>
> (b) The fact that the "if" function cannot be overriden: is this
> particular to specific implementation of Lisp? Does it happen only for
> interpreters? Or this is a general limitation?
>
> (c) What other functions are considered special and cannot be
> overriden? Is there a document somewhere that specifies the set of
> special forms?
>
> My understanding is that there are truly keywordless languages like
> Io, where every single function is overridable/interceptible. If
> functions like "setq" are not overridable, what do Lisp programmers do
> when they want to intercept assignments/bindings?

All the answers you seek may be found in the Common Lisp Hyperspec.