From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 1A845BC32 for ; Thu, 17 Mar 2005 06:40:53 +0100 (CET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j2H5epSp022654 for ; Thu, 17 Mar 2005 06:40:52 +0100 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j2H5elFQ003956; Thu, 17 Mar 2005 14:40:47 +0900 (JST) Date: Thu, 17 Mar 2005 14:40:40 +0900 (JST) Message-Id: <20050317.144040.107913221.garrigue@math.nagoya-u.ac.jp> To: twhitehe@uwo.ca Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Tail Recursion on Map, Append, etc. From: Jacques Garrigue In-Reply-To: <200503161105.49726.twhitehe@uwo.ca> References: <200503161105.49726.twhitehe@uwo.ca> X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 42391863.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 recursion:01 recursion:01 compiler:01 caml-list:01 caf:01 recursive:01 compiler:01 sourceforge:01 ocaml:01 ocaml:01 recursive:01 discusion:98 short:01 short:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: From: Tyson Whitehead > I was wondering about the status of map and friends with regard to tail > recursion. There was a big discussion back in 2003 about specific solutions > (implementation of the those functions using Obj) and more general compiler > support for holes/destination passing. It started with the following > message: > > http://caml.inria.fr/pub/ml-archives/caml-list/2003/01/4a9754e53ff07723caf21b4496d1d267.en.html > > It sounded to me like the general consensus was to immediately implement the > specific tail recursive versions of these functions for List and friends > (which were provided in the discusion), and then improve the compiler by > adding support for advanced hole/destination passing solutions. The result of this consensus is the extlib library. It provides those tail-recursive functions. http://ocaml-lib.sourceforge.net/ There is no project to include specific support in the compiler, but the extlib implementation shows that this is not required if a bit of magic is permitted. Note that this list is not the core developers list, so the result of discussions here does not imply anything on the ocaml distribution itself. > Looking at the list implementation in the OCaml Debian unstable source, it > doesn't look like the more efficient version has been implemented. Further, > looking at the assembler emitted for the code it doesn't look like the > compiler supports holes/destination passing either. To correct a misunderstanding: tail-recursive versions are not necessarily more efficient (at least on short lists; short meaning less than 10000 elements), but they are safer. Non tail-recursive functions are carefully marked in the list module, and alternative functions are included. For instance: let safe_map f l = List.rev (List.rev_map f l) let safe_append l1 l2 = List.rev_append (List.rev l1) l2 provide you with tail recursive versions of map and append. In practice they perform relatively well, if you don't want to download extlib. Jacques Garrigue