Search code examples
functional-programmingsmlsmlnj

SML Value Restriction - Heap


Basically, I want a function to make a heap from a node and 2 subheaps. The heap representation is as follows (where the int in the Cons represents the rank of the node)

datatype 'a heap = Empty | Heap of int * 'a * 'a heap *  'a heap

and my function is:

fun makeHeap x h1 h2 = Heap ((rank h1)+1, x, h1, h2)

The programs comipiles but when I call makeHeap, I get a weird value instead of a Heap:

makeHeap(#"c", Empty, Empty);
stdIn:72.1-72.29 Warning: type vars not generalized because of
   value restriction are instantiated to dummy types (X1,X2,...)
val it = fn
 : (char * ?.X1 heap * ?.X2 heap) heap
    -> (char * ?.X1 heap * ?.X2 heap) heap
      -> (char * ?.X1 heap * ?.X2 heap) heap

Solution

  • You have defined makeHeap in curried form but call it with tupled arguments. The two forms imply different types and thus are not interchangeable. Simply change the call to

    makeHeap #"c" Empty Empty
    

    Alternatively, change the definition to

    fun makeHeap(x, h1, h2) = ...