caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Strange Gadt error
@ 2015-10-08 18:46 Anders Peter Fugmann
  2015-10-08 23:17 ` Jacques Garrigue
  0 siblings, 1 reply; 4+ messages in thread
From: Anders Peter Fugmann @ 2015-10-08 18:46 UTC (permalink / raw)
  To: caml-list

Hi,

I the following example (boiled down from a real use case):

type _ elem =
   | Int: int elem

let rec incr: type a. a elem -> a -> int = function
   | Int -> fun i -> add i 1
and add n m = n + m

I get the error (Ocaml 4.02.3):
File "example.ml", line 5, characters 24-25:
Error: This expression has type int but an expression was expected of type
          int
        This instance of int is ambiguous:
        it would escape the scope of its equation

I can get rid of the error by annotating the type of i in line 5 like this:

  | Int -> fun (i : int) -> add i 1
                    ^^^

Or move add above incr like this:

let rec add n m = n + m
and incr: type a. a elem -> a -> int = function
   | Int -> fun i -> add i 1

Is there an explanation to why I need to give the type of i in this 
case? As 'i' _must_ be an int (from the type annotation of incr), 
annotating the function seems ambiguous.

/Anders







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

end of thread, other threads:[~2015-10-13  1:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-08 18:46 [Caml-list] Strange Gadt error Anders Peter Fugmann
2015-10-08 23:17 ` Jacques Garrigue
2015-10-12 14:20   ` Anders Peter Fugmann
2015-10-13  1:41     ` Jacques Garrigue

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