caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nicolás Ojeda Bär" <nicolas.ojeda.bar@lexifi.com>
To: Grumpy <grumpy@drno.eu>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Type inference curiosity
Date: Fri, 24 Mar 2017 10:55:02 +0100	[thread overview]
Message-ID: <CADK7aFNAqdXY6CMp7=ce=AzCs33PRzooVF+kR3P6Gfj5fziMNQ@mail.gmail.com> (raw)
In-Reply-To: <167e229f-2ffc-14ab-8725-5d1af71c167b@drno.eu>

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

Hello,

Recursive definitions need to be explicitly annotated to be polymorphic.
The (type a) annotation
introduces an abstract type a in the body of the function but does not make
it polymorphic.
The annotation type a. on the other hand, does both.

See http://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec227 for the
details.

Cheers,
Nicolas


On Fri, Mar 24, 2017 at 10:20 AM, Grumpy <grumpy@drno.eu> wrote:

> Hello,
>
> I think there is an incoherency or a bug somewhere in the type inference
> engine with the following code with version 4.02.3 (I have not tested in
> previous versions). Both functions eval and eval2 are identical but the
> inferred types are different, and the type inferred for eval2 is
> actually wrong.
>
> The function eval is typed correcly ('a exp -> 'a) while the type
> inferred for funcion function eval2 is (int exp -> int), which is wrong
> because of the Inc case returning ('a exp -> 'a).
>
> It seems the syntax (type a) leads to this incorrect behaviour...
>
>
> type _ exp =
>   | Stop : int exp
>   | Inc : (int exp -> int) exp
>
> let rec eval : type a. a exp -> a = function
>   | Stop -> 0
>   | Inc -> (fun (p : int exp) -> 1 + eval p)
>
> let rec eval2 (type a) (p : a exp) : a =
>   match p with
>   | Stop -> 0
>   | Inc -> (fun (p : int exp) -> 1 + eval2 p)
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

  reply	other threads:[~2017-03-24  9:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-24  9:20 Grumpy
2017-03-24  9:55 ` Nicolás Ojeda Bär [this message]
2017-03-24 10:13   ` Grumpy
2017-03-28  3:11     ` Jacques Garrigue

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='CADK7aFNAqdXY6CMp7=ce=AzCs33PRzooVF+kR3P6Gfj5fziMNQ@mail.gmail.com' \
    --to=nicolas.ojeda.bar@lexifi.com \
    --cc=caml-list@inria.fr \
    --cc=grumpy@drno.eu \
    /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).