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
Your exercice seems to ask you to start with
module M = struct
class t ... = object inherit Mesurable.mesurable ... end
...
end
which is a bit surprising.
Thank you for your answer.
Is this compatible with the signature TYPE_ORDONNE?
Ive really had difficulty understanding this exercice.
Yes, you can satisfy the two constraints simultaneously.
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
You should first make your code compile, then take the time to think on how to be sure that M implements TYPE_ORDONNE.
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” ?
In this case, class t
defines a class named t
.
If you wish, you could define first a class, then the type t
with
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
:
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 *)
Thanks a lot for your explanation.