Heterogeneous sets in OCaml

suppose i have a type defined as

type value =
      None
    | Int of int
    | Float of float
    | Complex of Complex.t
    | String of string
    | Char of char
    | Bool of bool

and I want to be able to work with Setsthese values. From what I understood, I should use a functor to instantiate a module Setwith a specific type and its ordering.

How do I do this in this example? So how valuecan you not use it directly inside the functor Set.Make?

Then, of course, I need to give the full order of these values, so I have to come up with something like providing a predefined order to different types, and then sort them by their effective value. I'm right?

So, for example, I can decide to have Int of int < Float of intand Int x < Int yif x < y. Is this a practical approach to what I'm trying to achieve?

+3
1

Set.Make functor Set.OrderedType:

module type OrderedType = sig type t val compare : t -> t -> int end

Pervasives.compare, , min_elt/max_elt. , , :

module T = struct type t = value let compare = compare end
+7

Source: https://habr.com/ru/post/1753891/


All Articles