Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Browse thread
Is this interface a good idea?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-05-20 (12:55)
From: Remi VANICAT <vanicat+egroups@l...>
Subject: Re: "ocaml_beginners"::[] Re: Is this interface a good idea?
Matt Armstrong <matt@l...> writes:

> Remi VANICAT <vanicat+egroups@l...> writes:
>
>> Matt Armstrong <matt@l...> writes:
>>
>>> As my first "real" OCaml module, I want to produce a filename module
>>> that will type the different kinds of filename components and only
>>> allow me to combine them in the correct way. E.g.
>
> [...]
>
>> yes, phantom type can be the solution :
>>
>> (* file.mli *)
>>
>> type ('a, 'b) file_or_dir
>>
>> val parse_relative_dir : string -> ([ `Relative ], [ `Dir ]) file_or_dir
>> val parse_relative_file : string -> ([ `Relative ], [ `File ]) file_or_dir
>> val parse_absolute_dir : string -> ([ `Absolute ], [ `Dir ]) file_or_dir
>> val parse_absolute_file : string -> ([ `Absolute ], [ `File ])file_or_dir
>> val concat : ('a,[> `Dir]) file_or_dir ->
>> ([> `Relative],'b) file_or_dir -> ('a,'b) file_or_dir
>> val file_or_dir_to_string : ('a, 'b) file_or_dir -> string
>
> [...]
>
> Your example works perfectly! I'll have to study up on polymorphic
> variants, because I don't exactly know why it works. :-)

in fact, as Alain Frisch say, we don't need polymorphic variants here,
we only need some type. But in my mind phantom type implies polymorphic
variants, which is false.

>
> I am having trouble playing around with this idea in the interactive
> toplevel, without using modules:
>
> ----------------------------------------------------------------------
> # type ('a, 'b) path;;
> type ('a, 'b) path
> # let parse_relative_dir st = ([ `Relative ], [ `Dir ]) st;;

of course, what you mean is :

let parse_relative_dir st = (st : ([ `Relative ], [ `Dir ]) path)

but it won't work, because you have to say that

type ('a, 'b) path = string.

the fact is that what you type in the toplevel is what is in the .ml,
not in the .mli.

the solution is to use explicit module :

module type PathType =
sig
(* put here what is in the .mli *)
end;;

module Path : PathType =
struct
(= put here what is in the .ml *)
end;;
-- 
Rémi Vanicat
vanicat@l...
http://dept-info.labri.u-bordeaux.fr/~vanicat