From: Paul F. Dietz
Subject: Should the FOR ... ACROSS clause in LOOP observe the fill-pointer?
Date: 
Message-ID: <3DC46190.2040700@dls.net>
Section 6.1.2.1.5 of the CL spec states that for-as-across binds the
iteration variable to each element of a vector.  "Iteration stops when
there are no more elements in the supplied array that can be referenced."

Now... elements beyond the fill pointer are still elements, they are
just not 'active' elements.  They can still be referenced by AREF.  So
as described (LOOP FOR x ACROSS a DO ...) should operate on all
elements, including the inactive ones.

However, the MIT loop package doesn't work this way; it uses LENGTH
to determine how far to iterate into the vector (and LENGTH respects
the fill pointer).  From CMU CL:

* (macroexpand '(loop for e across x do (f e)))

(LET ((E NIL) (#:G844 X) (#:G845 0) (#:G846 0))
   (DECLARE (TYPE FIXNUM #:G846)
            (TYPE FIXNUM #:G845)
            (TYPE (OR (MEMBER NIL) VECTOR) #:G844))
   (BLOCK NIL
     (ANSI-LOOP::LOOP-BODY ((SETQ #:G846 (LENGTH #:G844)))
                           ((WHEN (>= #:G845 #:G846) (GO ANSI-LOOP::END-LOOP))
                            (ANSI-LOOP::LOOP-REALLY-DESETQ E
                                                           (AREF #:G844 #:G845))
                            NIL
                            (ANSI-LOOP::LOOP-REALLY-DESETQ #:G845 (1+ #:G845)))
                           ((F E))
                           ((WHEN (>= #:G845 #:G846) (GO ANSI-LOOP::END-LOOP))
                            (ANSI-LOOP::LOOP-REALLY-DESETQ E
                                                           (AREF #:G844 #:G845))
                            NIL
                            (ANSI-LOOP::LOOP-REALLY-DESETQ #:G845 (1+ #:G845)))
                           NIL)))
T
* (loop for e across (make-array '(5) :initial-contents '(a b c d e) :fill-pointer 3) collect e)

(A B C)
*


What was intended here?

	Paul

From: Nils Goesche
Subject: Re: Should the FOR ... ACROSS clause in LOOP observe the fill-pointer?
Date: 
Message-ID: <874razxz6x.fsf@darkstar.cartan>
"Paul F. Dietz" <·····@dls.net> writes:

> Section 6.1.2.1.5 of the CL spec states that for-as-across
> binds the iteration variable to each element of a vector.
> "Iteration stops when there are no more elements in the
> supplied array that can be referenced."
> 
> Now... elements beyond the fill pointer are still elements,
> they are just not 'active' elements.  They can still be
> referenced by AREF.  So as described (LOOP FOR x ACROSS a DO
> ...) should operate on all elements, including the inactive
> ones.
> 
> However, the MIT loop package doesn't work this way; it uses
> LENGTH to determine how far to iterate into the vector (and
> LENGTH respects the fill pointer).  From CMU CL:

Hm, but frankly, I would be rather disappointed if for-as-across
would loop over the fill pointer; I can hardly imagine that that
is what was intended; at least some indication is in

# 3.6 Traversal Rules and Side Effects

# Array traversal 

#  For array traversal operations, the array is not allowed to be
#  adjusted and its fill pointer, if any, is not allowed to be
#  changed.

Apparently somebody was thinking, too, that the fill pointer /is/
relevant when traversing an array.  So, we get

CL-USER 13 > (let ((foo (make-array 42 :fill-pointer 3 :initial-element 'bar)))
               (map 'list (lambda (x) (cons 'foo x)) foo))
((FOO . BAR) (FOO . BAR) (FOO . BAR))

It is one of the nicest features of having a fill pointer that we
can use FOO here as if it were an array of length 3 in pretty
much every situation.

Regards,
-- 
Nils G�sche
Ask not for whom the <CONTROL-G> tolls.

PGP key ID #xD26EF2A0
From: Paul F. Dietz
Subject: Re: Should the FOR ... ACROSS clause in LOOP observe the fill-pointer?
Date: 
Message-ID: <lR6cnfnZ8qIPD1mgXTWc3g@dls.net>
Nils Goesche wrote:

> Apparently somebody was thinking, too, that the fill pointer /is/
> relevant when traversing an array.  So, we get
> 
> CL-USER 13 > (let ((foo (make-array 42 :fill-pointer 3 :initial-element 'bar)))
>                (map 'list (lambda (x) (cons 'foo x)) foo))
> ((FOO . BAR) (FOO . BAR) (FOO . BAR))

That's a consequence of this sentence from the page for class SEQUENCE:

"When viewing a vector as a sequence, only the active elements of that
  vector are considered elements of the sequence; that is, sequence
  operations respect the fill pointer when given sequences represented
  as vectors."

But does FOR-AS-ACROSS view the vector as a sequence, or just as a vector?
You can't use this clause with an arbitrary sequence.

	Paul
From: Nils Goesche
Subject: Re: Should the FOR ... ACROSS clause in LOOP observe the fill-pointer?
Date: 
Message-ID: <87wunvwd3i.fsf@darkstar.cartan>
"Paul F. Dietz" <·····@dls.net> writes:

> Nils Goesche wrote:
> 
> > Apparently somebody was thinking, too, that the fill pointer
> > /is/ relevant when traversing an array.  So, we get

> > CL-USER 13 > (let ((foo (make-array 42 :fill-pointer 3
> >                          :initial-element 'bar)))
> >                (map 'list (lambda (x) (cons 'foo x)) foo))
> > ((FOO . BAR) (FOO . BAR) (FOO . BAR))
> 
> That's a consequence of this sentence from the page for class
> SEQUENCE:
> 
> "When viewing a vector as a sequence, only the active elements
>  of that vector are considered elements of the sequence; that
>  is, sequence operations respect the fill pointer when given
>  sequences represented as vectors."

Even better, thanks.  I overlooked that, somehow.

> But does FOR-AS-ACROSS view the vector as a sequence, or just
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> as a vector?  You can't use this clause with an arbitrary
> sequence.

Yes, that seems to be the question here.  Unfortunately (or not),
the HyperSpec is written in English, not in something as, say,
denotational semantics.  Does it ``view the vector as a
sequence��?  One might think ``Well, it better should as every
vector /is/ a sequence, after all!��.  I don't think that in
cases like this it is very helpful to look ``too closely�� at the
words.  So, what do /you/ think?  Wouldn't you be quite
flabbergasted if for-as-across hopped happily over the fill
pointer?  I know I would!  Wouldn't that be against your
expectations, too?

Regards,
-- 
Nils G�sche
Ask not for whom the <CONTROL-G> tolls.

PGP key ID #xD26EF2A0
From: Paul F. Dietz
Subject: Re: Should the FOR ... ACROSS clause in LOOP observe the fill-pointer?
Date: 
Message-ID: <kzidnb0Vssp4BVmgXTWc2Q@dls.net>
Nils Goesche wrote:

>    So, what do /you/ think?

I think the spec writers were trying to reflect what the existing
LOOP macro implementation(s) already did, and they screwed up here.

But I'd like someone who was there to confirm this.

	Paul
From: Barry Margolin
Subject: Re: Should the FOR ... ACROSS clause in LOOP observe the fill-pointer?
Date: 
Message-ID: <Wgxx9.7$zB.704@paloalto-snr1.gtei.net>
In article <··············@darkstar.cartan>,
Nils Goesche  <···@cartan.de> wrote:
>"Paul F. Dietz" <·····@dls.net> writes:
>> But does FOR-AS-ACROSS view the vector as a sequence, or just
>                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> as a vector?  You can't use this clause with an arbitrary
>> sequence.
>
>Yes, that seems to be the question here.  Unfortunately (or not),
>the HyperSpec is written in English, not in something as, say,
>denotational semantics.  Does it ``view the vector as a
>sequence��?  One might think ``Well, it better should as every
>vector /is/ a sequence, after all!��.  I don't think that in
>cases like this it is very helpful to look ``too closely�� at the
>words.  So, what do /you/ think?  Wouldn't you be quite
>flabbergasted if for-as-across hopped happily over the fill
>pointer?  I know I would!  Wouldn't that be against your
>expectations, too?

Functions that "view the vector as a sequence" are generally described as
operating on a sequence, not a vector.  Since FOR-AS-ACROSS specifically
says "vector", not "sequence", it's not treating it as a sequence.

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Nils Goesche
Subject: Re: Should the FOR ... ACROSS clause in LOOP observe the fill-pointer?
Date: 
Message-ID: <lkwuntz2kt.fsf@cartan.de>
Barry Margolin <······@genuity.net> writes:

> In article <··············@darkstar.cartan>,
> Nils Goesche  <···@cartan.de> wrote:
> >"Paul F. Dietz" <·····@dls.net> writes:
> >> But does FOR-AS-ACROSS view the vector as a sequence, or just
> >                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >> as a vector?  You can't use this clause with an arbitrary
> >> sequence.
[*]
> >Does it ``view the vector as a sequence��?  One might think ``Well,
> >it better should as every vector /is/ a sequence, after all!��.
[/*]
> >So, what do /you/ think?  Wouldn't you be quite flabbergasted if
> >for-as-across hopped happily over the fill pointer?  I know I
> >would!  Wouldn't that be against your expectations, too?
> 
> Functions that "view the vector as a sequence" are generally
> described as operating on a sequence, not a vector.  Since
> FOR-AS-ACROSS specifically says "vector", not "sequence", it's not
> treating it as a sequence.

Yes, the passage [*] was ironical, I thought that was clear, sorry.  I
agree that one could read the standard as if for-as-across would have
to ignore the fill pointer.  However, I think that nobody would
actually expect this and suspect that they simply forgot to mention
what happens if the vector has a fill pointer.

But I wouldn't call this a ``screw-up��.  Whenever a language is
described in English, rather than something mathematical, you can come
up with lots of cases where a literal interpretation would lead to
strange results, or contradictions, or simply fail to give an answer.
You do not read a non-mathematical document like you read a
mathematical proof.  A reader of a mathematical proof is literally
invited to read it as maliciously as possible in order to find some
case where it doesn't work.  Although, at least in published papers,
you are at least asked to find a set of definitions that make the
proofs in the paper work -- so the author doesn't have to mention, for
instance, which sign he likes to give the curvature tensor, or the
Laplace operator, or whether 0 is a natural number, and a million
other things before he can even begin saying what it is he wants to
tell us in his paper.

I mean, if we start taking everything so literally, without some
benevolence and common sense, we might as well start demanding from
our vendors to properly interpret and compile code like

(defun fac (n)
  (let ((ret 1))
    #1=(if (zerop n)
           ret
         (progn
           (setq ret (* n ret)
                 n (1- n))
           #1#))))

provided it doesn't rule out code like this somewhere :-)

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0