a short overview of type theory
play

A short overview of Type Theory Yves Bertot June 2015 1 / 35 - PowerPoint PPT Presentation

A short overview of Type Theory Yves Bertot June 2015 1 / 35 -calculus A small-scale model of programming languages, Extremely simple Three constructs function descriptions, function calls, variables Only one input to


  1. A short overview of Type Theory Yves Bertot June 2015 1 / 35

  2. λ -calculus ◮ A small-scale model of programming languages, ◮ Extremely simple ◮ Three constructs ◮ function descriptions, function calls, variables ◮ Only one input to functions ◮ Only one outputto functions ◮ No complications ◮ Higher order: programs are values ◮ No control on memory usage ◮ several possible evaluation strategies 2 / 35

  3. Syntax of λ -calculus ◮ λ x . e is the function that maps x to e ◮ A function is applied to anargument by writing it on the left ◮ a e 1 e 2 = ( a e 1 ) e 2 , ◮ Several argument functions are a particular case ◮ if plus is the adding function and 1 and 2 numbers, then plus 1 2 is a number, and plus 1 is a function ◮ notation λ xy . e for λ x .λ y . e , ◮ numbers, pairs, and data lists can be modeled. 3 / 35

  4. Computing with the λ -calculus ◮ the value of ( λ x . e ) a is the same as the value of e where all occurrences of x are replaced by a , ◮ exemple: ( λ x . plus 1 x ) 2 = plus 1 2, ◮ beware of bound variables: they are the occurrences of e that should be replaced when computing x ( λ x . plus (( λ x . x ) 1) x ) 2 = ( λ x . plus 1 x ) 2 ( λ x . plus (( λ x . x ) 1) x ) 2 = plus (( λ x . x ) 1) 2 ◮ the occurrences of x in e of λ x . e are called the bound variables, ◮ the free occurrences of x in e are bound in λ x . e . 4 / 35

  5. recursion and infinite computation ◮ A recursive program can call itself ◮ Example x ! = 1 (si x = 0) ou bien x ! = x ∗ ( x − 1)! ◮ In other words, there exists a function F F such that f = F f ◮ For fact , fact = λ x . if x = 0 then 1 else x ∗ fact ( x − 1) fact is a fixed point of λ f x . if x = 0 then 1 else x ∗ f ( x − 1) ◮ In pur λ calculus, there exists Y T = ( λ xy . y ( xxy )) λ xy . y ( xxy ), so that Y F = F ( Y F ) ◮ Y can be used to construct recusive functions ◮ Be careful for the evaluation strategy in presence of Y T Y T F → F ( Y T F ) → F ( F ( Y T F )) → · · · 5 / 35

  6. A detailed explanation of fixed point computation Name θ = ( λ xy . y ( xxy )) θθ F = ( λ xy . y ( xxy )) θ F = ( λ y . y ( θθ y )) F = ( λ y . y ( θθ y )) F = F ( θθ F ) 6 / 35

  7. Usual theorems about λ -calculus Church Rosser property if t ∗ ∗ → t 1 and t → t 2 , then there exists t 3 ∗ ∗ such that t 1 → t 3 and t 2 → t 3 , → t ′ and t ′ can not be reduced ∗ Uniqueness of normal forms if t further, then t ′ is unique, R´ eduction standard the strategy “outermost-leftmost” reaches the normal form when it exists. ◮ Beware that some terms have no normal form ( λ x . xx ) λ x . xx → ( λ x . xx ) λ x . xx → · · · . 7 / 35

  8. Representing data-types ◮ Boolean: T is encoded as λ xy . x , F as λ xy . y , If as λ bxy . b x y , ◮ pairs P : λ xyz . z x y , and projections π i : λ p . p ( λ x 1 x 2 . x i ), ◮ Church encoding of numbers: n is represented by n � �� � λ fx . f ( · · · f x ) · · · ), ◮ addition: λ nm .λ fx . n f ( m f x ), multiplication: λ nm .λ f . n ( m f ), ◮ comparison to 0 (let’s call it Q ): λ n . n ( λ x . F ) T , ◮ predecessor: λ n .π 1 ( n ( λ p . P ( π 2 p )( add 1 ( π 2 p )))( P 0 0)), ◮ factorial: Y T λ fx . If ( Q x ) 1 ( mult x ( f ( pred x ))). 8 / 35

  9. Simply typed λ -calculus ◮ Annotate the functions with information about their input, ◮ provide documentation on programs, ◮ The consistency of programs can be verified without executing programs ◮ collections used in annotations are called types , ◮ notation: λ x : t . e , ◮ primitive types, int , bool , . . . but also function types t 1 → t 2 (convention: t 1 → ( t 2 → t 3 ) ≡ t 1 → t 2 → t 3 ), 9 / 35

  10. Data-types and primitive operations ◮ Typing can handle new data-types and primitive operations, ◮ Making sure that operations are applied to compatible data, ◮ For instance, we add pairs and projectors, � e 1 , e 2 � fst � e 1 , e 2 � � e 1 ◮ New type for pairs: t 1 ∗ t 2 , and for fst : t 1 ∗ t 2 → t 1 , ◮ Also possible to have native intgers 10 / 35

  11. Examples λ f : int → int → int .λ x : int . f x ( f x x ) well typed λ f : int → int .λ g : int → int . f ( g ( f x )) well typed if x : int , λ f : int .λ x : int → int . f x badly typed, f f badly typed, whatever the type of f . 11 / 35

  12. Type verification ◮ First stage: choose types for free variables ◮ verify that functions are applied to expressions of the right type, ◮ recursive traversal of terms ◮ An algorithm described using inference rules 12 / 35

  13. Typing rules Γ ⊢ x : t x � = y Γ , x : t ⊢ x : t (1) (2) Γ , y : t ′ ⊢ x : t Γ ⊢ e 1 : t 1 Γ ⊢ e 2 : t 2 (3) Γ ⊢ � e 1 , e 2 � : t 1 ∗ t 2 Γ , x : t ⊢ e : t ′ Γ ⊢ λ x : t . e : t → t ′ (4) Γ ⊢ e 1 : t → t ′ Γ ⊢ e 2 : t (5) Γ ⊢ e 1 e 2 : t ′ (6) (7) Γ ⊢ fst : t 1 ∗ t 2 → t 1 Γ ⊢ snd : t 1 ∗ t 2 → t 2 13 / 35

  14. Interpretation for logic ◮ Primitive types should be read as propositional variables, ◮ Read function types t 1 → t 2 as implications, ◮ Read pair types t 1 ∗ t 2 as conjunctions (“and”), ◮ The type of closed well-formed term is always a tautology, ◮ Curry-Howard isomorphism , types-as-propositions , ◮ For a type t , finding e with this type, this means proving that it is a tautoogy ◮ Beware, all tautologies are not provable ◮ example: (( A → B ) → A ) → A (Peirce’s formula). 14 / 35

  15. Peirce’s formula A B A → B ( A → B ) → A (( A → B ) → A ) → A T T T T T T F F T T F T T F T F F T F T 15 / 35

  16. Types and logic ◮ λ x : A ∗ B . � snd x , fst x � is a proof of A ∧ B ⇒ B ∧ A , ◮ Several proof systems are based on this principle Nuprl, Coq, Agda, Epigram, ◮ A type verification tool is a simple program ◮ Finding proofs is a difficult problem, ◮ Verifying proofs is easy, ◮ typed λ -calculus is also a small-scale model of a proof verification tool 16 / 35

  17. Typed reduction ◮ Same computation rule as for pure λ -calculus, ◮ We can add a computation rule for pairs and projections ◮ Standard theorems: subject reduction theorem types are preserved during computation, weak normalization Every typed term has a normal form, strong normalization Every reduction chain is finite 17 / 35

  18. A crossroad ◮ Toward programming languages ◮ Type inference ◮ Polymorphism ◮ General recursion ◮ Vers les syst` emes de preuve ◮ Universal quantification ◮ Proofs by induction ◮ Guaranteeing computation termination 18 / 35

  19. Structural recursion ◮ Avoid infinite computations, which are “indefined”, ◮ Prviding recursive computations only for some types, ◮ Generalize primitive recursion, ◮ Well-formed types represent provable formulas les termes bien typ´ es repr´ esentent des formules prouvables ◮ reference : G¨ odel’s system T (cf. Girard & Lafont & Taylor Proofs and types ), 19 / 35

  20. Structural recursion for iintegers ◮ A new type nat , ◮ Three new constants: ◮ 0 : nat (represents 0) ◮ S : nat → nat (represents successor ), ◮ rec nat ◮ rec nat is a recursor, it makes it possible to define recursive functions, ◮ Execution by pattern-matching ( rec nat v f is a recursive function) ◮ rec nat v f O = v ◮ rec nat v f ( S n ) = f n ( rec nat v f n ) ◮ Accordingly the type of rec nat is: ◮ rec nat : t → ( nat → t → t ) → nat → t , for any type t , ◮ Termination of computation is again guaranteed by typing 20 / 35

  21. Examples of recursive functions ◮ addition: plus ≡ λ xy . rec nat y ( λ nv . S v ) x , ◮ predecessor: pred ≡ rec nat 0 ( λ nv . n ), ◮ subtraction: minus ≡ λ xy . rec nat x ( λ nv . pred v ) y ), ◮ subtraction is also a comparison test, minus x y = 0 si x ≤ y , ◮ multiplication: λ xy . rec nat O ( λ nv . plus y v ), ◮ any function for which we can predict the number of recursive calls (for instance division) ◮ Even functions that are not recursive primitive: Ackermann. 21 / 35

  22. Example of binary trees (if time allows) ◮ Introduce a new type bin , ◮ Two constructors: ◮ leaf : bin , ◮ node : nat → bin → bin → bin , 22 / 35

  23. Example of binary trees (2) ◮ The recursor is defined accordingly to the constructors ◮ rec bin has three arguments (2+1), rec bin f 1 f 2 x , is well-typed if the type of f 1 (resp. f 2 ) is adapted to pattern-matching and recursion by leaf (resp. node ). ◮ f 1 is a value of type t , ◮ f 2 has (3+2) arguments, ◮ 3 is the number of arguments of node , ◮ 2 is the number of arguments of node in type bin , ◮ extra arguments are values for recursive calls rec bin f 1 f 2 ( node n t 1 t 2 ) = f 2 n t 1 ( rec bin f 1 f 2 t 1 ) t 2 ( rec bin f 1 f 2 t 2 ) 23 / 35

  24. Recursors and pattern-matching ◮ Ocaml : let rec nat fun v f x = match x with O -> v | S p -> f p (rec nat v f p) 24 / 35

  25. Dependent types: Type families ◮ Functions whose values are types, ◮ “Diagonal” function: each value is in a different type (determined by a type family) ◮ example: A i a sequence of types represented by a the function A : nat → Type , we can think of a function f such that: ◮ f 0 has type A 0, ◮ f 1 has type A 1, ◮ f 2 has type A 2, ◮ and so on, ◮ the type of f is noted f : Π x : nat . A x . 25 / 35

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend