# The type in the module

**URL:** <https://discuss.ocaml.org/t/the-type-in-the-module/2119>\
**Category:** Learning\
**Tags:** build\
**Created:** [June 10, 2018, 7:28am UTC](https://discuss.ocaml.org/t/the-type-in-the-module/2119 "2018-06-10T07:28:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![herosgogo](https://avatars.discourse-cdn.com/v4/letter/h/3ec8ea/32.png) [@herosgogo](https://discuss.ocaml.org/u/herosgogo)\
**Post date:** [June 10, 2018, 7:28am UTC](https://discuss.ocaml.org/t/the-type-in-the-module/2119/1 "2018-06-10T07:28:32Z")

</div>

Hi everyone  
I’ve got a question I can’t understand:

type comp = Inf | Egal | Sup

module type TYPE\_ORDONNE =  
sig  
type t  
val compare: t -\> t -\> comp  
end

module Mesurable = struct  
class virtual mesurable =  
object  
method virtual distance: unit -\> float  
end  
end

Implement a module M which is compatible with the signature TYPE\_ORDONNE and whose type t corresponds to the type measurable defined in the module Mesurable

I’ve written:  
module M =  
struct  
type t = float  
let compare ((a:t),(b:t)) = let c = a -. b in  
match c with  
|0. -\> Egal  
| x -\> if x \< 0. then Inf else Sup  
end

Is it right or wrong?  
And What does mean “type t corresponds to the type measurable defined in the module Mesurable”?

I will be grateful for any help you can offer

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [June 10, 2018, 9:37am UTC](https://discuss.ocaml.org/t/the-type-in-the-module/2119/2 "2018-06-10T09:37:20Z")

</div>

Your exercice seems to ask you to start with

```auto
  module M = struct
    class t ... = object inherit Mesurable.mesurable ... end
    ...
 end

```

which is a bit surprising.

---

<div class="post-metadata">

**Author:** ![herosgogo](https://avatars.discourse-cdn.com/v4/letter/h/3ec8ea/32.png) [@herosgogo](https://discuss.ocaml.org/u/herosgogo)\
**Post date:** [June 10, 2018, 9:47am UTC](https://discuss.ocaml.org/t/the-type-in-the-module/2119/3 "2018-06-10T09:47:35Z")

</div>

Thank you for your answer.  
Is this compatible with the signature TYPE\_ORDONNE?  
Ive really had difficulty understanding this exercice.

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [June 10, 2018, 9:51am UTC](https://discuss.ocaml.org/t/the-type-in-the-module/2119/4 "2018-06-10T09:51:54Z")

</div>

Yes, you can satisfy the two constraints simultaneously.

---

<div class="post-metadata">

**Author:** ![herosgogo](https://avatars.discourse-cdn.com/v4/letter/h/3ec8ea/32.png) [@herosgogo](https://discuss.ocaml.org/u/herosgogo)\
**Post date:** [June 10, 2018, 10:03am UTC](https://discuss.ocaml.org/t/the-type-in-the-module/2119/5 "2018-06-10T10:03:22Z")

</div>

Like this: ?

```
module M = struct
class t (a,b) = 
    object
        inherit Mesurable.mesurable
        let distance = a - b
    end
let compare ((a:float),(b:float)) = let c = (new t (a,b))#distance in
    match c with
    |0. -> Egal
    | x -> if x < 0. then Inf else Sup

```

end

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [June 10, 2018, 10:44am UTC](https://discuss.ocaml.org/t/the-type-in-the-module/2119/6 "2018-06-10T10:44:05Z")

</div>

You should first make your code compile, then take the time to think on how to be sure that M implements TYPE\_ORDONNE.

---

<div class="post-metadata">

**Author:** ![herosgogo](https://avatars.discourse-cdn.com/v4/letter/h/3ec8ea/32.png) [@herosgogo](https://discuss.ocaml.org/u/herosgogo)\
**Post date:** [June 10, 2018, 11:36am UTC](https://discuss.ocaml.org/t/the-type-in-the-module/2119/7 "2018-06-10T11:36:10Z")

</div>

Thank you , i’ll do that.  
But firstly I’d like to focus on your first response: “class t … = object inherit Mesurable.mesurable … end”.  
I’ve stated the language ocaml for a few months, I alwais have difficulty understanding the structure of caml.  
If you don’t mind, can you explain me “class t” you wrote? please.

I’ve learnt the structure of class in ocaml like this:  
class name paramter1 parameter2 …= object … end;;

What does mean “class t” ?

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [June 10, 2018, 12:22pm UTC](https://discuss.ocaml.org/t/the-type-in-the-module/2119/8 "2018-06-10T12:22:47Z")

</div>

In this case, `class t` defines a class named `t`.  
If you wish, you could define first a class, then the type `t` with

```auto
class your_prefered_class_name = ...
type t = your_prefered_class_name

```

But this is fact unnecessary, since declaring a class `t` also defines the corresponding class type and type also named `t`:

```auto
class t = object val v = 0 end
type w = t (* <- this is the type t, here, t ≡ < > *)
class a: t (* <- this is the class type t, here t ≡ object val v: int end *)
  = object val v = 1 end 
class b = t (* <- this is the class , here t ≡ object val v=0 end *)

```

---

<div class="post-metadata">

**Author:** ![herosgogo](https://avatars.discourse-cdn.com/v4/letter/h/3ec8ea/32.png) [@herosgogo](https://discuss.ocaml.org/u/herosgogo)\
**Post date:** [June 10, 2018, 12:31pm UTC](https://discuss.ocaml.org/t/the-type-in-the-module/2119/9 "2018-06-10T12:31:31Z")

</div>

Thanks a lot for your explanation.
