Questions about sharing
Sat, 8 Dec 2001 00:06:06 +0000
On Friday 07 December 2001 3:09 pm, Simon Marlow wrote:
> > The second is..
> > If, following pattern matching, the matched pattern appears in an
> > expression, is it shared or duplicated..
> > e.g. (a:as) -> f (a:as)
> > Maybe I should write something like this..
> > x@(a:as) -> f x
> > (I know that sometimes the type checker won't allow you to do this)
> Yes, GHC will common these up (with -O). I'm not sure I understand the
> point about the type checker not letting you do this yourself, though:
> surely x and (a:as) have the same type?
In this particular case they do, but sometimes this isn't so.
IE in.. case <expression1>
x@<pattern> -> <expression2>
the x has the same type as <expression1>, which isn't
necessarily the same as would be inferred using..
(here the pattern is really an expression of course)
A long long time ago I griped about this in a thread the main
haskell.org mailing list (called "Pattern match success changes
IMHO variables bound using 'as patterns' like..
x@<pattern> -> <expression>
should be re-typed as if they had been written..
<pattern> -> let x=<pattern> in <expression>
in order to make sharing possible.
I don't think anybody agrees with me though :-)