Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: Pierre Weis <Pierre.Weis@inria.fr>
Cc: caml-list@inria.fr
Subject: Re: Typing of patterns
Date: Mon, 5 Jun 2000 17:29:22 +0200	[thread overview]
Message-ID: <20000605172922.A13490@miss.wu-wien.ac.at> (raw)
In-Reply-To: <200006051356.PAA17512@pauillac.inria.fr>; from Pierre.Weis@inria.fr on Mon, Jun 05, 2000 at 15:56:20 +0200

On Mon, 05 Jun 2000, Pierre Weis wrote:
> > But isn't this a bit strange that an identifier cannot be used in a context
> > where replacing it syntactically with its bound value would be perfectly
> > ok?
> 
> Yes it is strange, but this is exactly what ML polymorphism specifies:
> polymorphism is introduced by the let-binding construct, and only
> occurrences of let-bound identifiers can be used with different
> types. Since your ``num'' above is not let-bound it is monomorphic and
> constraints are accumulating.

Ah - I think I see now: I was fooled by the syntactic "disguise" of the
problem! So this is similar to:

This does not work:

  fun id -> id 42, id "foo"

This works:

  let id x = x in id 42, id "foo"

> For instance, in fun [] as l -> e, the type of l is 'a list, and it
> can be generalized to all 'a. 'a list during the type-checking of e.
> In contrast, in fun x :: l as mylist -> e,
> the type 'a list of identifier mylist cannot be generalized, since 'a
> is the type of x and also occurs in the type of l. 

So the restriction is required to prevent problems with references, I
think, but those cannot occur if the type parameter is not used by any of
the parameters of the constructor, i.e. they are monomorphic (or there are
no parameters as in our case).

> Jacques may explain us if the above suggested generalization scheme is
> used for identifiers bound in as clauses of patterns (and if not,
> which scheme is used ?)...

I first thought it was some strange "special feature" of polymorphic
variants, but as it seems then, it is just that the corresponding typing
rule is obviously implemented differently...

> Conclusion: this strongly suggests to generalize the typing of
> synonymous identifiers in patterns, in order to obtain the same typing
> assignments for pattern matching on ``closed'' polymorphic variants
> and pattern matching on their (semantically equivalent) sum data types
> counterparts.
>
> Thank you for your interesting remark and question.

Thanks for your interesting explanations!

Since we are at it, there is another sometimes annoying type restriction,
this time with record updates, that comes to my mind, e.g.:

  type 'a t = { foo : 'a; bar : int };;

  let x = { foo = "foo"; bar = 3 };;

  let ok = { x with foo = "bla" };;
  let not_ok = { x with foo = 7 };;

Here the updated record "x" could have a less rigidly typed "foo"-field -
or should we rather say it has a "default" type if the field is not
updated? It can be a bit painful to do updates without such a
generalisation if there are many record names that one would have to
mention explicitely to create the wanted value as in:

  let now_ok = { foo = 7; bar = x.bar }

Best regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl




  reply	other threads:[~2000-06-06  6:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-06-01 13:23 Markus Mottl
2000-06-05 13:56 ` Pierre Weis
2000-06-05 15:29   ` Markus Mottl [this message]
2000-06-06  0:55   ` Jacques Garrigue
2000-06-06 15:32     ` Pierre Weis
2000-06-06  1:10   ` Patrick M Doane
2000-06-06  8:55     ` Jacques Garrigue
2000-06-06  7:24 Pierre Weis

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=20000605172922.A13490@miss.wu-wien.ac.at \
    --to=mottl@miss.wu-wien.ac.at \
    --cc=Pierre.Weis@inria.fr \
    --cc=caml-list@inria.fr \
    /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