Search code examples
haskellnewtype

Haskell newtype definition


I read this statement in the code provided for a homework:-

newtype STR a = STR (Store -> (Result a, Store))

The above link makes it seem like:

a === (Store -> (Result a, Store))

How can this be a valid statement? Does it mean that a is a function which takes Store as argument and returns ('the same function wrapped in Result', Store) ?


Solution

  • The newtype definition is a bit confusing, because the symbol STR is used in two different meanings: namely that of type name (the first occurrence) and that of constructor (the second). Renaming both to something different leads to the equivalent

    newtype STRType a = STRConstructor (Store -> (Result a, Store))
    

    In other words, this introduces a type STRType a which is structurally the same as Store -> (Result a, Store) (but needs to be wrapped in a STRConstructor)

    I hope your course/book already went into the differences between type, data and newtype; otherwise this is going to remain rather mysterious, I'm afraid...