From: Philippe Veber <philippe.veber@gmail.com>
To: caml users <caml-list@inria.fr>
Subject: [Caml-list] Explicitely named type variable and type constraints.
Date: Wed, 21 Mar 2012 08:48:09 +0100 [thread overview]
Message-ID: <CAOOOohQ64Fz+xp_r4pbBAXZzoSfMR=u8jXUsoLchUMO9DADVFA@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1778 bytes --]
Hi,
I found myself defining a type that would both contain a module type and a
type constraint:
module type Screen = sig
type state
type message
val init : state
[...]
val emit : state -> message option
end
type 'a screen = (module Screen with type message = 'a) constraint 'a =
[> `quit]
That is supposed to express that screens emit messages, and that one of the
messages can be "quit". Now I've got some trouble when using the 'a screen
type in a function that unpack the module it contains:
let f (screen : 'a screen) =
let module Screen = (val *screen* : Screen) in
match Screen.(emit init) with
| Some `quit -> 1
| _ -> 0
;;
Error: This expression has type
([> `quit ] as 'a) screen = (module Screen with type message = 'a)
but an expression was expected of type (module Screen)
New attempt:
# let f (screen : 'a screen) =
let module Screen = (val screen : Screen with type message = 'a) in
match Screen.(emit init) with
| Some `quit -> 1
| _ -> 0
;;
Error: Unbound type parameter 'a
Though here I'm not sure the error is right. New attempt:
# let f (type s) (screen : s screen) =
let module Screen = (val screen : Screen with type message = s) in
match Screen.(emit init) with
| Some `quit -> 1
| _ -> 0
;;
Error: This type s should be an instance of type [> `quit ]
Which makes sense. So here is my question: is there a way to impose a
constraint on the "newtype" introduced in argument? Let me say that I'm
aware I should write this more simply. For instance in the module type
Screen, emit could have type state -> [`quit | `message of message]. So my
question is only a matter of curiosity. Still, I'd be happy to know :o).
Cheers,
Philippe.
[-- Attachment #2: Type: text/html, Size: 5502 bytes --]
next reply other threads:[~2012-03-21 7:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-21 7:48 Philippe Veber [this message]
2012-03-21 8:21 ` Jacques Garrigue
2012-03-21 8:54 ` Alan Schmitt
2012-03-21 9:01 ` Thomas Braibant
2012-03-21 9:29 ` Alain Frisch
2012-03-21 9:41 ` Philippe Veber
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='CAOOOohQ64Fz+xp_r4pbBAXZzoSfMR=u8jXUsoLchUMO9DADVFA@mail.gmail.com' \
--to=philippe.veber@gmail.com \
--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