From: Pietro Abate <pietro.abate@inria.fr>
To: caml-list@inria.fr
Subject: [Caml-list] small GADT problem
Date: Thu, 19 May 2016 11:51:35 +0200 [thread overview]
Message-ID: <20160519095135.GA18204@pps.univ-paris-diderot.fr> (raw)
Bonjour,
I fail to understand why in this simple example below I get a warning.
Why the type annotations are not enough to convince the compiler that
the method vartoint cannot accept anything else then UID.a ?
module UID = struct
type a
type b
type c
type _ value =
| A : int -> a value
| B : int -> b value
| C : int -> c value
let toa v : a value = A v
let tob v : b value = B v
let toc v : c value = C v
end
class type ['a,'b] projection =
object
method add : 'a UID.value -> unit
method inttovar : 'b UID.value -> 'a UID.value
method vartoint : 'a UID.value -> 'b UID.value
end
class identity : [UID.a, UID.b] projection = object
method add v = ()
method inttovar (UID.B v) = UID.toa v
method vartoint (UID.A v) = UID.tob v
end
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
A _
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
B _
class identity : [UID.a, UID.b] projection
The class is correctly typed and the compiler correctly
identifies typing errors :
# let map = new identity ;;
val map : identity = <obj>
# map#vartoint (UID.toc 1) ;;
Error: This expression has type UID.c UID.value
but an expression was expected of type UID.a UID.value
Type UID.c is not compatible with type UID.a
# map#vartoint (UID.toa 1) ;;
- : UID.b UID.value = UID.B 1
#
(The OCaml compiler, version 4.02.3)
p
next reply other threads:[~2016-05-19 9:51 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-19 9:51 Pietro Abate [this message]
2016-05-19 10:13 ` Jeremy Yallop
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=20160519095135.GA18204@pps.univ-paris-diderot.fr \
--to=pietro.abate@inria.fr \
--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