From mboxrd@z Thu Jan 1 00:00:00 1970 X-Sympa-To: caml-list@inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q1E73kAX008531 for ; Tue, 14 Feb 2012 08:03:46 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgMCAMAGOk9KfVI0imdsb2JhbABChRSaFIhNAYguCCIBAQEKCQ0HEgYjgXIBAQEDARICDwQZAS0LAQMBCwEFBQQHGh0CAiISAQUBChIGExIQh1oJnxYKiyaDO4UXgUoCBQuLSwoBCAEBDAMXAgJCHINMH0AEAwkIghuBFgSCXJJWh2aGPz2BU4EBgS8 X-IronPort-AV: E=Sophos;i="4.73,416,1325458800"; d="scan'208";a="131237156" Received: from mail-ww0-f52.google.com ([74.125.82.52]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 14 Feb 2012 08:03:40 +0100 Received: by wgbds10 with SMTP id ds10so6218683wgb.9 for ; Mon, 13 Feb 2012 23:03:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=ovQWLUm81xtCx8jWm+hR84r/EEsLr1ruqcIXyhFf4Po=; b=tDysq6MBY8hyHE75MynaEplIp5jb6qL05PW/b0RD5dRILdQIJzmAi77FCgwkawtgWm tQEOCQZ2KNnMUtlr/uPrm+RkIY7OhleDRzBx25YR75OiPcZLNy+6HRZQU5xGwhbTVtgs 5PfwYTtPeTo3dF56gkbb0dhvvGv/zG8H51kTw= Received: by 10.216.134.30 with SMTP id r30mr2489457wei.42.1329203020281; Mon, 13 Feb 2012 23:03:40 -0800 (PST) MIME-Version: 1.0 Sender: arnaud.spiwack@gmail.com Received: by 10.227.196.141 with HTTP; Mon, 13 Feb 2012 23:03:00 -0800 (PST) In-Reply-To: <4F399DD5.3000507@digirati.com.br> References: <4F399DD5.3000507@digirati.com.br> From: Arnaud Spiwack Date: Tue, 14 Feb 2012 08:03:00 +0100 X-Google-Sender-Auth: PWBMUaivbOLZ4iF6sp1KwuAPvWU Message-ID: To: Andre Nathan Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=0016e68dce89dbb86004b8e731b0 X-Validation-by: aspiwack@lix.polytechnique.fr Subject: Re: [Caml-list] What am I reinventing here? --0016e68dce89dbb86004b8e731b0 Content-Type: text/plain; charset=UTF-8 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 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 > Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-**bugs > > --0016e68dce89dbb86004b8e731b0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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.c= om.br> wrote:
Hello

I have some code that behaves like the following.

module M =3D struct
=C2=A0type ('a, 'b) ops =3D {
=C2=A0 =C2=A0bar_of_foo : 'a -> 'b;
=C2=A0 =C2=A0foo_of_bar : 'b -> 'a
=C2=A0}

=C2=A0let foo ops x =3D ops.bar_of_foo x
=C2=A0let bar ops x =3D ops.foo_of_bar x
end

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

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

let () =3D
=C2=A0match M.foo ops A with
=C2=A0| C -> ()
=C2=A0| 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.<= br>
With the scheme above I can do this, but passing a table of functions aroun= d doesn't look very nice. Is there any way I can use functors to do wha= t this code does, while still being able to use pattern-matching? Would I h= ave to resort to objects to be able to do this?

Thanks in advance,
Andre

--
Caml-list mailing list. =C2=A0Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners<= /a>
Bug reports:
http://caml.inria.fr/bin/caml-bugs


--0016e68dce89dbb86004b8e731b0--