liveBook · Manning

Type constructor

As explained in Functional Programming in Scala, Second Edition

A type constructor is a type-level function that takes one or more types as parameters and produces a new parameterized type.

A type constructor takes type arguments and produces a parameterized type. `List`, `Option`, `Gen`, and `Parser` each take one result type: `List[Int]` and `Option[String]` are proper types, while `List` and `Option` are type constructors. Scala writes a one-argument constructor as `F[_]`; the underscore marks the missing type argument. section 10.6 section 9.1

This distinction matters when an interface must work for many container-like or computational contexts without choosing one representation. `Foldable[F[_]]` can describe folding a `List`, `IndexedSeq`, `LazyList`, `Tree`, or `Option`; `Functor[F[_]]` can describe mapping over `Gen`, `Parser`, `Option`, or `List`. The abstraction is over the shape `F`, while methods remain polymorphic in the contained type `A`. section 10.6 section 11.1

trait Functor[F[_]]:
  extension [A](fa: F[A])
    def map[B](f: A => B): F[B]

`F[_]` is chosen once for a functor instance, while `A` and `B` vary for each call to `map`. section 11.1

A constructor may take more than one type argument. `State[S, A]` has both a state type and a result type, but `Monad` expects a constructor with one open argument. Fixing `S` turns it into the required shape: `type IntState[A] = State[Int, A]`. This is partial type application: applying one type argument now and leaving another for later. section 11.5.2 section 11.2.1

Making a State constructor fit Monad

For any fixed state type `S`, `[x] =>> State[S, x]` is an anonymous one-argument type constructor. It accepts a result type `x` and returns `State[S, x]`, allowing a `Monad` instance for each choice of `S` rather than a separately named instance for every state type. section 11.5.2

Type constructors also name the context carried by an effectful value. `Par`, `Option`, `List`, `Parser`, and `Gen` augment ordinary values with capabilities such as parallel computation or possible failure. Applicative and monad interfaces organize how values in such contexts are combined: applicatives combine context-free computations, whereas monads allow earlier results to choose later computations. section 12.9 section 12.3.1

The same parameterization can separate a program description from its execution mechanism. `Free[F, A]` stores suspended operations of an arbitrary `F[A]`; choosing `Function0` yields `TailRec`, while choosing `Par` yields `Async`. A type constructor can therefore stand for the instruction language used inside a larger program type. section 13.4

Useful questions when encountering a type constructor:

  • Which arguments are already fixed, and which remain open for an abstraction such as `Functor`, `Monad`, or `Foldable`? section 10.6 section 11.5.2
  • What capability does the resulting context add to its contained values, and can computations be combined independently or depend on previous results? section 12.9 section 12.3.1
  • Can an operation be defined once for the constructor’s interface instead of repeated for each concrete type? section 11.1 section 11.2.1

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines unary constructors, then works through partial application and type lambdas for two-argument State before applying constructors in abstractions.
Examples
Uses List, Option, Gen, Parser, State, Par, Function0, Async, TailRec, and Free as concrete constructor examples.
What it assumes
Introduces `F[_]` as a constructor notation while deferring incidental Parser syntax details.
What you can do after
Apply it via Scala type aliases and type lambdas to supply one-argument constructors required by Monad instances.
How it connects
Connects type constructors to Foldable, Functor, Monad, Applicative, State, Free, Parser, and effects.

Also explained in

Compare how 2 books explain type constructor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.