* [Caml-list] Generic printer patch @ 2011-12-07 15:28 Jérémie Dimino 2011-12-07 16:46 ` Alex Rubinsteyn ` (3 more replies) 0 siblings, 4 replies; 23+ messages in thread From: Jérémie Dimino @ 2011-12-07 15:28 UTC (permalink / raw) To: caml-list Hi, I have made a patch for ocaml 3.12.1 which allow to have a generic printing function. The patch is available here: http://www.dimino.org/ocaml-3.12.1-generic-print.patch Here is how to use it: external show : 'a -> string = "%show" let () = print_endline (show ([1; 2; 3], Some "foo")) this will print: ([1; 2; 3], Some "foo") It is also possible to define custom printers, for example: module StringMap : sig include Map.S with type key = string val string_of_t : ('a -> string) -> 'a t -> string end = struct include Map.Make(String) let string_of_t (type a) string_of_a (m : a t) = show (bindings m) end let () = print_endline (show (StringMap.singleton "x" 1)) will print: [("x", 1)] Of course it is limited to what the compiler knows, for example the following function will always returns "[<poly>; <poly>]": let f x = show [x; x] But i think it is already very useful for debugging. The git repo is here: http://www.dimino.org/gitweb/?p=ocaml-3.12.1-print.git;a=summary Cheers, -- Jérémie ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-07 15:28 [Caml-list] Generic printer patch Jérémie Dimino @ 2011-12-07 16:46 ` Alex Rubinsteyn 2011-12-07 17:10 ` Jérémie Dimino 2011-12-07 16:56 ` François Bobot ` (2 subsequent siblings) 3 siblings, 1 reply; 23+ messages in thread From: Alex Rubinsteyn @ 2011-12-07 16:46 UTC (permalink / raw) To: Caml List [-- Attachment #1: Type: text/plain, Size: 1632 bytes --] Cool! Thanks for writing this. Can you explain how defining a custom printer works? The new language features still bewilder me. On Dec 7, 2011 10:29 AM, "Jérémie Dimino" <jeremie@dimino.org> wrote: > Hi, > > I have made a patch for ocaml 3.12.1 which allow to have a generic > printing function. The patch is available here: > > http://www.dimino.org/ocaml-3.12.1-generic-print.patch > > Here is how to use it: > > external show : 'a -> string = "%show" > > let () = print_endline (show ([1; 2; 3], Some "foo")) > > this will print: > > ([1; 2; 3], Some "foo") > > It is also possible to define custom printers, for example: > > module StringMap : sig > include Map.S with type key = string > val string_of_t : ('a -> string) -> 'a t -> string > end = struct > include Map.Make(String) > let string_of_t (type a) string_of_a (m : a t) = > show (bindings m) > end > > let () = print_endline (show (StringMap.singleton "x" 1)) > > will print: > > [("x", 1)] > > Of course it is limited to what the compiler knows, for example the > following function will always returns "[<poly>; <poly>]": > > let f x = show [x; x] > > But i think it is already very useful for debugging. > > The git repo is here: > > http://www.dimino.org/gitweb/?p=ocaml-3.12.1-print.git;a=summary > > Cheers, > > -- > Jérémie > > > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > [-- Attachment #2: Type: text/html, Size: 2512 bytes --] ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-07 16:46 ` Alex Rubinsteyn @ 2011-12-07 17:10 ` Jérémie Dimino 0 siblings, 0 replies; 23+ messages in thread From: Jérémie Dimino @ 2011-12-07 17:10 UTC (permalink / raw) To: Alex Rubinsteyn; +Cc: Caml List Le mercredi 07 décembre 2011 à 11:46 -0500, Alex Rubinsteyn a écrit : > Cool! Thanks for writing this. Can you explain how defining a custom > printer works? The new language features still bewilder me. If you want to define a custom printer for a type named foo, you have to define a function named string_of_foo. It must be in the same module as foo. It takes as arguments printers for its type variables, the value to print and returns a string. For example, if your type is: type ('a, 'b) foo a custom printer for foo will have the signature: val string_of_foo : ('a -> string) -> ('b -> string) -> ('a, 'b) foo -> string For example: module Id : sig type t val string_of_t : t -> string val create : unit -> t end = struct type t = int let string_of_t x = "<id=" ^ string_of_int x ^ ">" let next = ref 0 let create () = incr next; !next end You may also want to use "show" in your custom printer. In order to tell show to use the printers given as arguments, you must use type parameters like that: let string_of_foo (type a) (type b) string_of_a string_of_b (x : (a, b) foo) = ... For example to export a generated printer for an abstract type: module M : sig type ('a, 'b) t val string_of_t : ('a -> string) -> ('b -> string) -> ('a, 'b) t -> string end = struct type ('a, 'b) t = A of 'a | B of 'b let string_of_t (type a) (type b) string_of_a string_of_b (x : (a, b) t) = show x end Cheers, -- Jérémie ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-07 15:28 [Caml-list] Generic printer patch Jérémie Dimino 2011-12-07 16:46 ` Alex Rubinsteyn @ 2011-12-07 16:56 ` François Bobot 2011-12-07 17:34 ` Jérémie Dimino 2011-12-08 2:33 ` Edgar Friendly 2011-12-08 5:26 ` Martin Jambon 3 siblings, 1 reply; 23+ messages in thread From: François Bobot @ 2011-12-07 16:56 UTC (permalink / raw) To: caml-list Le 07/12/2011 16:28, Jérémie Dimino a écrit : > Hi, > > I have made a patch for ocaml 3.12.1 which allow to have a generic > printing function. The patch is available here: > [...] > Of course it is limited to what the compiler knows, for example the > following function will always returns "[<poly>;<poly>]": > > let f x = show [x; x] > > But i think it is already very useful for debugging. Great job in so few time. That's right that can be an awesome feature. I haven't tested it yet. I just read the diff. But I have some questions : 1) If I understand well the "%show" command is expanded into the ocaml code (lambda-code) which can print the type. So, for instance, in the case of a list, your code generates a recursive function "aux" and it applies this new "aux" function to the argument of "%show". Do you plan to add in the module List a function : val string_of_list : ('a -> string) -> 'a list -> string and to generate only the call to this function instead of generating the "aux" function every time? Or do you do it purposely? 2) Could you imagine to generalize it to Format.formatter or to out_channel (without creating a string and concatenating)? Romain Bardou add in the mantis tracker (I can't give you the bugtracking number since mantis "is currently offline for maintenance") a feature wish for a new conversion specification that can print anything. Do you think you can fulfill is dream? 3) Is it impossible to reuse/merge with the code of the top level that print ocaml values? Thank you for providing this code. Funny the way you find the user defined function. Be careful to not implement type classes in Ocaml ;) -- François Bobot ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-07 16:56 ` François Bobot @ 2011-12-07 17:34 ` Jérémie Dimino 2011-12-08 12:00 ` Romain Bardou 0 siblings, 1 reply; 23+ messages in thread From: Jérémie Dimino @ 2011-12-07 17:34 UTC (permalink / raw) To: François Bobot; +Cc: caml-list Le mercredi 07 décembre 2011 à 17:56 +0100, François Bobot a écrit : > 1) If I understand well the "%show" command is expanded into the ocaml > code (lambda-code) which can print the type. So, for instance, in the > case of a list, your code generates a recursive function "aux" and it > applies this new "aux" function to the argument of "%show". Do you plan > to add in the module List a function : > > val string_of_list : ('a -> string) -> 'a list -> string > > and to generate only the call to this function instead of generating the > "aux" function every time? Or do you do it purposely? It is not in the lambda-code: i get the typed tree, generate for every occurrence of %show a parsetree using types and type this parsetree. The result is inserted in place of %show. For the rest, yes, it is how it works. Of course it is better if the printer is defined once and for all in its module. I did not modify the standard library because i did not wanted to rebuild all my libraries. > 2) Could you imagine to generalize it to Format.formatter or to > out_channel (without creating a string and concatenating)? Romain Bardou > add in the mantis tracker (I can't give you the bugtracking number since > mantis "is currently offline for maintenance") a feature wish for a new > conversion specification that can print anything. Do you think you can > fulfill is dream? For using formatter, yes, we can do that. This patch is more a proof of concept. If we want to really use it we can do something more generic than just generating printers. For example we could generate type representation values from which we would generate printers, marshallers, ... For the new conversion specification i don't think it is possible with the way the stdlib implements printf. The best you can do is something like: printf "%a" show value However it is possible with batteries' formatted printing (Print.printf p"%s" ...). > 3) Is it impossible to reuse/merge with the code of the top level that > print ocaml values? No, the toplevel use outcome trees (defined in Outcometree). > Thank you for providing this code. Funny the way you find the user > defined function. Be careful to not implement type classes in Ocaml ;) :-) -- Jérémie ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-07 17:34 ` Jérémie Dimino @ 2011-12-08 12:00 ` Romain Bardou 2011-12-08 14:21 ` Gerd Stolpmann 2011-12-08 18:20 ` Martin Jambon 0 siblings, 2 replies; 23+ messages in thread From: Romain Bardou @ 2011-12-08 12:00 UTC (permalink / raw) To: caml-list >> 2) Could you imagine to generalize it to Format.formatter or to >> out_channel (without creating a string and concatenating)? Romain Bardou >> add in the mantis tracker (I can't give you the bugtracking number since >> mantis "is currently offline for maintenance") a feature wish for a new >> conversion specification that can print anything. Do you think you can >> fulfill is dream? Here is the feature request I proposed: http://caml.inria.fr/mantis/view.php?id=4956 Here is the response by Pierre Weis: "This is a major feature wish that requires careful thinking and a lot of work! Furthermore, we would not have a completely satisfactory solution in the end (due to this <poly> catch all case that tend to propagate, as far as you use polymorphic functions). The correct solution to get this feature in its full glory is a major modification of the type system along the lines of G'Caml. In short, a natural feature wish in a strongly typed polymorphic language; we had it in mind for decades; unfortunately, we are not yet ready to offer it, even in the rather limited extent you proposed." In other words: what you did is awesome but I'm not sure that it will be added in the trunk :( Cheers, -- Romain Bardou ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 12:00 ` Romain Bardou @ 2011-12-08 14:21 ` Gerd Stolpmann 2011-12-08 18:20 ` Martin Jambon 1 sibling, 0 replies; 23+ messages in thread From: Gerd Stolpmann @ 2011-12-08 14:21 UTC (permalink / raw) To: Romain Bardou; +Cc: caml-list >>> 2) Could you imagine to generalize it to Format.formatter or to >>> out_channel (without creating a string and concatenating)? Romain >>> Bardou >>> add in the mantis tracker (I can't give you the bugtracking number >>> since >>> mantis "is currently offline for maintenance") a feature wish for a new >>> conversion specification that can print anything. Do you think you can >>> fulfill is dream? > > Here is the feature request I proposed: > > http://caml.inria.fr/mantis/view.php?id=4956 > > Here is the response by Pierre Weis: > > "This is a major feature wish that requires careful thinking and a lot > of work! > > Furthermore, we would not have a completely satisfactory solution in the > end (due to this <poly> catch all case that tend to propagate, as far as > you use polymorphic functions). The correct solution to get this feature > in its full glory is a major modification of the type system along the > lines of G'Caml. Well, G'Caml did not appear finally. But this explains why no generic printer has been added so far: There was always the hope to get this feature automatically by some generalization in the compiler. Same for type-safe unmarshalling, and other features needing introspection. On the one hand, these features are badly needed by users. On the other hand, the language designers strive for an excellent solution, and not one that will become obsolete sooner or later. The situation is different today. We'll now have GADTs in the next release, which work quite differently, and which are no replacement for a separate introspection solution (like the one Alain suggests), but could be a clever add-on. So, it has become more likely that the pragmatism wins this time, because it makes the new playground GADTs more enjoyable (in particular when the compiler supports the generation of "standard GADTs" just representing the structure of a type). Gerd > > In short, a natural feature wish in a strongly typed polymorphic > language; we had it in mind for decades; unfortunately, we are not yet > ready to offer it, even in the rather limited extent you proposed." > > In other words: what you did is awesome but I'm not sure that it will be > added in the trunk :( > > Cheers, > > -- > Romain Bardou > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > > -- Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de Creator of GODI and camlcity.org. Contact details: http://www.camlcity.org/contact.html Company homepage: http://www.gerd-stolpmann.de *** Searching for new projects! Need consulting for system *** programming in Ocaml? Gerd Stolpmann can help you. ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 12:00 ` Romain Bardou 2011-12-08 14:21 ` Gerd Stolpmann @ 2011-12-08 18:20 ` Martin Jambon 2011-12-08 21:39 ` Gabriel Scherer 2011-12-09 7:15 ` Jérémie Dimino 1 sibling, 2 replies; 23+ messages in thread From: Martin Jambon @ 2011-12-08 18:20 UTC (permalink / raw) To: caml-list On 12/08/2011 04:00 AM, Romain Bardou wrote: >>> 2) Could you imagine to generalize it to Format.formatter or to >>> out_channel (without creating a string and concatenating)? Romain Bardou >>> add in the mantis tracker (I can't give you the bugtracking number since >>> mantis "is currently offline for maintenance") a feature wish for a new >>> conversion specification that can print anything. Do you think you can >>> fulfill is dream? > > Here is the feature request I proposed: > > http://caml.inria.fr/mantis/view.php?id=4956 > > Here is the response by Pierre Weis: > > "This is a major feature wish that requires careful thinking and a lot > of work! > > Furthermore, we would not have a completely satisfactory solution in the > end (due to this <poly> catch all case that tend to propagate, as far as > you use polymorphic functions). The correct solution to get this feature > in its full glory is a major modification of the type system along the > lines of G'Caml. The feature we want is exactly Jeremie's "hack" (his words). We need this feature for debugging and displaying data in log files. This kind of data is almost never polymorphic, so there is no practical issue here. Also we don't need a standardized output format. However we would often like to truncate the data to a reasonable size. I understand that this feature could be replaced in the future by a more complete solution, but we would be happy if it were provided as an "experimental extension" of OCaml. Martin > In short, a natural feature wish in a strongly typed polymorphic > language; we had it in mind for decades; unfortunately, we are not yet > ready to offer it, even in the rather limited extent you proposed." > > In other words: what you did is awesome but I'm not sure that it will be > added in the trunk :( > > Cheers, > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 18:20 ` Martin Jambon @ 2011-12-08 21:39 ` Gabriel Scherer 2011-12-09 7:22 ` Jérémie Dimino 2011-12-09 7:15 ` Jérémie Dimino 1 sibling, 1 reply; 23+ messages in thread From: Gabriel Scherer @ 2011-12-08 21:39 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list Martin, in the meantime, you can use Extlib's (Std.dump : 'a -> string) function, which is also integrated into Batteries. `dump` does not require any modification to the compiler or toolchain. For those that are not familiar with it, 'dump' uses the low-level representation of OCaml values to print a sensible string representing a value. As it only uses information that is available at runtime, it is much less precise that Jeremie's printer, as for example None, false, [] and 0 will all be printed as 0 (they have the same runtime representation). Of course, the "right" way to print/marshal data without changing the language is to build a printer for your type from printer combinators. Such combinators are available for example: - in Jane Street Core library ( a Sexpable interface in each datatype module ) - in Xavier Clerc's Kaputt testing framework : http://kaputt.x9c.fr/distrib/api/Utils.html You can also use metaprogramming (.. camlp4) to generate printer functions from datatypes description automatically, using such combinators. See: - Markus Mottl's 'type-conv': http://hg.ocaml.info/release/type-conv - Jeremy Yallop's 'deriving': http://code.google.com/p/deriving/ Of course, printing values magically is still easier: you don't have to build the printer yourself, passing subtype printers when necessary, etc. I think Std.dump is reasonable for quick hacks or debugging usage. On Thu, Dec 8, 2011 at 7:20 PM, Martin Jambon <martin.jambon@ens-lyon.org> wrote: > On 12/08/2011 04:00 AM, Romain Bardou wrote: >>>> 2) Could you imagine to generalize it to Format.formatter or to >>>> out_channel (without creating a string and concatenating)? Romain Bardou >>>> add in the mantis tracker (I can't give you the bugtracking number since >>>> mantis "is currently offline for maintenance") a feature wish for a new >>>> conversion specification that can print anything. Do you think you can >>>> fulfill is dream? >> >> Here is the feature request I proposed: >> >> http://caml.inria.fr/mantis/view.php?id=4956 >> >> Here is the response by Pierre Weis: >> >> "This is a major feature wish that requires careful thinking and a lot >> of work! >> >> Furthermore, we would not have a completely satisfactory solution in the >> end (due to this <poly> catch all case that tend to propagate, as far as >> you use polymorphic functions). The correct solution to get this feature >> in its full glory is a major modification of the type system along the >> lines of G'Caml. > > The feature we want is exactly Jeremie's "hack" (his words). We need > this feature for debugging and displaying data in log files. This kind > of data is almost never polymorphic, so there is no practical issue > here. Also we don't need a standardized output format. However we would > often like to truncate the data to a reasonable size. > > I understand that this feature could be replaced in the future by a more > complete solution, but we would be happy if it were provided as an > "experimental extension" of OCaml. > > > Martin > >> In short, a natural feature wish in a strongly typed polymorphic >> language; we had it in mind for decades; unfortunately, we are not yet >> ready to offer it, even in the rather limited extent you proposed." >> >> In other words: what you did is awesome but I'm not sure that it will be >> added in the trunk :( >> >> Cheers, >> > > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 21:39 ` Gabriel Scherer @ 2011-12-09 7:22 ` Jérémie Dimino 2011-12-09 9:26 ` Gabriel Scherer 0 siblings, 1 reply; 23+ messages in thread From: Jérémie Dimino @ 2011-12-09 7:22 UTC (permalink / raw) To: Gabriel Scherer; +Cc: Martin Jambon, caml-list Le jeudi 08 décembre 2011 à 22:39 +0100, Gabriel Scherer a écrit : > Of course, the "right" way to print/marshal data without changing the > language is to build a printer for your type from printer combinators. > Such combinators are available for example: > - in Jane Street Core library ( a Sexpable interface in each datatype module ) > - in Xavier Clerc's Kaputt testing framework : > http://kaputt.x9c.fr/distrib/api/Utils.html > > You can also use metaprogramming (.. camlp4) to generate printer > functions from datatypes description automatically, using such > combinators. See: > - Markus Mottl's 'type-conv': http://hg.ocaml.info/release/type-conv > - Jeremy Yallop's 'deriving': http://code.google.com/p/deriving/ These solutions would be usable only if everybody were using it. Right now almost nobody use them, and so if you want to print types coming from external library you still have to write the printer yourself. > Of course, printing values magically is still easier: you don't have > to build the printer yourself, passing subtype printers when > necessary, etc. I think Std.dump is reasonable for quick hacks or > debugging usage. If think my hack is reasonable too, you just have to replace ocamlc and ocamlopt (if you already use ocaml 3.12.1) by the new compilers and it will work. Of course don't use it in production code ;-) Cheers, -- Jérémie ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-09 7:22 ` Jérémie Dimino @ 2011-12-09 9:26 ` Gabriel Scherer 0 siblings, 0 replies; 23+ messages in thread From: Gabriel Scherer @ 2011-12-09 9:26 UTC (permalink / raw) To: Jérémie Dimino, caml users > If think my hack is reasonable too, you just have to replace ocamlc and > ocamlopt (if you already use ocaml 3.12.1) by the new compilers and it > will work. Indeed. It's just that in some cases (eg. you are no expert user and/or use the ocaml binary packages of your distribution) the cost of patching the compiler is quite high, and Std.dump is useful a degraded but work-out-of-the-box solution. On Fri, Dec 9, 2011 at 8:22 AM, Jérémie Dimino <jeremie@dimino.org> wrote: > Le jeudi 08 décembre 2011 à 22:39 +0100, Gabriel Scherer a écrit : >> Of course, the "right" way to print/marshal data without changing the >> language is to build a printer for your type from printer combinators. >> Such combinators are available for example: >> - in Jane Street Core library ( a Sexpable interface in each datatype module ) >> - in Xavier Clerc's Kaputt testing framework : >> http://kaputt.x9c.fr/distrib/api/Utils.html >> >> You can also use metaprogramming (.. camlp4) to generate printer >> functions from datatypes description automatically, using such >> combinators. See: >> - Markus Mottl's 'type-conv': http://hg.ocaml.info/release/type-conv >> - Jeremy Yallop's 'deriving': http://code.google.com/p/deriving/ > > These solutions would be usable only if everybody were using it. Right > now almost nobody use them, and so if you want to print types coming > from external library you still have to write the printer yourself. > >> Of course, printing values magically is still easier: you don't have >> to build the printer yourself, passing subtype printers when >> necessary, etc. I think Std.dump is reasonable for quick hacks or >> debugging usage. > > If think my hack is reasonable too, you just have to replace ocamlc and > ocamlopt (if you already use ocaml 3.12.1) by the new compilers and it > will work. > > Of course don't use it in production code ;-) > > Cheers, > > -- > Jérémie > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 18:20 ` Martin Jambon 2011-12-08 21:39 ` Gabriel Scherer @ 2011-12-09 7:15 ` Jérémie Dimino 1 sibling, 0 replies; 23+ messages in thread From: Jérémie Dimino @ 2011-12-09 7:15 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list Le jeudi 08 décembre 2011 à 10:20 -0800, Martin Jambon a écrit : > I understand that this feature could be replaced in the future by a more > complete solution, but we would be happy if it were provided as an > "experimental extension" of OCaml. If you want to use it i can maintain it (and make it a bit better) until a real solution is added to OCaml. I am already using it myself for debugging. I uploaded it here: https://github.com/diml/ocaml-3.12.1-print Cheers, -- Jérémie ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-07 15:28 [Caml-list] Generic printer patch Jérémie Dimino 2011-12-07 16:46 ` Alex Rubinsteyn 2011-12-07 16:56 ` François Bobot @ 2011-12-08 2:33 ` Edgar Friendly 2011-12-08 7:28 ` François Bobot 2011-12-08 9:00 ` Alain Frisch 2011-12-08 5:26 ` Martin Jambon 3 siblings, 2 replies; 23+ messages in thread From: Edgar Friendly @ 2011-12-08 2:33 UTC (permalink / raw) To: caml-list On 12/07/2011 10:28 AM, Jérémie Dimino wrote: > Hi, > > I have made a patch for ocaml 3.12.1 which allow to have a generic > printing function. The patch is available here: > > external show : 'a -> string = "%show" > > Of course it is limited to what the compiler knows, for example the > following function will always returns "[<poly>;<poly>]": I'm interested in having compile-time reflection like this, but maybe with a bit more generality. Maybe an identifier can be built out of the type in a generic way... external show : 'a -> string = "%type_to_string" external print : out_channel -> 'a -> unit external cmp : 'a -> 'a -> int = "%type_compare" etc. E. ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 2:33 ` Edgar Friendly @ 2011-12-08 7:28 ` François Bobot 2011-12-08 9:00 ` Alain Frisch 1 sibling, 0 replies; 23+ messages in thread From: François Bobot @ 2011-12-08 7:28 UTC (permalink / raw) To: caml-list Le 08/12/2011 03:33, Edgar Friendly a écrit : > I'm interested in having compile-time reflection like this, but maybe > with a bit more generality. Maybe an identifier can be built out of the > type in a generic way... > > external show : 'a -> string = "%type_to_string" > external print : out_channel -> 'a -> unit > external cmp : 'a -> 'a -> int = "%type_compare" When does this function return something different than 0? Jérémie Dimino a écrit : > If we want to really use it we can do something more generic > than just generating printers. For example we could generate type > representation values from which we would generate printers, > marshallers, ... I'm not sure that we can use a function like : external type_expr : 'a -> 'a type_expr (with GADT) (or just external type_expr : 'a -> type_expr) in order to write a generic printer, since during the definition the value is polymorphic. We only know the type at the call site. Of course the user can call the function himself : val gen_to_string : 'a type_expr -> 'a -> string let v = ... in (** debug *) print_string (gen_to_string (type_expr v) v) Perhaps we can ask that the first argument is implicitly given by the OCaml compiler (extending the "type a" notation) : let gen_to_string type (a:repr) (x:a) = ... (repr : a type_expr) ... .... let v = ... in (** debug *) print_string (gen_to_string v) In fact the LexiFi extension to OCaml called implicit argument, can do something similar : http://www.lexifi.com/blog/implicit-values (You can look for "Combined with GADTs, one could do something like (not tested!):") But with all these solutions we will never be able to write a generic printer which can print values that come from a dynamic plugin (um.. wait and with first order modules?...) -- François Bobot ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 2:33 ` Edgar Friendly 2011-12-08 7:28 ` François Bobot @ 2011-12-08 9:00 ` Alain Frisch 2011-12-08 9:24 ` Gerd Stolpmann 1 sibling, 1 reply; 23+ messages in thread From: Alain Frisch @ 2011-12-08 9:00 UTC (permalink / raw) To: Edgar Friendly; +Cc: caml-list On 12/08/2011 03:33 AM, Edgar Friendly wrote: > On 12/07/2011 10:28 AM, Jérémie Dimino wrote: >> Hi, >> >> I have made a patch for ocaml 3.12.1 which allow to have a generic >> printing function. The patch is available here: >> >> external show : 'a -> string = "%show" >> >> Of course it is limited to what the compiler knows, for example the >> following function will always returns "[<poly>;<poly>]": > > I'm interested in having compile-time reflection like this, but maybe > with a bit more generality. You might be interested in my proposal to extend OCaml with a notion of runtime representation of types: http://www.lexifi.com/blog/runtime-types A generic value pretty-printer can be written in "user-land" using this extension (in a type-safe way). -- Alain ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 9:00 ` Alain Frisch @ 2011-12-08 9:24 ` Gerd Stolpmann 2011-12-08 10:32 ` Alain Frisch 0 siblings, 1 reply; 23+ messages in thread From: Gerd Stolpmann @ 2011-12-08 9:24 UTC (permalink / raw) To: Alain Frisch; +Cc: Edgar Friendly, caml-list > On 12/08/2011 03:33 AM, Edgar Friendly wrote: >> On 12/07/2011 10:28 AM, Jérémie Dimino wrote: >>> Hi, >>> >>> I have made a patch for ocaml 3.12.1 which allow to have a generic >>> printing function. The patch is available here: >>> >>> external show : 'a -> string = "%show" >>> >>> Of course it is limited to what the compiler knows, for example the >>> following function will always returns "[<poly>;<poly>]": >> >> I'm interested in having compile-time reflection like this, but maybe >> with a bit more generality. > > You might be interested in my proposal to extend OCaml with a notion of > runtime representation of types: > > http://www.lexifi.com/blog/runtime-types > > A generic value pretty-printer can be written in "user-land" using this > extension (in a type-safe way). Want it! Want it! Want it! Any plans for including this into the official compiler? Gerd > > > -- Alain > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > > -- Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de Creator of GODI and camlcity.org. Contact details: http://www.camlcity.org/contact.html Company homepage: http://www.gerd-stolpmann.de *** Searching for new projects! Need consulting for system *** programming in Ocaml? Gerd Stolpmann can help you. ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 9:24 ` Gerd Stolpmann @ 2011-12-08 10:32 ` Alain Frisch 2011-12-08 10:41 ` Jonathan Protzenko 0 siblings, 1 reply; 23+ messages in thread From: Alain Frisch @ 2011-12-08 10:32 UTC (permalink / raw) To: Gerd Stolpmann; +Cc: Edgar Friendly, caml-list On 12/08/2011 10:24 AM, Gerd Stolpmann wrote: >> http://www.lexifi.com/blog/runtime-types >> > Want it! Want it! Want it! > > Any plans for including this into the official compiler? Since the reception was not bad when I presented it to the Caml Consortium meeting, yes, I'm proposing this as an extension to the official compiler. The plan is to create a branch in the OCaml SVN (I don't know when I'll be able to do it, hopefully before end of January) and follow the same approach as for GADTs (i.e. ask the community for some feedback, and discuss the proposal amongst the core team). I cannot commit on the final outcome, of course. -- Alain ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 10:32 ` Alain Frisch @ 2011-12-08 10:41 ` Jonathan Protzenko 2011-12-08 12:00 ` Philippe Veber 0 siblings, 1 reply; 23+ messages in thread From: Jonathan Protzenko @ 2011-12-08 10:41 UTC (permalink / raw) To: Alain Frisch; +Cc: Edgar Friendly, caml-list, Gerd Stolpmann Do you plan on opening up your automated gui generation library and have it distributed so that other users can take advantage of it? Say, make it rely on lablgtk and bundle it as a semi-official 3rd-party library that other people can reuse :-). jonathan On Thu 08 Dec 2011 11:32:28 AM CET, Alain Frisch wrote: > On 12/08/2011 10:24 AM, Gerd Stolpmann wrote: >>> http://www.lexifi.com/blog/runtime-types >>> >> Want it! Want it! Want it! >> >> Any plans for including this into the official compiler? > > Since the reception was not bad when I presented it to the Caml > Consortium meeting, yes, I'm proposing this as an extension to the > official compiler. The plan is to create a branch in the OCaml SVN (I > don't know when I'll be able to do it, hopefully before end of > January) and follow the same approach as for GADTs (i.e. ask the > community for some feedback, and discuss the proposal amongst the core > team). I cannot commit on the final outcome, of course. > > -- Alain > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 10:41 ` Jonathan Protzenko @ 2011-12-08 12:00 ` Philippe Veber 0 siblings, 0 replies; 23+ messages in thread From: Philippe Veber @ 2011-12-08 12:00 UTC (permalink / raw) To: Jonathan Protzenko Cc: Alain Frisch, Edgar Friendly, caml-list, Gerd Stolpmann [-- Attachment #1: Type: text/plain, Size: 1564 bytes --] And the SQL schema generation lib as well :o) ? 2011/12/8 Jonathan Protzenko <jonathan.protzenko@gmail.com> > Do you plan on opening up your automated gui generation library and have > it distributed so that other users can take advantage of it? Say, make it > rely on lablgtk and bundle it as a semi-official 3rd-party library that > other people can reuse :-). > > jonathan > > > On Thu 08 Dec 2011 11:32:28 AM CET, Alain Frisch wrote: > >> On 12/08/2011 10:24 AM, Gerd Stolpmann wrote: >> >>> http://www.lexifi.com/blog/**runtime-types<http://www.lexifi.com/blog/runtime-types> >>>> >>>> Want it! Want it! Want it! >>> >>> Any plans for including this into the official compiler? >>> >> >> Since the reception was not bad when I presented it to the Caml >> Consortium meeting, yes, I'm proposing this as an extension to the official >> compiler. The plan is to create a branch in the OCaml SVN (I don't know >> when I'll be able to do it, hopefully before end of January) and follow the >> same approach as for GADTs (i.e. ask the community for some feedback, and >> discuss the proposal amongst the core team). I cannot commit on the final >> outcome, of course. >> >> -- Alain >> >> > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/**wws/info/caml-list<https://sympa-roc.inria.fr/wws/info/caml-list> > Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners<http://groups.yahoo.com/group/ocaml_beginners> > Bug reports: http://caml.inria.fr/bin/caml-**bugs<http://caml.inria.fr/bin/caml-bugs> > > [-- Attachment #2: Type: text/html, Size: 2561 bytes --] ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-07 15:28 [Caml-list] Generic printer patch Jérémie Dimino ` (2 preceding siblings ...) 2011-12-08 2:33 ` Edgar Friendly @ 2011-12-08 5:26 ` Martin Jambon 2011-12-08 6:52 ` Jérémie Dimino 3 siblings, 1 reply; 23+ messages in thread From: Martin Jambon @ 2011-12-08 5:26 UTC (permalink / raw) To: caml-list Thanks Jérémie for doing this, it's a very useful feature. I would like to fetch the git repository but it won't let me: $ git clone 'http://www.dimino.org/git/ocaml-3.12.1-print.git' Cloning into ocaml-3.12.1-print... fatal: http://www.dimino.org/git/ocaml-3.12.1-print.git/info/refs not found: did you run git update-server-info on the server? Martin On 12/07/2011 07:28 AM, Jérémie Dimino wrote: > Hi, > > I have made a patch for ocaml 3.12.1 which allow to have a generic > printing function. The patch is available here: > > http://www.dimino.org/ocaml-3.12.1-generic-print.patch > > Here is how to use it: > > external show : 'a -> string = "%show" > > let () = print_endline (show ([1; 2; 3], Some "foo")) > > this will print: > > ([1; 2; 3], Some "foo") > > It is also possible to define custom printers, for example: > > module StringMap : sig > include Map.S with type key = string > val string_of_t : ('a -> string) -> 'a t -> string > end = struct > include Map.Make(String) > let string_of_t (type a) string_of_a (m : a t) = > show (bindings m) > end > > let () = print_endline (show (StringMap.singleton "x" 1)) > > will print: > > [("x", 1)] > > Of course it is limited to what the compiler knows, for example the > following function will always returns "[<poly>; <poly>]": > > let f x = show [x; x] > > But i think it is already very useful for debugging. > > The git repo is here: > > http://www.dimino.org/gitweb/?p=ocaml-3.12.1-print.git;a=summary > > Cheers, > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 5:26 ` Martin Jambon @ 2011-12-08 6:52 ` Jérémie Dimino 2011-12-08 7:44 ` Martin Jambon 0 siblings, 1 reply; 23+ messages in thread From: Jérémie Dimino @ 2011-12-08 6:52 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list Le mercredi 07 décembre 2011 à 21:26 -0800, Martin Jambon a écrit : > I would like to fetch the git repository but it won't let me: > > $ git clone 'http://www.dimino.org/git/ocaml-3.12.1-print.git' > Cloning into ocaml-3.12.1-print... > fatal: http://www.dimino.org/git/ocaml-3.12.1-print.git/info/refs not > found: did you run git update-server-info on the server? I ran the command. It should work now. -- Jérémie ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 6:52 ` Jérémie Dimino @ 2011-12-08 7:44 ` Martin Jambon 2011-12-08 9:37 ` Jérémie Dimino 0 siblings, 1 reply; 23+ messages in thread From: Martin Jambon @ 2011-12-08 7:44 UTC (permalink / raw) To: Jérémie Dimino; +Cc: caml-list On 12/07/2011 10:52 PM, Jérémie Dimino wrote: > Le mercredi 07 décembre 2011 à 21:26 -0800, Martin Jambon a écrit : >> I would like to fetch the git repository but it won't let me: >> >> $ git clone 'http://www.dimino.org/git/ocaml-3.12.1-print.git' >> Cloning into ocaml-3.12.1-print... >> fatal: http://www.dimino.org/git/ocaml-3.12.1-print.git/info/refs not >> found: did you run git update-server-info on the server? > > I ran the command. It should work now. It works. Thanks. What do you think of moving the project to GitHub so we could follow its progress and contribute more easily? For now, I would just like to report that so far it works just as I imagined, with the following bug: let f x = show x results in a segfault when running bytecode and an Assert_failure while compiling with ocamlopt: external show : 'a -> string = "%show" let f l = show l let () = print_endline (f ()) $ ocamlopt -o toto toto.ml Fatal error: exception Assert_failure("asmcomp/cmmgen.ml", 1682, 10) Martin ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [Caml-list] Generic printer patch 2011-12-08 7:44 ` Martin Jambon @ 2011-12-08 9:37 ` Jérémie Dimino 0 siblings, 0 replies; 23+ messages in thread From: Jérémie Dimino @ 2011-12-08 9:37 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list Le mercredi 07 décembre 2011 à 23:44 -0800, Martin Jambon a écrit : > What do you think of moving the project to GitHub so we could follow its > progress and contribute more easily? This projet is just a quick hack. If i do something more serious i will consider creating a real project. > For now, I would just like to report that so far it works just as I > imagined, with the following bug: > > let f x = show x > > results in a segfault when running bytecode and an Assert_failure while > compiling with ocamlopt: > > external show : 'a -> string = "%show" > let f l = show l > let () = print_endline (f ()) > > $ ocamlopt -o toto toto.ml > Fatal error: exception Assert_failure("asmcomp/cmmgen.ml", 1682, 10) Fixed. I sometimes generated "let rec ... in ..." without any bindings which confused the compiler. -- Jérémie ^ permalink raw reply [flat|nested] 23+ messages in thread
end of thread, other threads:[~2011-12-09 9:26 UTC | newest] Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2011-12-07 15:28 [Caml-list] Generic printer patch Jérémie Dimino 2011-12-07 16:46 ` Alex Rubinsteyn 2011-12-07 17:10 ` Jérémie Dimino 2011-12-07 16:56 ` François Bobot 2011-12-07 17:34 ` Jérémie Dimino 2011-12-08 12:00 ` Romain Bardou 2011-12-08 14:21 ` Gerd Stolpmann 2011-12-08 18:20 ` Martin Jambon 2011-12-08 21:39 ` Gabriel Scherer 2011-12-09 7:22 ` Jérémie Dimino 2011-12-09 9:26 ` Gabriel Scherer 2011-12-09 7:15 ` Jérémie Dimino 2011-12-08 2:33 ` Edgar Friendly 2011-12-08 7:28 ` François Bobot 2011-12-08 9:00 ` Alain Frisch 2011-12-08 9:24 ` Gerd Stolpmann 2011-12-08 10:32 ` Alain Frisch 2011-12-08 10:41 ` Jonathan Protzenko 2011-12-08 12:00 ` Philippe Veber 2011-12-08 5:26 ` Martin Jambon 2011-12-08 6:52 ` Jérémie Dimino 2011-12-08 7:44 ` Martin Jambon 2011-12-08 9:37 ` Jérémie Dimino
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox