* [Caml-list] Higher order functors over tuples
@ 2012-11-06 11:48 oleg
2012-11-12 6:29 ` [Caml-list] " Ivan Gotovchits
0 siblings, 1 reply; 7+ messages in thread
From: oleg @ 2012-11-06 11:48 UTC (permalink / raw)
To: ivg, Didier.Cassirame; +Cc: caml-list
Ivan Gotovchits wrote
> In general, the problem is to compute a tuple of objects that have
> types different, but conforming to the same interface
That seems to be the right problem for existentials -- which exist in
OCaml under the name of objects. Methods are the interface and fields
are private data. Of course we can get by with mere records -- but if
we have objects anyway we might as well use them.
> 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.
Here is the implementation of that example using objects. It seems the
object implementation provides the same static assurances and
abstractions as the module implementation. Since generators can
expire, the effective number of generators does change
dynamically. The static tupling doesn't buy us much.
(* The interface of generators *)
class type ['r] generator = object ('self)
method result : unit -> 'r option
method step : unit -> 'self option
end;;
(* Function `proceed' iterates over the generator and print results *)
let rec proceed : int generator -> unit = fun gen ->
match gen#step () with
| Some gen -> begin
match gen#result () with
| Some r -> print_int r; proceed gen
| None -> proceed gen
end
| None -> print_newline ();
print_endline "generator finished"
;;
(* Just for a completness of the example a pair of simple generators: *)
class odd : int -> [int] generator = fun init -> object
val v = init
method step () = if v <= 10 then Some {< v = v + 2 >} else None
method result () = if v <= 9 then Some v else None
end
;;
(* A different way to constrain the type *)
class even init = object (self : int #generator)
val v = init
method step () = if v <= 10 then Some {< v = v + 2 >} else None
method result () = if v <= 8 then Some v else None
end
;;
(* Make a list of generators itself a generator *)
let gen_list : 'r generator list -> 'r generator = fun gens -> object
val v = gens
method step () =
let rec loop = function
| [] -> None
| h :: t ->
begin match h#step () with
| None -> loop t
| Some gen -> Some {< v = t @ [gen] >}
end
in loop v
(* find the first generator that gives a result *)
method result () =
let rec loop = function
| [] -> None
| h :: t ->
begin match h#result () with
| None -> loop t
| r -> r
end
in loop (List.rev v)
end
;;
let a_gen_list = gen_list [new even 0;
new odd (-1);
new even 0;
new even 0];;
proceed a_gen_list;;
212243446566878889999
generator finished
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Caml-list] Re: Higher order functors over tuples
2012-11-06 11:48 [Caml-list] Higher order functors over tuples oleg
@ 2012-11-12 6:29 ` Ivan Gotovchits
2012-11-13 8:19 ` oleg
0 siblings, 1 reply; 7+ messages in thread
From: Ivan Gotovchits @ 2012-11-12 6:29 UTC (permalink / raw)
To: oleg; +Cc: Didier.Cassirame, caml-list
oleg@okmij.org writes:
> That seems to be the right problem for existentials -- which exist in
> OCaml under the name of objects. Methods are the interface and fields
> are private data. Of course we can get by with mere records -- but if
> we have objects anyway we might as well use them.
Yes, but objects suffer from two slight problems: lower performance due
to dynamic method dispatching and some issues with type system. Well,
not an issues really, but I think that you understood me. Though it
seems that this problems do not outweight the power of object system,
that was gracefully demonstrated by you.
In my real application I do shifted to objects. Though I still define
modules and then use functors as factories, producing objects of the
particular types. I'm not sure that this dual solution worths, but I
feel myself more comfortable with modules, rather than objects.
And, by the way, I've met the following signatures many times in
different frameworks written in different languages (matlab functional
objects, GnuRadio blocks, to name a few):
>> module type Generator =
>> sig
>> type t
>> type r
>> val create: unit -> t
>> val result: t -> r option
>> val step: t -> t option
>> end
extended with Transformer and Consumer:
module type Consumer =
sig
type t
type d
val push: d -> t -> t
val step: t -> t option
end
module type Transformer =
sig
type t
type d
type r
val push: d -> t -> t
val step: t -> t option
val result: t -> r option
(* or include Transformer and Consumer in ocaml 3.12+ *)
end
Haven't you seen this pattern in functional programming? May be it can
be expressed in a more natural way using FP idioms?
--
(__)
(oo)
/------\/
/ | ||
* /\---/\
~~ ~~
...."Have you mooed today?"...
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Caml-list] Re: Higher order functors over tuples
2012-11-12 6:29 ` [Caml-list] " Ivan Gotovchits
@ 2012-11-13 8:19 ` oleg
2012-11-15 3:17 ` Ivan Gotovchits
0 siblings, 1 reply; 7+ messages in thread
From: oleg @ 2012-11-13 8:19 UTC (permalink / raw)
To: ivg; +Cc: Didier.Cassirame, caml-list
> And, by the way, I've met the following signatures many times in
> different frameworks written in different languages (matlab functional
> objects, GnuRadio blocks, to name a few):
>> module type Generator =
>> sig
>> type t
>> type r
>> val create: unit -> t
>> val result: t -> r option
>> val step: t -> t option
>> end
Well, if we re-write it in object style, but without objects, we obtain
type 'r stream_v = EOF | Stream of 'r option * 'r stream
and 'r stream = unit -> 'r stream_v
which is what it says. Here's the important paper that describes the
pattern in great detail and with many applications:
Duncan Coutts and Roman Leshchinskiy and Don Stewart
Stream Fusion. From Lists to Streams to Nothing at All
ICFP 07
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.104.7401
Incidentally, iteratees are somewhat similar.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Caml-list] Re: Higher order functors over tuples
2012-11-13 8:19 ` oleg
@ 2012-11-15 3:17 ` Ivan Gotovchits
0 siblings, 0 replies; 7+ messages in thread
From: Ivan Gotovchits @ 2012-11-15 3:17 UTC (permalink / raw)
To: oleg; +Cc: Didier.Cassirame, caml-list
oleg@okmij.org writes:
>
> Well, if we re-write it in object style, but without objects, we obtain
>
> type 'r stream_v = EOF | Stream of 'r option * 'r stream
> and 'r stream = unit -> 'r stream_v
>
> which is what it says. Here's the important paper that describes the
> pattern in great detail and with many applications:
>
> Duncan Coutts and Roman Leshchinskiy and Don Stewart
> Stream Fusion. From Lists to Streams to Nothing at All
> ICFP 07
> http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.104.7401
>
> Incidentally, iteratees are somewhat similar.
>
Many thanks! This is what I was searching for.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Higher order functors over tuples
2012-10-31 11:09 ` Didier Cassirame
@ 2012-10-31 12:02 ` Ivan Gotovchits
0 siblings, 0 replies; 7+ messages in thread
From: Ivan Gotovchits @ 2012-10-31 12:02 UTC (permalink / raw)
To: Didier Cassirame; +Cc: caml-list
Didier Cassirame <didier.cassirame@gmail.com> writes:
> Do you wish to have parallel computation done with these modules, or
> do you mean that you want them to be combined sequentially?
Ideally, computation must be combined arbitrary. It will be nice to have
MAP functor, or MAP2 functor, etc.
> 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.
I've heard that it is possible, but unfortunately I'm stucked with 3.11
;'( And I'm not still sure that it will work. Need to try it myself =)
> 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.
Hmmm, the problem is that I do not understand monads quite well. So I've
solved the task in a «my» way. And so I want to know how the problem can
(and could it?) be solved with monads, which are considered a common and
idiomatic way of solving such problems.
>
> If you don't have access to that feature, you could perhaps use higher order functors?
I do use them. I generate a list of modules using higher order
functor. For example I build structure of modules [Even, Odd, Even, Odd]
with
module M = Cons(Even)(Cons(Odd)(Cons(Even)(Odd)))
Calling ``let m = M.create ()'' will build some structure of objects of different
types. (Their can be more two types of modules). A call to ``M.step m''
will apply ``Even.step'' or ``Odd.step'' to the last objects in a list,
and move it to the head. A call ``M.result m'' will return the
result of calling ``Even.result m'' or ``Odd.result m'' dependingly on a
current type in head of list.
--
(__)
(oo)
/------\/
/ | ||
* /\---/\
~~ ~~
...."Have you mooed today?"...
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Higher order functors over tuples
2012-10-31 9:26 [Caml-list] " Ivan Gotovchits
@ 2012-10-31 11:09 ` Didier Cassirame
2012-10-31 12:02 ` Ivan Gotovchits
0 siblings, 1 reply; 7+ messages in thread
From: Didier Cassirame @ 2012-10-31 11:09 UTC (permalink / raw)
To: Ivan Gotovchits; +Cc: caml-list
[-- 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 --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Caml-list] Higher order functors over tuples
@ 2012-10-31 9:26 Ivan Gotovchits
2012-10-31 11:09 ` Didier Cassirame
0 siblings, 1 reply; 7+ messages in thread
From: Ivan Gotovchits @ 2012-10-31 9:26 UTC (permalink / raw)
To: caml-list
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.
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?
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?
Thanks for reading down to this line =) Any comments are welcome!
P.S. Sorry for such a long article. I haven't enough brains to make it
shorter =)
--
(__)
(oo)
/------\/
/ | ||
* /\---/\
~~ ~~
...."Have you mooed today?"...
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2012-11-15 3:17 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-06 11:48 [Caml-list] Higher order functors over tuples oleg
2012-11-12 6:29 ` [Caml-list] " Ivan Gotovchits
2012-11-13 8:19 ` oleg
2012-11-15 3:17 ` Ivan Gotovchits
-- strict thread matches above, loose matches on Subject: below --
2012-10-31 9:26 [Caml-list] " Ivan Gotovchits
2012-10-31 11:09 ` Didier Cassirame
2012-10-31 12:02 ` Ivan Gotovchits
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox