Tail call optimization and OO
From: Matthew D Swank (akopa-is-very-much-like-my-mail-address_at_c.net)
Date: 03/24/05
- Next message: jiang_cpsc_at_hotmail.com: "ole calls"
- Previous message: M Jared Finder: "Re: Newbie lisper looking for hints"
- Next in thread: Pascal Costanza: "Re: Tail call optimization and OO"
- Reply: Pascal Costanza: "Re: Tail call optimization and OO"
- Reply: Ulrich Hobelmann: "Re: Tail call optimization and OO"
- Reply: Eric Daniel: "Re: Tail call optimization and OO"
- Reply: Juliusz Chroboczek: "Re: Tail call optimization and OO"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 24 Mar 2005 11:39:42 -0600
Matthias Felleisen gave a presentatation at ECOOP 2004 titled
"Functional Objects"
(http://www.ccs.neu.edu/home/matthias/Presentations/ecoop2004.pdf).
In it he points to what he sees as deficiencies in OO languages and
practice that could be addressed with ideas from functional programming.
In Part III (starting at slide 54) he presents an example that is
supposed to show that an OO language is deficient if it does not provide
tail call optimization.
A paraphrase of the example: It is a good idea to replace a
union with a hierarchy of classes. For example we can think
of lists as an abstract list --'alist', and two concrete
subclasses: 'empty' and 'cons-cell'. Felleisen gives his
implementation in Java (which seems to have a few errors
in the slides). I give a CLOS equivalent:
(defclass alist () ())
(defclass empty (alist) ())
(defclass cons-cell (alist)
((head :initarg :head :accessor the-head)
(tail :initarg :tail :accessor the-tail)))
(defgeneric how-many (o a))
(defgeneric cons-length (sequence))
(defmethod how-many ((o cons-cell) a)
(how-many (the-tail o) (+ 1 a)))
(defmethod how-many ((o empty) a)
a)
(defmethod cons-length ((sequence alist))
(how-many sequence 0))
(defun kons (kar kdr)
(let ((cell (make-instance 'cons-cell :head kar :tail kdr)))
cell))
;; just a util to make large lists
(defun make-alist (n cons)
(if (> n 0)
(make-alist (- n 1) (kons 1 cons))
cons))
(defvar naught (make-instance 'empty))
Of course the implementation of 'how-many' breaks on large list in
languages (and lisp implementations) with out tail call elimination.
(Felleisen makes the blanket claim that how-many breaks in CLOS!)
My question: is this a straw-man? Felleisen implements
a "fix" by implementing 'how-many' in 'alist' by using a for
loop (which terminates by explicitly testing for an
instance of 'empty'), and claims this violates principles of
good object oriented design.
Thanks,
Matt
-- "You do not really understand something unless you can explain it to your grandmother." — Albert Einstein.
- Next message: jiang_cpsc_at_hotmail.com: "ole calls"
- Previous message: M Jared Finder: "Re: Newbie lisper looking for hints"
- Next in thread: Pascal Costanza: "Re: Tail call optimization and OO"
- Reply: Pascal Costanza: "Re: Tail call optimization and OO"
- Reply: Ulrich Hobelmann: "Re: Tail call optimization and OO"
- Reply: Eric Daniel: "Re: Tail call optimization and OO"
- Reply: Juliusz Chroboczek: "Re: Tail call optimization and OO"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|