Local definitions in the class instances

Sebastian Fischer fischer at nii.ac.jp
Wed Feb 2 03:25:59 CET 2011


On Tue, Feb 1, 2011 at 9:23 PM, Ben Millwood <haskell at benmachine.co.uk>wrote:

> On Tue, Feb 1, 2011 at 9:52 AM, Max Bolingbroke
> <batterseapower at hotmail.com> wrote:
> >
> > Local declarations at module scope can be emulated using pattern
> bindings:
> >
> > """
> > (foo, bar) = (foo, bar)
> >  where
> >    foo = ..
> >    bar = ..
> >    private = ...
> > """
> >
> > If instance declarations supported pattern bindings you could get the
> > same effect for your instances too. This would be a minimal change
> > that avoided introducing any extra syntax.
>

It's a nice trick! Although it does look strange, it may be reasonable to
allow pattern bindings in instance declarations regardless of the original
proposal. Is it correct that, currently, pattern bindings are allowed
everywhere but in instance declarations? If so, why not in instance
declarations too?


> This is kind of ugly, I think, and there are proposals to make pattern
> bindings monomorphic which would make this sort of thing no longer
> possible in general.


I think the proposals to make pattern bindings monomorphic only concern
pattern bindings without type annotations. Instance methods do have type
annotations in the class declaration so even if pattern bindings without
type signatures would be monomorphic, instance methods bound using pattern
bindings need not be.


> I think I would be in favour of a declaration analogue to let.


I agree that using pattern bindings for the original task would work around
a missing syntax extension. But at least this workaround may be easily
implementable and making it possible seems to fix an inconsistency by making
the use of pattern bindings more orthogonal.

Sebastian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-prime/attachments/20110202/9422312c/attachment.htm>


More information about the Haskell-prime mailing list