English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Functions over polymorphic variants
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-03-23 (06:11)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Functions over polymorphic variants
From: Kaspar Rohrer <kaspar.rohrer@gmail.com>
> I am tinkering with streams of polymorphic variants at the
> moment. One thing that I would like to do is to write a stream
> transformer (not sure this is the correct terminology) that simply
> drops certain values of an open polymorphic type, and have the type
> of the function reflect this.

Sorry, you can't.
This is a design choice with ocaml polymorphic variants, that the row
variable is kept hidden. This has the advantage of simplicity, but it
makes impossible to abstract on the row variable itself, like you want

> This is trivial with a closed polymorphic type:
> type abc = [`a | `b | `c]
> let transform (stream : [< `x | abc] Stream.t) : [> abc] Stream.t =
>  let rec fold () =
>      match Stream.next stream with
> 	| `x -> Stream.slazy fold
> 	| #abc as x -> Stream.icons x (Stream.slazy fold)
>    with
> 	Stream.Failure -> Stream.sempty
>  in
>    fold ()
> However, I fail to see how the same function could be implemented so it accepts an open polymorphic type as it's input.
> I.e. is there a way to express something like this (which is not valid OCaml)
> let transform : [> `x | 'a] Stream.t -> [> 'a] Stream.t
> Does this even make sense?

This makes sense, but would require a different type system.
With ocaml, you must keep to closed types.
This should be sufficient for concrete examples, but you cannot build
polymorphic combinators.

Note. In designing a type system where you would be able to express
such "differential" types, poinder the problem: what is the type of
transform composed with itself.

Jacques Garrigue