> For the complete documentation index, see [llms.txt](https://harmeetsingh.gitbook.io/scala-type-system/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://harmeetsingh.gitbook.io/scala-type-system/phase-ii/chapter-12-higher-kinded-types.md).

# Chapter 12: Higher Kinded Types

## **Higher Kinded Types (HKT)?**

From the previous chapters, we get enough information regarding **types** and **Kinds**, in this chapter, we will be going to explore the next version of types or we can say that detailed abstraction of **Kinds** via **Higher Kinded Types** (**HKT**). As per our understanding, **HKT** provides a way to build functional data structures and I feel without **HKT** it is not possible to build a language with pure functional constructs(**this is my opinion**).&#x20;

In the previous chapter, we figured out, **Kinds** are the more abstract way to define types and we are using some characters or in a symbolic way using **\* (asterisk)** to explain the **Kind** of **types**. <br>

***Now the question is***, what if **parameterize constructor contains another parameterize constructor** similar to **function takes function as a parameter** in functional programming. In general, we know if the function accepts a function as an argument or returns a function we called **Higher-Order functions,** similarly, if parameterize constructor which has another parameter constructor like **`Abstract[F[T]]`** we called those types are **Higher Kinded Types**. As you can see, abstract type **F** itself contains parameterize constructor which is **T**. In this way, we can design complex **Higher Kinded Types**.&#x20;

**These terms function-within-function or Types-with-Types are sounds like inception**

![Inception](/files/-M7TDnZyc0ViP8oOwNkz)

For experimenting with **HKT**, let’s create our own custom type:&#x20;

```
scala> trait Abstract[F[T]]
```

Now we have a type of **`Abstract`** that contains parameterize constructor **`F[T]`**, which itself contains parameterize constructor `[T]`.<br>

Now, with the help of **Kind** command, lets trying to explain **HKT**:&#x20;

```
scala> :k -v Abstract
Abstract's kind is X[F[T]]
(* -> *) -> *
This is a type constructor that takes type constructor(s): a higher-kinded type.
```

According to the type, we have a slightly different symbolic explanation via `:kind` command. As we know, our type parameter **F** has a type constructor with **T**, which explains as `(* -> *)`. Here our inner constructors are wrapped via brackets and then `-> *` denotes to the outer part. This is the complete **reverse** representation of **Kinds**, which we saw in the previous chapter, where we evaluate **Kinds** **symbols** from **left to right,** but in the case of **HKT**, the **evaluation** looks from **right to left**. Where **left** defined **outer types** and **right** defined for **inner detailed types** as we discussed for **`Abstract`** type example.

Let’s take some advance example:&#x20;

```
scala> trait EitherHKT[L[T], R[P]]

scala> :k -v EitherHKT
EitherHKT's kind is X[F1[A1],F2[A2]]
(* -> *) -> (* -> *) -> *
This is a type constructor that takes type constructor(s): a higher-kinded type.
```

As you can see in this example, we have type **`EitherHKT`**, where the **left** and the **right** types itself contains type constructor and while we explain via **`:kind`** command, left side brackets shows inner types first then outer one. With the help of **`:kind`** command, we can experiment any types which are available in our existing functional libraries like **Cats**, **Scalaz**, **Zio**, or more.&#x20;

**For more detailed explanations please go through the below articles:**

* [**http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.135.575\&rep=rep1\&type=pdf**](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.135.575\&rep=rep1\&type=pdf)
* [**https://pdfs.semanticscholar.org/e881/79be2309476c0ff58215a57e5fd5c8413b1e.pdf**](https://pdfs.semanticscholar.org/e881/79be2309476c0ff58215a57e5fd5c8413b1e.pdf)
