Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Pierre Weis <Pierre.Weis@inria.fr>
To: proff@iq.org (Julian Assange)
Cc: caml-list@inria.fr
Subject: Re: polymorphic variant oddity
Date: Sun, 8 Oct 2000 18:00:57 +0200 (MET DST)	[thread overview]
Message-ID: <200010081600.SAA30931@pauillac.inria.fr> (raw)
In-Reply-To: <wxitr4naws.fsf@suburbia.net> from Julian Assange at "Oct 7, 100 10:33:23 pm"

Julian Assange played with polymorphic variants:

> let `F x = x;;   
> Unbound value x
> # let `F x = 1;;
> This expression has type int but is here used with type [< `F of 'a | ..]
> # let `F x = `F 1;;
> val x : int = 1
> # `F 4;;
> - : [> `F of int] = `F 4
> 
> What exactly is the meaning this?

This is not specific to polymorphic variants, you may observe the same
behaviour with regular concrete data types. Since, it is simpler with
usual types and constructors, let's explain with a simple type with F
as single constructor:

type 'a t = F of 'a;;

# let F x = x;;
            ^
Unbound value x

Explanation: when defining let pattern = expr, the compiler
verifies that the expr can be computed; it is not the case here, since
x is unknown (as not been previously defined). Is there something
surprising here ?

# let F x = 1;;
            ^
This expression has type int but is here used with type 'a t

When writing let pattern = expr with a complex pattern such that F x,
your using a ``destructuring let'', which means that the value of expr
will be matched against pattern, and the variables in pattern will be
bound to the corresponding parts in (the computed value of)
expr. Hence expr should have the same type as pattern. Hence the type
checking error since F x has type 'a t and 1 has type int.

# let F x = F 1;;
val x : int = 1

One successful destructurig let, that bounds x (the variable of the
left-hand pattern) to 1 (the value corresponding to that variable in
the right-hand expression).

# F 4;;
- : int t = F 4

Nothing strange: we just build a value of type t.

Is there some more magic to explain ?

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/




  reply	other threads:[~2000-10-08 21:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-10-07 11:33 Julian Assange
2000-10-08 16:00 ` Pierre Weis [this message]
2000-10-08 17:22 ` Markus Mottl
2000-10-08 20:09 ` John Prevost

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=200010081600.SAA30931@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=proff@iq.org \
    /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