caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Didier Cassirame <didier.cassirame@gmail.com>
To: Ivan Gotovchits <ivg@ieee.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Higher order functors over tuples
Date: Wed, 31 Oct 2012 12:09:09 +0100	[thread overview]
Message-ID: <CA+LkvyrPzuOo5dMnOHDzn1zWg6Lya9k6Ym6Wz6qJQQLYq6q6ZA@mail.gmail.com> (raw)
In-Reply-To: <87625rhu04.fsf@golf.niidar.ru>

[-- Attachment #1: Type: text/plain, Size: 5049 bytes --]

Hi Ivan,

2012/10/31 Ivan Gotovchits <ivg@ieee.org>

>
> Good day,
>
> 1. A problem in general
>
> In general, the problem is to compute a tuple of objects that have
> types different, but conforming to the same interface, i.e., given a
> group of modules (A,B,C), conforming to signature S to construct some
>
>    module M: functor(A:S) -> functor(B:S) -> functor(C:S) -> S
>
> that will, in a some way, «map/reduce» computation on corresponding
> modules.
>

Do you wish to have parallel computation done with these modules, or do you
mean that you want them to be combined sequentially?


>
> The problem can be generalized further, but, it seems to me, that it is
> already quite (too?) abstract.
>
> As far as I can see, there is no ad hoc solution for this problem in
> OCaml, except, of course, objects with their late binding. But, in some
> circumstances, the late binding can be avoided. In particular, when
> types of modules and their amount are known and fixed at compile time.
>

> 2. Particular problem
>
> For example, we have several different data generator modules conforming
> to the signature
>
>    module type Generator =
>    sig
>      type t
>      type r
>      val create: unit  -> t
>      val result: t -> r option
>      val step:   t -> t option
>    end
>
> Each generator can be stepped forward and queried for a result. Generator
> can expire, in such case it will return None as a result. I want to make a
> «list» of different generators and do some arbitary iterations on them.
>
> The «list» can be made of pairs, using the cons (meta)constructor:
>
>    module Cons :
>      functor(G1:Generator) -> functor(G2:Generator) -> Generator
>
> So, if I have three modules A,B,C, I can construct module
>
>    M = Cons(A)(Cons(B)(C))
>
> and call M.step, to move a chain of generators forward, or M.result to
> get the current result.
>
>
> 3. Particular solution
>
> Here is the implementation on Cons module (named ConsGenerator), that
> "steps"
> contained generators in a sequence (swapping them on each step).
>
>    module ConsGenerator
>        (G1:Generator)
>        (G2:Generator with type r = G1.r)
>        : Generator with type r = G2.r =
>    struct
>      type t =
>        | G1G2 of (G1.t option * G2.t option )
>        | G2G1 of (G2.t option * G1.t option )
>
>      type r = G2.r
>
>      let create () =
>        G1G2 ((Some (G1.create ())), Some (G2.create ()))
>
>      let step = function
>        | G1G2 (g1, Some g2)   -> Some (G2G1 (G2.step g2, g1))
>        | G2G1 (g2, Some g1)   -> Some (G1G2 (G1.step g1, g2))
>        | G2G1 (Some g2, None) -> Some (G2G1 (G2.step g2, None))
>        | G1G2 (Some g1, None) -> Some (G1G2 (G1.step g1, None))
>        | G1G2 (None,None) | G2G1 (None,None) -> None
>
>      let result = function
>        | G1G2 (Some g1,_) -> G1.result g1
>        | G2G1 (Some g2,_) -> G2.result g2
>        | G1G2 (None,_) | G2G1 (None,_) -> None
>    end
>
> Function `proceed' iterates module M and print results
>
>    let rec proceed oe = match M.step oe with
>      | Some oe -> begin
>        match M.result oe with
>          | Some r -> print_int r; proceed oe
>          | None   -> proceed oe
>      end
>      | None -> print_newline ();
>          print_endline "generator finished"
>
> Just for a completness of the example a pair of simple generators:
>
>    module Odd : Generator with type r = int = struct
>      type t = int
>      type r = int
>      let create () = -1
>      let step v = if v < 10 then Some (v+2) else None
>      let result v = if v <= 9 then Some v else None
>    end
>
>    module Even : Generator with type r = int = struct
>      type t = int
>      type r = int
>      let create () = 0
>      let step v = if v < 10 then Some (v+2) else None
>      let result v = if v <= 8 then Some v else None
>    end
>
> and the result was
>    # module M = Cons(Even)(Cons(Cons(Odd)(Even))(Even))
>    # let _  = proceed (M.create ())
>    22244618648365879
>    generator finished
>    - : unit = ()
>
>
> 4. Question
>
> And now the questions!
>
> 1) Is there any idiomatic solution to this pattern?
>
> 2) If not, can my solution be improved in some way? And how?
>

Thanks to a very recent post, I rediscovered that OCaml can handle modules
as first class values (since ver. 3.12) :

- you can define types based on module types like this.

module type M = sig ... end

type m = (module M)

and then you should be able to build simple lists of type m.



> 3) My intution says that it can be solved with monads (Generator really
> incapsulates side effects in a step. Several computation are combined in
> one big chain... ). Am I right? If so, how to implement it in monads?
>

I guess that you could use the type above with regular monads too.

If you don't have access to that feature, you could perhaps use higher
order functors?


Cheers,

didier

[-- Attachment #2: Type: text/html, Size: 6293 bytes --]

  reply	other threads:[~2012-10-31 11:09 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-31  9:26 Ivan Gotovchits
2012-10-31 11:09 ` Didier Cassirame [this message]
2012-10-31 12:02   ` Ivan Gotovchits
2012-11-30 12:49   ` [Caml-list] ocamlc, ocamlopt stackoverflow on list Christophe Raffalli
2012-11-30 13:20     ` Gabriel Scherer
2012-11-30 13:31       ` Christophe Raffalli
2012-11-30 14:15         ` Gabriel Scherer
2012-11-06 11:48 [Caml-list] Higher order functors over tuples oleg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+LkvyrPzuOo5dMnOHDzn1zWg6Lya9k6Ym6Wz6qJQQLYq6q6ZA@mail.gmail.com \
    --to=didier.cassirame@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=ivg@ieee.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).