I'm trying to be an expert in several topics at the intersection of technology and finance.
So typeclasses are sort of the abstract base class, and instances are the concrete implementations...
I took notes pretty hard on this chapter, but it's been a while.
Looks like typeclasses also serve as mixins, so it's sort-of like a parent-child relationship, where parents are classes and children are instances. Man I don't like that usage of "instance" for what's semantically a subclass.
A typeclass defines a set of functions that a given member of that type must implement. Some type classes will provide default implementations for some of the functions which rely on some fundamental building blocks. For example, a Comp class can provide an implementation of (>=) which relies on an instance that provides an implementation for (<).
"A typeclass defines a set of functions that a given member of that type must implement" - that's an ABC. "Some type classes will provide default implementations for some of the functions which rely on some fundamental building blocks." - that makes the ABC also a mixin.
Ord is a good example to start understanding type classes. A member of this type class must minimally define (<=) and all the other comparisons are provided for free
oh...and (==), I think, since Ord a must also fit the constraint Eq a.
@AaronHall the way you stated it definitely makes a lot of sense
> The Ord class is used for totally ordered datatypes.
yup
With any Haskell you write, you are already using type classes, even if you don't know it. Some of the fundamental ones include Num, Eq, Ord, and Show.
These are all probably a good place to start to at least understand what type classes are and how they are used even if you don't dig into the details of the implementations.
hmm...I should find some time to work through that tutorial. It's got some really good stuff. And Haskell is one of at least a dozen programming topics I am interested in.
As Haskell is the quintessential functional language, I'm prioritizing it over other langs, and if you know me, you know I like to go deep.
my id: "ooh, venerable programming language" my superego: "no, you're studying Haskell, remember?" my ego: "yeah, but I've got to study Finance now..."
@AaronHall I'm a new-ish convert as well. It's really fun. Any part in particular you find difficult I could try to explain? I think I finally have IO down but I can't be certain :P
@AaronHall You can think of a monad as a type of object that supports two operations specifically: "bind" and "return". In Haskell, "bind" is represented by the >>= operator. List and IO are two examples of monads.
@AaronHall For instance, in python the "bind" operation would be coded as such: bind = lambda f, list: [item for sublist in map(f, list) for item in sublist]
yup, I don't really understand monads, so I'm not even attempting to explain them. I just know they are really cool, and I want to understand them better.
well I think it's obvious that linked lists aren't the most efficient way to represent lists in a lot of contexts, and having text strings be linkedlists of characters is actually not so good.
In category theory, a functor is a mapping from one set to another - while in Haskell, a functor is a type that can be mapped over. This seems to be a contradiction.
Haskell Functors are mappings from Hask (the category of Haskell types and functions) to Hask itself.
(That's why you'll sometimes hear it being said that Functors are Hask endofunctors.)
In a Functor instance, fmap maps the morphisms (i.e. Haskell functions), while the type constructor that gets the instance maps the objects (i.e. Haskell types).
So, while there is a little indirection involved, it is not wrong to say things like "Maybe is a functor". At worst, that is a mild metonym.
@AaronHall Not quite, I'd say. Any function in Haskell is a morphism in the Hask category. What a Functor does with it is translating it, giving you a different function as the result.
To say a function is a functor, you'd have to point out which would be the source and target categories of the functor.
@AaronHall Hask, the category in which Functors are endofunctors, has types as objects. Values lie, in a sense, one level of abstraction below.
It is possible to identify categories and functors both above and below it. When it comes to Functor, though, it is specifically a functor from Hask to Hask.
So we have Category of Hask, where:
Types are the objects of the category
Functions are the morphisms from object to object in the category.
Similarly for Functor we have:
a Type constructor as the mapping of objects from one category to another
fmap for the mapping of morphisms from one ca...