caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Explicitely named type variable and type constraints.
@ 2012-03-21  7:48 Philippe Veber
  2012-03-21  8:21 ` Jacques Garrigue
  0 siblings, 1 reply; 6+ messages in thread
From: Philippe Veber @ 2012-03-21  7:48 UTC (permalink / raw)
  To: caml users

[-- 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 --]

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

end of thread, other threads:[~2012-03-21  9:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-21  7:48 [Caml-list] Explicitely named type variable and type constraints Philippe Veber
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

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