Search code examples
typessmlsmlnj

SML: difference between type and datatype


I'm pretty new at SML and I would like to make sure I really know the basics. What is the difference between type and datatype in SML, and when to use which?


Solution

  • type declarations just give another name to an existing type. Declaring type t = int * int simply means that you can now write t instead of int * int - it doesn't actually provide any functionality.

    datatype definitions let you create brand new types by introducing new data constructors. Data constructors are the keywords and symbols you use to create and pattern-match values, such as the list type's nil and ::. There's nothing particularly special about those identifiers; you can define them yourself as easily as this:

    datatype 'a list = nil | :: of 'a * 'a list