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
Type constraint to explain that a polymorphic variants is included into another
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-10-08 (20:09)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Type constraint to explain that a polymorphic variants is included into another
On 2010/10/09, at 2:13, Sylvain Le Gall wrote:

> Hello all,
> I would like to build an interface for plugins that allow to extract at the
> same time a very specific data for a plugin family and to extract
> general help for plugins.
> Here is an example:


> This code doesn't compile because I see no way to explain that F.kind is
> included into plugin_kind. 

I'm not sure of what you are trying to do, but private rows where introduced
with this goal in mind.

The idea is to change the abstract definition of kind in PLUGIN_FAMILY to

  type kind = private [< plugin_kind]

meaning that kind can be instantiated to any subset of plugin_kind.
You can then use subtyping to convert from kind to plugin_kind.

Here is a typable version of your code.
Note that I had to do a few other changes to make the types match.


(** All the plugins I want to manage *)
type plugin_kind = [`Build | `Install]

(** Generic plugin *)
type 'a plugin = 'a * string

(** Help data for all plugin *)
module MapPlugin = 
      type t = plugin_kind plugin
      let compare = compare

let all_help: string MapPlugin.t ref = 
 ref MapPlugin.empty

let help plg = 
 MapPlugin.find plg !all_help

(** Functor to build function related to one type of plugin *)
module type PLUGIN_FAMILY = 
 type act
 type kind = private [< plugin_kind]
 val kind_default: kind

module Make (F: PLUGIN_FAMILY) = 

 module MapPluginSelf = 
        type t = F.kind plugin
        let compare = compare

 let all_act: F.act MapPluginSelf.t ref = 
   ref MapPluginSelf.empty

 let act (plg : F.kind plugin) =
   MapPluginSelf.find plg !all_act

 let create name help act = 
   let id = 
     F.kind_default, name
     all_help := MapPlugin.add (id :> plugin_kind * _) help !all_help;
     all_act  := MapPluginSelf.add id act !all_act;

(** Functions for build plugins *)
module Build =
      type act = unit -> unit 
      type kind = [`Build]
      let kind_default = `Build

(** Functions for install plugins *)
module Install = 
      type act = string list -> unit
      type kind = [`Install]
      let kind_default = `Install

type package = 
     name: string;
     plugin_build: [`Build] plugin;
     plugin_install: [`Install] plugin;

let run pkg = 
 prerr_endline (help (pkg.plugin_build :> MapPlugin.key));
 prerr_endline (help (pkg.plugin_install :> MapPlugin.key));
 (Build.act pkg.plugin_build) ();
 Install.act pkg.plugin_install []