RE: [Caml-list] Marshalling question
Jean Krivine wrote: Dear ocaml users, A simple question: is it safe to marshalize a data structure that contains imperative elements (like arrays or hashtbl) ? Simple answer: yes. Marshal works on the runtime representation of data which is imperative (immutability is enforced by the type system, not the runtime). I found the documentation of the Marshal module rather obscure to me. In particular it is not clear whether I should use the No_sharing flag. Unless you know how the data structures you are marshalling work, you should never use this flag (i.e. if you're working with an abstract data type like Hashtbl.t then you certainly shouldn't be using this flag). The obvious reason is that if the data contain a cycle that you're not aware of then your program will not terminate. Less obvious reasons include structures (e.g. for memoizing) which rely on value sharing so that they can use physical, rather than structural equality for speed. The worst thing that can happen when this flag isn't given is that the operation might be a teensy bit slower than it strictly needs to be. David ___ Caml-list mailing list. Subscription management: http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.inria.fr Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs
Re: [Caml-list] Marshalling question
Le vendredi 08 octobre 2010 à 15:37 +0200, Jean Krivine a écrit : Dear ocaml users, A simple question: is it safe to marshalize a data structure that contains imperative elements (like arrays or hashtbl) ? It's relatively safe to do so. The only thing is that if it is unmarshalled in the process where it was marshalled you will still and up with a new array or hashtbl. Exception are some complex datastructure which may require additional care when marshalled. An example of which are the graphs of the ocamlgraph library (even the functional one), but there is none in the standard library. I found the documentation of the Marshal module rather obscure to me. In particular it is not clear whether I should use the No_sharing flag. This flag is never needed but there is cases where it is mandatory *not* to use it (with cyclic data). Mathias Thank you! J ___ Caml-list mailing list. Subscription management: http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.inria.fr Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs ___ Caml-list mailing list. Subscription management: http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.inria.fr Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs
[Caml-list] Type constraint to explain that a polymorphic variants is included into another
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: (** 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 = Map.Make (struct type t = plugin_kind plugin let compare = compare end) 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 = sig type act type kind val kind_default: kind end module Make (F: PLUGIN_FAMILY) = struct module MapPluginSelf = Map.Make (struct type t = F.kind plugin let compare = compare end) 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 in all_help := MapPlugin.add id help !all_help; all_act := MapPlugin.add id act !all_act; id end (** Functions for build plugins *) module Build = Make (struct type act = unit - unit type kind = [`Build] let default = `Build end) (** Functions for install plugins *) module Install = Make (struct type act = string list - unit type kind = [`Install] let default = `Install end) type package = { name: string; plugin_build: [`Build] plugin; plugin_install: [`Install] plugin; } let run pkg = prerr_endline (help pkg.plugin_build); prerr_endline (help pkg.plugin_install); (Build.act pkg.plugin_build) (); (Install.act pkg.plugin_install) () This code doesn't compile because I see no way to explain that F.kind is included into plugin_kind. Here is the precise error: camlc -o test test.ml File test.ml, line 51, characters 32-34: Error: This expression has type F.kind * 'a but an expression was expected of type MapPlugin.key = plugin_kind * string Type F.kind is not compatible with type plugin_kind = [ `Build | `Install ] make: *** [all] Erreur 2 Does anyone know a good solution to this problem? Does anyone have a better solution to this problem? (different design?) Thank you for your answers, Sylvain Le Gall ___ Caml-list mailing list. Subscription management: http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.inria.fr Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs
Re: [Caml-list] Type constraint to explain that a polymorphic variants is included into another
On Fri, Oct 8, 2010 at 10:13 AM, Sylvain Le Gall sylv...@le-gall.net wrote: This code doesn't compile because I see no way to explain that F.kind is included into plugin_kind. As you have written it, F.kind is of course completely abstract. I am not sure where you need F.kind to be a strict subtype of plugin_kind, but you could say type kind = plugin_kind (this seems a bit useless however). I don't think there is a way to use subtyping implicitly when applying a functor, but you can always do it explicitly by interposing a module of signature PLUGIN_FAMILY which embeds the specific kind in plugin_kind and passes the other components through. You could also have a general and a specific type in the plugin signature, and use the general one for general operations (e.g. help) but the specific one wherever that is needed. I am not sure I understand what you're trying to achieve however. Jake ___ Caml-list mailing list. Subscription management: http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.inria.fr Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs
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. Jacques (** 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 = Map.Make (struct type t = plugin_kind plugin let compare = compare end) 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 = sig type act type kind = private [ plugin_kind] val kind_default: kind end module Make (F: PLUGIN_FAMILY) = struct module MapPluginSelf = Map.Make (struct type t = F.kind plugin let compare = compare end) 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 in all_help := MapPlugin.add (id : plugin_kind * _) help !all_help; all_act := MapPluginSelf.add id act !all_act; id end (** Functions for build plugins *) module Build = Make (struct type act = unit - unit type kind = [`Build] let kind_default = `Build end) (** Functions for install plugins *) module Install = Make (struct type act = string list - unit type kind = [`Install] let kind_default = `Install end) 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 [] ___ Caml-list mailing list. Subscription management: http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.inria.fr Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs
[Caml-list] Re: Type constraint to explain that a polymorphic variants is included into another
On 08-10-2010, Jake Donham j...@donham.org wrote: On Fri, Oct 8, 2010 at 10:13 AM, Sylvain Le Gall sylv...@le-gall.net wrote: This code doesn't compile because I see no way to explain that F.kind is included into plugin_kind. As you have written it, F.kind is of course completely abstract. I am not sure where you need F.kind to be a strict subtype of plugin_kind, but you could say type kind = plugin_kind (this seems a bit useless however). I don't think there is a way to use subtyping implicitly when applying a functor, but you can always do it explicitly by interposing a module of signature PLUGIN_FAMILY which embeds the specific kind in plugin_kind and passes the other components through. You could also have a general and a specific type in the plugin signature, and use the general one for general operations (e.g. help) but the specific one wherever that is needed. I am not sure I understand what you're trying to achieve however. My goal is that the compiler prevents me to do Build.act pkg.plugin_install - because plugin_install is of type [`Install] plugin and Build.act needs [`Build] plugin but allow me to do help pkg.plugin_install - because help needs [`Build | `Install] plugin. But maybe I am missing something here and try to overengineer something simple. Regards, Sylvain Le Gall ___ Caml-list mailing list. Subscription management: http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.inria.fr Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs