caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Prevost <j.prevost@cs.cmu.edu>
To: ohl@physik.uni-wuerzburg.de
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Creating mutually dependent objects?
Date: 03 Aug 2002 20:53:07 -0400 (3.689 UMT)	[thread overview]
Message-ID: <86eldfa1ho.fsf@laurelin.dementia.org> (raw)
In-Reply-To: <15690.48916.704957.774489@wptx47.physik.uni-wuerzburg.de>

>>>>> "to" == Thorsten Ohl <ohl@physik.uni-wuerzburg.de> writes:

    to> I need to create tuples of mutually dependent objects.  A
    to> working implementation adds a mutable instance variable that
    to> is updated after all objects in a tuple have been created.
    to> With a little bit of work, this variable can then be hidden.
    to> However, this is a bit tedious.  Why is the much more elegant
    to> approach using `let rec' forbidden?

Think of it in terms of "if a variable holds a value of type t, the
value *must* be a legal value of that type."  In your example, you'd
like to be able to say:

    to> let rec o1 = new o 1 o2 and o2 = new o 2 o1;;

But this can't be done, because the compiler can't know what you mean
to do with the two "new o" calls.  Consider that instantiating an
object is essentially a function call, and can run arbitrary code.
It's like wanting to be able to say:

let fun test x y = (x,y)

let rec o1 = test 1 o2 and o2 = test 2 o1

You can convert this definition to:

let rec o1 = (1,o2) and o2 = (2,o1)

This statement is indeed legal, if you have -rectypes turned on.  And
class types always hae rectypes turned on.


So what's the problem?  Why doesn't the compiler allow the version
with function calls even though it can be converted to an expression
that's allowed?  Well, this function has the same type as test, but
can't be converted to anything good:

let fun test2 (a,b) y = (a+1,y)

In this case, the two "objects" would have to be constructed by
sharing values.  You can't construct either o1 or o2 first because it
requires a valid, fully constructed copy of the other value before it
can be constructed itself.


So that's what's going on.  Since object constructors are like
functions and can use their arguments in code, the arguments must be
well-typed values.  Value constructors (record {..} expressions, sum
type constructors, and the like) are not allowed to run code--they
just plop the value down in the appropriate place.  And even then, if
they're hidden behind functions, they can't be used this way, since
it's *syntactic analysis* of the code that allows these constructions
to be used in a let rec.


Hope this was helpful,

John.

-------------------
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


  parent reply	other threads:[~2002-08-04  0:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-02 17:19 Thorsten Ohl
2002-08-03 23:28 ` Jacques Garrigue
2002-08-04  0:53 ` John Prevost [this message]
2002-08-04  2:08   ` Thorsten Ohl

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=86eldfa1ho.fsf@laurelin.dementia.org \
    --to=j.prevost@cs.cmu.edu \
    --cc=caml-list@inria.fr \
    --cc=ohl@physik.uni-wuerzburg.de \
    /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).