caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
Cc: jonathan.roewen@gmail.com, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] HOFs, recursion, and being tail-rec...
Date: Mon, 13 Feb 2006 13:05:14 +1100	[thread overview]
Message-ID: <1139796314.8591.79.camel@rosella.wigram> (raw)
In-Reply-To: <20060213.085348.126575598.garrigue@math.nagoya-u.ac.jp>

On Mon, 2006-02-13 at 08:53 +0900, Jacques Garrigue wrote:
> From: Jonathan Roewen <jonathan.roewen@gmail.com>
> > 
> > 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 <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


  reply	other threads:[~2006-02-13  2:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-12 22:08 Jonathan Roewen
2006-02-12 23:53 ` Jacques Garrigue
2006-02-13  2:05   ` skaller [this message]
2006-02-13  2:47     ` Jonathan Roewen
2006-02-13  3:23       ` skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1139796314.8591.79.camel@rosella.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@yquem.inria.fr \
    --cc=garrigue@math.nagoya-u.ac.jp \
    --cc=jonathan.roewen@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).