ho fatto un funtore per i set formato-grado, come segue:Estendere Mappe OCaml per formattabili Maps
module type POrderedType =
sig
type t
val compare : t -> t -> int
val format : Format.formatter -> t -> unit
end
module type SET =
sig
include Set.S
val format : Format.formatter -> t -> unit
end
module MakeSet (P : POrderedType) : SET with type elt = P.t
L'attuazione di questo è semplice:
module MakeSet (P : OrderedType) =
struct
include Set.Make(P)
let format ff s =
let rec format' ff = function
| [] ->()
| [v] -> Format.fprintf ff "%a" format v
| v::tl -> Format.fprintf ff "%a,@ %a" format v format' tl in
Format.fprintf ff "@[<4>%[email protected]]" format' (elements s)
end
ho voluto fare qualcosa di simile con mappe. POrderedType
va bene per le chiavi, ma ho bisogno di un tipo più semplice per i valori:
module type Printable =
sig
type t
val format : Format.formatter -> t -> unit
end
Poi ho voluto fare qualcosa di simile a quello che avevo fatto per i set, ma mi imbatto nel seguente problema. I valori Map.S
hanno il tipo +'a t
. Non riesco a capire un modo per includere la definizione Map.S
vincolando allo 'a
come a Printable.t
. Quello che voglio è qualcosa di simile alla seguente (ignorando il fatto che è illegale):
module MakeMap (Pkey : POrderedType) (Pval : Printable) :
MAP with type key = Pkey.t and type 'a t = 'a t constraint 'a = Pval.t
Esiste un modo per fare quello che voglio senza copiare l'intero firma di Map a mano?