my permutation function:
fun perms [] = [[]]
| perms (x::xs) = let
fun insertEverywhere [] = [[x]]
| insertEverywhere (y::ys) = let
fun consY list = y::list
in
(x::y::ys) :: (map consY (insertEverywhere ys))
end
in
List.concat (map insertEverywhere (perms xs))
end;
input:
perms [];
output:
stdIn:813.1-813.9 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
val it = [[]] : ?.X1 list list
Can someone explain why the type vars aren’t generalized?
I should note, the type of perms is given after inputting perms; as
perms;
val it = fn : 'a list -> 'a list list
So it looks like I have achieved generalized variables, to me at least.
Empty list is a special list which could potentially have any type for its elements. When you invoke
perms [], the compiler is confused about the type of elements. You can either use:or
then the compiler is happy because in can inference a specific type of the lists.