comparative programming languages cm20253value (agda, coq, idris, etc.) for example intvec as a type...

42
Types Dependent Types Next, there are dependent types, where a type depends on a value (Agda, Coq, Idris, etc.)

Upload: others

Post on 01-Aug-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

Next, there are dependent types, where a type depends on avalue (Agda, Coq, Idris, etc.)

For example IntVec<n> as a type of vectors of int of fixedlength n

Here, IntVec<3> is a different type to IntVec<7>

After all, it doesn’t make sense to pass a vector of length 3 to afunction that expects a vector of length 7

Advanced Exercise. But what about passing a vector of length7 to a function that expects a vector of length 3?

Page 2: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

Next, there are dependent types, where a type depends on avalue (Agda, Coq, Idris, etc.)

For example IntVec<n> as a type of vectors of int of fixedlength n

Here, IntVec<3> is a different type to IntVec<7>

After all, it doesn’t make sense to pass a vector of length 3 to afunction that expects a vector of length 7

Advanced Exercise. But what about passing a vector of length7 to a function that expects a vector of length 3?

Page 3: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

Next, there are dependent types, where a type depends on avalue (Agda, Coq, Idris, etc.)

For example IntVec<n> as a type of vectors of int of fixedlength n

Here, IntVec<3> is a different type to IntVec<7>

After all, it doesn’t make sense to pass a vector of length 3 to afunction that expects a vector of length 7

Advanced Exercise. But what about passing a vector of length7 to a function that expects a vector of length 3?

Page 4: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

Next, there are dependent types, where a type depends on avalue (Agda, Coq, Idris, etc.)

For example IntVec<n> as a type of vectors of int of fixedlength n

Here, IntVec<3> is a different type to IntVec<7>

After all, it doesn’t make sense to pass a vector of length 3 to afunction that expects a vector of length 7

Advanced Exercise. But what about passing a vector of length7 to a function that expects a vector of length 3?

Page 5: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

Next, there are dependent types, where a type depends on avalue (Agda, Coq, Idris, etc.)

For example IntVec<n> as a type of vectors of int of fixedlength n

Here, IntVec<3> is a different type to IntVec<7>

After all, it doesn’t make sense to pass a vector of length 3 to afunction that expects a vector of length 7

Advanced Exercise. But what about passing a vector of length7 to a function that expects a vector of length 3?

Page 6: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

This would allow a compiler to typecheck code like

// pair of vectors -> vector of pairs

fn pairvec<n: int>(v: DoubleVec<n>, w: DoubleVec<n>) -> ...

...

let pv = pairvec(a, b);

The compiler could check that the two vectors a and b havebeen declared with the same length

And possibly optimise the generated code as it then doesn’tneed a runtime length check, for example

Page 7: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

This would allow a compiler to typecheck code like

// pair of vectors -> vector of pairs

fn pairvec<n: int>(v: DoubleVec<n>, w: DoubleVec<n>) -> ...

...

let pv = pairvec(a, b);

The compiler could check that the two vectors a and b havebeen declared with the same length

And possibly optimise the generated code as it then doesn’tneed a runtime length check, for example

Page 8: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

This would allow a compiler to typecheck code like

// pair of vectors -> vector of pairs

fn pairvec<n: int>(v: DoubleVec<n>, w: DoubleVec<n>) -> ...

...

let pv = pairvec(a, b);

The compiler could check that the two vectors a and b havebeen declared with the same length

And possibly optimise the generated code as it then doesn’tneed a runtime length check, for example

Page 9: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

It may be that the lengths of a and b can only be determined atruntime

In that case, the compiler would try to prove that the lengths ofa and b must be the same

If it couldn’t prove that, it would raise an error and refuse tocompile the code: a type error

Page 10: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

It may be that the lengths of a and b can only be determined atruntime

In that case, the compiler would try to prove that the lengths ofa and b must be the same

If it couldn’t prove that, it would raise an error and refuse tocompile the code: a type error

Page 11: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

It may be that the lengths of a and b can only be determined atruntime

In that case, the compiler would try to prove that the lengths ofa and b must be the same

If it couldn’t prove that, it would raise an error and refuse tocompile the code: a type error

Page 12: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

More generally, the type can depend on any property

• Vectors of even length• Vectors whose elements are ordered (e.g., increasing)

according to a given function• Odd integers that are bounded by n• Pairs of integers where the first element is negative and

the second positive• And so on

Page 13: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

More generally, the type can depend on any property

• Vectors of even length

• Vectors whose elements are ordered (e.g., increasing)according to a given function

• Odd integers that are bounded by n• Pairs of integers where the first element is negative and

the second positive• And so on

Page 14: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

More generally, the type can depend on any property

• Vectors of even length• Vectors whose elements are ordered (e.g., increasing)

according to a given function

• Odd integers that are bounded by n• Pairs of integers where the first element is negative and

the second positive• And so on

Page 15: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

More generally, the type can depend on any property

• Vectors of even length• Vectors whose elements are ordered (e.g., increasing)

according to a given function• Odd integers that are bounded by n

• Pairs of integers where the first element is negative andthe second positive

• And so on

Page 16: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

More generally, the type can depend on any property

• Vectors of even length• Vectors whose elements are ordered (e.g., increasing)

according to a given function• Odd integers that are bounded by n• Pairs of integers where the first element is negative and

the second positive

• And so on

Page 17: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

More generally, the type can depend on any property

• Vectors of even length• Vectors whose elements are ordered (e.g., increasing)

according to a given function• Odd integers that are bounded by n• Pairs of integers where the first element is negative and

the second positive• And so on

Page 18: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

Dependent types are very hard to support in full generality, asthey can require the compiler to run arbitrary code to check thetype

E.g., vectors of prime number length

Exercise. Read about how C++ uses templates to support aform of dependent types

Exercise. Read about how Rust supports a very simple kind ofdependent types

Page 19: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

Dependent types are very hard to support in full generality, asthey can require the compiler to run arbitrary code to check thetype

E.g., vectors of prime number length

Exercise. Read about how C++ uses templates to support aform of dependent types

Exercise. Read about how Rust supports a very simple kind ofdependent types

Page 20: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

TypesDependent Types

Dependent types are very hard to support in full generality, asthey can require the compiler to run arbitrary code to check thetype

E.g., vectors of prime number length

Exercise. Read about how C++ uses templates to support aform of dependent types

Exercise. Read about how Rust supports a very simple kind ofdependent types

Page 21: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types

Such advanced types (higher kinds and ranks, dependent) areusually only seen in more experimental or research-drivenlanguages (Agda, Idris, some support in Haskell)

They are harder for the programmer to understand, and somehave theoretical problems, such as undecidable type inference

Simple fixed cases (like vector or structure constructors) arewidespread, but more programmatic use of higher level types isstill in the future for general-purpose languages

Though we are starting to see moderate support in languageslike C++ (templates), F# and Scala

And there is a history of “experimental” features eventuallyfinding their way into mainstream languages (e.g., classes)

Page 22: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types

Such advanced types (higher kinds and ranks, dependent) areusually only seen in more experimental or research-drivenlanguages (Agda, Idris, some support in Haskell)

They are harder for the programmer to understand, and somehave theoretical problems, such as undecidable type inference

Simple fixed cases (like vector or structure constructors) arewidespread, but more programmatic use of higher level types isstill in the future for general-purpose languages

Though we are starting to see moderate support in languageslike C++ (templates), F# and Scala

And there is a history of “experimental” features eventuallyfinding their way into mainstream languages (e.g., classes)

Page 23: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types

Such advanced types (higher kinds and ranks, dependent) areusually only seen in more experimental or research-drivenlanguages (Agda, Idris, some support in Haskell)

They are harder for the programmer to understand, and somehave theoretical problems, such as undecidable type inference

Simple fixed cases (like vector or structure constructors) arewidespread, but more programmatic use of higher level types isstill in the future for general-purpose languages

Though we are starting to see moderate support in languageslike C++ (templates), F# and Scala

And there is a history of “experimental” features eventuallyfinding their way into mainstream languages (e.g., classes)

Page 24: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types

Such advanced types (higher kinds and ranks, dependent) areusually only seen in more experimental or research-drivenlanguages (Agda, Idris, some support in Haskell)

They are harder for the programmer to understand, and somehave theoretical problems, such as undecidable type inference

Simple fixed cases (like vector or structure constructors) arewidespread, but more programmatic use of higher level types isstill in the future for general-purpose languages

Though we are starting to see moderate support in languageslike C++ (templates), F# and Scala

And there is a history of “experimental” features eventuallyfinding their way into mainstream languages (e.g., classes)

Page 25: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types

Such advanced types (higher kinds and ranks, dependent) areusually only seen in more experimental or research-drivenlanguages (Agda, Idris, some support in Haskell)

They are harder for the programmer to understand, and somehave theoretical problems, such as undecidable type inference

Simple fixed cases (like vector or structure constructors) arewidespread, but more programmatic use of higher level types isstill in the future for general-purpose languages

Though we are starting to see moderate support in languageslike C++ (templates), F# and Scala

And there is a history of “experimental” features eventuallyfinding their way into mainstream languages (e.g., classes)

Page 26: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types Conclusion

Exercise. Read about sum and product types (see union andstruct in C and C++; or enum and struct in Rust)

Exercise. Function types can be constructed in somelanguages (using notation like lambda or -> or Fn). Read aboutthese

Exercise. Then find out about covariance and contravariance

Exercise. Read about algebraic data types

Exercise. Read about the Curry-Howard Correspondence

Page 27: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types Conclusion

These days types are considered to be an essential part of alanguage

And so appear in many different kinds of ways

They are intended to reduce the opportunity for errors, or finderrors more quickly, or enable code to be more expressive

Even in early untyped languages there was a recommendationthat the intended type of a value be reflected in the name of avariable

iAge, fSalary. See Hungarian notation

Page 28: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types Conclusion

These days types are considered to be an essential part of alanguage

And so appear in many different kinds of ways

They are intended to reduce the opportunity for errors, or finderrors more quickly, or enable code to be more expressive

Even in early untyped languages there was a recommendationthat the intended type of a value be reflected in the name of avariable

iAge, fSalary. See Hungarian notation

Page 29: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types Conclusion

These days types are considered to be an essential part of alanguage

And so appear in many different kinds of ways

They are intended to reduce the opportunity for errors, or finderrors more quickly, or enable code to be more expressive

Even in early untyped languages there was a recommendationthat the intended type of a value be reflected in the name of avariable

iAge, fSalary. See Hungarian notation

Page 30: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types Conclusion

These days types are considered to be an essential part of alanguage

And so appear in many different kinds of ways

They are intended to reduce the opportunity for errors, or finderrors more quickly, or enable code to be more expressive

Even in early untyped languages there was a recommendationthat the intended type of a value be reflected in the name of avariable

iAge, fSalary. See Hungarian notation

Page 31: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types Conclusion

These days types are considered to be an essential part of alanguage

And so appear in many different kinds of ways

They are intended to reduce the opportunity for errors, or finderrors more quickly, or enable code to be more expressive

Even in early untyped languages there was a recommendationthat the intended type of a value be reflected in the name of avariable

iAge, fSalary. See Hungarian notation

Page 32: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types ConclusionThere are many places to check for errors

• compile time: mostly type errors• run time: e.g., division by 0, null pointers, buffer overruns

(accessing beyond the ends of a vector)

Rust has pointers, but its type system is so strong it can avoidnull pointers at compile time and so can avoid this kind of runtime error

Haskell has no (explicit) pointers, and avoids these errors, too

Java has no explicit pointers, but still manages to get nullpointer exceptions

I don’t think there could reasonably be a language that checksfor 0 division at compile time!

Page 33: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types ConclusionThere are many places to check for errors

• compile time: mostly type errors

• run time: e.g., division by 0, null pointers, buffer overruns(accessing beyond the ends of a vector)

Rust has pointers, but its type system is so strong it can avoidnull pointers at compile time and so can avoid this kind of runtime error

Haskell has no (explicit) pointers, and avoids these errors, too

Java has no explicit pointers, but still manages to get nullpointer exceptions

I don’t think there could reasonably be a language that checksfor 0 division at compile time!

Page 34: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types ConclusionThere are many places to check for errors

• compile time: mostly type errors• run time: e.g., division by 0, null pointers, buffer overruns

(accessing beyond the ends of a vector)

Rust has pointers, but its type system is so strong it can avoidnull pointers at compile time and so can avoid this kind of runtime error

Haskell has no (explicit) pointers, and avoids these errors, too

Java has no explicit pointers, but still manages to get nullpointer exceptions

I don’t think there could reasonably be a language that checksfor 0 division at compile time!

Page 35: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types ConclusionThere are many places to check for errors

• compile time: mostly type errors• run time: e.g., division by 0, null pointers, buffer overruns

(accessing beyond the ends of a vector)

Rust has pointers, but its type system is so strong it can avoidnull pointers at compile time and so can avoid this kind of runtime error

Haskell has no (explicit) pointers, and avoids these errors, too

Java has no explicit pointers, but still manages to get nullpointer exceptions

I don’t think there could reasonably be a language that checksfor 0 division at compile time!

Page 36: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types ConclusionThere are many places to check for errors

• compile time: mostly type errors• run time: e.g., division by 0, null pointers, buffer overruns

(accessing beyond the ends of a vector)

Rust has pointers, but its type system is so strong it can avoidnull pointers at compile time and so can avoid this kind of runtime error

Haskell has no (explicit) pointers, and avoids these errors, too

Java has no explicit pointers, but still manages to get nullpointer exceptions

I don’t think there could reasonably be a language that checksfor 0 division at compile time!

Page 37: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types ConclusionThere are many places to check for errors

• compile time: mostly type errors• run time: e.g., division by 0, null pointers, buffer overruns

(accessing beyond the ends of a vector)

Rust has pointers, but its type system is so strong it can avoidnull pointers at compile time and so can avoid this kind of runtime error

Haskell has no (explicit) pointers, and avoids these errors, too

Java has no explicit pointers, but still manages to get nullpointer exceptions

I don’t think there could reasonably be a language that checksfor 0 division at compile time!

Page 38: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types ConclusionThere are many places to check for errors

• compile time: mostly type errors• run time: e.g., division by 0, null pointers, buffer overruns

(accessing beyond the ends of a vector)

Rust has pointers, but its type system is so strong it can avoidnull pointers at compile time and so can avoid this kind of runtime error

Haskell has no (explicit) pointers, and avoids these errors, too

Java has no explicit pointers, but still manages to get nullpointer exceptions

I don’t think there could reasonably be a language that checksfor 0 division at compile time!

Page 39: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types Conclusion

There are other places for errors we often forget about

• link time, load time: making sure libraries are present,consistent and correctly called

• coding time: getting it right in the first place

Page 40: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types Conclusion

There are other places for errors we often forget about

• link time, load time: making sure libraries are present,consistent and correctly called

• coding time: getting it right in the first place

Page 41: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types Conclusion

There are other places for errors we often forget about

• link time, load time: making sure libraries are present,consistent and correctly called

• coding time: getting it right in the first place

Page 42: Comparative Programming Languages CM20253value (Agda, Coq, Idris, etc.) For example IntVec as a type of vectors of int of fixed length n Here, IntVec is a different

Types Conclusion

“Strong types are for weak minds”Anon.