caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: John Prevost <j.prevost@gmail.com>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] looping recursion
Date: 28 Jul 2004 11:17:27 +1000	[thread overview]
Message-ID: <1090977446.5870.816.camel@pelican.wigram> (raw)
In-Reply-To: <d849ad2a04072717381ea7b0ee@mail.gmail.com>

On Wed, 2004-07-28 at 10:38, John Prevost wrote:

> 
> exception Boom2 of int
> 
> let rec loop2 x =
>   try
>     if x < 100 then loop2 (x + x) else raise (Boom2 0)
>   with Boom2 y -> raise (Boom2 (y + 1))
> 
> let use_loop2 x = try loop2 x with Boom2 y -> y
> 
> In this second example, use_loop2 is using the exceptions in loop2 to
> calculate the result.  (Admittedly, this is a bizarre little setup.)

I'm actually doing something like this (unfortunately)
in the Felix compiler.

Basically, Felix has overloading and requires the function
argument type to be specified, but the return type is deduced
bottom up by actually examining the function's return statement
argument type.

Unfortunately, this situation is complicated by the fact
a function can call itself. However, we cant just plug in
a type variable for the function's return type, calculate
the result, and then unify to eliminate the variable,
as Ocaml could, because in Felix functions can be overloaded.
So to calculate in function f, what the type of f(a) is,
we again have to do overload resolution. Having done that
we need the return type of the function .. but we can't
calculate it because that's what we're already in the middle
of doing, and we'd get an infinite loop.

you might think a type variable could be introduced here,
and eliminated when we pop back, but that isn't so.
The problem is we might have in f:

   return g (f a);

and now, the recursion isn't tail, and we have to have
a definite type for f a, in order to calculate which g
is called, in order to calculate the type of f.

So I just throw an exception and ignore that return statement.
Such recursions (in the client Felix code) usually 
have conditionals wrapped around them and one branch 
had better give a result: otherwise
the client must specify the return type.

[I suspect in terminating code this cannot be needed but
I'm not sure]

the actual algorithm unifies the specified return
type (if given) with all the types of return
statements (which don't lead to infinite recursion).

I have tried to localise exception handling in my
code but this is one case where I couldn't figure
out how to do so. It is of course vital that
the exception handlers stack up here, so the exceptions
propagate far enough but no further.

[The code is extremely messy and I'm not even sure it 
gives the correct result when it does succeed :]

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2004-07-28  1:17 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-27 23:43 briand
2004-07-28  0:27 ` John Prevost
2004-07-28  0:38   ` John Prevost
2004-07-28  1:17     ` skaller [this message]
2004-07-28  1:05   ` briand
2004-07-28  1:43     ` Brian Hurt
2004-07-28  2:49       ` briand
2004-07-28  3:12         ` Brian Hurt
2004-07-28  3:20         ` Brian Hurt
2004-07-28  5:54         ` brogoff
2004-07-28  7:22           ` Alex Baretta
2004-07-28 16:38             ` brogoff
2004-07-28 19:40               ` Jon Harrop
2004-07-28 20:18                 ` Brandon J. Van Every
2004-07-29  6:01                   ` Alex Baretta
2004-07-28 21:22                 ` brogoff
2004-07-29  9:13                   ` Daniel Andor
2004-07-29  9:25                     ` Keith Wansbrough
2004-07-29  9:41                       ` Nicolas Cannasse
2004-07-29  9:57                       ` Xavier Leroy
2004-07-29 10:44                         ` Daniel Andor
2004-07-29 12:56                           ` brogoff
2004-07-29 10:11                     ` skaller
2004-07-29 12:41                     ` brogoff
2004-07-29  6:28               ` Alex Baretta
2004-07-29 14:58                 ` brogoff
2004-07-29 16:12                   ` Brian Hurt
2004-07-29 17:49                     ` james woodyatt
2004-07-29 19:25                       ` Brian Hurt
2004-07-29 20:01                         ` brogoff
2004-07-30  4:42                           ` james woodyatt
2004-07-29 17:44                   ` james woodyatt
2004-07-29 23:12                     ` skaller
2004-07-29 22:42                   ` Alex Baretta
2004-07-30  2:38                     ` Corey O'Connor
     [not found]                     ` <200407300136.14042.jon@jdh30.plus.com>
2004-07-30 12:45                       ` Alex Baretta
2004-07-30 17:07                     ` brogoff
2004-07-30 18:25                       ` [Caml-list] kaplan-okasaki-tarjan deque (was "looping recursion") james woodyatt
2004-07-30 21:20                         ` brogoff
2004-07-31  5:37                           ` james woodyatt
2004-07-28  7:27       ` [Caml-list] looping recursion skaller
2004-07-28 14:36         ` Brian Hurt
2004-07-28 22:05           ` skaller
2004-07-28  0:37 ` 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=1090977446.5870.816.camel@pelican.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@inria.fr \
    --cc=j.prevost@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).