From: ············@gmail.com
Subject: keyword o package?
Date: 
Message-ID: <1126973454.692830.270740@z14g2000cwz.googlegroups.com>
Hi,

I am developing a program consisting of a several parts communicating
with each other via character-based protocols. The protocol use
s-expressions-based syntax; the parts then read messages using lisp's
reader.

Different parts of the program are in different packages.

Should I use a separate package for symbols in each of the protocols
(and does it give any advantages)? Or should I just use keywords for
all protocol symbols?

That is, does

(defpackage "PROTO"
  (:export  "SESSION" "RUN" "TURN" "LEFT" "RIGHT"))

and the communication looking like

(proto:session
  (proto:run 10)
  (proto:turn proto:left)
  (proto:run 20)
  (proto:turn proto:right))

has any advantage over just:

(:session
   (:run 10)
  (:turn :left)
  (:run 20)
  (:turn :right))

to convey communication between to parts of a program (over a pipe or a
socket)?

David

From: Barry Margolin
Subject: Re: keyword o package?
Date: 
Message-ID: <barmar-6E4C34.12444217092005@comcast.dca.giganews.com>
In article <························@z14g2000cwz.googlegroups.com>,
 ·············@gmail.com" <············@gmail.com> wrote:

> Hi,
> 
> I am developing a program consisting of a several parts communicating
> with each other via character-based protocols. The protocol use
> s-expressions-based syntax; the parts then read messages using lisp's
> reader.
> 
> Different parts of the program are in different packages.
> 
> Should I use a separate package for symbols in each of the protocols
> (and does it give any advantages)? Or should I just use keywords for
> all protocol symbols?

Does your application allow for user-defined protocol symbols?  If so, 
you should use packages, so that users can define their own symbols in 
their own packages.  If you use keywords, then there could be collisions 
if someone tries to load enhancements created by different users or 
organizations.

But if these things are fixed by the application, like the way keyword 
arguments to functions are, then you might as well just use keywords.  
The purpose of the package system is to allow coexistence of multiple 
applications without worrying about names being redefined, but if you 
can't define new names then this is not a concern.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Peter Seibel
Subject: Re: keyword o package?
Date: 
Message-ID: <m2vf0zzlte.fsf@gigamonkeys.com>
·············@gmail.com" <············@gmail.com> writes:

> Hi,
>
> I am developing a program consisting of a several parts communicating
> with each other via character-based protocols. The protocol use
> s-expressions-based syntax; the parts then read messages using lisp's
> reader.
>
> Different parts of the program are in different packages.
>
> Should I use a separate package for symbols in each of the protocols
> (and does it give any advantages)? Or should I just use keywords for
> all protocol symbols?
>
> That is, does
>
> (defpackage "PROTO"
>   (:export  "SESSION" "RUN" "TURN" "LEFT" "RIGHT"))
>
> and the communication looking like
>
> (proto:session
>   (proto:run 10)
>   (proto:turn proto:left)
>   (proto:run 20)
>   (proto:turn proto:right))
>
> has any advantage over just:
>
> (:session
>    (:run 10)
>   (:turn :left)
>   (:run 20)
>   (:turn :right))
>
> to convey communication between to parts of a program (over a pipe or a
> socket)?

My rule of thumb is when you need to use names whose identity is
purely determined by some local context, use keywords but if the set
of potential names is not known in advance (and especially if
different folks may neeed to invent their own names for things) then
use non-keyword symbols and packages as appropriate. So in this case,
if the protocol is fixed then the meaning of the names is determined
by the protocol and you should use keywords. But if you provide a
facility for folks to extend the protocol then you should define a
package to use for your own names used in the protocol but leave open
the possibility that other folks will use names in other packages.

-Peter


-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: Barry Margolin
Subject: Re: keyword o package?
Date: 
Message-ID: <barmar-1D21A6.12562217092005@comcast.dca.giganews.com>
In article <··············@gigamonkeys.com>,
 Peter Seibel <·····@gigamonkeys.com> wrote:

> My rule of thumb is when you need to use names whose identity is
> purely determined by some local context, use keywords but if the set
> of potential names is not known in advance (and especially if
> different folks may neeed to invent their own names for things) then
> use non-keyword symbols and packages as appropriate. So in this case,
> if the protocol is fixed then the meaning of the names is determined
> by the protocol and you should use keywords. But if you provide a
> facility for folks to extend the protocol then you should define a
> package to use for your own names used in the protocol but leave open
> the possibility that other folks will use names in other packages.

That's why you write books and I just review them.  You said what I was 
trying to say in my response so much more clearly. :)

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Pascal Bourguignon
Subject: Re: keyword o package?
Date: 
Message-ID: <87u0gjve68.fsf@thalassa.informatimago.com>
·············@gmail.com" <············@gmail.com> writes:
> I am developing a program consisting of a several parts communicating
> with each other via character-based protocols. The protocol use
> s-expressions-based syntax; the parts then read messages using lisp's
> reader.
>
> Different parts of the program are in different packages.
>
> Should I use a separate package for symbols in each of the protocols
> (and does it give any advantages)? Or should I just use keywords for
> all protocol symbols?
>
> That is, does
>
> (defpackage "PROTO"
>   (:export  "SESSION" "RUN" "TURN" "LEFT" "RIGHT"))
>
> and the communication looking like
>
> (proto:session
>   (proto:run 10)
>   (proto:turn proto:left)
>   (proto:run 20)
>   (proto:turn proto:right))
>
> has any advantage over just:
>
> (:session
>    (:run 10)
>   (:turn :left)
>   (:run 20)
>   (:turn :right))
>
> to convey communication between to parts of a program (over a pipe or a
> socket)?

If the symbols are public (you would export them),  static (they're
generally not defined dynamically), and used only for themselves (you
don't generally attach values, functions or other attributes (plist)
to them) then KEYWORD would be a good package for them.

In the other cases, it's better to use a specific package.

In addition, you really need keywords only if you want to use EQL on them.

In your case, perhaps you could do something like CL:LOOP: instead of
symbol, take symbol or string designators.  You can accept as well all
these notations:

(proto:session ( :run  10) ( :turn  :left)  ( :run  20) ( :turn   :right))
(proto:session ( "run" 10) ( "turn" "left") ( "run" 20) ( "turn"  "right"))
(proto:session (  run  10) (  turn   left)  (  run  20) (  turn    right))
(proto:session (x:run  10) (y:turn z:left)  (w:run  20) (v:turn  u:right))

Just use EQUALP (if you want to take only symbols) 
or STRING-EQUAL (if you can accept strings too) instead of EQL.
Or, if you need to be case sensitive, EQUAL and STRING=.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Nobody can fix the economy.  Nobody can be trusted with their finger
on the button.  Nobody's perfect.  VOTE FOR NOBODY.
From: ············@gmail.com
Subject: Re: keyword o package?
Date: 
Message-ID: <1126977719.889689.62600@g49g2000cwa.googlegroups.com>
> In addition, you really need keywords only if you want to use EQL on them.

Yes, that's why I use symbols. I use argument dispatch on symbols:

  (defgeneric process-command (processor command &rest args) )

  (defmethod process-commnand ((processor a-processor) (command (eql
:run)) &rest args))
From: Kent M Pitman
Subject: Re: keyword o package?
Date: 
Message-ID: <uu0gjyv92.fsf@nhplace.com>
·············@gmail.com" <············@gmail.com> writes:

> I am developing a program consisting of a several parts communicating
> with each other via character-based protocols. The protocol use
> s-expressions-based syntax; the parts then read messages using lisp's
> reader.
> 
> Different parts of the program are in different packages.
> 
> Should I use a separate package for symbols in each of the protocols
> (and does it give any advantages)? Or should I just use keywords for
> all protocol symbols?

Keywords won't give you checking for mis-spellings.
The reader will make new keywords automatically.
Exported symbols in a package, by contrast, will cause reader failures.

This may or may not be good.  You might want read to tend to succeed
and might want to error-check on a post-pass.

You could, of course, choose to define that your protocol will always
read relative to a given package, so that explicit package references are
not needed.

> That is, does
> 
> (defpackage "PROTO"
>   (:export  "SESSION" "RUN" "TURN" "LEFT" "RIGHT"))
> 
> and the communication looking like
> 
> (proto:session
>   (proto:run 10)
>   (proto:turn proto:left)
>   (proto:run 20)
>   (proto:turn proto:right))

(defpackage "PROTO-USER"
  (:use "PROTO"))

(defvar *proto-user-package* (find-package "PROTO-USER"))

(defun read-proto-form (stream)
  (let ((*package* *proto-user-package*))
    (read stream)))

then use

(session
 (run 10)
 (turn ...)
 ...)

If you use this trick instead of requiring explicit prefixes, though, 
you again won't catch misspellings in the reader, since
if you write ruin instead of run, you'll just get proto-user::ruin
instead of proto:run.  Again, that may be good or bad depending on how
early you want your error checking.

> has any advantage over just:
> 
> (:session
>    (:run 10)
>   (:turn :left)
>   (:run 20)
>   (:turn :right))

Or you could use:

(defvar *keyword-package* (find-package "KEYWORD"))

(defun read-proto-form (stream)
  (let ((*package* *keyword-package*))
    (read stream)))

then use

(session
  (run 10)
  (turn left)
  ...)

> to convey communication between to parts of a program (over a pipe or a
> socket)?

I'd definitely use your own package if you are going to put data
on the plist of the symbol.  I don't like to put stuff on a keyword's
plist, mostly because it's a shared resource and (a) you have to be
careful to use a packaged key and (b) you end up with efficiency issues
if lots of applications do this because they have to skip past each other's
data (sometimes needlessly if it could be as usefully stored elsewhere).

I'd maybe use the keyword package if the keys were things that were lisp
names that you were using for non-lispy purposes, to emphasize that the
names were not lisp functions, etc.

I'd not use the keyword package if this was a namespace that users could
extend.  e.g., if there's going to be a deffoo form that can make more
words like :run, :turn, etc., then you should put it in a packaged namespace
so people can redefine / shadow / inherit each others' names.

But in many ways, it's a judgment call.
From: ············@gmail.com
Subject: Re: keyword o package?
Date: 
Message-ID: <1127023057.227173.219110@g43g2000cwa.googlegroups.com>
> (defpackage "PROTO-USER"
>   (:use "PROTO"))

Hi Kent,

I had thought about it, but this creates a problem. The protocol
vocabulary (actually, a markup grammar) is big, and doing so causes
conflicts with symbols from other used packages (most notably, from
"COMMON-LISP"). 

David