Tomáš Petříček, 309 (3rd floor)
petricek@d3s.mff.cuni.cz
https://tomasp.net | @tomaspetricek
Lectures: Monday 12:20, S7
https://d3s.mff.cuni.cz/teaching/nprg075
ML brings together data types, abstract types and checking
End of the history?
ML brings together data types, abstract types and checking
End of the history?
Developments in new directions in engineering and mathematics!
Are these two type
definitions equivalent?
type Contact =
| Email of string
| Phone of digits
| Both of string * digits
type Customer =
{ Name : string
Contact : Contact }
Can one represent some
values the other cannot?
type Option<'T> =
| Some of 'T
| None
type Customer =
{ Name : string
Phone : Option<digits>
Email : Option<string> }
A->B
behave as \(B^A\)\(Contact = (Phone * Email) + Email + Phone\)
\(Customer1\)
\(\quad =Name * Contact\)
\(\quad {\color{green}=Name * ((Phone * Email) + Email + Phone)}\)
\(Customer2\)
\(\quad = Name * (Phone+1) * (Email+1)\)
\(\quad = Name * ((Phone+1) * Email + (Phone+1) * 1)\)
\(\quad = {\color{blue}Name * ((Phone * Email) + Email + Phone }{\color{red}\;+\;1})\)
Types in programming are propositions in logic!
Programs are proofs!
Not that surprising..
Hard work to make it fit
Same origins in foundations of mathematics
Function \(A\rightarrow B\) corresponds to implication
Product \(A\times B\) corresponds to conjunction \(A \wedge B\)
Union \(A + B\) corresponds to disjunction \(A \vee B\)
A well-typed program of type \(A\) is a proof of \(A\)
Write program to show that a property holds!
Alf, Coq, Agda & more
Construct proofs by interactively creating programs
Show resulting program (Agda) or list of interactions (Coq)
Programs can run too
Proposition: \(((A \rightarrow B) \wedge (B \rightarrow C)) \rightarrow (A \rightarrow C)\)
Program as proof: \(\lambda (f, g). \lambda a.g (f a)\)
Proposition: \(A \wedge (B \vee C) \rightarrow (A \wedge B) \vee (A \wedge C)\)
Program as proof: \(\lambda (a, \textbf{inl}~b). \textbf{inl}~(a, b)\)
\(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\; \lambda (a, \textbf{inr}~c). \textbf{inr}~(a, c)\)
Variable must be
used exactly once!
Resource usage in programming!
Avoid aliasing, efficient memory management
Generalizations to control sharing
(x:A) -> B(x)
(x:A) * B(x)
Vector of a known length Vec (n:int) A
Other properties, like sortedness of a list
Dependent pair and function
vectWithLength : (n:int) * Vec n string
initVector : (x:int) -> (v:A) -> Vec x A
Type error on a train!
More useful when external service changes format
Well-typed programs
do not go wrong?
Except when the world breaks assumptions
about the schema
Unsound because of 'any', covariance, unchecked imports
Checking works
well enough!
More reliable editor auto-completion
\[\frac {\Gamma \vdash e : {\color{red}[f_1:\tau_1, \ldots, f_n:\tau_n]}} {\Gamma \vdash e.\text{drop}~f_i : {\color{cc} [f_1:\tau_1, \ldots, f_{i-1}:\tau_{i-1}, f_{i+1}:\tau_{i+1}, \ldots, f_n:\tau_n]}}\]
\[\frac {\Gamma \vdash e : {\color{blue} C_1}} {\Gamma \vdash e.\text{drop}~f_i : {\color{blue} C_2}} \qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\quad\]
\[\begin{array}{l} {fields({\color{blue} C_1}) = {\color{blue} \{f_1:\tau_1, \ldots, f_n:\tau_n\}}}\\ {fields({\color{blue} C_2}) = {\color{blue} \{f_1:\tau_1, \ldots, f_{i-1}:\tau_{i-1}, f_{i+1}:\tau_{i+1}, \ldots, f_n:\tau_n\}}} \end{array}\]
Complementary ways of designing & evaluating
Import ideas using maths, prove them correct
Adapt ideas for engineering purpose, show they work
When Technology Became Language: The Origins of the Linguistic Conception of Computer Programming
From davidnofre.com or direct link
Unexpected perspectives on types
Tomáš Petříček, 309 (3rd floor)
petricek@d3s.mff.cuni.cz
https://tomasp.net | @tomaspetricek
https://d3s.mff.cuni.cz/teaching/nprg075
Curry-Howard and dependent types
Type providers & related
Algebraic types