From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id E2EF9BB81 for ; Mon, 13 Feb 2006 03:05:37 +0100 (CET) Received: from smtp1.adl2.internode.on.net (smtp1.adl2.internode.on.net [203.16.214.181]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k1D25ZJ8014740 for ; Mon, 13 Feb 2006 03:05:37 +0100 Received: from rosella (ppp21-250.lns2.syd7.internode.on.net [59.167.21.250]) by smtp1.adl2.internode.on.net (8.13.5/8.13.5) with ESMTP id k1D25EHc058184; Mon, 13 Feb 2006 12:35:14 +1030 (CST) (envelope-from skaller@users.sourceforge.net) Subject: Re: [Caml-list] HOFs, recursion, and being tail-rec... From: skaller To: Jacques Garrigue Cc: jonathan.roewen@gmail.com, caml-list@yquem.inria.fr In-Reply-To: <20060213.085348.126575598.garrigue@math.nagoya-u.ac.jp> References: <20060213.085348.126575598.garrigue@math.nagoya-u.ac.jp> Content-Type: text/plain Date: Mon, 13 Feb 2006 13:05:14 +1100 Message-Id: <1139796314.8591.79.camel@rosella.wigram> Mime-Version: 1.0 X-Mailer: Evolution 2.4.1 Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 43EFE96F.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 hofs:01 recursion:01 ocaml:01 optimise:01 stack:01 backtracking:01 stack:01 transformed:01 depth-first:01 semantics:01 compilers:01 compilers:01 wrote:01 sourceforge:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 On Mon, 2006-02-13 at 08:53 +0900, Jacques Garrigue wrote: > From: Jonathan Roewen > > > > I have a simple implementation of depth-first-search, and was > > wondering if my approach would qualify as tail-rec (whether from the > > code it is/isn't, and whether ocaml can optimise it so it is). > > By definition a depth-first-search cannot be tail-recursive: you need > a stack to implement the backtracking. There is a need for a stack, but it doesn't have to be a machine (control) stack. A basic principle of duality seems to be that control and data can always be transformed into each other (proof: Turing only has conditional goto). In this case CPS provides the transform. There is category error in Jacques claim: depth-first search is an algorithm, it is a matter of *semantics*. Tail-rec is merely a *syntactic* property, which has semantic implications only for a particular implementation. So it isn't a well formed sentence to say depth first search cannot be tail-rec, it is easy to construct a depth first search in any FPL that is. However, no matter what you do, you will indeed need memory linear in the tree depth (unless it is 1-ary tree as pointed out). BTW: considering control/data duality you will find that most compilers miss very interesting optimisations. Ackermann's function can be implemented using only 2 words (for the arguments) per stack frame. No compilers I know of do this optimisation -- and I have not seen any reference to it in the literature. -- John Skaller Felix, successor to C++: http://felix.sf.net