caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] why is this definition less general?
@ 2015-08-27  9:59 Christoph Höger
  2015-08-27 10:11 ` Jeremie Dimino
  0 siblings, 1 reply; 2+ messages in thread
From: Christoph Höger @ 2015-08-27  9:59 UTC (permalink / raw)
  To: caml users

Dear all,

I found that I can evade the "first occurence unifies" behavior of
recursive polymorphic functions by using explicit polymorphic types:

utop # let rec a_to_opt : 'a . ('a -> 'a option) = fun x -> None and
int_to_opt (i:int) = a_to_opt i and str_to_opt (s:string)= a_to_opt s ;;

val a_to_opt : 'a -> 'a option = <fun>


               val int_to_opt : int -> int option = <fun>


                              val str_to_opt : bytes -> bytes option = <fun>

When I try to annotate the type in the body of a_to_opt, however, I get
a "less general" error:

utop # let rec a_to_opt : 'a . ('a -> 'a option) = fun x -> (None : 'a
option) and int_to_opt (i:int) = a_to_opt i and str_to_opt (s:string)=
a_to_opt s ;;
Error: This definition has type 'a -> 'a option which is less general
than

                          'a0. 'a0 -> 'a0 option

Obviously, 'a is bound by its first occurrence in the type annotation,
here. Can someone explain this behavior?

-- 
Christoph Höger

Technische Universität Berlin
Fakultät IV - Elektrotechnik und Informatik
Übersetzerbau und Programmiersprachen

Sekr. TEL12-2, Ernst-Reuter-Platz 7, 10587 Berlin

Tel.: +49 (30) 314-24890
E-Mail: christoph.hoeger@tu-berlin.de

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] why is this definition less general?
  2015-08-27  9:59 [Caml-list] why is this definition less general? Christoph Höger
@ 2015-08-27 10:11 ` Jeremie Dimino
  0 siblings, 0 replies; 2+ messages in thread
From: Jeremie Dimino @ 2015-08-27 10:11 UTC (permalink / raw)
  To: Christoph Höger; +Cc: caml users

[-- Attachment #1: Type: text/plain, Size: 590 bytes --]

On Thu, Aug 27, 2015 at 10:59 AM, Christoph Höger <
christoph.hoeger@tu-berlin.de> wrote:

> Obviously, 'a is bound by its first occurrence in the type annotation,
> here. Can someone explain this behavior?


​In a type annotation, the scope of variables is only the type annotation.
So in your example the ['a] in the annotation of [a_to_opt] and the ['a] in
the annotation of [None] are distinct. To refer to the same type, you need
to use a local abstract type:

    let rec a_to_opt : type a. (a -> a option) = fun x -> (None : a
​ option​
)​​

-- 
Jeremie

[-- Attachment #2: Type: text/html, Size: 1394 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-08-27 10:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-27  9:59 [Caml-list] why is this definition less general? Christoph Höger
2015-08-27 10:11 ` Jeremie Dimino

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).