caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <jeremy.yallop@ed.ac.uk>
To: Robert Fischer <robert@fischerventure.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Question about type unification
Date: Mon, 26 May 2008 20:45:34 +0100	[thread overview]
Message-ID: <483B135E.7010002@ed.ac.uk> (raw)
In-Reply-To: <483B0F81.1020907@fischerventure.com>

Robert Fischer wrote:
> How is the compiler magically getting from the float to a string value?  Can someone break down
> what's actually happening here for me?

Type aliases are analogous to functions.  If you have a variable "f" 
that denotes a pure function and some values u1 ... un and v1 ... vn 
then to determine whether

    f u1 ... un

is equal to

    f v1 ... vn

you call the function (twice) by substituting u1 ... un and v1 ... vn 
for the parameters.  If you have a constant function

    f = fun _ ... _ = v

then any application of the function will give the same result (v) 
regardless of the values of the arguments.  Analogously, if you have a 
type constructor "t" that denotes a type alias and some types s1 ... sn 
and t1 ... tn then to determine whether

    (s1, ... sn) t

is equal to

    (t1, ... tn) t

you "call" the type alias by substituting s1 ... sn and t1 ... tn for 
the type parameters.  If you have a "constant" type alias in which none 
of the type parameters appear on the right hand side of the definition

    type ('a1, ... 'an) t = e            a1...an not in fv(e)

then any application of the type constructor will give the same result 
(e) regardless of the values of the arguments.

Nominal types are analogous to constructors.  (By "nominal" I mean 
record and sum types, or types made abstract using a module signature.) 
  If you have a data constructor "C" and some values u1 ... un and v1 
... vn then to determine whether

    C u1 ... un

is equal to

    C v1 ... vn

you check whether ui is equal to vi for each i.  Analogously, if you 
have a type constructor "s" that denotes a nominal type and some types 
s1 ... sn and t1 ... tn then to determine whether

    (s1, ... sn) s

is equal to

    (t1, ... tn) s

you check whether si is equal to ti for each i.

In Richard's example "t" denotes a type alias.  Thus to determine whether

   unit t

is equal to

   string t

we look at the definition of t:

   type 'a t = float

then replace each 'a that occurs on the right hand side first with 
"unit" and then with "string" and compare the results.  In this case 
"'a" doesn't appear at all on the right hand side of the definition so 
we compare "float" with "float", and find that the two are equal, i.e. 
"unit t" and "string t" denote the same type (float).

Jeremy.


  parent reply	other threads:[~2008-05-26 19:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-26 12:22 Richard Jones
2008-05-26 12:33 ` [Caml-list] " Jon Harrop
2008-05-26 12:37 ` Romain Bardou
2008-05-26 17:21 ` Fabrice Marchant
2008-05-26 19:29   ` Robert Fischer
2008-05-26 19:38     ` DooMeeR
2008-05-26 19:43       ` Robert Fischer
2008-05-26 19:45     ` Jeremy Yallop [this message]
2008-05-26 21:44   ` Richard Jones

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=483B135E.7010002@ed.ac.uk \
    --to=jeremy.yallop@ed.ac.uk \
    --cc=caml-list@inria.fr \
    --cc=robert@fischerventure.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).