Reputation: 677
This is my functor ORDTYPE. I want to use compare inside EmptyQueue. I dont knew how to inject functor. All time i get errors that i have invalid signature. I tried to declare functor (Key:ORDTYPE) -> before struct but it was wrong. I dont understand idea of functors. I see some easy examples in OCaml wiki but i dont knew how to deal with something more complicated.
In short i want to use comparator in emptyqueue. But i dont knew how to deal with this to do this more abstract.
module type ORDTYPE =
sig
type t
val compare : t -> t -> int
end;;
module Icmp:ORDTYPE with type t= int=
struct
type t = int
let compare = fun x y -> if( x< y) then 0 else 1
end;;
module type STH=
sig
type priority
type 'a t
val comp: x -> x->bool
end;;
module Emptyqueue (Comp: ORDTYPE): STH with type priority= int =
struct
type priority = int
type 'a t = Empty
let comp = fun x y -> Comp.comp x y
end;;
I edited how i think i should do this but it doesn't. work.
Upvotes: 1
Views: 117
Reputation: 1895
What you miss is the need to define x
in the signature STH
. I will use a clearer name elt
instead of x
. Once STH
has elt
, we can either add it to Emptyqueue
as well, or use "destructive substitution", i.e. signature modification syntax with ... := ...
Pay attention to differences introduced in my "straightening" of your example, because you have some mismatches that are ambiguous to correct.
module type ORDTYPE =
sig
type t
val compare : t -> t -> int
end;;
module Icmp:ORDTYPE with type t= int=
struct
type t = int
let compare = fun x y -> if( x< y) then 0 else 1
end;;
module type STH=
sig
type priority
type 'a t
type elt
val comp: elt -> elt -> bool
end;;
module Emptyqueue (Comp: ORDTYPE):
(STH with type priority= int and type elt := Comp.t) =
struct
type priority = int
type 'a t = Empty
let comp = fun x y -> Comp.compare x y > 0
end;;
Upvotes: 2