LearnViz CompSci
Home / Learn / Languages & Compilers / Dependent Types

Advanced Topic 45 of 50 Languages & Compilers

Dependent Types

Types that depend on values.

How it works

Dependent type systems let types depend on values, so a type can express properties such as a vector of exactly n elements or a sorted list, and a function's return type can depend on its argument. Because types can carry proofs, type checking becomes proof checking by the Curry-Howard correspondence, programs and proofs are the same objects. Languages include Agda, Idris, Coq, Lean and F-star, and limited forms appear in refinement types and TypeScript's literal types.

Step-by-step walkthrough

  1. A type can mention a value, such as a vector of length n.
  2. Appending a vector of length n to one of length m has type length n + m.
  3. Out-of-bounds indexing becomes a compile-time error.
  4. Types act as precise specifications and proofs.
  5. Languages: Idris, Agda and Coq.

Where it is used

Verified software such as the CompCert compiler, formalized mathematics, protocol and cryptography proofs and expressive APIs that rule out invalid states use dependent types.

Common pitfall

Writing proofs requires effort and expertise, and type checking can be slow or undecidable if arbitrary computation is allowed in types, so languages require termination. Specifications can be wrong even when proofs are correct. Start with small properties, such as lengths of vectors, and use proof automation tactics.

Where it fits in the course

Dependent Types is topic 45 of 50 in Languages & Compilers, rated Advanced. It follows Hindley-Milner Inference. The next topic is Linear and Affine Types.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Languages & Compilers quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Dependent Types?
Types that depend on values. It belongs to Languages & Compilers and is an advanced-level topic.
Where do people use Dependent Types?
Verified software such as the CompCert compiler, formalized mathematics, protocol and cryptography proofs and expressive APIs that rule out invalid states use dependent types.
What is the most common mistake with Dependent Types?
Writing proofs requires effort and expertise, and type checking can be slow or undecidable if arbitrary computation is allowed in types, so languages require termination.
What should I learn before this topic?
Start with Hindley-Milner Inference, the topic just before it in Languages & Compilers.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Languages & Compilers quiz.
Is Dependent Types hard to learn?
It is an Advanced topic. Expect to read the explanation more than once and to connect it with earlier topics. Working through a small example by hand helps most.
How long does it take to learn Dependent Types?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Dependent Types?
Hindley-Milner Inference, Linear and Affine Types, Ownership and Borrowing are closely connected. The full list is in the related topics section of this page.
What should I study after Dependent Types?
Continue with Linear and Affine Types, the next topic in Languages & Compilers.

Related topics