labelled types efficiency

Hannah Schroeter
Fri, 8 Mar 2002 08:14:43 +0100


On Fri, Mar 08, 2002 at 12:52:58AM -0300, Andre W B Furtado wrote:
> Another question about labelled types, this time concerning about
> efficiency: is there any efficiency differences between functions f and g
> below?

> > data RType = R Int Char
> > data Stype = S {x :: Int, y :: Char}

> > f ::  RType -> Int
> > f (R x _) = x

> > g :: SType -> Int
> > g s = x s

> Thanks again,
> -- Andre

In principle, the representation of RType and Stype is the same.
The only difference of Stype over RType is the implicit definition
of the functions (accessors) x and y, as well as the syntactic
sugar for constructing values, pattern matching and functional
update using field names. But as said, that's just functional
sugar and should be just exactly as efficient as using the manual
translation of those constructs on RType.

Now, that said, g *looks* a bit more lazy than f, as in f, you
don't evaluate the right side if the parameter isn't
R _|_ _|_ or more. However, I said *looks*, but if one checkes,
it's thus, desugared:

f (R x _) = x
g s = x s
    x (S xElem _yElem) = xElem

And if you just evaluate both at _|_, the result is _|_ for both
f and g. So in fact, there equally strict. Finally, the code
for f and g should, save for renaming, be just the same.

Kind regards,