Reputation: 321
I'm trying to implement flatten : 'a list list -> 'a list list in SML. I thought this should be relatively straight forward with higher order functions. My implementation is
val flatten = List.reduce (op @) []
However I'm getting a bizarre error message: "append.sml:1.6-1.36 Warning: type vars not generalized because of value restriction are instantiated to dummy types (X1,X2,...)". Thus when I try to flatten an int list list I get a type error:
:> flatten [[1,2],[3]];
stdIn:2.1-2.20 Error: operator and operand don't agree [literal]
operator domain: ?.X1 list list
operand: int list list
in expression:
flatten ((1 :: 2 :: nil) :: (3 :: nil) :: nil)
Upvotes: 0
Views: 360
Reputation: 36118
As the error message hints, you ran into the value restriction -- see here for an explanation. The solution is very simple: just "eta-expand" your definition, i.e., make the parameter explicit instead of relying on partial application:
fun flatten xs = List.reduce op@ [] xs
Upvotes: 4