Friends
I’d like to propose a way to “promote” newtypes over their enclosing type. Here’s the writeup
http://hackage.haskell.org/trac/ghc/wiki/NewtypeWrappers
Any comments? Below is the problem statement, taken from the above page.
I’d appreciate
·
A sense of whether you care. Does this matter?
·
Improvements to the design I propose
Simon
The problem
Suppose we have
newtype Age = MkAge Int
Then if n :: Int, we can convert
n to an Age thus:
MkAge n :: Age. Moreover, this conversion is a type conversion only, and involves no runtime instructions whatsoever. This cost model -- that newtypes are free -- is important to Haskell programmers, and encourages
them to use newtypes freely to express type distinctions without introducing runtime overhead.
Alas, the newtype cost model breaks down when we involve other data structures. Suppose we have these declarations
data T a = TLeaf a | TNode (Tree a) (Tree a)
data S m a = SLeaf (m a) | SNode (S m a) (S m a)
and we have these variables in scope
x1 :: [Int]
x2 :: Char -> Int
x3 :: T Int
x4 :: S IO Int
Can we convert these into the corresponding forms where the Int is replaced by
Age? Alas, not easily, and certainly not without overhead.