3 ms·
Introduction to Haskell Typeclasses
- _query 5y agoIf this post got you interested in learning more about Haskell and you want to see type classes in some real code, a great starting point for Haskell is IHP :) IHP is a new Haskell framework with a focus on actual building applications. Imagine the productiveness of rails combined with the typesafety of Haskell. It's now already the biggest Haskell web framework, we just hit 3200 GitHub stars. I belive Haskell can reach a lot more than it's currently doing if we get things easy to use. If you want to build a web app with Haskell and play with type classes check it out: https://ihp.digitallyinduced.com/ https://ihp.digitallyinduced.com/ GitHub: https://github.com/digitallyinduced/ihp https://github.com/digitallyinduced/ihp
- deleted 5y ago[deleted]
- okkdev 5y agoI'm waiting for the new release because of the M1 fix that got merged...
- fn-mote 5y agoI'm not sure what this adds over LYaH. Possibly it is useful because you have to survive until LYaH Chapter 8 for "Typeclasses 102"? [1] The basic post was fine I didn't see any lightbulb moments. It works through an example showing how to add your own data type to Eq and Ord. The later post, "What's That Typeclass: Monoid" could have been interesting. To me it went to mathematically-abstract. Like every other Haskell post. Example quote: "...because a semigroup is a monoid without an identity element..." To be fair, they have defind SemiGroup, but really -- who is the audience for this? This is good for a laugh though: > Why do you need monoids? > In general, monoids are not very advanced, interesting, or cool. [1] http://learnyouahaskell.com/making-our-own-types-and-typeclasses#typeclasses-102 http://learnyouahaskell.com/making-our-own-types-and-typecla...
- aroccoli 5y agoThank you for the feedback! To compare with Learn You a Haskell, I wanted to provide a different option / way of grasping the concept with, hopefully, examples that are relatable to some people more than LYAH. Having a few different explanations of the same thing is not a bad thing in my eyes since different things click with different people. Regarding Monoid article -- it is sad to hear that since my goal was to provide something that would be unlike the "mathematically-abstract" posts. It has plenty of examples and a rather long exposition, and I am not sure how much more it is possible to improve it without losing any meaning for the word "monoid". Semigroups and monoids is a rather awkward moment there, I didn't manage to find a perfect way to cover that. But, to be honest, I did mention what a monoid is plenty of times, and I did mention what an identity element is several times as well. So a careful reader should be able to imagine what a semigroup is if necessary. Anyway, thank you for reading the articles and pointing my attention towards that. I will for sure look at that part again. :) -- Author
- m1kal 5y agoI liked the text about typeclasses. It is a standalone piece, "Typeclasses 102" in LYaH is a part of a chapter of a bigger piece. I think you achieved your goal - your Pokemon example is simple to grasp and shows how to derive type classes and implement functions by hand:) (not commenting about monoid/subgroup part)
- dmkolobov 5y agoRespectfully disagree about monoids! Plenty of cool stuff there, here’s one of my favorites, a diagramming library built around monoids: https://repository.upenn.edu/cgi/viewcontent.cgi?article=1773&context=cis_papers https://repository.upenn.edu/cgi/viewcontent.cgi?article=177...
- mhitza 5y agoMonoids are pretty cool in theory, and at the same time, the tool I use least of when working with Haskell. This page [0] really shows how elegant Monoid comprehension would be when implementing SQL-like operations within pure Haskell code. (Goes over desugaring, implementation details and comparison to list/monad comprehensions, just jump down to examples) [0] https://lptk.github.io/programming/2018/10/04/comprehending-monoids-with-class.html https://lptk.github.io/programming/2018/10/04/comprehending-...
- prezjordan 5y agoI really enjoyed this, thanks! I've been writing OCaml a lot and wish it had the expressiveness of `instance Eq Pokemon where`.
- somewhereoutth 5y agoWould it be fair to say that typeclasses are a formalisation of ad-hoc polymorphism? With all the good and bad things that come with that.
- dboreham 5y agoPretty much.
- mumblemumble 5y agoI may be way off on this, but I like to think of them as a bridge between ad-hoc and parametric polymorphism. Or maybe an ad hoc wolf in parametric sheep's clothing. Functions and types that refer to typeclasses are parametrically polymorphic insofar as they don't refer to a specific implementation of that type. This is quite different from run-of-the-mill function overloading, where you must supply separate implementations for every set of types you want to support. But, unlike how parametric polymorphism works in OOP, late binding is not an option. At the use site, the typeclass implementation you want to use must be statically resolvable. In that respect, it does behave like function overloading.
- rowanG077 5y agoMost of the pitfalls of ad-hoc polymorphism aren't possible with type classes. Unless you use some dangerous annotations.
- da39a3ee 5y agoWouldn’t it be helpful to use the word “interface” in the article (and other docs) since that’s basically what they are known as in other languages (or “trait” in Rust and Scala). I’m sure we can have a really long thread on the difference between these concepts in different languages but wouldn’t it help beginners to be told that those are more or less the same thing?
- HelloNurse 5y agoVery well written, but an introductory tutorial shouldn't ignore fundamental difficulties like multiple conflicting instances of the same typeclass for the same type. Your example would be a good fit, since Pokémon can be "naturally" ordered by number, by name, by which one has more types, and in infinite other ways.