From: Kamen TOMOV
Subject: packages and symbols mystery
Date: 
Message-ID: <ud516xrl9.fsf@cybuild.com>
I need help to understand a CL concept. I have 2 packages that export
the same symbol and a package that use those packages. I need help
understanding how to shadow one of the symbols. I thought I
understand it.

This is what I mean:

(in-package :cl-user)
(defpackage p1
  (:use
   #:common-lisp)
  (:export
   #:f1
   #:m1
   #:+t-exp+))
(in-package :p1)
(defmacro m1 ()
  `+t-exp+)
(defun f1 ()
  +t-exp+)
(defparameter +t-exp+ 'set-in-p1)

(in-package :cl-user)
(defpackage p2
  (:use
   #:common-lisp)
  (:export
   #:+t-exp+))
(in-package :p2)
(defparameter +t-exp+ 'set-in-p2)
(format t "Just set +t-exp+ to ~s~%" +t-exp+)

(in-package :cl-user)
(defpackage p-demo1
  (:use
   #:common-lisp
   #:p1
   #:p2)
  (:shadowing-import-from :p1 +t-exp+))
(in-package :p-demo1)
;; the following is supposed to have a value 'set-in-p1
;; and as we can see that's the case:
(m1)
(f1)

(in-package :cl-user)
(defpackage p-demo
  (:use
   #:common-lisp
   #:p1
   #:p2)
  (:shadowing-import-from :p2 +t-exp+))
(in-package :p-demo)
;; the following produces the result I can't uderstand
;; why the value isn't 'set-in-p2? what to do to make it so?
(m1)
(f1)

Thanks in advance!

-- 
Kamen
http://www.cybuild.com/~kat

From: Rainer Joswig
Subject: Re: packages and symbols mystery
Date: 
Message-ID: <joswig-699018.12495012052007@news-europe.giganews.com>
In article <·············@cybuild.com>, Kamen TOMOV <·····@evrocom.net> 
wrote:

> I need help to understand a CL concept. I have 2 packages that export
> the same symbol and a package that use those packages. I need help
> understanding how to shadow one of the symbols. I thought I
> understand it.
> 
> This is what I mean:
> 
> (in-package :cl-user)
> (defpackage p1
>   (:use
>    #:common-lisp)
>   (:export
>    #:f1
>    #:m1
>    #:+t-exp+))
> (in-package :p1)
> (defmacro m1 ()
>   `+t-exp+)
> (defun f1 ()
>   +t-exp+)
> (defparameter +t-exp+ 'set-in-p1)
> 
> (in-package :cl-user)
> (defpackage p2
>   (:use
>    #:common-lisp)
>   (:export
>    #:+t-exp+))
> (in-package :p2)
> (defparameter +t-exp+ 'set-in-p2)
> (format t "Just set +t-exp+ to ~s~%" +t-exp+)
> 
> (in-package :cl-user)
> (defpackage p-demo1
>   (:use
>    #:common-lisp
>    #:p1
>    #:p2)
>   (:shadowing-import-from :p1 +t-exp+))
> (in-package :p-demo1)
> ;; the following is supposed to have a value 'set-in-p1
> ;; and as we can see that's the case:
> (m1)
> (f1)
> 
> (in-package :cl-user)
> (defpackage p-demo
>   (:use
>    #:common-lisp
>    #:p1
>    #:p2)
>   (:shadowing-import-from :p2 +t-exp+))
> (in-package :p-demo)
> ;; the following produces the result I can't uderstand
> ;; why the value isn't 'set-in-p2? what to do to make it so?
> (m1)
> (f1)
> 
> Thanks in advance!

Package p1 has a symbol +t-exp+.
Package p2 has a different symbol +t-exp+ .

Using the symbol  +t-exp+  from p2
in some package p-demo does not magically
change the code of Lisp
functions defined before. They still use
the same old symbols as when defined.
Unless you explicitly look up the symbols
before/during read-time.

Side issue:
Sometimes the compiler gets rid of symbols
all together and the symbols only
exist at some read-time and then are no
longer available -> lexical variables and
lexical functions.


Btw.,

I find this version of the package declaration
much friendly to read and it avoids introducing
interned or uninterned symbols during read-time:

(defpackage "P1"
  (:use "COMMON-LISP")
  (:export "F1" "M1" "+T-EXP+"))

And no, I don't care about lower-case variants
of Common Lisp.

-- 
http://lispm.dyndns.org
From: Kamen TOMOV
Subject: Re: packages and symbols mystery
Date: 
Message-ID: <ud516guti.fsf@cybuild.com>
On ������, ��� 12 2007, Rainer Joswig wrote:

> In article <·············@cybuild.com>, Kamen TOMOV <·····@evrocom.net> 
> wrote:
>
>> I need help to understand a CL concept. I have 2 packages that export
>> the same symbol and a package that use those packages. I need help
>> understanding how to shadow one of the symbols. I thought I
>> understand it.
>> 
>> This is what I mean:
>> 
>> (in-package :cl-user)
>> (defpackage p1
>>   (:use
>>    #:common-lisp)
>>   (:export
>>    #:f1
>>    #:m1
>>    #:+t-exp+))
>> (in-package :p1)
>> (defmacro m1 ()
>>   `+t-exp+)
>> (defun f1 ()
>>   +t-exp+)
>> (defparameter +t-exp+ 'set-in-p1)
>> 
>> (in-package :cl-user)
>> (defpackage p2
>>   (:use
>>    #:common-lisp)
>>   (:export
>>    #:+t-exp+))
>> (in-package :p2)
>> (defparameter +t-exp+ 'set-in-p2)
>> (format t "Just set +t-exp+ to ~s~%" +t-exp+)
>> 
>> (in-package :cl-user)
>> (defpackage p-demo1
>>   (:use
>>    #:common-lisp
>>    #:p1
>>    #:p2)
>>   (:shadowing-import-from :p1 +t-exp+))
>> (in-package :p-demo1)
>> ;; the following is supposed to have a value 'set-in-p1
>> ;; and as we can see that's the case:
>> (m1)
>> (f1)
>> 
>> (in-package :cl-user)
>> (defpackage p-demo
>>   (:use
>>    #:common-lisp
>>    #:p1
>>    #:p2)
>>   (:shadowing-import-from :p2 +t-exp+))
>> (in-package :p-demo)
>> ;; the following produces the result I can't uderstand
>> ;; why the value isn't 'set-in-p2? what to do to make it so?
>> (m1)
>> (f1)
>> 
>> Thanks in advance!
>
> Package p1 has a symbol +t-exp+.
> Package p2 has a different symbol +t-exp+ .
>
> Using the symbol  +t-exp+  from p2
> in some package p-demo does not magically
> change the code of Lisp
> functions defined before. They still use
> the same old symbols as when defined.
> Unless you explicitly look up the symbols
> before/during read-time.

OK - right - that's the case with f1. What about the case with the
macro - m1?

> Side issue:
> Sometimes the compiler gets rid of symbols
> all together and the symbols only
> exist at some read-time and then are no
> longer available -> lexical variables and
> lexical functions.
>
>
> Btw.,
>
> I find this version of the package declaration
> much friendly to read and it avoids introducing
> interned or uninterned symbols during read-time:
>
> (defpackage "P1"
>   (:use "COMMON-LISP")
>   (:export "F1" "M1" "+T-EXP+"))
>
> And no, I don't care about lower-case variants
> of Common Lisp.

Naah writing in uppercase is too much work. #:f1 must also be
uninterned.

-- 
Kamen
http://www.cybuild.com/~kat
From: Rainer Joswig
Subject: Re: packages and symbols mystery
Date: 
Message-ID: <joswig-3F6162.13232012052007@news-europe.giganews.com>
In article <·············@cybuild.com>, Kamen TOMOV <·····@evrocom.net> 
wrote:

> On ҙ����, ��� 12 2007, Rainer Joswig wrote:
> 
> > In article <·············@cybuild.com>, Kamen TOMOV <·····@evrocom.net> 
> > wrote:
> >
> >> I need help to understand a CL concept. I have 2 packages that export
> >> the same symbol and a package that use those packages. I need help
> >> understanding how to shadow one of the symbols. I thought I
> >> understand it.
> >> 
> >> This is what I mean:
> >> 
> >> (in-package :cl-user)
> >> (defpackage p1
> >>   (:use
> >>    #:common-lisp)
> >>   (:export
> >>    #:f1
> >>    #:m1
> >>    #:+t-exp+))
> >> (in-package :p1)
> >> (defmacro m1 ()
> >>   `+t-exp+)
> >> (defun f1 ()
> >>   +t-exp+)
> >> (defparameter +t-exp+ 'set-in-p1)
> >> 
> >> (in-package :cl-user)
> >> (defpackage p2
> >>   (:use
> >>    #:common-lisp)
> >>   (:export
> >>    #:+t-exp+))
> >> (in-package :p2)
> >> (defparameter +t-exp+ 'set-in-p2)
> >> (format t "Just set +t-exp+ to ~s~%" +t-exp+)
> >> 
> >> (in-package :cl-user)
> >> (defpackage p-demo1
> >>   (:use
> >>    #:common-lisp
> >>    #:p1
> >>    #:p2)
> >>   (:shadowing-import-from :p1 +t-exp+))
> >> (in-package :p-demo1)
> >> ;; the following is supposed to have a value 'set-in-p1
> >> ;; and as we can see that's the case:
> >> (m1)
> >> (f1)
> >> 
> >> (in-package :cl-user)
> >> (defpackage p-demo
> >>   (:use
> >>    #:common-lisp
> >>    #:p1
> >>    #:p2)
> >>   (:shadowing-import-from :p2 +t-exp+))
> >> (in-package :p-demo)
> >> ;; the following produces the result I can't uderstand
> >> ;; why the value isn't 'set-in-p2? what to do to make it so?
> >> (m1)
> >> (f1)
> >> 
> >> Thanks in advance!
> >
> > Package p1 has a symbol +t-exp+.
> > Package p2 has a different symbol +t-exp+ .
> >
> > Using the symbol  +t-exp+  from p2
> > in some package p-demo does not magically
> > change the code of Lisp
> > functions defined before. They still use
> > the same old symbols as when defined.
> > Unless you explicitly look up the symbols
> > before/during read-time.
> 
> OK - right - that's the case with f1. What about the case with the
> macro - m1?


m1 is a macro which uses a symbol +t-exp+ from package p1.
Why should that change somehow later?

> 
> > Side issue:
> > Sometimes the compiler gets rid of symbols
> > all together and the symbols only
> > exist at some read-time and then are no
> > longer available -> lexical variables and
> > lexical functions.
> >
> >
> > Btw.,
> >
> > I find this version of the package declaration
> > much friendly to read and it avoids introducing
> > interned or uninterned symbols during read-time:
> >
> > (defpackage "P1"
> >   (:use "COMMON-LISP")
> >   (:export "F1" "M1" "+T-EXP+"))
> >
> > And no, I don't care about lower-case variants
> > of Common Lisp.
> 
> Naah writing in uppercase is too much work

Really? Has your keyboard a SHIFT-Lock key? No?
Emacs has m-u to upcase text.

> #:f1 must also be
> uninterned.

-- 
http://lispm.dyndns.org
From: Kamen TOMOV
Subject: Re: packages and symbols mystery
Date: 
Message-ID: <u4pmigsbn.fsf@cybuild.com>
On събота, Май 12 2007, Rainer Joswig wrote:

> In article <·············@cybuild.com>, Kamen TOMOV <·····@evrocom.net> 
> wrote:
>
>> On Ò™·ÓÚý, åýÈ 12 2007, Rainer Joswig wrote:
>> 
>> > In article <·············@cybuild.com>, Kamen TOMOV <·····@evrocom.net> 
>> > wrote:
>> >
>> >> I need help to understand a CL concept. I have 2 packages that export
>> >> the same symbol and a package that use those packages. I need help
>> >> understanding how to shadow one of the symbols. I thought I
>> >> understand it.
>> >> 
>> >> This is what I mean:
>> >> 
>> >> (in-package :cl-user)
>> >> (defpackage p1
>> >>   (:use
>> >>    #:common-lisp)
>> >>   (:export
>> >>    #:f1
>> >>    #:m1
>> >>    #:+t-exp+))
>> >> (in-package :p1)
>> >> (defmacro m1 ()
>> >>   `+t-exp+)
>> >> (defun f1 ()
>> >>   +t-exp+)
>> >> (defparameter +t-exp+ 'set-in-p1)
>> >> 
>> >> (in-package :cl-user)
>> >> (defpackage p2
>> >>   (:use
>> >>    #:common-lisp)
>> >>   (:export
>> >>    #:+t-exp+))
>> >> (in-package :p2)
>> >> (defparameter +t-exp+ 'set-in-p2)
>> >> (format t "Just set +t-exp+ to ~s~%" +t-exp+)
>> >> 
>> >> (in-package :cl-user)
>> >> (defpackage p-demo1
>> >>   (:use
>> >>    #:common-lisp
>> >>    #:p1
>> >>    #:p2)
>> >>   (:shadowing-import-from :p1 +t-exp+))
>> >> (in-package :p-demo1)
>> >> ;; the following is supposed to have a value 'set-in-p1
>> >> ;; and as we can see that's the case:
>> >> (m1)
>> >> (f1)
>> >> 
>> >> (in-package :cl-user)
>> >> (defpackage p-demo
>> >>   (:use
>> >>    #:common-lisp
>> >>    #:p1
>> >>    #:p2)
>> >>   (:shadowing-import-from :p2 +t-exp+))
>> >> (in-package :p-demo)
>> >> ;; the following produces the result I can't uderstand
>> >> ;; why the value isn't 'set-in-p2? what to do to make it so?
>> >> (m1)
>> >> (f1)
>> >> 
>> >> Thanks in advance!
>> >
>> > Package p1 has a symbol +t-exp+.
>> > Package p2 has a different symbol +t-exp+ .
>> >
>> > Using the symbol  +t-exp+  from p2
>> > in some package p-demo does not magically
>> > change the code of Lisp
>> > functions defined before. They still use
>> > the same old symbols as when defined.
>> > Unless you explicitly look up the symbols
>> > before/during read-time.
>> 
>> OK - right - that's the case with f1. What about the case with the
>> macro - m1?
>
>
> m1 is a macro which uses a symbol +t-exp+ from package p1.
> Why should that change somehow later?

Because it is is a macro, but I'm still learning macrology and that's
why I need help.

How can we make m1 use +t-exp+ from p2?

>> > Side issue:
>> > Sometimes the compiler gets rid of symbols
>> > all together and the symbols only
>> > exist at some read-time and then are no
>> > longer available -> lexical variables and
>> > lexical functions.
>> >
>> >
>> > Btw.,
>> >
>> > I find this version of the package declaration
>> > much friendly to read and it avoids introducing
>> > interned or uninterned symbols during read-time:
>> >
>> > (defpackage "P1"
>> >   (:use "COMMON-LISP")
>> >   (:export "F1" "M1" "+T-EXP+"))
>> >
>> > And no, I don't care about lower-case variants
>> > of Common Lisp.
>> 
>> Naah writing in uppercase is too much work
>
> Really? 

Apparently. See for yourself:

#:common-lisp - 2 extra key-strokes
- 1 for #
- 1 for :
Easy to use as no extra knowledge or configuration of Emacs necessary.

> HAS your keyboard a SHIFT-Lock key? No?

"COMMON-LISP" - using Caps Lock key - 4 extra key-strokes
- 2 key-strokes for quotes (or one if Emacs is configured)
- 2 key-strokes to turn Caps Lock on and off

> Emacs has m-u to upcase text.

"COMMON-LISP" - using (M-u) - 6 extra key-strokes
- 2 key-strokes for quotes (or one if Emacs is configured)
- 2 key-strokes (C-u) to make it upper case
- 2 key-strokes to pass the string again (M-b) so that (C-u) is
  applied.

-- 
Kamen
http://www.cybuild.com/~kat
From: Rainer Joswig
Subject: Re: packages and symbols mystery
Date: 
Message-ID: <joswig-A5D5E4.15583812052007@news-europe.giganews.com>
In article <·············@cybuild.com>, Kamen TOMOV <·····@evrocom.net> 
wrote:

> > m1 is a macro which uses a symbol +t-exp+ from package p1.
> > Why should that change somehow later?
> 
> Because it is is a macro, but I'm still learning macrology and that's
> why I need help.
> 
> How can we make m1 use +t-exp+ from p2?

Packages are just for symbols. They provided no module
system. Switching to another package
does not switch to another module. It just
affects how symbols are READ, how
they might be interned and where you might be
looking up symbols.

1) If you want the macro to use another symbol,
you have to pass it as an argument.

2) For extreme hackery you
can write a macro that looks which *package*
is current and look up the symbol from
the current package by its name. This is
hackery. ;-)

(FIND-SYMBOL "FOO" *PACKAGE*)

Or like 

 (FIND-SYMBOL (SYMBOL-NAME 'P1:+T-EXP+) *PACKAGE*)

3) you can redefine your macro to use the
new symbol.

It is always useful to use MACROEXPAND,
DESCRIBE and INSPECT.

With MACROEXPAND you can see what the Lisp
system generates when applying the macro.
You can take this form then to INSPECT or
DESCRIBE and see which package the symbols
are belonging to,

CL-USER 48 > (in-package :p-demo)
#<The P-DEMO package, 1/16 internal, 0/16 external>

P-DEMO 49 > (cl:macroexpand '(m1))
P1:+T-EXP+

The package system is a bit tricky to use. After
a few years of usage one is a bit confused,
some more years later understanding the package
system gets easier again. ;-) Especially if one
has a few heuristics that cover the basic use cases.
A similar topic like yours here just came up,
I think yesterday.

Was that 'help' enough?

> 
> >> > Side issue:
> >> > Sometimes the compiler gets rid of symbols
> >> > all together and the symbols only
> >> > exist at some read-time and then are no
> >> > longer available -> lexical variables and
> >> > lexical functions.
> >> >
> >> >
> >> > Btw.,
> >> >
> >> > I find this version of the package declaration
> >> > much friendly to read and it avoids introducing
> >> > interned or uninterned symbols during read-time:
> >> >
> >> > (defpackage "P1"
> >> >   (:use "COMMON-LISP")
> >> >   (:export "F1" "M1" "+T-EXP+"))
> >> >
> >> > And no, I don't care about lower-case variants
> >> > of Common Lisp.
> >> 
> >> Naah writing in uppercase is too much work
> >
> > Really? 
> 
> Apparently. See for yourself:
> 
> #:common-lisp - 2 extra key-strokes
> - 1 for #
> - 1 for :
> Easy to use as no extra knowledge or configuration of Emacs necessary.
> 
> > HAS your keyboard a SHIFT-Lock key? No?
> 
> "COMMON-LISP" - using Caps Lock key - 4 extra key-strokes
> - 2 key-strokes for quotes (or one if Emacs is configured)
> - 2 key-strokes to turn Caps Lock on and off

THIS DOES NOT SEEM MUCH TO ME. ;-)

> 
> > Emacs has m-u to upcase text.
> 
> "COMMON-LISP" - using (M-u) - 6 extra key-strokes
> - 2 key-strokes for quotes (or one if Emacs is configured)
> - 2 key-strokes (C-u) to make it upper case
> - 2 key-strokes to pass the string again (M-b) so that (C-u) is
>   applied.

If that is a problem, I'd write a short editor command
which stringifies and upcases the current symbol -> one keystroke.
There might be editors which have that, not sure.

-- 
http://lispm.dyndns.org
From: Kamen TOMOV
Subject: Re: packages and symbols mystery
Date: 
Message-ID: <uy7juf5zd.fsf@cybuild.com>
On ������, ��� 12 2007, Rainer Joswig wrote:

>> How can we make m1 use +t-exp+ from p2?
>
> Packages are just for symbols. They provided no module
> system. Switching to another package
> does not switch to another module. It just
> affects how symbols are READ, how
> they might be interned and where you might be
> looking up symbols.
>
> 1) If you want the macro to use another symbol,
> you have to pass it as an argument.

I should have mentioned that I have a real situation where the P1 and
P2 packages are provided by third parties. It is either an error of
the designer of P1, or my environment is different.

> 2) For extreme hackery you
> can write a macro that looks which *package*
> is current and look up the symbol from
> the current package by its name. This is
> hackery. ;-)
>
> (FIND-SYMBOL "FOO" *PACKAGE*)
>
> Or like 
>
>  (FIND-SYMBOL (SYMBOL-NAME 'P1:+T-EXP+) *PACKAGE*)

I agree it's hackery and not a good idea.

> 3) you can redefine your macro to use the
> new symbol.

This is what I did, but I got confused by what the author of the
package had to say.

> It is always useful to use MACROEXPAND,
> DESCRIBE and INSPECT.
>
> With MACROEXPAND you can see what the Lisp
> system generates when applying the macro.
> You can take this form then to INSPECT or
> DESCRIBE and see which package the symbols
> are belonging to,
>
> CL-USER 48 > (in-package :p-demo)
> #<The P-DEMO package, 1/16 internal, 0/16 external>
>
> P-DEMO 49 > (cl:macroexpand '(m1))
> P1:+T-EXP+
>
> The package system is a bit tricky to use. After
> a few years of usage one is a bit confused,
> some more years later understanding the package
> system gets easier again. ;-) Especially if one
> has a few heuristics that cover the basic use cases.
> A similar topic like yours here just came up,
> I think yesterday.
>
> Was that 'help' enough?

Yes! It was great discussing this issue - thanks for your help.

>> Apparently. See for yourself:
>> 
>> #:common-lisp - 2 extra key-strokes
>> - 1 for #
>> - 1 for :
>> Easy to use as no extra knowledge or configuration of Emacs necessary.
>> 
>> > HAS your keyboard a SHIFT-Lock key? No?
>> 
>> "COMMON-LISP" - using Caps Lock key - 4 extra key-strokes
>> - 2 key-strokes for quotes (or one if Emacs is configured)
>> - 2 key-strokes to turn Caps Lock on and off
>
> THIS DOES NOT SEEM MUCH TO ME. ;-)

Depends how lazy you are ;-)

>> > Emacs has m-u to upcase text.
>> 
>> "COMMON-LISP" - using (M-u) - 6 extra key-strokes
>> - 2 key-strokes for quotes (or one if Emacs is configured)
>> - 2 key-strokes (C-u) to make it upper case
>> - 2 key-strokes to pass the string again (M-b) so that (C-u) is
>>   applied.
>
> If that is a problem, I'd write a short editor command
> which stringifies and upcases the current symbol -> one keystroke.
> There might be editors which have that, not sure.

In fact an Emacs macro might do it all in one step, if that's how you
prefer.

-- 
Kamen
http://www.cybuild.com/~kat
From: Kalle Olavi Niemitalo
Subject: Re: packages and symbols mystery
Date: 
Message-ID: <87sla2ue9m.fsf@Astalo.kon.iki.fi>
Rainer Joswig <······@lisp.de> writes:

> 2) For extreme hackery you
> can write a macro that looks which *package*
> is current and look up the symbol from
> the current package by its name. This is
> hackery. ;-)

It's not unheard of: DEFSTRUCT does so.
DEFCLASS doesn't, probably for good reasons....