Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I automatically implement classes?

Tags:

haskell

scalaz

In Scalaz every Monad instance is automatically an instance of Applicative.

implicit val listInstance = new Monad[List] {
  def point[A](a: => A) = List(a)
  def bind[A, B](fa: List[A])(f: A => List[B]) = fa flatMap f
}

List(2) <*> List((x: Int) => x + 1) // Works!

Another example: Arrow is automatically a Profunctor.

However, in Haskell I must provide an instance of Applicative for every Monad again and again.

Is it possible to avoid this repetitive job?

like image 682
ZhekaKozlov Avatar asked Apr 02 '15 13:04

ZhekaKozlov


People also ask

How do you auto implement property?

Auto-implemented properties declare a private instance backing field, and interfaces may not declare instance fields. Declaring a property in an interface without defining a body declares a property with accessors that must be implemented by each type that implements that interface.

What is true about auto-implemented properties?

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property.

What is an automatic property?

What is automatic property? Automatic property in C# is a property that has backing field generated by compiler. It saves developers from writing primitive getters and setters that just return value of backing field or assign to it. Instead of writing property like this: public class Dummy.

What is the point of automatic properties in C#?

Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects.


1 Answers

The problem comes when there are two places from which to derive the Applicative instance. For instance, suppose m is the type a b where Arrow a. Then there's an obvious instance of Applicative from this definition as well. Which one should the compiler use? It should work out the same, of course, but Haskell has no way to check this. By making us write out the instances, Haskell at least forces us to think about the consistency of our definitions.

If you want, there's the WrappedMonad class in Control.Applicative, which provides all the obvious instances with a newtype wrapper, but using WrapMonad and unwrapMonad all the time isn't that attractive either.

like image 99
Chad Groft Avatar answered Nov 02 '22 18:11

Chad Groft