[Haskell-cafe] OOP'er with (hopefully) trivial questions.....

Nicholls, Mark Nicholls.Mark at mtvne.com
Mon Dec 17 08:04:25 EST 2007

No that's fine....its all as clear as mud!......but that's not your

To recap...

"type" introduces a synonym for another type, no new type is
created....it's for readabilities sake.

"Newtype" introduces an isomorphic copy of an existing type...but
doesn't copy it's type class membership...the types are
disjoint/distinct but isomorphic (thus only 1 constructor param).

"data" introduces a new type, and defines a composition of existing
types to create a new one based on "->" and "(".

"class" introduces a constraint that any types declaring themselves to
be a member of this class...that functions must exist to satisfy the

I'm sure that's wrong, but it's a good as I've got at the moment.

And to a degree it's all upside down....what Haskell thinks are
types...I think are "singnatures" and what Haskell thinks is a type
"class" I think of as a type.....it's not going to be easy.

-----Original Message-----
From: Thomas Davie [mailto:tom.davie at gmail.com] 
Sent: 17 December 2007 12:35
To: Nicholls, Mark
Cc: Haskell Cafe
Subject: Re: [Haskell-cafe] OOP'er with (hopefully) trivial

On 17 Dec 2007, at 12:22, Nicholls, Mark wrote:

> Ok...
> Thanks I need to revisit data and newtype to work out what the
> difference is I think.

Beware in doing so -- type, and newtype are not the same either.  type  
creates a type synonim.  That is, if I were to declare

type Jam = Int

then Jam and Int from that point on become completely interchangable,  
the only thing this does is make things readable.  For example, a  
parser might be described as a function that takes a list of tokens,  
and outputs a parse tree, and a list of unparsed tokens:

type Parser = [Token] -> (ParseTree, [Token])

if I write some parser combinators, I can now give them clear types like

<|> :: Parser -> Parser -> Parser

I could however still write this, and it would have *exactly* the same  

<|> :: ([Token] -> (ParseTree, [Token])) -> ([Token] -> (ParseTree,  
[Token])) -> [Token] -> (ParseTree, [Token])

newtype on the other hand introduces a new type to the type system.   
Because of this, the type system has to be able to tell when you're  
using your new type, so a tag gets attached.

newtype Ham = Ham Int

This creates a type that contains only an integer, but is different  
from Int (and Jam) in the type system's eyes.  Thus, I cannot for  
example write

(Ham 5) + (Ham 6)

Because Ham is not Int and thus (+) does not work (or actually, more  
specifically, Ham is not a member of the class Num, the numeric types,  
and therefore (+) doesn't work).  This can of course be fixed thus:

newtype Ham = Ham Int deriving Num

Hope that helps

Tom Davie

p.s. Sorry for the slip with the newtype Rectangle.

More information about the Haskell-Cafe mailing list