caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: tim@fungible.com (Tim Freeman)
To: oliver@first.in-berlin.de
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Records with same structure in OCaml
Date: Sun,  7 Apr 2002 08:38:54 -0700	[thread overview]
Message-ID: <20020407154807.98EE97F5D@lobus.fungible.com> (raw)
In-Reply-To: <Pine.LNX.3.95.1020406194455.3960A-100000@first.in-berlin.de> (message from Oliver Bandel on Sat, 6 Apr 2002 19:52:34 +0200 (MET DST))

>type complex = { re: float; im: float };
>type foo     = { re: float; im: float };
>
>let x = { re =2.0; im = 55.0 };
>
>
>The x here has type foo.
>How can I choose type complex?

>From experiments with the system it seems that declaring foo binds new
meanings to the .re and .im field operators.  Before the declaration
they fetched fields from complex numbers, and after the declaration
they fetch fields from "foo".  After doing your code above, the
following happens:

># x.re ;;
>- : float = 2
># type complex = { re: float; im: float };;
>type complex = { re : float; im : float; } 
># x.re;;
>Toplevel input:
># x.re;;
>  ^
>This expression has type foo but is here used with type complex

See how the redeclaration of complex changes the meaning of ".re" so
you can't get "x.re" any more, if x is a foo.

If the field operators were ambiguous, that would make type inference
much more difficult, so I'm not surprised by this.  If we have the declaration

   let z y = y.re;;

then we can give z the type complex -> float, or foo -> float, but
there's no way within OCaml's language of types to express
(either a complex or foo) -> float.  Thus type inference needs a
commitment that .re either takes a complex or a foo, but not either.

-- 
Tim Freeman       
tim@fungible.com
-------------------
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:[~2002-04-07 15:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-06 17:52 Oliver Bandel
2002-04-07 15:38 ` Tim Freeman [this message]
2002-04-07 16:43 ` Brian Rogoff

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=20020407154807.98EE97F5D@lobus.fungible.com \
    --to=tim@fungible.com \
    --cc=caml-list@inria.fr \
    --cc=oliver@first.in-berlin.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).