Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@lri.fr>
To: jtbryant@valdosta.edu
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Mututal Recursion and Tail Recursion
Date: Fri, 15 Jul 2005 08:40:01 +0200	[thread overview]
Message-ID: <17111.23105.448745.536629@gargle.gargle.HOWL> (raw)
In-Reply-To: <1121281425.30107.36.camel@starlight.valdosta.edu>


Jonathan Bryant writes:
 > Is is possible to have mutually recursive functions that are also tail
 > recursive?  

Yes.

 > For example, attached is some code I wrote to flatten a list
 > of lists using mutually recursive functions.  I've tried this with large
 > lists (5,000,000+) and have not encountered a stack overflow, but that
 > does not necessarily mean that tail recursion is happening.

Looking at the assembly code (obtained with "ocamlopt -S") clearly
gives the answer: all three recursive calls in flat_in and flat_out
are realized by jumps (and not calls). 

I copy below the assembly code for the functions flat_in and flat_out.

Hope this helps,
-- 
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)


======================================================================
	.data
	.globl	camlTest__data_begin
camlTest__data_begin:
	.text
	.globl	camlTest__code_begin
camlTest__code_begin:
	.data
	.long	2048
	.globl	camlTest
camlTest:
	.space	8
	.data
	.long	7415
camlTest__1:
	.long	caml_curry2
	.long	5
	.long	camlTest__flat_out_57
	.long	4345
	.long	caml_curry3
	.long	7
	.long	camlTest__flat_in_58
	.text
	.align	16
	.globl	camlTest__flat_in_58
	.type	camlTest__flat_in_58,@function
camlTest__flat_in_58:
.L101:
	movl	%eax, %edx
	cmpl	$1, %edx
	je	.L100
.L102:	movl	caml_young_ptr, %eax
	subl	$12, %eax
	movl	%eax, caml_young_ptr
	cmpl	caml_young_limit, %eax
	jb	.L103
	leal	4(%eax), %esi
	movl	$2048, -4(%esi)
	movl	(%edx), %eax
	movl	%eax, (%esi)
	movl	%ecx, 4(%esi)
	movl	4(%edx), %eax
	movl	%esi, %ecx
	jmp	.L101
	.align	16
.L100:
	movl	%ebx, %eax
	movl	%ecx, %ebx
	jmp	camlTest__flat_out_57
.L103:	call	caml_call_gc
.L104:	jmp	.L102
	.text
	.align	16
	.globl	camlTest__flat_out_57
	.type	camlTest__flat_out_57,@function
camlTest__flat_out_57:
.L106:
	movl	%ebx, %ecx
	cmpl	$1, %eax
	je	.L105
	movl	4(%eax), %ebx
	movl	(%eax), %eax
	jmp	camlTest__flat_in_58
	.align	16
.L105:
	movl	%ecx, %eax
	ret
	.text
	.align	16
	.globl	camlTest__entry
	.type	camlTest__entry,@function
camlTest__entry:
.L107:
	movl	$camlTest__1, %eax
	movl	%eax, %ebx
	addl	$16, %eax
	movl	%ebx, camlTest
	movl	%eax, camlTest + 4
	movl	$1, %eax
	ret
	.text
	.globl	camlTest__code_end
camlTest__code_end:
	.data
	.globl	camlTest__data_end
camlTest__data_end:
	.long	0
	.globl	camlTest__frametable
camlTest__frametable:
	.long	1
	.long	.L104
	.word	4
	.word	3
	.word	5
	.word	3
	.word	7
	.align	4
======================================================================


  reply	other threads:[~2005-07-15  6:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-13 19:03 Jonathan Bryant
2005-07-15  6:40 ` Jean-Christophe Filliatre [this message]
2005-07-15 12:15   ` [Caml-list] " Jonathan Bryant
2005-07-18  0:56     ` Jacques Garrigue

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=17111.23105.448745.536629@gargle.gargle.HOWL \
    --to=jean-christophe.filliatre@lri.fr \
    --cc=caml-list@inria.fr \
    --cc=jtbryant@valdosta.edu \
    /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