From: Jeremy Yallop <yallop@gmail.com>
To: Reed Wilson <cedilla@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Objects and polymorphic variants
Date: Sun, 3 Feb 2013 00:13:22 +0000 [thread overview]
Message-ID: <CAAxsn=HyDTWUidRM7Y-jChN-DtcD+W1+7juQLM7836yVeVZ-Yw@mail.gmail.com> (raw)
In-Reply-To: <CALLFq5SYVOEszYH9jcDTgpA9ZpZb95RXVtsCHfUMZuqqW4ZP2w@mail.gmail.com>
On 2 February 2013 23:18, Reed Wilson <cedilla@gmail.com> wrote:
> What I really want is a signature like this:
> method private method_12 : int -> [ > `One | `Two ]
> method method_123 : int -> [ `One | `Two | `Three ]
> method method_124 : int -> [ `One | `Two | `Four ]
>
> If I replace method_12 with a function outside the class it works fine, but
> for whatever reason method_12 really wants to be the exact same type as
> method_123 and method_124.
>
> Is there any way around this typing requirement for methods?
I think that the problem arises because methods are typed similarly to
mutually-recursive functions. Unless you give type signatures, both
functions that are marked as mutually recursive and methods are
assumed to be monomorhpic. For example, in
let rec f = fun x -> x
and g = fun x -> f (x + 1)
the types are
val f : int -> int
val g : int -> int
i.e. f is assigned the type with which it is used in the body of g.
If you remove the (unnecessary) mutual recursion then the more general
types will be inferred; for example, in
let f = fun x -> x
let g = fun x -> f (x + 1)
the types are
val f : 'a -> 'a
val g : int -> int
It's also possible to ensure that f is assigned the more general type
by using a type signature:
let rec f : 'a. 'a -> 'a = fun x -> x
and g = fun x -> f (x + 1)
With objects, the situation is similar, except that you can't mark
methods non-recursive, so you have to give a type signature to avoid
the monomorphising. So
object (self)
method f = fun x -> x
method g = fun x -> self#f (x + 1)
end
receives the type
< f : int -> int;
g : int -> int >
whereas
object (self)
method f : 'a. 'a -> 'a = fun x -> x
method g = fun x -> self#f (x + 1)
end
receives the more general type
< f : 'a. 'a -> 'a;
g : int -> int >
In your example you can ensure that the type you want is inferred by
annotating method_12 with a polymorphic signature:
object (self)
method private method_12 : 'a. int -> ([> `One | `Two] as 'a) = function
| 1 -> `One
| _ -> `Two
method method_123 = function
| 3 -> `Three
| x -> self#method_12 x
method method_124 = function
| 4 -> `Four
| x -> self#method_12 x
end
Now the inferred types for method_123 and method_125 are distinct:
< method_123 : int -> [> `One | `Three | `Two ];
method_124 : int -> [> `Four | `One | `Two ] >
Hope that helps a bit,
Jeremy.
next prev parent reply other threads:[~2013-02-03 0:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-02-02 23:18 Reed Wilson
2013-02-03 0:13 ` Jeremy Yallop [this message]
2013-02-03 1:53 ` Reed Wilson
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='CAAxsn=HyDTWUidRM7Y-jChN-DtcD+W1+7juQLM7836yVeVZ-Yw@mail.gmail.com' \
--to=yallop@gmail.com \
--cc=caml-list@inria.fr \
--cc=cedilla@gmail.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).