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

Re: "ocaml_beginners"::[] Module parameterization by ints

Expand Messages
  • Phil Tomson
    ... Following up on this question: I need to do something very similar to the original poster s question but the answer given isn t quite clear to me. I would
    Message 1 of 8 , Jun 30, 2009
    • 0 Attachment
      On 1/8/08, Ashish Agarwal <agarwal1975@...> wrote:
      > You can define a module type with the required integers. For example,
      >
      > module type S = sig
      > val i : int
      > val j : int
      > end
      >
      > And then require a module of this type as the argument to the functor.
      >
      >
      > On Jan 8, 2008 6:44 PM, Mathias Kende <mathias@...> wrote:
      >>
      >> Hello,
      >>
      >> I am currently writing a binding in OCaml for a C++ library (Pythia).
      >> This library implements a Histograms class with a constructor taking 3
      >> integers as arguments. The class then overloads some binary operators,
      >> but these operators are well defined only if the integers given to the
      >> constructors of the two instances of the class were the same.
      >>
      >> As I don't need any OO functionality with this class, I think that the
      >> best way to translate it in OCaml is with a parameterized module whose
      >> type could enforce the meaning of the operators.
      >>
      >> I am not very familiar with the module system, I know how to write a
      >> functor taking a module in parameter, but what is the most simple way to
      >> write a module parameterized with some integers (or what is the best way
      >> to achieve something similar) ?
      >>
      >> Thanks
      >> Mathias
      >>
      >
      >
      > [Non-text portions of this message have been removed]
      >
      >

      Following up on this question: I need to do something very similar to
      the original poster's question but the answer given isn't quite clear
      to me.

      I would like to create a FIFO that is limited to N slots. I'd like to
      create a FIFO module that can be parameterized by an integer (N) so
      that I can create a FIFO of a specific size. How would I go about
      doing this?

      Phil
    • Matthieu Dubuget
      It seems to me that this is exactly the same? define a functor MyFunctor like this: module MyFunctor (S : sig val sz : int end) = struct ... S.sz ...
      Message 2 of 8 , Jul 1, 2009
      • 0 Attachment
        It seems to me that this is exactly the same?

        define a functor MyFunctor like this:
        module MyFunctor (S : sig val sz : int end) =
        struct
        ... S.sz ... ← in your module, you now have
        access to S.sz
        end



        Your module can now be generated like this:

        module MyModule = MyFunctor (struct let sz = 3 end);;

        Hoping this helps?
      • Vincent Aravantinos
        ... Good starting points: - if you accept an imperative FIFO : module Queue of the standard library - if you want a functional FIFO : module Fqueue of Jane
        Message 3 of 8 , Jul 1, 2009
        • 0 Attachment
          --- In ocaml_beginners@yahoogroups.com, Phil Tomson <rubyfan@...> wrote:
          > Following up on this question: I need to do something very similar to
          > the original poster's question but the answer given isn't quite clear
          > to me.
          >
          > I would like to create a FIFO that is limited to N slots. I'd like to
          > create a FIFO module that can be parameterized by an integer (N) so
          > that I can create a FIFO of a specific size. How would I go about
          > doing this?

          Good starting points:
          - if you accept an imperative FIFO : module Queue of the standard library
          - if you want a functional FIFO : module Fqueue of Jane Street's Core library (http://ocaml.janestreet.com/?q=node/13, also available with Godi)
          - finally module Squeue of Jane Street's Core library seems to do kinda what you want

          This last module's signature contains the following:

          (** [create maxsize] returns a synchronized queue bounded to have no more than
          [maxsize] elements. *)
          val create : int -> 'a t

          Then you can apply the previous answers (but maybe it is this last point which is the problem ?).

          Cheers
        • Ashish Agarwal
          ... If Squeue.create returns a queue bounded to a specific size, wouldn t that be a non-functorized solution? Then, you no longer need the solutions previously
          Message 4 of 8 , Jul 1, 2009
          • 0 Attachment
            > Then you can apply the previous answers
            If Squeue.create returns a queue bounded to a specific size, wouldn't that
            be a non-functorized solution? Then, you no longer need the solutions
            previously mentioned.


            On Wed, Jul 1, 2009 at 4:47 AM, Vincent Aravantinos <
            vincent.aravantinos@...> wrote:

            >
            >
            > --- In ocaml_beginners@yahoogroups.com <ocaml_beginners%40yahoogroups.com>,
            > Phil Tomson <rubyfan@...> wrote:
            > > Following up on this question: I need to do something very similar to
            > > the original poster's question but the answer given isn't quite clear
            > > to me.
            > >
            > > I would like to create a FIFO that is limited to N slots. I'd like to
            > > create a FIFO module that can be parameterized by an integer (N) so
            > > that I can create a FIFO of a specific size. How would I go about
            > > doing this?
            >
            > Good starting points:
            > - if you accept an imperative FIFO : module Queue of the standard library
            > - if you want a functional FIFO : module Fqueue of Jane Street's Core
            > library (http://ocaml.janestreet.com/?q=node/13, also available with Godi)
            > - finally module Squeue of Jane Street's Core library seems to do kinda
            > what you want
            >
            > This last module's signature contains the following:
            >
            > (** [create maxsize] returns a synchronized queue bounded to have no more
            > than
            > [maxsize] elements. *)
            > val create : int -> 'a t
            >
            > Then you can apply the previous answers (but maybe it is this last point
            > which is the problem ?).
            >
            > Cheers
            >
            >
            >


            [Non-text portions of this message have been removed]
          • Vincent Aravantinos
            ... Well he can stil functorize it if he wants. The advantage being to bound all possibly created squeues to have the wanted size thus avoiding potential
            Message 5 of 8 , Jul 1, 2009
            • 0 Attachment
              --- In ocaml_beginners@yahoogroups.com, Ashish Agarwal <agarwal1975@...> wrote:
              >
              > > Then you can apply the previous answers
              > If Squeue.create returns a queue bounded to a specific size, wouldn't that
              > be a non-functorized solution? Then, you no longer need the solutions
              > previously mentioned.

              Well he can stil functorize it if he wants. The advantage being to bound all possibly created squeues to have the wanted size thus avoiding potential mistakes.
            • Phil Tomson
              ... Thanks, that should do what I need. Phil
              Message 6 of 8 , Jul 1, 2009
              • 0 Attachment
                On 7/1/09, Matthieu Dubuget <matthieu.dubuget@...> wrote:
                > It seems to me that this is exactly the same?
                >
                > define a functor MyFunctor like this:
                > module MyFunctor (S : sig val sz : int end) =
                > struct
                > ... S.sz ... ← in your module, you now have
                > access to S.sz
                > end
                >
                >
                >
                > Your module can now be generated like this:
                >
                > module MyModule = MyFunctor (struct let sz = 3 end);;
                >
                > Hoping this helps?
                >

                Thanks, that should do what I need.

                Phil
              Your message has been successfully submitted and would be delivered to recipients shortly.