caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Multiple value declaration in .mli file
@ 2008-08-14 16:25 mohamed iguernelala
  2008-08-15  1:23 ` [Caml-list] " blue storm
  0 siblings, 1 reply; 4+ messages in thread
From: mohamed iguernelala @ 2008-08-14 16:25 UTC (permalink / raw)
  To: caml-list

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

Hello everyone,

We've just noticed that Ocaml permits to declare a value several times in an .mli file.

For example : We can write in a.mli

----
val f : int
val f : bool
----

and it compiles.

But if we define a value f in a.ml it compiles only when f's value is 'a.
From
external files, only the second declaration is visible. Then why Ocaml
allows multiple declaration for the same value in .mli files. Are there
any reason to allow it ?

------
David & Mohamed


      _____________________________________________________________________________ 
Envoyez avec Yahoo! Mail. Une boite mail plus intelligente http://mail.yahoo.fr

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

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

* Re: [Caml-list] Multiple value declaration in .mli file
  2008-08-14 16:25 Multiple value declaration in .mli file mohamed iguernelala
@ 2008-08-15  1:23 ` blue storm
  0 siblings, 0 replies; 4+ messages in thread
From: blue storm @ 2008-08-15  1:23 UTC (permalink / raw)
  To: mohamed iguernelala; +Cc: caml-list

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

I guess you need it to mirror the value shadowing on the implementation side
:
   include Foo
   let bar = baz

This is legal if Foo already defines bar, and most of the time desired and
useful.
In the interface file you'll want to write :
  include FOO (* Foo's signature *)
  val bar : ...

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

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

* Re: Multiple value declaration in .mli file
  2008-08-15 17:07 Jérémie Lumbroso
@ 2008-08-15 17:36 ` Jérémie Lumbroso
  0 siblings, 0 replies; 4+ messages in thread
From: Jérémie Lumbroso @ 2008-08-15 17:36 UTC (permalink / raw)
  To: caml-list

On Fri, Aug 15, 2008 at 7:07 PM, Jérémie Lumbroso
<jeremie.lumbroso@etu.upmc.fr> wrote:

>> I guess you need it to mirror the value shadowing on the implementation side:
>>    include Foo
>>    let bar = baz
>>
>> This is legal if Foo already defines bar, and most of the time desired and
>> useful.
>> In the interface file you'll want to write :
>>   include FOO (* Foo's signature *)
>>   val bar : ...
>
> Actually, this is untrue. I think that up until recently (3.09.2?), what
> you describe would have caused an error (one of the many annoying things
> about signatures in ML languages that prompted that clever paper by Ramsey
> et al.).

I may have gotten carried away! :-) I was thinking of type
definitions, and have no idea if the behavior for values was the same.

  <toplevel>
  # module type Foo = sig type t = int end;;
  module type Foo = sig type t = int end

  # module type Bar = sig include Foo type t = string end;;
  Multiple definition of the type name t.
  Names must be unique in a given structure or signature.
  </toplevel>

Jérémie


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

* Re: Multiple value declaration in .mli file
@ 2008-08-15 17:07 Jérémie Lumbroso
  2008-08-15 17:36 ` Jérémie Lumbroso
  0 siblings, 1 reply; 4+ messages in thread
From: Jérémie Lumbroso @ 2008-08-15 17:07 UTC (permalink / raw)
  To: caml-list

Hello,

> I guess you need it to mirror the value shadowing on the implementation side:
>    include Foo
>    let bar = baz
>
> This is legal if Foo already defines bar, and most of the time desired and
> useful.
> In the interface file you'll want to write :
>   include FOO (* Foo's signature *)
>   val bar : ...

Actually, this is untrue. I think that up until recently (3.09.2?), what
you describe would have caused an error (one of the many annoying things
about signatures in ML languages that prompted that clever paper by Ramsey
et al.).

In the latest version, 3.10.2, however, this does not produce an error,
but reaches a very peculiar state:

  <toplevel>
  # module type FOO =
    sig
      val foo : int
    end;;
  module type FOO = sig val foo : int end

  # module type BAR =
    sig
      include FOO
      val foo : string
    end;;
  module type BAR = sig val foo : int val foo : string end

  # module Bar : BAR = struct let foo = 1 end;;
  Signature mismatch:
  Modules do not match: sig val foo : int end is not included in BAR
  Values do not match: val foo : int is not included in val foo : string

  # module Bar : BAR = struct let foo = "1" end;;
  Signature mismatch:
  Modules do not match: sig val foo : string end is not included in BAR
  Values do not match: val foo : string is not included in val foo : int

  # module Bar : BAR = struct let foo = Obj.magic 1 end;;
  module Bar : BAR

  # Bar.foo;;
  Erreur de segmentation (core dumped)
  </toplevel>

Indeed, since foo is simultaneously an int and a string, the only way to
create a module conforming to the BAR signature, is to make foo an 'a. I
think this is what the original poster was referring to, and it is indeed
a bug. Noteworthy:

  <toplevel>
  # module Bar : BAR = struct let foo = Obj.magic "1" end;;
  module Bar : BAR

  # Bar.foo;;
  - : string = "1"
  </toplevel>

I am (blind) guessing that at some point, someone decided to make
redefining symbols in signatures legal, but overlooked a quirk in the way
OCaml stores signatures.

Regards,

Jérémie


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

end of thread, other threads:[~2008-08-15 17:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-14 16:25 Multiple value declaration in .mli file mohamed iguernelala
2008-08-15  1:23 ` [Caml-list] " blue storm
2008-08-15 17:07 Jérémie Lumbroso
2008-08-15 17:36 ` Jérémie Lumbroso

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