Re: Local definitions in the class instances

2011-02-01 Thread Max Bolingbroke
On 27 January 2011 17:11, Iavor Diatchki iavor.diatc...@gmail.com wrote:
 instance Num Wrapped where
    local
      lift2 f (Wrapped a) (Wrapped b) = Wrapped (f a b)
      lift f (Wrapped a) = Wrapped (f a)
    in
     (+) = lift2 (+)
     (-) = lift2 (-)
     (*) = lift2 (*)
     abs = lift abs
     signum = lift signum

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.

Cheers,
Max

___
Haskell-prime mailing list
Haskell-prime@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-prime


Re: Local definitions in the class instances

2011-02-01 Thread Sebastian Fischer
On Tue, Feb 1, 2011 at 9:23 PM, Ben Millwood hask...@benmachine.co.ukwrote:

 On Tue, Feb 1, 2011 at 9:52 AM, Max Bolingbroke
 batterseapo...@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
___
Haskell-prime mailing list
Haskell-prime@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-prime