Subject: Re: Misusing LOOP correctly?
From: rpw3@rpw3.org (Rob Warnock)
Date: Sun, 11 Nov 2007 18:50:11 -0600
Newsgroups: comp.lang.lisp
Message-ID: <ldmdnR741qjePqranZ2dnUVZ_gmdnZ2d@speakeasy.net>
<squash+go@math.ufl.edu> wrote:
+---------------
| The below might be considered a misuse of LOOP.  My
| question is whether the LOOP spec guarantees the behavior
| that I am seeing in CLISP [in interpreted code].
| 
| (loop for EXIT = (return 'Quit-Abruptly)
|       for var from 3 upto 2
|       finally (return 'Normal-Quit) )     =>  QUIT-ABRUPTLY
| 
| ;; Reversing the order of the `for' clauses:
| 
| (loop for var from 3 upto 2
|       for EXIT = (return 'Quit-Abruptly)
|       finally (return 'Normal-Quit) )     =>  NORMAL-QUIT
+---------------

In CMUCL, both of these give the same return values as you show for
CLISP, but also belch loud complaints about "Deleting unreachable code":

    cmu> (loop for EXIT = (return 'Quit-Abruptly)
	  for var from 3 upto 2
	  finally (return 'Normal-Quit))

    ; In: LOOP FOR

    ;   'NORMAL-QUIT
    ; Note: Deleting unreachable code.
    ; 
    ;   (LOOP FOR EXIT = #...)
    ; --> BLOCK LET 
    ; ==>
    ;   (LET (#)
    ;     (DECLARE #)
    ;     (ANSI-LOOP::LOOP-BODY NIL # NIL # ...))
    ; Note: Variable VAR defined but never used.
    ; 
    QUIT-ABRUPTLY
    cmu> (loop for var from 3 upto 2
	  for EXIT = (return 'Quit-Abruptly)
	  finally (return 'Normal-Quit))
    ;   (LOOP FOR VAR FROM 3...)
    ; --> BLOCK LET LET ANSI-LOOP::LOOP-BODY TAGBODY ANSI-LOOP::LOOP-REALLY-DESETQ 
    ; --> SETQ 1+ 
    ; ==>
    ;   VAR
    ; Note: Deleting unreachable code.
    ; 
    ; --> BLOCK LET 
    ; ==>
    ;   (LET (#)
    ;     (ANSI-LOOP::LOOP-BODY NIL # NIL # ...))
    ; Note: Variable EXIT defined but never used.
    ; 
    NORMAL-QUIT
    cmu> 

If you look carefully at the FOR clauses you have written and
consider them in light of CLHS 6.1.1.6 Order of Execution
<http://alu.org/HyperSpec/Body/sec_6-1-1-6.html> you should
be able to see both why these loops produce the results that
they do and why CMUCL is complaining about unreachable code.


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607