caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Fine-grained types with more general library
@ 2008-02-10 19:07 Dawid Toton
  2008-02-10 19:15 ` [Caml-list] " Edgar Friendly
  2008-02-10 19:22 ` David Teller
  0 siblings, 2 replies; 5+ messages in thread
From: Dawid Toton @ 2008-02-10 19:07 UTC (permalink / raw)
  To: Caml List

I have a type (e.g. of path to file)

type path = string list

 and many operations (e.g. path manipulation). Then I need to have two 
kinds of paths (e.g. local and remote) and want type system to enforce 
proper usage (some functions act on remote paths only etc.). So I create 
new two types:

type remote_t = Remote path
type local_t = Local path

Then I avoid using type path for greater safety. But I have a problem: 
can't use path manipulation library on these new types. I have created 
"mirror" versions of the library for each new type, but this made my 
source code unmaintainable. What is the right solution?

I'd prefer not to contaminate the path library with things specific to 
this particular problem, since it's reused.

Dawid


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

* Re: [Caml-list] Fine-grained types with more general library
  2008-02-10 19:07 Fine-grained types with more general library Dawid Toton
@ 2008-02-10 19:15 ` Edgar Friendly
  2008-02-10 19:20   ` Bünzli Daniel
  2008-02-10 19:56   ` Dawid Toton
  2008-02-10 19:22 ` David Teller
  1 sibling, 2 replies; 5+ messages in thread
From: Edgar Friendly @ 2008-02-10 19:15 UTC (permalink / raw)
  To: Dawid Toton; +Cc: Caml List

Dawid Toton wrote:
> I have a type (e.g. of path to file)
> 
> type path = string list
> 
> and many operations (e.g. path manipulation). Then I need to have two
> kinds of paths (e.g. local and remote) and want type system to enforce
> proper usage (some functions act on remote paths only etc.). So I create
> new two types:
> 
> type remote_t = Remote path
> type local_t = Local path
> 
> Then I avoid using type path for greater safety. But I have a problem:
> can't use path manipulation library on these new types. I have created
> "mirror" versions of the library for each new type, but this made my
> source code unmaintainable. What is the right solution?
> 
> I'd prefer not to contaminate the path library with things specific to
> this particular problem, since it's reused.
> 
> Dawid
> 
type 'a path = string list
type remote_t = `Remote path
type local_t = `Local path

val works_on_remote_t : [> `Remote] path -> whatever
val works_on_local_t : [> `Local] path -> whatever
val works_on_any_t : 'a path -> whatever

See the following URL for more details on this trick (called Phantom Types):
http://caml.inria.fr/pub/ml-archives/caml-list/2001/09/081c77179ee2a3787233902a51633122.en.html

E


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

* Re: [Caml-list] Fine-grained types with more general library
  2008-02-10 19:15 ` [Caml-list] " Edgar Friendly
@ 2008-02-10 19:20   ` Bünzli Daniel
  2008-02-10 19:56   ` Dawid Toton
  1 sibling, 0 replies; 5+ messages in thread
From: Bünzli Daniel @ 2008-02-10 19:20 UTC (permalink / raw)
  To: Caml List


Le 10 févr. 08 à 20:15, Edgar Friendly a écrit :

> type 'a path = string list
> type remote_t = `Remote path
> type local_t = `Local path

Yes, but make sure that the type path is abstract or private otherwise  
it won't work.

Daniel


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

* Re: [Caml-list] Fine-grained types with more general library
  2008-02-10 19:07 Fine-grained types with more general library Dawid Toton
  2008-02-10 19:15 ` [Caml-list] " Edgar Friendly
@ 2008-02-10 19:22 ` David Teller
  1 sibling, 0 replies; 5+ messages in thread
From: David Teller @ 2008-02-10 19:22 UTC (permalink / raw)
  To: Dawid Toton; +Cc: Caml List

I'm not sure I understand the question. Still, if you haven't used
phantom types yet, it sounds like the right thing to do:
 type 'a path = string list
 type remote_t = Remote path
 type local_t  = Local path

Now, you can write things such as

val print_local : Local path  -> unit  (*only works on local paths*)
val print_remote: Remote path -> unit  (*only works on remote paths*)
val print_any   : 'a path     -> unit  (*works with either*)

Does this help you ?

Cheers,
 David

-- 
David Teller
 Security of Distributed Systems
  http://www.univ-orleans.fr/lifo/Members/David.Teller
 Angry researcher: French Universities need reforms, but the LRU act
brings liquidations. 


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

* Re: [Caml-list] Fine-grained types with more general library
  2008-02-10 19:15 ` [Caml-list] " Edgar Friendly
  2008-02-10 19:20   ` Bünzli Daniel
@ 2008-02-10 19:56   ` Dawid Toton
  1 sibling, 0 replies; 5+ messages in thread
From: Dawid Toton @ 2008-02-10 19:56 UTC (permalink / raw)
  To: caml-list


> type 'a path = string list
> type remote_t = `Remote path
> type local_t = `Local path
>
> val works_on_remote_t : [> `Remote] path -> whatever
> val works_on_local_t : [> `Local] path -> whatever
> val works_on_any_t : 'a path -> whatever
>
> See the following URL for more details on this trick (called Phantom Types):
> http://caml.inria.fr/pub/ml-archives/caml-list/2001/09/081c77179ee2a3787233902a51633122.en.html
>
>   
Thank you all for immediate responses!
After playing with toplevel I managed to get the correct syntax:

type 'a path = string list
type remote_t = [`Remote] path
type local_t = [`Local] path

This seems as yet to be very good solution.:)

Dawid


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

end of thread, other threads:[~2008-02-10 19:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-10 19:07 Fine-grained types with more general library Dawid Toton
2008-02-10 19:15 ` [Caml-list] " Edgar Friendly
2008-02-10 19:20   ` Bünzli Daniel
2008-02-10 19:56   ` Dawid Toton
2008-02-10 19:22 ` David Teller

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