Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module signature depends of order of definition #8034

Closed
vicuna opened this issue Feb 27, 2003 · 3 comments
Closed

Module signature depends of order of definition #8034

vicuna opened this issue Feb 27, 2003 · 3 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Feb 27, 2003

Original bug ID: 1563
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Full_Name: eric fillatre
Version: ocaml version 3.06
OS: windows 2000
Submission from: curie.noos.net (212.198.0.93)

Module signature depends of order of definitions

I define a Foo module

module Foo =
struct
type 'a t = {min : 'a ; max : 'a}

let merge i j =
  let min = (if i.min < j.min then i.min else j.min) in
  let max = (if i.max > j.max then i.max else j.max) in
  {min = min ; max = max}

let (<) i j = (i.max < j.min) 
let (>) i j = (i.min > j.max)       

end

if i load it in the ocaml interpreter, i obtain the following signature :

module Foo :
sig
type 'a t = { min : 'a; max : 'a; }
val merge : 'a t -> 'a t -> 'a t
val ( < ) : 'a t -> 'a t -> bool
val ( > ) : 'a t -> 'a t -> bool
end

if now i change the order of definitions :

module Foo =
struct
type 'a t = {min : 'a ; max : 'a}

let (<) i j = (i.max < j.min) 
let (>) i j = (i.min > j.max)       

let merge i j =
  let min = (if i.min < j.min then i.min else j.min) in
  let max = (if i.max > j.max then i.max else j.max) in
  {min = min ; max = max}

end

if i load it in the ocaml interpreter, i obtain a different signature (see
merge) :

module Foo :
sig
type 'a t = { min : 'a; max : 'a; }
val ( < ) : 'a t -> 'a t -> bool
val ( > ) : 'a t -> 'a t -> bool
val merge : 'a t t -> 'a t t -> 'a t t
end

@vicuna
Copy link
Author

vicuna commented Feb 28, 2003

Comment author: administrator

From: fillatre@noos.fr

Module signature depends of order of definitions

Short answer: Yes.

This is not a bug: at least for the bytecode interpreter, a module is
just a big array of closures, and you access functions by their index
in this array. So clearly order is relevant to the implementation.

Note however that you can coerce a module to a signature with a
different order. That is, you don't need to use the same order in the
.mli file, and you can pass a module with a different order to a
functor. So, at the programming level, this order is irrelevant.

Jacques Garrigue

@vicuna
Copy link
Author

vicuna commented Feb 28, 2003

Comment author: administrator

Module signature depends of order of definitions

I define a Foo module

module Foo =
struct
type 'a t = {min : 'a ; max : 'a}

let merge i j =
  let min = (if i.min < j.min then i.min else j.min) in
  let max = (if i.max > j.max then i.max else j.max) in
  {min = min ; max = max}

let (<) i j = (i.max < j.min) 
let (>) i j = (i.min > j.max)       

end

Note that "merge" uses the two functions "<" and ">". These are the
comparison functions from Pervasives.

if now i change the order of definitions :

Then "merge" will use your "<" and ">" functions, which do not have
the same type as the ones from Pervasives. Hence the type of "merge"
is different.

Your two modules define different functions because the order of
definitions is significant when you are redefining identifiers.

-- Damien

@vicuna vicuna closed this as completed Feb 28, 2003
@vicuna
Copy link
Author

vicuna commented Feb 28, 2003

Comment author: administrator

Full_Name: eric fillatre
Version: ocaml version 3.06
OS: windows 2000
Submission from: curie.noos.net (212.198.0.93)

Module signature depends of order of definitions

Short answer: yes it depends. But this is irrelevant to your ``problem''
which is just a regular consequence of the static binding discipline
of the language.

In your first module you define merge before defining ( < ) and ( > ).
Hence the function merge uses the system defined polymorphic
comparisons.

In the second module, you define ( < ) and ( > ) before defining
merge. Hence the function merge now uses the locally defined
specialized comparisons.

The compiler correctly flagged the type assignments consequences of
those two different module definitions.

So there is nothing strange here, just regular Caml semantics :)

Thanks for the ``bug'' report and your interest for Caml.

Pierre Weis.

I define a Foo module

module Foo =
struct
type 'a t = {min : 'a ; max : 'a}

let merge i j =
  let min = (if i.min < j.min then i.min else j.min) in
  let max = (if i.max > j.max then i.max else j.max) in
  {min = min ; max = max}

let (<) i j = (i.max < j.min) 
let (>) i j = (i.min > j.max)       

end

if i load it in the ocaml interpreter, i obtain the following signature :

module Foo :
sig
type 'a t = { min : 'a; max : 'a; }
val merge : 'a t -> 'a t -> 'a t
val ( < ) : 'a t -> 'a t -> bool
val ( > ) : 'a t -> 'a t -> bool
end

if now i change the order of definitions :

module Foo =
struct
type 'a t = {min : 'a ; max : 'a}

let (<) i j = (i.max < j.min) 
let (>) i j = (i.min > j.max)       

let merge i j =
  let min = (if i.min < j.min then i.min else j.min) in
  let max = (if i.max > j.max then i.max else j.max) in
  {min = min ; max = max}

end

if i load it in the ocaml interpreter, i obtain a different signature (see
merge) :

module Foo :
sig
type 'a t = { min : 'a; max : 'a; }
val ( < ) : 'a t -> 'a t -> bool
val ( > ) : 'a t -> 'a t -> bool
val merge : 'a t t -> 'a t t -> 'a t t
end

@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant