Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does instance mean in Haskell?

In object-oriented languages (e.g; Java and Python) we can make objects/instances from classes. In Haskell we can make instances from type-classes, ex:

data ShirtSize = S | M | L      -- Here ShirtSize is a enum data-type

class MyEq a where
  (==) :: a -> a -> Bool
instance MyEq ShirtSize where   -- Here ShirtSize is an instance of the MyEq type-class
  S == S = True
  M == M = True
  L == L = True
  _ == _ = False

My question is: What does instance mean in haskell? In java we can make instances from classes, but in haskell it seems like instances are types (like ShirtSize) which you can apply type-class functions on (e.g the (==) function from MyEq). Am I right? and also what is an instance in Haskell compared to an instance/object in Java?

like image 686
August Jelemson Avatar asked Jul 03 '17 13:07

August Jelemson


People also ask

How do I make an instance in Haskell?

If you actually want to define an instance, then you, well, instantiate n with N : instance Printable N where print n = ... read str = ... At this point the type signatures are all fixed (from the class definition) and what you need to write are the actual bindings of these functions, hence it must be = , not :: .

What does () mean in Haskell?

() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .

What is a Typeclass in Haskell?

What's a typeclass in Haskell? A typeclass defines a set of methods that is shared across multiple types. For a type to belong to a typeclass, it needs to implement the methods of that typeclass. These implementations are ad-hoc: methods can have different implementations for different types.

What is constraint Haskell?

Constraints in Haskell mean one of the following things: Class constraints, e.g. Show a. Implicit parameter constraints, e.g. ? x::Int (with the -XImplicitParams flag) Equality constraints, e.g. a ~ Int (with the -XTypeFamilies or -XGADTs flag)


2 Answers

In Java, the class system is a way to group similar objects. An instance of a class is an individual object which belongs to that class.

In Haskell, the class system is (roughly speaking) a way to group similar types. (This is the reason we call them "type classes"). An instance of a class is an individual type which belongs to that class. (That is, until you start considering multiparametric type classes).

Incidentally, a Haskell (monoparametric) class somewhat resembles a Java interface and, by extension, a Java class. Or perhaps a Haskell instance resembles Java class. It's better to view this as a coincidence. Approach the term keeping its mathematical origins in mind. A class is just a bunch of things that belong together, and an instance is one of these things.

like image 153
n. 1.8e9-where's-my-share m. Avatar answered Sep 18 '22 03:09

n. 1.8e9-where's-my-share m.


If you're interested in explanation of type classes and difference from Java interfaces you should read this post by <❤>. It also explains instances as well.

As for me, I look at instance as connection between data type and interface. data contains some information, class contains methods. data is about data (sorry for tautology) and class is about behavior. When you look at data type you don't see what you can do with it, instead you see what it stores. When you look at class you see what type should be able to do, you don't care what it stores internally. In real programming you actually care about details of implementations and how methods implemented using specific data. So instance just shows you relation between some data and some behavior — how this behavior implemented using given data.

In case you're interested more in model of type classes then read this blog post: http://www.haskellforall.com/2012/05/scrap-your-type-classes.html

You can look at instances as a values! It might blow your mind if you face such definition first time.

In some dependently type languages instances are really values that you can pass to other functions. Take a look at this question:

In Idris, is "Eq a" a type, and can I supply a value for it?

like image 24
Shersh Avatar answered Sep 22 '22 03:09

Shersh