Suppose I have these signatures :
module type CharS = sig
type c
type t = BoW | C of c | EoW
val compare : t -> t -> int
val print : Format.formatter -> t -> unit
end
module type GraphemS = sig
type c
type t
val compare : t -> t -> int
val print : Format.formatter -> t -> unit
end
And these two functors :
module MakeDiGraphem (C : CharS) : GraphemS with type c = C.t = struct
type c = C.t
type t = c * c
let compare (cb1, ce1) (cb2, ce2) =
let r1 = C.compare cb1 cb2 in
if r1 = 0 then
C.compare ce1 ce2
else r1
let print fmt (cb, ce) =
Format.fprintf fmt "#[%a%a#]#," C.print cb C.print ce
end
module MakeMonoGraphem (C : CharS) : GraphemS with type c = C.t = struct
type c = C.t
type t = c
let compare c1 c2 = C.compare c1 c2
let print fmt c =
Format.fprintf fmt "#[%a#]#," C.print c
end
Now, I'd like to have a functor which would allow me to create a module of type GraphemS with either the first functor or the second one. What I did is this :
module type SizeS = sig
type t = int
val param : int
end
module MakeGraphem (C : CharS) (I : SizeS) : GraphemS with type c = C.t = struct
module MonoGraphem = MakeMonoGraphem(C)
module DiGraphem = MakeDiGraphem(C)
let select_graphem =
if I.param = 1 then
(module MonoGraphem : GraphemS)
else
(module DiGraphem : GraphemS)
include (val select_graphem)
end
But sadly I got :
Error: This expression creates fresh types.
It is not allowed inside applicative functors.
My question is, then, is it possible to do what I want to do and what does this error means ?
Basically, you are not allowed to do first-class calculations in an applicative functor application include. Basically, the typing system has no guarantee that I.param is a constant so it can't ensure that the functor will always return the same type. An applicative functor (the default in OCaml) has to always return the same type for the same expression (in a sense, it's pure).
If you are on OCaml 4.02 or more, you can declare your functor as generative through a unit argument:
module MakeGraphem (C : CharS) (I : SizeS) () :
GraphemS with type c = C.t = struct
My favorite way of doing the trick would be to get the functor to apply as an argument instead of I.
Related
I'm studying functors, and I was trying to receive a Module.t as argument where Module is the result of a functor call SomeFunctor(sig type t = int end) but I receive Unbound type constructor FooInt.t
Here is the code
module type Foo_S = sig
type t
end
module type Foo = sig
type t
val id : t -> t
end
module FooExtend(Arg : Foo_S) : (Foo with type t := Arg.t) = struct
let id a = a
end
module FooInt = FooExtend(Int)
module FooString = FooExtend(String)
let () =
assert (FooInt.id 1 = 1);
assert (FooString.id "x" = "x")
module FooSimple = struct type t = int end
let foo (x : FooInt.t) = (* This doens't compile: Unbound type constructor FooInt.t *)
FooInt.id x
let foo' (x : FooSimple.t) = (* This works fine *)
FooInt.id x
Two changes are needed, 1) type t needs to be defined in the module, and 2) don't use destructive substitution:
module FooExtend(Arg : Foo_S) : (Foo with type t = Arg.t) = struct
type t = Arg.t
let id a = a
end
The problem is that destructive substitution (type t := Arg.t) replaces every occurrence of t with Arg.t. Instead you want to specify type equality, using a "sharing constraint" (type t = Arg.t).
Notice the difference in the resulting module signature between the two:
module FooExtend(Arg : Foo_S) : (Foo with type t := Arg.t) = struct
type t = Arg.t
let id a = a
end
module FooExtend : functor (Arg : Foo_S) -> sig
val id : Arg.t -> Arg.t
end
module FooExtend(Arg : Foo_S) : (Foo with type t = Arg.t) = struct
type t = Arg.t
let id a = a
end
module FooExtend : functor (Arg : Foo_S) -> sig
type t = Arg.t
val id : t -> t
end
I've get the follow code about the functors in OCaml:
type comparison = Less | Equal | Greater;;
module type ORDERED_TYPE =
sig
type t
val compare: t -> t -> comparison
end
;;
module Set =
functor (Elt: ORDERED_TYPE) ->
struct
type element = Elt.t
type set = element list
let empty = []
let rec add x s =
match s with
| [] -> [x]
| hd :: tl ->
match Elt.compare x hd with
| Equal -> s
| Less -> x :: s
| Greater -> hd :: add x tl
let rec member x s =
match s with
| [] -> false
| hd :: tl ->
match Elt.compare x hd with
| Equal -> true
| Less -> false
| Greater -> member x tl
end
;;
module OrderedString : ORDERED_TYPE =
struct
type t = string
let compare x y =
if x = y then Equal
else if x < y then Less
else Greater
end
;;
module StringSet = Set(OrderedString);;
let out = StringSet.member "foo" (StringSet.add "foo" StringSet.empty);; (*compile error, where "foo" is expected OrderedString.t but actually is string*)
The above error can be avoided by eliminating the : ORDERED_TYPE in module OrderedString : ORDERED_TYPE =
Just can't understand why.
Analogously, if there is any type in a module like
module A = struct type t = string end;;
How can I specify a string value as the type A.t but not an actual string
Thanks.
You can look at how it is done in the standard library : set.mli.
The signature of the functor is
module Make (Ord : OrderedType) : S with type elt = Ord.t
the with type elt = Ord.t part indicates that the elt type is not abstract.
As mentioned by Tomash, you're lacking a type constraint, but not in the functor signature (there isn't any in your code in fact), but in the argument you're giving to it. Basically, when you write
module OrderedString : ORDERED_TYPE = struct ... end
the definition of the type t in OrderedString will be abstracted away, since t is an abstract type in the ORDERED_TYPE signature. What you want here is to say that OrderedString is indeed an implementation of ORDERED_TYPE, but with a known type t. This is exactly what you'll get with
module OrderedString: ORDERED_TYPE with type t = string = struct ... end
I would like to make my code generic over strings and arrays (any indexable type really) using the following signature:
module type Indexable = sig
type 'a t
val get : int -> 'a t -> 'a
end
module MyCode (I : Indexable) = struct ... end
But of course I cannot apply my signature to strings as follows:
module StrMyCode = MyCode(struct
type 'a t = string
let get i a = a.[i]
end)
Is there any way to fix this issue? Or perhaps a different aprroach? I know I can use arrays of characters in the worst case but I'd rather save my code from ugly casts and this is something that was on my mind before so I'd like to get a clear answer for this.
GADT can be used with the functorized approach:
module type Indexable = sig
type 'a t
val get: int -> 'a t -> 'a
end
module MyCode(I:Indexable) = struct
let head x = I.get 0 x
end
Arrays can of course be made Indexable trivially:
module IndexableArray = struct
type 'a t = 'a array
let get i x = x.(i)
end
For string, you can just use a GADT with a single constructor. Note however, that you have to put some type annotation for get in order to force the polymorphic type (otherwise, the inferred type is int -> char t -> char):
module IndexableString = struct
type 'a t = String: string -> char t
let of_string s = String s
let get: type a. int -> a t -> a =
fun i s -> match s with String s -> s.[i]
end
Here is something I made using GADTs. I'm just wrapping my head around them, so there may be something a little wrong here. But it seems to work as far as I can see (with OCaml 4):
type _ indexable =
| A : 'a array -> 'a indexable
| C : string -> char indexable
let index (type s) (x: s indexable) i : s =
match x with
| A a -> a.(i)
| C s -> s.[i]
let main () =
let x = A [| 1; 2 |] in
let y = C "abc" in
Printf.printf "%d\n" (index x 0);
Printf.printf "%c\n" (index y 1)
If I load into the toplevel, I get this:
val index : 'a indexable -> int -> 'a = <fun>
val main : unit -> unit = <fun>
# main ();;
1
b
- : unit = ()
#
This might not be as general as what you're looking for.
If you declare the element type of the indexable as a separate type, you can do something like this:
module type Indexable = sig
type t
type elt
val get : int -> t -> elt
end
module IndexableString : Indexable = struct
type t = string
type elt = char
let get i a = a.[i]
end
module MyCode (I : Indexable) = struct
(* My code implementation *)
end
module StrMyCode = MyCode(IndexableString)
For arrays, you can do more or less the same:
module ArrayIndexable = struct
type elt = char
type t = char array
let get i a = a.(i)
end
Now, if you wish to retain some flexibility with arrays, you may change the above into a functor:
module ArrayIndexable (E : sig type e end) : Indexable with type elt = E.e =
struct
type elt = e
type t = elt array
let get i a = a.(i)
end
It is more verbose than the polymorphic version you are looking for, but it let you encode both "indexable" types uniformly.
I'm using the Bitstring module in the following code:
let build_data_32 v wid =
let num = wid / 32 in
let v' = Int32.of_int(v) in
let rec aux lst vv w = match w with
0 -> lst
| _ -> (BITSTRING { vv : 32 } ) :: ( aux lst (Int32.succ vv) (w-1)) in
Bitstring.concat ( aux [] v' num ) ;;
Note that when you have BITSTRING { vv : 32 }
that vv is expected to be an Int32 value. I'd like to generalize this function to work with different widths of bitstrings; ie, I'd like to create a build_data_n function where the bitstring would be constructied with BITSTRING { vv : n } .
However, the problem here is that if n is less than 32 then the succ function used above would just be the succ for type int. If it's greater than 32 it would be Int64.succ Same issue above in the line let v' = Int32.of_int(v) in - for values less than 32 it would simply be: let v' = v in , whereas for values greater than 32 it would be: let v' = Int64.of_int(v) in
Is this a case where a functor would come in handy to generalize this function and if so, how would I set that up? (and if there's some other way to do this that doesn't require functors, that would be nice to know as well)
There are a few approaches available. One is to use a functor, similar to the following:
(* The signature a module needs to match for use below *)
module type S = sig
type t
val succ : t -> t
val of_int : int -> t
end
(* The functor *)
module Make(M : S) = struct
(* You could "open M" here if you wanted to save some typing *)
let build_data v =
M.succ (M.of_int v)
end
(* Making modules with the functor *)
module Implementation32 = Make(Int32)
module Implementation64 = Make(Int64)
let foo32 = Implementation32.build_data 12
let foo64 = Implementation64.build_data 12
Another is to wrap your data type in a record:
(* A record to hold the relevant functions *)
type 'a wrapper_t = { x : 'a; succ : 'a -> 'a }
(* Use values of type 'a wrapper_t in *)
let build_data v =
v.succ v.x
(* Helper function to create 'a wrapper_t values *)
let make_int32_wrapper x = { x = Int32.of_int x; succ = Int32.succ }
let make_int64_wrapper x = { x = Int64.of_int x; succ = Int64.succ }
(* Do something with a wrapped int32 *)
let foo32 = build_data (make_int32_wrapper 12)
let foo64 = build_data (make_int64_wrapper 12)
And finally, if you are using OCaml 3.12.0 or later, you can use first class modules:
(* You can use the module type S from the first example here *)
let build_data (type s) m x =
let module M = (val m : S with type t = s) in
M.succ x
let int32_s = (module Int32 : S with type t = Int32.t)
let int64_s = (module Int64 : S with type t = Int64.t)
let foo32 = build_data int32_s 12l
let foo64 = build_data int64_s 12L
Each of these approaches can be mixed and matched. You may also be able to wrap your values in variant types or objects to get a similar result.
With BITSTRING { vv : n }, i.e. using runtime-specified field length, the type of vv cannot depend on n as it is not the compile-time constant anymore, so vv is forced to int64.
The aim of the module described below is to implement a module which once initiated by an integer n does all the operations based on the value of n.
module ReturnSetZero =
functor ( Elt : int ) ->
struct
let rec sublist b e l =
match l with
[] -> failwith "sublist"
| h :: t ->
let tail = if e = 0 then [] else sublist (b - 1) (e - 1) t in
if b > 0 then tail else h :: tail
let rec zerol = 0:: zerol
let zeron = sublist 0 n zerol
(*other operations based on n which is selected once when the module is initialized*)
end;;
Error: Unbound module type int
What is the issue here? Is there an alternate implementation which is more effective/intuitive?
A functor maps modules to modules. An integer is not a module, so you cannot use it to as a functor parameter.
You need to define a module type:
module type WITH_INTEGER = sig
val integer : int
end
module PrintInteger =
functor (Int:WITH_INTEGER) -> struct
let print_my_integer () = print_int Int.integer
end
Of course, unless your module needs to expose types that are dependent on the value of the integer (or you have to expose a lot of values dependent on that integer), you're probably better off with a plain function that takes that integer as an argument:
let my_function integer =
let data = complex_precomputations integer in
function arg -> do_something_with arg data
This lets you run the complex pre-computations only once on the integer (when you pass it to the function).