Type Theory and Practical Application
description
Transcript of Type Theory and Practical Application
![Page 1: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/1.jpg)
Type TheoryAnd Practical Application
![Page 2: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/2.jpg)
The Institute for Advanced Study in Princeton, in my opinion, has ruined more good scientists than any institution has created, judged by what they did before they came and judged by what they did after. Not that they weren't good afterwards, but they were superb before they got there and were only good afterwards.
-- Richard Hamming
![Page 3: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/3.jpg)
Vladimir Voevodsky• Cutting-edge Math so complex probability
of mistake in any given proof very high
• Automated proof checking the present and future of Math
• Video: Univalent Foundations: New Foundations of Mathematicshttp://video.ias.edu/node/6395
![Page 4: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/4.jpg)
Frege Martin-Löf Voevodsky• Gottlob Frege, active 1879 – 1923
works in logic, foundations of arithmetic, and philosophy
• Per Martin-Löf An Intuitionistic Theory of Types, 1975
• Vladimir VoevodskyA very short note on the homotopy λ-calculus, 2006
• Institute for Advanced StudyHomotopy Type Theory: Univalent Foundation of Mathematics, 2013a.k.a. The HoTT Book
![Page 5: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/5.jpg)
“The relevance of theory to practical work is it shows us what is possible.”
![Page 6: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/6.jpg)
“A language is type safe if running a program in the language cannot result in … the underlying operating system catching the error, not the language itself. It is the untrapped errors that hackers take advantage of to break into computer systems. [I]t's a good idea to only run code that is in a type safe language!”
-- Jeremy Siek
http://siek.blogspot.com/2012/08/type-safety-in-five-easy-lemmas.html
![Page 7: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/7.jpg)
![Page 8: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/8.jpg)
Harper’s Computer Science Holy Trinity • Category Theory
• Type Theory
• Proof Theory
![Page 9: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/9.jpg)
Today’s State of the Art• Functional programming
• Type systems
largely missing dependent typesIdris is a notable exception
• Theorem provers (Coq, Agda, F7, F*)
still largely in the research stagetesting is poor man’s substitute for proofpractical security research (e.g. Heart bleed bug in Open SSL)proving assertions about language compiler
![Page 10: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/10.jpg)
Every-thing has a type• Objects are types
• Propositions are types
• Functions are types
• Proofs are types
![Page 11: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/11.jpg)
A theory of rules and no axioms(For our purposes. There are 2 axioms in higher homotopy type theory)
• “axioms” appear when we introduce concrete types
• The game is to manipulate concrete types and elements with the rules
• i.e. Type Theory provides an algebra over types
![Page 12: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/12.jpg)
a:A ”a is of type A ”• Type theory is a deductive system based on 2 forms of judgment
• ”a is of type A ” is the most basic form of judgment
• Construction of a proposition is a proof in intuitionistic mathematics
![Page 13: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/13.jpg)
Equality has special cases• Judgmental equality a :≡ b is a “witness”
• p : a =A b is a proposition (and of course a type)
• p-1 : b =A a is a different type (we will come back to this)
![Page 14: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/14.jpg)
Universes and families• Universe : a type whose elements are types
• U0 : U1 : U2 : …
• Every type belongs to some universe A : Ui
• A family of types: type B varying over a type A
B : A U indicates the universe U is the codomainnot to be confused with function signature
![Page 15: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/15.jpg)
0 : U the type that is not• False, ⊥, bottom
• You cannot construct it
• Hence it does not exist. Anything can derive from it.
ex falso quodlibit
![Page 16: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/16.jpg)
1 : U unit• True, ⊤, top, ()
• Always the same
one-valued logic
• C family of languages oddly names this type “void”
![Page 17: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/17.jpg)
2 : U boolean• 2-valued logic
highest n-valued fully consistent logic
• Can be derived as a special case of coproduct / summation type
![Page 18: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/18.jpg)
Infinite types• E.g. Natural numbers
which have a special place in practical application
• Defined recursively
![Page 19: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/19.jpg)
A B function type• Signature matters to define type
not the body of function
![Page 20: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/20.jpg)
A + B : U Coproduct type• a.k.a. Summation type
F# discriminated union
• To construct the function A + B C
requires the functions
A CB C
![Page 21: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/21.jpg)
Brief tangent on introducing types• Formation rule
e.g. you can form A B when A is a type and B is a type
• Introduction rule (a.k.a. constructor)e.g. functions have one constructor, λ-abstraction
• Elimination rulehow to use elements of type, e.g. function application
• Uniqueness principle (optional)unique maps into and out of type
![Page 22: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/22.jpg)
A x B : U Product type• Tuple, Cartesian product
![Page 23: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/23.jpg)
Lots of other types• E.g. Lists
which of course are defined recursively
• Not so important from the standpoint of theory
because we can construct them from what we have…
![Page 24: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/24.jpg)
EXCEPT…
![Page 25: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/25.jpg)
∏(x:A)B(x) Dependent Functions• Type of output depends on element of input
not the type of inputhence, not just a “parameterized function”
• Normal function is a special case
resulting type family a constant type
![Page 26: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/26.jpg)
∑(x:A)B(x) Dependent pair type• Second element depends on a dependently typed function on first
• Example:
1, 102, “I am a string”
![Page 27: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/27.jpg)
Logical operations, represented in types• True 1
• False 0
• A and B A x B
• A or B A + B
• If A then B A B
• A if and only if B (A B) x (B A)
• Not A A 0
![Page 28: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/28.jpg)
Predicate logic in type theory
• For all x
x∀ corresponds to ∏(x:A)P(x)
• There exists x
x∃ corresponds to ∑(x:A)P(x)
![Page 29: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/29.jpg)
IdA(a,b) Indentity function• Has special importance in type theory
IdA(a,b) type representing proposition of equalityp : a =A b a = b (shorthand)
refl : ∏(a:A)(a=Aa) reflexivity
• Homotopy Type Theory – there is a path between equals reflexivity is an infinitesimally short path from self to selfpaths are types (inverse path is not the same type)
![Page 30: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/30.jpg)
Fundamental “Mathy” Properties of Path• Reflexivity
constant path, e.g. identity
• Symmetry
both directions of a “proof”
• Transitivity
concatenation of paths
![Page 31: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/31.jpg)
Operations on paths• Identity (fundamental compositional element)
• Associativity
(p ∙ q) ∙ r = p ∙ (q ∙ r)
• Transitivity
p ∙ p-1 = Id(p)
![Page 32: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/32.jpg)
…but equalities are proofs• So paths are proofs p : x =A y
• The equivalence of 2 paths (proofs) is called a homotopy
• And equality of proofs can have proofs
p' : p =x=Ay q Paths between paths (i.e. equality of paths)
• And these are all types
• It’s turtles all the way down (equivalences of paths)
![Page 33: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/33.jpg)
The Fundamental Theorem of Type Theory• Induction principal for identity types• Similar to 2 step number theory induction (prove base case, prove a step)• Except there is only one step to prove• To prove a statement (or construction an object) that depends on path
identity, p: x = A y
it suffices to prove (or construct) in the special case where x and y are the same. p is thus the reflexivity element.
• “by induction it suffices to assume…”
![Page 34: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/34.jpg)
Connection to computability
• Constructive logic confines itself to that which can be effectively constructed by computation
• It does not include Law of Excluded Middle or Proof by Contradiction
• In general the tautologies of classical logic involving NOT cannot be constructed
![Page 35: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/35.jpg)
Coming up for air (gasp, gasp)
• This is where type checking comes in
• Construction is a proof of a type
• Type checking proves…
![Page 36: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/36.jpg)
CORRECTNESS!“The basic principal of the logic of type theory is that a proposition is not merely true or false, but rather can be seen as the collection of all possible witnesses of its truth.”
-- The HoTT Book, p. 53
![Page 37: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/37.jpg)
Theorem Provers• Agda
type checking
• Coq
proof strategies
![Page 38: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/38.jpg)
Idris“…because Idris has full dependent types, the total subset of the language can be used freely in the type system…”
• Idris: General Purpose Programming with Dependent Types https://www.youtube.com/watch?v=vkIlW797JN8
• https://github.com/idris-lang/Idris-dev/wiki/Manual
![Page 39: Type Theory and Practical Application](https://reader033.fdocuments.net/reader033/viewer/2022061104/53fe9dbb8d7f72835c8b457d/html5/thumbnails/39.jpg)
Further Study• http://siek.blogspot.com/2012/07/crash-course-on-notation-in-programming.html
• Simply-typed Lambda Calculus
• Hindley-Milner type inference
• Software FoundationsBenjamin C. Pierce, et al.
http://www.cis.upenn.edu/~bcpierce/sf/
• Types and Programming Languages (2002)Benjamin C. Pierce
• Oregon Programming Languages Summer School