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

Re: Optional types




   Date: Thu, 6 Dec 2001 17:07:25 -0400
   From: shivers@cc.gatech.edu
   To: ll1-discuss@ai.mit.edu
   Subject: Optional types
   
   One problem with this idea of "optional type declarations" is that crossing
   from a dynamically-typed world into a declared-type world can be quite
   expensive. An example which has bedeviled me before: suppose we write a
   function foo that takes a list of strings. Because this function is an 
exported
   part of some module, we declare its type, because we agree with Dan (I do,
   actually). Now, in a Lisp setting, we can't control how clients call this
   function -- it could, for example, be called like this:
      (foo (list (read) (read)))
   or perhaps
      (let ((lis (list (read) (read))))
        (if (read)				; If this item is a true value
            (+ (car lis) (cadr lis))	; the list is a list of ints,
            (foo lis)))			; otw, it's a list of strings.
   
   Are we going to typecheck foo's argument when we enter foo? That could 
involve
   an *arbitrarily large* amount of work -- we have to scan the entire list,
   checking each element. It's proportional to the length of the input list.

But so was the cost of the read.  When you take that into account,
the cost of

	(every #'stringp x)

(forgive my Common Lisp accent :-)  is not so bad.  I would probably
like to write your second example as

      (let ((lis (list (read) (read))))
        (declare (optimize (safety 3)))      ;Check types at run time?
        (if (read)			     ; If this item is a true value
            (+ (car lis) (cadr lis))	     ; the list is a list of ints,
            (foo (the (list string) lis))))  ; otw, it's a list of strings.
            
--Guy