From: Gilles Dubochet <gilles.dubochet@epfl.ch>
To: Caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Polymorphic variant typing
Date: Tue, 15 Feb 2005 23:15:38 +0000 [thread overview]
Message-ID: <141c991e0f84781450c6f0948e9f6cb0@epfl.ch> (raw)
In-Reply-To: <421275ED.1060607@laposte.net>
>> let incr g x = match x with
>> | `Int i -> `Int (i+1)
>> | x -> (g x);;
>> Receives type:
>> (([> `Int of int ] as 'a) -> ([> `Int of int ] as 'b)) -> 'a -> 'b
>
> Now, how about this :
>
> let incr x g = match x with
> | `Int i -> `Int (i+1)
> | x -> g x;;
>
> Doesn't it look like your code ? OCaml 3.08.2 types it as follows :
> val incr : ([> `Int of int ] as 'a) -> ('a -> ([> `Int of int ] as
> 'b)) -> 'b = <fun>
>
> This is more like it !
I am afraid I can't agree with you: As far as I can tell, this type
with the 'x g' version is exactly equivalent to the type I obtained
with the 'g x' version. If you remember that "... as 'a" is the binding
of a type variable, it is quite intuitive why.
> incr (`Int 1) (fun (`Int i) -> (`Int (i+1)));;
> - : [> `Int of int ] = `Int 2
> incr (`Float 1.0) (fun x -> match x with (`Float f) -> (`Float (f +.
> 1.0)) | x -> x);;
> - : [> `Float of float | `Int of int ] = `Float 2.
Yes, but in your case the g function you pass as a parameter has a last
generic case. This means it has type "([> `Float of float ] as 'a) ->
'a" which is an "at least" instead of a "at most" variant type. With
that function it works also with my original 'g x' version. The goal is
to be able to make this run:
incr (`Float 1.0) (fun x -> match x with (`Float f) -> (`Float (f +.
1.0)));;
And your solution doesn't more than mine.
Just to make things clear, I am not looking for a workaround to this
problem. I am rather looking for an explanation about why the mentioned
type is inferred for this expression. I am developing a type inference
system for a language with similar variants to O'Caml's polymorphic
variants. My inference algorithm calculates a different type than
O'Caml's for a equivalent expression. I try to understand why O'Caml
does it that way to decide how I should do it. To make it even more
clear, the type I obtain with my algorithm is (in row variable
notation): (['a] -> [#int:int | 'b]) -> [#int:int | 'a] -> [#int:int |
'b]
Voilà, but thanks for the answer anyway,
Cheers,
Gilles.
next prev parent reply other threads:[~2005-02-15 23:15 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-02-15 21:28 Gilles Dubochet
2005-02-15 22:21 ` [Caml-list] " Olivier Pérès
2005-02-15 23:15 ` Gilles Dubochet [this message]
2005-02-16 0:36 ` Jacques Garrigue
2005-02-16 1:21 ` Gilles Dubochet
2005-02-16 6:40 ` Jacques Garrigue
2005-02-16 10:12 ` Radu Grigore
2005-02-16 0:43 ` David Brown
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=141c991e0f84781450c6f0948e9f6cb0@epfl.ch \
--to=gilles.dubochet@epfl.ch \
--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;
as well as URLs for NNTP newsgroup(s).