Dependent Programming Paradigm
Dependent Programming Paradigm
Programming Paradigm
Introduction
A constant problem:
• Writing a correct computer program is hard and proving that a program is correct is even harder
• Dependent Types allow us to write programs and know they are correct before running them.
• dependent types: you can specify types that can check the value of your variables at compile time
Example:
Here is how you can declare a Vector that contains the values 1, 2, 3 :
val l1 = 1 :#: 2 :#: 3 :#: Vnil
This creates a variable l1 who’s type signature specifies not only that it’s a Vector that contains Ints, but also that it is a Vector of
length 3. The compiler can use this information to catch errors. Let’s use the vAdd method in Vector to perform a pairwise addition
between two Vectors:
val l1 = 1 :#: 2 :#: 3 :#: VNil
val l2 = 1 :#: 2 :#: 3 :#: VNil
val l3 = l1 vAdd l2
// Result: l3 = 2 :#: 4 :#: 6 :#: VNil
Introduction
The example above works fine because the type system knows both Vectors have length 3. However, if we tried to vAdd two Vectors
of different lengths, we’d get an error at compile time instead of having to wait until run time!
val l3 = l1 vAdd l2
What is type?
• In programming, types are a means of classifying values
• Exp: values 94, "thing", and [1,2,3,4,5] classified as an integer, a string, and a list of integers
• For a machine, types describe how bit patterns in memory are to be interpreted.
• For a compiler or interpreter, types help ensure that bit patterns are interpreted consistently when a program runs.
• For a programmer, types help name and organize concepts, aiding documentation and supporting interactive
editing environments.
Introduction
In computer science and logic, a dependent type is a type whose definition depends on a value.
It is an overlapping feature of type theory and type systems.
Used to encode logic's quantifiers like "for all" and "there exists".
Dependent types may help reduce bugs by enabling the programmer to assign types that further restrain the set of possible
implementations.
Quantifiers
Quantifiers
All man drink coffee.
Let a variable x which refers to a cat so all x can be represented in UOD as below: