The initialism "ADT" usually means *Abstract* Data Type, but GADT usually means Generalized *Algebraic* Data Type. Sums and products can be repeatedly combined into an arbitrarily large structures.Īlgebraic Data Type is not to be confused with *Abstract* Data Type, which (ironically) is its opposite, in some sense.
![haskell algebraic javascript emulator haskell algebraic javascript emulator](https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Qt_logo_2016.svg/1200px-Qt_logo_2016.svg.png)
In this case, the tags I and D are used (in constructors and pattern matching) to distinguish between the two alternatives. data Pair = I Int | D Double is just one number, either an Int or else a Double.The tag P is used (in constructors and pattern matching) to combine the contained values into a single structure that can be assigned to a variable. data Pair = P Int Double is a pair of numbers, an Int and a Double together."product" is combination ( A B, meaning A and B together)."sum" is alternation ( A | B, meaning A or B but not both).The "algebra" here is "sums" and "products":
![haskell algebraic javascript emulator haskell algebraic javascript emulator](https://i.ytimg.com/vi/EVBFoA2DmD4/maxresdefault.jpg)
"Algebraic" refers to the property that an Algebraic Data Type is created by "algebraic" operations. It might look something like this: class BoolTuple2 from ".This is a type where we specify the shape of each of the elements. We’re going to create a special type that can only contain two boolean values. To understand why, let’s write some code. Why? Well, you might be wondering: Why do we call them product types? It’s all about the possibilities. I mean that last sentence quite literally. You can shove an object into an array, and more arrays into that object. JavaScript doesn’t restrict what you can shove in an array or an object. We already discussed how this includes things like arrays, objects, maps, and sets. Product types allow you to have more than one value in a single structure, at the same time. We’ll start with product types, because they’re the most familiar. After taking a deep breath, you might wonder: What are sum and product types? And why are they called that? Many people growing up in the 80s probably have fond memory of.
#Haskell algebraic javascript emulator series
I had a lot of fun building this emulator and is planning to write a series of post documenting my experience and maybe help anyone who also want to write one. I’ve recently wrote a NES Emulator NesChan. Now, let’s assume you manage to refrain from physical violence. Writing your own NES emulator Part 1 - overview. That’s where algebraic data types really shine.” In functional programming we use sum types to model the business domain. “Oh, you’re so cute,” they say, “Those are all product types. Its type checker is able to infer pretty much anything. And after bracing yourself, you ask them “What’s so amusing?” Obviously, Haskell supports algebraic data types, but it also supports typeclasses. The one with a smug look and condescending smile on their face. But, you might also notice a functional programmer over there. You can put values of many types ‘inside’ them (so to speak). Arrays, Objects, Maps, WeakMaps, and Sets are all algebraic data types. If you’re a working programmer, you use them already every day.Īnd after reading this, you might think: ‘What’s so special about that? In JavaScript, that’s arrays and objects, right? Why does that even need a special name?’ And you’d be right. Or, even shorter, it’s a type made of other types. What are algebraic data types then? What’s an algebraic data type?Īn algebraic data type is a structured type that’s formed by composing other types. We’ve already talked about algebraic structures. Understanding the difference will help if you’re trying to learn functional programming.
![haskell algebraic javascript emulator haskell algebraic javascript emulator](https://www.haskell.org/communities/05-2015/html/HEE2014.png)
They both have ‘algebraic’ in the name, so it’s confusing. It’s like they ought to be the same thing. This is part four of a four-part series: Things I wish someone had explained to me about functional programming.Īlgebraic data types and algebraic data structures sound similar.