Ask HN: How to be fluent in functional language speak?
&gt; So how does a programmer from non-functional world become fluent in understanding sentence such as &quot;abstraction over type constructors&quot;?<p>Incoming essay...<p>Pure functional programming is fundamentally about software components called &quot;pure functions&quot;, or just &quot;functions&quot; for short. (I&#x27;m quoting the term because they&#x27;re not the same as what are called functions in other languages. I&#x27;d call those other things &quot;procedures&quot; instead, but alas.) Other programming communities rally around components like &quot;objects&quot;, and that&#x27;s fine. The important thing is to pick something to break your system down into, and here, we&#x27;re talking about &quot;functions&quot;.<p>&quot;Functions&quot; are simpler kinds of components than objects or procedures: the ways in which they behave are much more limited, so they&#x27;re easier to reason about. When a &quot;function&quot; is interacted with, it can&#x27;t perform any side effects, so interacting with the &quot;function&quot; multiple times gives the same behavior every time. Procedures can manipulate global state, and objects can manipulate internal state, so reasoning about them takes more effort. You have to keep track of time: what happened before this interaction?<p>&quot;Functions&quot; have two interfaces: an input side and an output side. When a &quot;function&quot; receives a value on the input side, it will always emit a value on the output side. We can wire &quot;functions&quot; together by connecting the input of one to the output of another. The result is a system with a single free input side and a single free output side -- that is, it&#x27;s also a &quot;function&quot;. This makes it very easy to wire up lots of &quot;functions&quot;.<p>Usually, a &quot;function&quot; has certain expectations of the input values that it receives, as well as some guarantees about the output values it emits. In a dynamically typed language, when those expectations are not met, a runtime error is emitted. This is fine -- it&#x27;s just one way to handle failed expectations. But sometimes we can formalize those expectations statically, and let the compiler check up-front whether those expectations are met. This does add some complexity: you might not be able to wire up two &quot;functions&quot; if one can&#x27;t satisfy the other&#x27;s expectations. Statically-typed programmers have decided that they&#x27;re willing to put up with that.<p>In a statically-typed world, the input and output interfaces of our &quot;function&quot; software components can be tagged with specifications. Functional languages are often judged by how expressive these specifications can be; a specification language like this is called a &quot;type system&quot;. You can have type systems for components that are not &quot;functions&quot;, but the rules of composition become more complex. (For &quot;objects&quot;, class systems are quite popular.)<p>Most type systems allow you to break down an interface specification into smaller pieces, which themselves are valid specifications. That means we now have two kinds of components in our system: &quot;functions&quot;, which exist at runtime, and types, which exist at compilation time. The rules for how types compose can be more complicated than those for &quot;functions&quot;, but we&#x27;re usually okay with that, as long as &quot;functions&quot; are kept simple.<p>Some type systems allow a value to satisfy multiple types. For example, class-based systems allow this via subtyping relations. Others might describe types as predicates (truth functions) on a pre-existing universe of values (e.g. TypeScript). Pure functional programming typically requires mutual exclusion: a value cannot be part of multiple types. We often say the the type <i>defines</i> its values because of this.<p>The low bar for a static type system is &quot;algebraic types&quot;. This means that we have two ways to compose types, conventionally called &quot;product&quot; and &quot;sum&quot;. Most type systems have products, but sums are historically rarer. Values of a product type `X * Y` look like `(x, y)`, and we can pull out either element of the pair. Values of a sum type `X + Y` look like either `Left x` or `Right y`, and we can ask which one it is and do something different depending on the result.<p>Java and C++ do not have sum types. You can approximate their specifications to some extent using the Visitor pattern or discriminated unions, but the implementations in these languages are verbose, and unpleasant to read and write. Rust and Haskell do have sum types, so specifications of that nature are used much more readily.<p>We can generalize further. We can think of the product and sum of types as &quot;type functions&quot;, or &quot;type constructors&quot;, that take a pair of types as input and produce a type as output. Many type systems allow you to define your own &quot;type functions&quot;. In Java, these are generic classes: &quot;List&lt;T&gt;&quot; is a type function from some type T to the type of lists containing that type. In C++, these are templates. But in most pure functional programming, static type functions look just like runtime value functions, since they behave just like them too. It&#x27;s just a matter of when they&#x27;re evaluated.<p>Once again, when you have functions, you often want to place demands on their inputs, and guarantees on their outputs. Type functions are no different. There are many possible ways to &quot;type types&quot;, as it were, but a popular approach is by using &quot;traits&quot; or &quot;typeclasses&quot;. Just as with types and values, a typeclass places requirements upon the types that inhabit it. (But a type can also satisfy multiple typeclasses.)<p>For instance, &quot;Monoid&quot; is a typeclass on types with two associated items: a &quot;zero&quot; value of that type, and a &quot;concat&quot; function that takes two values of the type and gives another value of that type. If you know that a type is a Monoid, then you know that these associated items are also available to you, regardless of what the actual type is. (For instance, you can write a &quot;concatAll&quot; function that iterates over a list of elements of a monoid and concatenates them together <i>regardless of what the monoid type actually is</i>.)<p>With typeclasses in mind, instead of taking a &quot;list of X&quot; or a &quot;set of X&quot;, you might abstract over all such things as &quot;collections of X&quot;. But you don&#x27;t want to say what a collection is for every possible X. We need to <i>abstract over type constructors</i>. A &quot;collection&quot; is any type such that, given a type X, you get a type with certain associated items expected of all collections. Java approximates this with subtyping.<p>&quot;Functor&quot; is, in some ways, a generalization of &quot;Collection&quot;. A type function that is a &quot;Functor&quot; is a type that, if you have a value of that type over Xs, and you have a function transforming Xs into Ys, then there must be a mechanism to get a value of that type over Ys instead. We call this mechanism &quot;map&quot;, and it can be instantiated over any collection. Got a list of X and a function from X to Y? We&#x27;ve got a way to get a list of Y. Got a set of X and a function from X to Y? We&#x27;ve got a way to get a set of Y.<p>Because &quot;set&quot; and &quot;list&quot; are type constructors, we say that we are <i>abstracting over type constructors</i> to get a single statement about all such things. &quot;For every Functor F, if I have an F containing Xs and a function transforming Xs to Ys, then I can get an F containing Ys.&quot;