Search the web
Sign In
New User? Sign Up
ocaml_beginners · Ocaml Beginners
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Abstracting a variant type   Message List  
Reply | Forward Message #11176 of 11550 |
Re: Abstracting a variant type

--- In ocaml_beginners@yahoogroups.com, Jon Harrop <jon@...> wrote:
>
> On Monday 29 June 2009 14:57:43 Vincent Aravantinos wrote:
> > 1) either I define the type as private, ie.
> > module M :
> > sig
> > type t = private A of a | B of b | C of c
> > ...
> > end
> > in which case I can still destructure the data from the outside; but I
> > don't really like this solution as it still reveals some part of the
> > implementation.
>
> Yes. That solutions works well when it is suitable. Specifically, when the
> arguments of the type constructors can all be public and do not reflect
> implementation details.

Exactly. I could not manage to express it as clear...

> > 2) or I keep the type completely abstract and define the following function
> > : let destruct (case_a : a -> 'a) (case_b: b -> 'a) (case_c : c -> 'a) =
> > function
> >
> > |A(a) -> case_a a
> > |B(b) -> case_b b
> > |C(c) -> case_c c
> >
> > which completely hides the structure but is absolutely not handy.
>
> Yes. Assuming you mean a higher-order function that accepts "case_a" etc. in
> the general case then this is cumbersome because you're reduced to Lisp-style
> coding and cannot leverage pattern matching.

Exactly. That's the solution I first chose, this is of course very general but
we lose all the advantages of pattern matching (we cannot make guards nor
factorise cases). Furthermore the strict semantics of ocaml imposes that the
parameters are evaluated before the call, i.e. case_a, case_b, case_c will be
first evaluated indepently of the case that will be really considered. With
functions that's not real problem but for values this can be quite annoying,
leading to big inefficencies, or even worse in the case of my app, to
non-termination... Adding some artificial unit types to force evaluation order
does the trick but turns the code into a real mess.

> > 3) I was also thinking of defining a front type e.g.
> > module M :
> > sig
> > type t = private A of a | B of b | C of c
> > ...
> > end
> > =
> > struct
> > type t = private A of a | B of b | C of c (* supposed no to change *)
> > type t' = A' of a' | ... (* may change *)
> > let t_to_t' = ...
> > let t'_to_t = ...
> > end
>
> That is essentially a manual implementation of views (aka "active patterns" in
> F#). They are very powerful and particularly useful in the context of
> dressing up alien data structures (e.g. the .NET representation of XML trees
> in F# so they can be destructured using pattern matching). I also use them
> extensively in F# for Visualization to wrap WPF code in an elegant functional
> API.

Great, I didn't know what was "views" ! The problem I see in my "manual
implementation" is that it imposes to make frequent conversions from one data to
another.
Views allow to keep the syntactic pleasant aspect of pattern matching while not
having to maintain an artificial data structure, am I right ?

> There is an OCaml implementations of views that automates the tediousness.

Is it micmatch ?


> Depends entirely upon the specifics of what you're doing. You have described
> several techniques that I have found to be applicable in different
> circumstances. In general, I suggest you start by making the exposing the
> variant type but making its constructors accept arguments of abstract types.

Ok. I finally stuck to private types but I will give a try to views.
At least I know now that those questions have some sense :-)

Thanks a lot,
--
Vincent Aravantinos
PhD Student - LIG - CAPP Team
Grenoble, France
+33.6.11.23.34.72
vincent.aravantinos@...
http://membres-lig.imag.fr/aravantinos/




Sun Jul 5, 2009 1:27 pm

vincent.arav...
Offline Offline
Send Email Send Email

Forward
Message #11176 of 11550 |
Expand Messages Author Sort by Date

Hi list, I have a module with a variant type : module M = struct type t = A of ... | B of ... | C of ... ... end and I would like it to be abstract : module M...
Vincent Aravantinos
vincent.arav...
Offline Send Email
Jun 29, 2009
1:58 pm

No one ? It seems to be a rather frequent situation, isn't it ? Maybe my question is not clear enough ?...
Vincent Aravantinos
vincent.arav...
Offline Send Email
Jul 3, 2009
7:43 am

Vincent Aravantinos wrote: ... without really looking into your mail, why not something allong: module M: sig type t end = struct type t = A of int | B of...
JM Nunes
jose.manuel.nunes@...
Send Email
Jul 3, 2009
9:11 am

... I would still like to access the structure in some way. But this way should not reveal the data structure. Notice my question is highly informal and I...
Vincent Aravantinos
vincent.arav...
Offline Send Email
Jul 3, 2009
9:58 am

... The purpose of an abstract type is to hide how it is implemented. You may want to provide an interface to your type, and the end-users will be tied to it....
citromatik
miguel.pignatelli@...
Send Email
Jul 3, 2009
10:16 am

... What is a nonsense? The use of abstract types or the opening up of the type and implementation details to external users? On a similar note: I too am...
Hugo Ferreira
hugotwo3
Offline Send Email
Jul 3, 2009
10:30 am

... I think he is talking about doing both at a time. Which is indeed nonsense. ... I am having the same problem and am thinking of just keeping the black box...
Vincent Aravantinos
vincent.arav...
Offline Send Email
Jul 3, 2009
12:22 pm

... Since I keep my tests in separate files from the module source this might not be the way to go for me. H.F....
Hugo Ferreira
hugotwo3
Offline Send Email
Jul 3, 2009
2:04 pm

... Hash: SHA1 ... For testing and debugging purposes I find it convenient to have some access to raw (unabstracted) types. My solution is for every module to...
Peng Zang
peng.zang@...
Send Email
Jul 3, 2009
2:20 pm

... Nice! Thanks Regards, Hugo F....
Hugo Ferreira
hugotwo3
Offline Send Email
Jul 3, 2009
2:46 pm

... I was not clear, settled this way this is indeed non-sense. But what I meant by "structure" in the quotation was not the "implementation structure". I mean...
Vincent Aravantinos
vincent.arav...
Offline Send Email
Jul 3, 2009
12:13 pm

Sorry for the slow response, Vincent. I have been on holiday... ... Yes. That solutions works well when it is suitable. Specifically, when the arguments of the...
Jon Harrop
jon@...
Send Email
Jul 3, 2009
8:27 pm

... tediousness. Do you mean there is some general tool? Can you refer us to any examples? ... [Non-text portions of this message have been removed]...
Ashish Agarwal
ashish_a1975
Offline Send Email
Jul 4, 2009
2:03 am

... Exactly. I could not manage to express it as clear... ... Exactly. That's the solution I first chose, this is of course very general but we lose all the...
Vincent Aravantinos
vincent.arav...
Offline Send Email
Jul 5, 2009
1:27 pm

... Sorry, this is actually wrong now. I forgot to mention that a subtle but enormously important advancement was made in OCaml 3.11. Specifically, you can now...
Jon Harrop
jon@...
Send Email
Jul 5, 2009
2:16 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help