[Haskell-cafe] difference between type and newtype

Andrea Rossato mailing_list at istitutocolli.org
Sat Aug 26 10:40:06 EDT 2006

Il Sat, Aug 26, 2006 at 06:00:02PM +0400, Bulat Ziganshin ebbe a scrivere:
> type HashFunction = (String->Int)
> createHash :: HashFunction -> IO Hash
> and
> createHash :: (String->Int) -> IO Hash
> are equivalent. So, technically speaking, you can't declare function
> types, there is only one predefined type constructor that can
> construct any function type you need. But from practical POV, you can
> define synonyms for any function type you've constructed using '->'.

this possibility (POV) lead me to think it was possible to have a type
constructor that could construct a function type.
> Next point is that in Haskell functions are first-class values, i.e.
> you can hold them in data structures, pass as parameters to other
> functions and even return them as results. For example, the following
> function accepts function of type String->Int->Int and returns
> function of type (Int->Int):
> func :: (String->Int->Int) -> (Int->Int)
> func f = f ""

ok, I've got it.

> Of course, newtype can also be used to define type whose only field
> contains some function:
> newtype F = F (String->Int)
> Again, in this case language guarantees that internal F representation
> will be the same as for plain (Int->String) function and therefore the
> same as for type synonym:
> type S = String->Int
> But from programmer POV, the difference still holds: while S denotes
> function of given type and can be used interchangeably with full
> notion of this function type, F is just the type which internally
> contains such function. 

As I said in a previous message it took me almost two day to grasp
this distinction. 

> hope that it helps :)

sure it does!
thank you very mu!

More information about the Haskell-Cafe mailing list