caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] What am I reinventing here?
@ 2012-02-13 23:33 Andre Nathan
  2012-02-14  7:03 ` Arnaud Spiwack
  0 siblings, 1 reply; 7+ messages in thread
From: Andre Nathan @ 2012-02-13 23:33 UTC (permalink / raw)
  To: caml-list

Hello

I have some code that behaves like the following.

module M = struct
   type ('a, 'b) ops = {
     bar_of_foo : 'a -> 'b;
     foo_of_bar : 'b -> 'a
   }

   let foo ops x = ops.bar_of_foo x
   let bar ops x = ops.foo_of_bar x
end

type foo = A | B
type bar = C | D

let ops = {
   M.bar_of_foo = (function A -> C | B -> D);
   M.foo_of_bar = (function C -> A | D -> B)
}

let () =
   match M.foo ops A with
   | C -> ()
   | D -> ()

The idea is to parametrize the behavior of some module M on two types 
and a set of operations involving these two types, with the catch that I 
want to keep the ability to pattern-match on the possible values of 
these types in the code that handles the return values from the 
functions exported by M.

With the scheme above I can do this, but passing a table of functions 
around doesn't look very nice. Is there any way I can use functors to do 
what this code does, while still being able to use pattern-matching? 
Would I have to resort to objects to be able to do this?

Thanks in advance,
Andre

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] What am I reinventing here?
  2012-02-13 23:33 [Caml-list] What am I reinventing here? Andre Nathan
@ 2012-02-14  7:03 ` Arnaud Spiwack
  2012-02-14  9:59   ` Andre Nathan
  0 siblings, 1 reply; 7+ messages in thread
From: Arnaud Spiwack @ 2012-02-14  7:03 UTC (permalink / raw)
  To: Andre Nathan; +Cc: caml-list

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

You're probably trying to use functors (
http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html#toc15 ). Though
your example code isn't doing anything in particular.

On 14 February 2012 00:33, Andre Nathan <andre@digirati.com.br> wrote:

> Hello
>
> I have some code that behaves like the following.
>
> module M = struct
>  type ('a, 'b) ops = {
>    bar_of_foo : 'a -> 'b;
>    foo_of_bar : 'b -> 'a
>  }
>
>  let foo ops x = ops.bar_of_foo x
>  let bar ops x = ops.foo_of_bar x
> end
>
> type foo = A | B
> type bar = C | D
>
> let ops = {
>  M.bar_of_foo = (function A -> C | B -> D);
>  M.foo_of_bar = (function C -> A | D -> B)
> }
>
> let () =
>  match M.foo ops A with
>  | C -> ()
>  | D -> ()
>
> The idea is to parametrize the behavior of some module M on two types and
> a set of operations involving these two types, with the catch that I want
> to keep the ability to pattern-match on the possible values of these types
> in the code that handles the return values from the functions exported by M.
>
> With the scheme above I can do this, but passing a table of functions
> around doesn't look very nice. Is there any way I can use functors to do
> what this code does, while still being able to use pattern-matching? Would
> I have to resort to objects to be able to do this?
>
> Thanks in advance,
> Andre
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/**wws/info/caml-list<https://sympa-roc.inria.fr/wws/info/caml-list>
> Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners<http://groups.yahoo.com/group/ocaml_beginners>
> Bug reports: http://caml.inria.fr/bin/caml-**bugs<http://caml.inria.fr/bin/caml-bugs>
>
>

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] What am I reinventing here?
  2012-02-14  7:03 ` Arnaud Spiwack
@ 2012-02-14  9:59   ` Andre Nathan
  2012-02-14 10:09     ` Gabriel Scherer
  0 siblings, 1 reply; 7+ messages in thread
From: Andre Nathan @ 2012-02-14  9:59 UTC (permalink / raw)
  To: Arnaud Spiwack; +Cc: caml-list

On Tue, 2012-02-14 at 08:03 +0100, Arnaud Spiwack wrote:
> You're probably trying to use functors
> ( http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html#toc15 ).
> Though your example code isn't doing anything in particular.

Can you give me an example? How can I retain the possibility of
pattern-matching on the types of the resulting module?

Thanks,
Andre
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] What am I reinventing here?
  2012-02-14  9:59   ` Andre Nathan
@ 2012-02-14 10:09     ` Gabriel Scherer
  2012-02-14 10:24       ` Andre Nathan
  0 siblings, 1 reply; 7+ messages in thread
From: Gabriel Scherer @ 2012-02-14 10:09 UTC (permalink / raw)
  To: Andre Nathan; +Cc: Arnaud Spiwack, caml-list

Here is an attempt at a functor translation. Note that's it's mostly a
glorified way to do exactly the same thing. Passing a module or a
record of operation is not very different; yet a module can carry
types so you don't have to parametrize your operations explicitly.

module type OPS = sig
  type bar
  type foo
  val bar_of_foo : foo -> bar
  val foo_of_bar : bar -> foo
end

module Make (O : OPS) = struct
  let foo = O.bar_of_foo
  let bar = O.foo_of_bar
end

module Op1 = struct
  type foo = A | B
  type bar = C | D
  let bar_of_foo = function A -> C | B -> D
  let foo_of_bar = function C -> A | D -> B
end

let () =
 let module M = Make(Op1) in
 (* you may want to open Op1 locally here *)
 match M.foo Op1.A with
 | Op1.C -> ()
 | Op1.D -> ()


On Tue, Feb 14, 2012 at 10:59 AM, Andre Nathan <andre@digirati.com.br> wrote:
> On Tue, 2012-02-14 at 08:03 +0100, Arnaud Spiwack wrote:
>> You're probably trying to use functors
>> ( http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html#toc15 ).
>> Though your example code isn't doing anything in particular.
>
> Can you give me an example? How can I retain the possibility of
> pattern-matching on the types of the resulting module?
>
> Thanks,
> Andre
>>
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] What am I reinventing here?
  2012-02-14 10:09     ` Gabriel Scherer
@ 2012-02-14 10:24       ` Andre Nathan
  2012-02-14 10:39         ` Gabriel Scherer
  0 siblings, 1 reply; 7+ messages in thread
From: Andre Nathan @ 2012-02-14 10:24 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: Arnaud Spiwack, caml-list

On Tue, 2012-02-14 at 11:09 +0100, Gabriel Scherer wrote:
> Here is an attempt at a functor translation. Note that's it's mostly a
> glorified way to do exactly the same thing. Passing a module or a
> record of operation is not very different; yet a module can carry
> types so you don't have to parametrize your operations explicitly.

Thank you Gabriel.

> module Make (O : OPS) = struct
>   let foo = O.bar_of_foo
>   let bar = O.foo_of_bar
> end

Would it be correct to say that it isn't possible to specify a signature
S for the output module like

module Make (O : OPS) : S = struct
  ...
end

that would still make pattern-matching outside of the module possible?

Thanks,
Andre


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] What am I reinventing here?
  2012-02-14 10:24       ` Andre Nathan
@ 2012-02-14 10:39         ` Gabriel Scherer
  2012-02-14 11:05           ` Andre Nathan
  0 siblings, 1 reply; 7+ messages in thread
From: Gabriel Scherer @ 2012-02-14 10:39 UTC (permalink / raw)
  To: Andre Nathan; +Cc: Arnaud Spiwack, caml-list

Yes it is, but you have to use judicious "with .." annotations to make
the type non-abstract.

Silly example that captures the problem I think you're thinking of:

  module type S = sig
    type t
    val v : t
  end

  type foo = A | B

  module M = struct
    type t = foo
    let v = A
  end

  let () = match M.v with A -> () | B -> ()
  (* works *)

  module M2 = (M : S)
  let () = match M2.v with A -> () | B -> ()
  (* Error: This pattern matches values of type foo
     but a pattern was expected which matches values of type M2.t *)

  module M3 = (M : S with type t = foo)
  let () = match M3.v with A -> () | B -> ()
  (* works *)


In your case, you should write (module Make(O : Ops) : S with type foo
= ... and bar = ...).

On Tue, Feb 14, 2012 at 11:24 AM, Andre Nathan <andre@digirati.com.br> wrote:
> On Tue, 2012-02-14 at 11:09 +0100, Gabriel Scherer wrote:
>> Here is an attempt at a functor translation. Note that's it's mostly a
>> glorified way to do exactly the same thing. Passing a module or a
>> record of operation is not very different; yet a module can carry
>> types so you don't have to parametrize your operations explicitly.
>
> Thank you Gabriel.
>
>> module Make (O : OPS) = struct
>>   let foo = O.bar_of_foo
>>   let bar = O.foo_of_bar
>> end
>
> Would it be correct to say that it isn't possible to specify a signature
> S for the output module like
>
> module Make (O : OPS) : S = struct
>  ...
> end
>
> that would still make pattern-matching outside of the module possible?
>
> Thanks,
> Andre
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] What am I reinventing here?
  2012-02-14 10:39         ` Gabriel Scherer
@ 2012-02-14 11:05           ` Andre Nathan
  0 siblings, 0 replies; 7+ messages in thread
From: Andre Nathan @ 2012-02-14 11:05 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: Arnaud Spiwack, caml-list

On Tue, 2012-02-14 at 11:39 +0100, Gabriel Scherer wrote:
> Yes it is, but you have to use judicious "with .." annotations to make
> the type non-abstract.

Figured out my error... I had tried defining a functor like this:

module type Ops = sig
  type foo
  type bar
  val bar_of_foo : foo -> bar
  val foo_of_bar : bar -> foo
end

module type S = sig
  type foo
  type bar
  val foo : foo -> bar
  val bar : bar -> foo
end

module M (O : Ops) : S
  with type foo = O.foo and type bar = O.bar =
struct
  type foo = O.foo
  type bar = O.bar
  let foo = O.bar_of_foo
  let bar = O.foo_of_bar
end

which is correct, but in the actual code I also have a .mli file and
there I had only written "module M (O : Ops) : S", without the "with
type ..." part. I completely forgot to replicate it there, and that's
why this attempt kept failing...

Thanks so much for your help!

Andre


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-02-14 11:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-13 23:33 [Caml-list] What am I reinventing here? Andre Nathan
2012-02-14  7:03 ` Arnaud Spiwack
2012-02-14  9:59   ` Andre Nathan
2012-02-14 10:09     ` Gabriel Scherer
2012-02-14 10:24       ` Andre Nathan
2012-02-14 10:39         ` Gabriel Scherer
2012-02-14 11:05           ` Andre Nathan

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).