[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Issue: DECLARE-TYPE-FREE (Version 3)



I added a reference to DECLARATION-SCOPE. I added a Discussion section. I
changed "Most people interpret..." to "Some people interpret..." in the problem
discussion, since we need not argue about how many people think that it is valid
Common Lisp.

Ready for release? (regrets only, please)

!
Issue:         DECLARE-TYPE-FREE

References:    CLtL p.158
		DECLARATION-SCOPE

Category:      CLARIFICATION/ADDITION

Edit history:  Version 1, 18-Sep-88, Moon
               Version 2, 22-Sep-88, Moon
                (small edits to reflect mail discussion)
		Version 3, 22-Sep-88, Masinter
		

Problem description:

  Some people interpret CLtL's phrase "(TYPE type var1 var2 ...) affects
  only variable bindings" to mean that code such as the following is
  not valid Common Lisp, because a type declaration can only be attached
  to a binding, not used free.
  
    (if (and (typep x 'fixnum) (typep y 'fixnum))
	(locally (declare (fixnum x y))
	  ...algorithm using x and y...)
	...similar algorithm using x and y...)

Proposal (DECLARE-TYPE-FREE:ALLOW):
  
  Avoid the phrase "affects only variable bindings".  Clarify that a type
  declaration means that it is an error for the value of the variable to
  be not a member of the declared type, within the scope of the
  declaration.  Clarify that this makes the above program a valid program
  and that this kind of declaration means the same thing as inserting
  THE in every reference to the variable and every setq of the variable.
  Clarify that if nested type declarations refer to the same variable,
  the value of the variable must be a member of the intersection of the
  declared types.

Rationale:

  There is no strong reason to forbid this usage, and people have 
  often asked  for it.

Current practice:

  Lucid implements DECLARE-TYPE-FREE:ALLOW already, with a warning that
  it is an extension to Common Lisp.

Cost to Implementors:

  None, it is valid to ignore type declarations.

Cost to Users:

  None, this is a compatible addition.

Cost of non-adoption:

  Common Lisp will be less self-consistent.

Benefits:

  The above example will not have to be written in the following silly way,
  which only works if x and y are used read-only:
  
    (if (and (typep x 'fixnum) (typep y 'fixnum))
	(let ((x x) (y y))
	  (declare (fixnum x y))
	  ...algorithm using x and y...)
	...similar algorithm using x and y...)

Esthetics:

  This provides a clean and simple way for the programmer to express what
  she knows about the values a variable may take on within a certain
  region of the program.  Different compilers will use this information in
  different ways; it's most aesthetic for the portable language to be free
  of assumptions about how the compiler will use this information.

Discussion:

Another cleanup issue, DECLARATION-SCOPE, addresses the scope 
of declarations. This proposal carefully uses the phrase "within the 
scope of the declaration" to avoid confounding the two issues. 

This issue has been discussed at length. There is some possibility 
that current implementations might be able to generate more
 efficient code when declarations are associated with a particular 
binding, e.g., 

(let ((x v)) (declare (type fixnum x)) (+ x x))

might be more efficient than

(let ((x v)) (locally (declare (type fixnum x)) (+ x x)))

However, the local type declarations allowed by this proposal
 do provide useful information even when it is not the *most* useful.