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

Haskell Type Classes In OCaml

Expand Messages
  • Jason Yeo
    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
    Message 1 of 7 , Feb 19, 2013
    • 0 Attachment
      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
    • Francois Berenger
      ... Maybe you are looking for this: http://mmottl.bitbucket.org/projects/sexplib/README.html
      Message 2 of 7 , Feb 19, 2013
      • 0 Attachment
        On 02/20/2013 02:59 PM, Jason Yeo 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?

        Maybe you are looking for this:
        http://mmottl.bitbucket.org/projects/sexplib/README.html

        > Thanks!
        > Jason
        >
        >
      • Philippe Veber
        Hi Jason, I m affraid what you re looking for (ad-hoc polymorphism) doesn t exist in ocaml. You have to give your print functions different names and apply the
        Message 3 of 7 , Feb 19, 2013
        • 0 Attachment
          Hi Jason,

          I'm affraid what you're looking for (ad-hoc polymorphism) doesn't exist in
          ocaml. You have to give your print functions different names and apply the
          appropriate one for each datatype. If you insist on having this kind of
          genericity, the only way is through objects:

          class type stringable = object
          method to_string : string
          end

          and represent all basic types using objects, e.g.:

          class ['a] list : 'a -> 'a list -> object
          constraint 'a = < to_string : string; .. >
          method to_string : string
          end

          But I'm not sure it's worth the trouble :o).


          Now to generate the conversion functions there are a couple of
          alternatives, most notably syntax extensions like deriving and sexp (quoted
          by Fran├žois) or using combinators from Batteries.

          cheers,
          ph.

          2013/2/20 Jason Yeo <jasonyeo88@...>

          > 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
          >
          >
          > ------------------------------------
          >
          > 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]
        • Marek Kubica
          On Wed, 20 Feb 2013 13:59:43 +0800 ... Well, not really cleanly but Batteries has some kind-of-solution, check out the dump function there. regards, Marek
          Message 4 of 7 , Feb 21, 2013
          • 0 Attachment
            On Wed, 20 Feb 2013 13:59:43 +0800
            Jason Yeo <jasonyeo88@...> wrote:

            > 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?

            Well, not really cleanly but Batteries has some kind-of-solution, check
            out the "dump" function there.

            regards,
            Marek
          • 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 5 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 6 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 7 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.