Loading ...
Sorry, an error occurred while loading the content.

Re: "ocaml_beginners"::[] Haskell Type Classes In OCaml

Expand Messages
  • Jason Yeo
    Hi, ... What s the difference between sexplib and camlp4? They both seem to be libraries that do some form of meta-programming right? Thanks, Jason
    Message 1 of 7 , Feb 23, 2013
    • 0 Attachment
      Hi,

      On Wed, Feb 20, 2013 at 2:25 PM, Francois Berenger <berenger@...> wrote:
      > Maybe you are looking for this:
      > http://mmottl.bitbucket.org/projects/sexplib/README.html

      What's the difference between sexplib and camlp4? They both seem to be
      libraries that do some form of meta-programming right?

      Thanks,
      Jason
    • Gabriel Scherer
      They are rather different. Camlp4 is a tool/library to write syntactic preprocessors: you use Camlp4 to develop camlp4 extensions , which are small programs
      Message 2 of 7 , Feb 23, 2013
      • 0 Attachment
        They are rather different. Camlp4 is a tool/library to write syntactic
        preprocessors: you use Camlp4 to develop "camlp4 extensions", which are
        small programs you can ask to run during the ocaml compilation process,
        taking your input code and producing a (standard) OCaml AST as a result.
        This allows to pass source programs that are not valid OCaml code (because
        of syntax changes), but preprocessed into valid OCaml code, or transform
        OCaml programs in any way (for example to add some instrumentation in some
        places).

        Sexplib is a convenient serialization and deserialization format, basically
        lispy s-expressions (trees with lots of parenthesis). The relation with
        Camlp4 is that the Sexplib provides a syntax extension that, from OCaml
        type declarations, meta-generates serializers and deserializers for this
        type. This relies on a rather small and modular camlp4 extension, Markus
        Mottl's "type-conv", that allows to attach boilerplate generators to type
        declarations (Jeremy Yallop's "deriving" extension also does something like
        that). This would correspond to "deriving Eq, Ord, Typeable" in Haskell,
        but implemented outside the language as a syntax extension.

        Sexplib was suggested to you because the particular use of ad-hoc
        polymorphism you requested was data dumping/pickling/serialization. (There
        are other serialization libraries that cover other aspects, but Sexplib is
        a reasonable first choice as it is both human-readable and reasonably
        not-too-inefficient.) Neither Sexplib nor Camlp4 solve the general problem
        of lack of ad-hoc polymorphism in OCaml, but type-directed boilerplate code
        generation is often a reasonable solution to ad-hoc needs.

        For general typeclasses envy, the solution is to implement dictionary
        passing. This can be done at the module level using functors, or at the
        value level passing dictionaries as records, objects or first-class
        modules. See
        http://alaska-kamtchatka.blogspot.fr/2009/08/fun-with-type-class.html for
        example. There are experiments towards better support for implicit argument
        passing in http://gitorious.org/ocaml-ty , but they're not for the short
        term.



        On Sat, Feb 23, 2013 at 9:35 AM, Jason Yeo <jasonyeo88@...> wrote:

        > Hi,
        >
        > On Wed, Feb 20, 2013 at 2:25 PM, Francois Berenger <berenger@...>
        > wrote:
        > > Maybe you are looking for this:
        > > http://mmottl.bitbucket.org/projects/sexplib/README.html
        >
        > What's the difference between sexplib and camlp4? They both seem to be
        > libraries that do some form of meta-programming right?
        >
        > Thanks,
        > Jason
        >
        >
        > ------------------------------------
        >
        > Archives up to December 31, 2011 are also downloadable at
        > http://www.connettivo.net/cntprojects/ocaml_beginners
        > The archives of the very official ocaml list (the seniors' one) can be
        > found at http://caml.inria.fr
        > Attachments are banned and you're asked to be polite, avoid flames
        > etc.Yahoo! Groups Links
        >
        >
        >
        >


        [Non-text portions of this message have been removed]
      • Brian Hurt
        ... As a general rule, any where you d use type classes in Haskell, the right solution in Ocaml is to use modules & functors (as a side note, the term
        Message 3 of 7 , Mar 1 1:24 PM
        • 0 Attachment
          On Wed, Feb 20, 2013 at 12:59 AM, Jason Yeo <jasonyeo88@...> wrote:

          > Hi,
          >
          > I am trying to achieve some form of polymorphism to print my data
          > structures (ints, floats, List of ints, List of floats or List of
          > ADTs).
          >
          > Is there a way to do it just like in Haskell? Maybe using type classes?
          >
          > For example, I just have to use a function to print both ints and floats.
          >
          > my_print : 'a -> String
          >
          > I understand that they are already papers written about using modules
          > or functors to emulate type classes but the programmer is required to
          > specify what type the function is working on by passing in an extra
          > parameter. This seems kinda cumbersome to me. Is there anyway to do
          > this cleanly like in Haskell?
          >
          > Thanks!
          > Jason
          >
          >
          As a general rule, any where you'd use type classes in Haskell, the right
          solution in Ocaml is to use modules & functors (as a side note, the term
          "functor" means something completely different in Ocaml than in Haskell).

          So you could implement this as:
          module type Printable = sig
          type t
          val my_print : t -> string
          end

          module PrintableList(Printable P) = struct
          type t = P.t list
          let my_print lst = ...
          end

          And so on. This is one of the situations where modules & ocaml functors
          are a little bit clunky compared to type classes, but there are other cases
          where the converse is true.

          Brian


          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.