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
- A type can mention a value, such as a vector of length n.
- Appending a vector of length n to one of length m has type length n + m.
- Out-of-bounds indexing becomes a compile-time error.
- Types act as precise specifications and proofs.
- 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
- Open the interactive lesson, with a mini quiz.
- Take the Languages & Compilers quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Languages & Compilers quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Dependent Types?
Where do people use Dependent Types?
What is the most common mistake with Dependent Types?
What should I learn before this topic?
How can I practice it?
Is Dependent Types hard to learn?
How long does it take to learn Dependent Types?
Which topics are related to Dependent Types?
What should I study after Dependent Types?
Related topics
- Hindley-Milner Inference (Languages & Compilers)
- Linear and Affine Types (Languages & Compilers)
- Ownership and Borrowing (Languages & Compilers)
- Garbage Collection Algorithms (Languages & Compilers)
- Data Types (Programming Fundamentals)
- Malware Types (Security & Cryptography)
- Data Types in SQL (Databases)