(* Définition *) module type ORDONNE = sig type t val comp : t -> t -> int (* Ordre total sur t, renvoie un négatif pour inférieur, 0 pour égal, un positif pour supérieur *) end module Tri = functor (E : ORDONNE) -> struct let infegal x y = (E.comp x y <= 0) let max x y = if (E.comp x y < 0) then y else x end (* Instanciation *) module Entier = struct type t = int let comp x y = x - y end module TriEntier = Tri(Entier);; TriEntier.infegal 1 2;; TriEntier.max 12 2;; (* Une autre instanciation *) module Chaine = struct type t = string let rec comprec s1 s2 i = if String.length s1 <= i then if String.length s2 <= i then 0 else -1 else if String.length s2 <= i then 1 else if s1.[i] = s2.[i] then comprec s1 s2 (i+1) else (int_of_char s1.[i]) - (int_of_char s2.[i]) let comp s1 s2 = comprec s1 s2 0 end module TriChaine = Tri(Chaine);; TriChaine.max "abc" "abd";; (* Foncteurs et abstraction *) module type ENS = sig type elt type ens val ensvide : ens val member : elt -> ens -> bool val insert : elt -> ens -> ens end module Ensemble : functor (T : ORDONNE) -> ENS with type elt = T.t = functor (T : ORDONNE)-> struct type elt = T.t type ens = elt list let ensvide = [] let rec member x e = match e with | t::q -> x=t || member x q | [] -> false let insert x e = if member x e then e else x::e end module EnsEntier = Ensemble(Entier) let e1 = EnsEntier.insert 1 EnsEntier.ensvide