Implementing Lambda Calculi in Prolog

Total Page:16

File Type:pdf, Size:1020Kb

Implementing Lambda Calculi in Prolog Resources: Some of the slides of this lecture were derived from [Järvi], with permission of the original author, by copy x = 1 & paste or by selection, annotation, or rewording. [Järvi] is let x = 1 in ... in turn based on [Pierce] as the underlying textbook. x(1). !x(1) x.set(1) Programming Paradigms and Formal Semantics Implementing Lambda Calculi in Prolog Ralf Lämmel <1h [Järvi] Slides by J. Järvi: “Programming Languages”, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002 Table of contents • Untyped NB Objective: compare grammar- and rule-based formalization • Typed NB with the corresponding Prolog encodings / implementation. • Untyped lambda calculus • Applied, untyped lambda calculus • Applied, typed lambda calculus © Ralf Lämmel, 2009-2011 unless noted otherwise 192 Untyped NB © Ralf Lämmel, 2009-2011 unless noted otherwise 193 This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU. SyntaxMore than one syntactic of category NB Language NB t ::= terms: v value if t1 then t2 else t3 conditional succ t successor pred t predecessor iszero t test for zero v ::= values: true constant true false constant false nv numeric value nv ::= numeric values: 0 zero value succ nv successor value © Ralf Lämmel, 2009-2011 unless noted otherwise 194 54 / 58 Prolog Syntax of NB term(V) :- value(V). term(if(T1,T2,T3)) :- term(T1), term(T2), term(T3). term(succ(T)) :- term(T). term(pred(T)) :- term(T). term(iszero(T)) :- term(T). We are faithful to the distinction of the value(true). syntactical categories. value(false). value(NV) :- nvalue(NV). nvalue(zero). nvalue(succ(NV)) :- nvalue(NV). © Ralf Lämmel, 2009-2011 unless noted otherwise 195 Prolog NB: sample terms supposed to evaluate to 0 if(iszero(pred(succ(zero))),zero,succ(succ(zero))). supposed to evaluate to false if(iszero(zero),iszero(succ(zero)),true). © Ralf Lämmel, 2009-2011 unless noted otherwise 196 This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU. Typ es for EvaluationNB rules of NB Reminder evaluation rules NB (SOS) E-Iszero E-IszeroZero t t → iszero 0 true iszero t iszero t → → E-Succ E-Pred E-IszeroSucc t t t t iszero (succ nv) false → → → succ t succ t pred t pred t → → E-PredZero E-PredSucc E-IfTrue pred 0 0 pred (succ nv) nv if true then t else t t → → 2 3 → 2 E-IfFalse Exercise: what if false then t2 else t3 t3 → happens to our type system E-If t t when we omit 1 → 1 if t then t else t if t then t else t the rule? 1 2 3 → 1 2 3 14 / 28 © Ralf Lämmel, 2009-2011 unless noted otherwise 197 Prolog Evaluation rules of NB (SOS) % eval(pred(zero),zero). Disfavored eval(pred(succ(NV)),NV) :- nvalue(NV). semantics eval(succ(T1),succ(T2)) :- eval(T1,T2). eval(pred(T1),pred(T2)) :- eval(T1,T2). Note: appearances of eval(iszero(zero),true). metavariables in SOS eval(iszero(succ(NV)),false) :- nvalue(NV). translate into tests for values. eval(iszero(T1),iszero(T2)) :- eval(T1,T2). eval(if(true,T2,_),T2). eval(if(false,_,T3),T3). eval(if(T1,T2,T3),if(T4,T2,T3)) :- eval(T1,T4). © Ralf Lämmel, 2009-2011 unless noted otherwise 198 Prolog Reflexive, transitive closure manysteps(V,V) :- value(V). manysteps(T1,V) :- eval(T1,T2), manysteps(T2,V). This is like ➝* in the formal setup, and the predicate works for any language with a binary reduction relation eval/2. © Ralf Lämmel, 2009-2011 unless noted otherwise 199 Prolog Composing everything :- [...]. % Import syntax and semantics :- [...]. % Import main predicate :- current_prolog_flag(argv,Argv), ( append(_,['--',Input],Argv), main(Input), halt; true ). Hence, we can invoke the language processor from the command-line prompt. © Ralf Lämmel, 2009-2011 unless noted otherwise 200 Prolog main(Input) :- see(Input), read(Term), seen, format('Input term: ~w~n',[Term]), manysteps(Term,X), show(X,Y), format('Value of term: ~w~n',[Y]). show(zero,0) :- !. show(succ(X),Z) :- !, show(X,Y), Z is Y + 1. show(X,X). © Ralf Lämmel, 2009-2011 unless noted otherwise 201 Running the NB interpreter $ swipl -q -f main.pro -- ../samples/sample1.nb Input term: if(iszero(pred(succ(zero))),zero,succ(succ(zero))) Value of term: 0 $ © Ralf Lämmel, 2009-2011 unless noted otherwise 202 Typed NB © Ralf Lämmel, 2009-2011 unless noted otherwise 203 This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU. Typ es for NB typing rules NB Types in NB First, we need a few new syntactic forms T ::= types: Bool the Boolean type Nat the type of numeric values And typing rules: T-If T-True T-False t1 : Bool t2 : Tt3 : T true : Bool false : Bool if t1 then t2 else t3 : T T-Succ T-Pred T-Iszero © Ralf Lämmel, T-Zero2009-2011 unless noted otherwise 204 t : Nat t : Nat t : Nat 0 : Nat succ t : Nat pred t : Nat iszero t : Bool 15 / 28 Typ es for NB typing rules NB This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU. First, we need a few new syntactic forms T ::= types: Bool typingthe Boolean typerules NatNBthe type of numeric values And typing rules: T-If T-True T-False t1 : Bool t2 : Tt3 : T true : Bool false : Bool if t1 then t2 else t3 : T T-Succ T-Pred T-Iszero T-Zero t : Nat t : Nat t : Nat 0 : Nat succ t : Nat pred t : Nat iszero t : Bool 15 / 28 © Ralf Lämmel, 2009-2011 unless noted otherwise 205 Prolog NB typing rules welltyped(true,bool). 1:1 mapping welltyped(false,bool). welltyped(zero,nat). welltyped(succ(T),nat) :- welltyped(T,nat). welltyped(pred(T),nat) :- welltyped(T,nat). welltyped(iszero(T),bool) :- welltyped(T,nat). welltyped(if(T1,T2,T3),T) :- welltyped(T1,bool), welltyped(T2,T), welltyped(T3,T). © Ralf Lämmel, 2009-2011 unless noted otherwise 206 Prolog Conditional evaluation for typed NB main(Input) :- see(Input), read(Term), seen, format('Input term: ~w~n',[Term]), welltyped(Term, Type), format('Type of term: ~w~n',[Type]), manysteps(Term,X), show(X,Y), format('Value of term: ~w~n',[Y]). © Ralf Lämmel, 2009-2011 unless noted otherwise 207 The Untyped Lambda Calculus © Ralf Lämmel, 2009-2011 unless noted otherwise 208 Summarizing Summarizing the system This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU. The grammar and (small-step) operational semantics of lambda-calculus Formalizationlambda-calculus of the lambda calculus Evaluation rules (call-by-name) Syntax Summarizing Summarizing the system Syntax t ::=x • t ::=x t t t t t1 → t1 t → t λx.t Terms → → t1 t2 t1 t2 vt vt 1 2 → 1 2 → The grammar and (small-step) operational semantics of tt (λx.t) v [v/x]t lambda-calculus → v ::=λx.t Values (normal forms) Evaluation rules (call-by-name)v ::=λx.t Syntax is the smallest binary relation on • Evaluation → terms satisfying the rules t ::=x t t t t 1 → 1 → λx.t 34 / 36 t1 t2 t1 t2 vt vt tt → → (λx.t) v [v/x]t → v ::=λx.t © Ralf Lämmel, 2009-2011 unless noted otherwise 209 is the smallest binary relation on → terms satisfying the rules 34 / 36 Prolog Syntax of the untyped lambda calculus term(var(X)) :- variable(X). term(app(T1,T2)) :- term(T1), term(T2). term(lam(X,T)) :- variable(X), term(T). value(lam(X,T)) :- variable(X), term(T). variable(X) :- atom(X). Variables are Prolog atoms. © Ralf Lämmel, 2009-2011 unless noted otherwise 210 Prolog λ: sample term app(app(app( % TEST (if-then-else) lam(l,lam(m,lam(n,app(app(var(l),var(m)),var(n))))), % Church Boolean True lam(t,lam(f,var(t)))), We illustrate Church Booleans % Church Numeral 0 and numerals. That is, we use a conditional (TEST) to select lam(s,lam(z,var(z)))), either C0 or C1. % Church Numeral 1 lam(s,lam(z,app(var(s),var(z))))). © Ralf Lämmel, 2009-2011 unless noted otherwise 211 Prolog Evaluation rules of the untyped lambda calculus eval(app(T1,T2),app(T3,T2)) :- eval(T1,T3). eval(app(V,T1),app(V,T2)) :- Substitution (as needed for beta reduction) is the value(V), interesting part--both in the eval(T1,T2). formal setting. and in Prolog. eval(app(lam(X,T1),V),T2) :- value(V), substitute(V,X,T1,T2). © Ralf Lämmel, 2009-2011 unless noted otherwise 212 This slide is derived from Jaakko Järvi’s slides for his course ”ProgrammingIntroduction Languages”, CPSC 604 @ TAMU. InductiveSubstitution definition of substitution in λ calculus [N/x]x = N [N/x]y = y, y any variable different from x [N/x](M1 M2)=([N/x]M1)([N/x]M2) [N/x](λx.M)=λx.MIntroduction Variable[N/x binding](λy.M)= —λ precisey.([N/x definition]M), y not free in N Alpha renaming can be applied freely, variables drawn from the infinite FV(M) defines the set of free variables in the term M pool of variable names FV(x)=Examples:x { } FV(MN)=FV(M) FV(N) [z/x]x ∪ FV(λx.M)=FV(M) x [z/x](λx.xx) \{ } © Ralf Lämmel, 2009-2011[z/x unless](λ ynoted.yx otherwise) 213 [Spotz/x]( freeλz. andxz) bound occurrences here: 12 / 36 (λx.y)(λy.y) λx.(λy.xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9 / 36 Substitution 1/3 Prolog substitute(N,X,var(X),N).
Recommended publications
  • Lecture Notes on the Lambda Calculus 2.4 Introduction to Β-Reduction
    2.2 Free and bound variables, α-equivalence. 8 2.3 Substitution ............................. 10 Lecture Notes on the Lambda Calculus 2.4 Introduction to β-reduction..................... 12 2.5 Formal definitions of β-reduction and β-equivalence . 13 Peter Selinger 3 Programming in the untyped lambda calculus 14 3.1 Booleans .............................. 14 Department of Mathematics and Statistics 3.2 Naturalnumbers........................... 15 Dalhousie University, Halifax, Canada 3.3 Fixedpointsandrecursivefunctions . 17 3.4 Other data types: pairs, tuples, lists, trees, etc. ....... 20 Abstract This is a set of lecture notes that developed out of courses on the lambda 4 The Church-Rosser Theorem 22 calculus that I taught at the University of Ottawa in 2001 and at Dalhousie 4.1 Extensionality, η-equivalence, and η-reduction. 22 University in 2007 and 2013. Topics covered in these notes include the un- typed lambda calculus, the Church-Rosser theorem, combinatory algebras, 4.2 Statement of the Church-Rosser Theorem, and some consequences 23 the simply-typed lambda calculus, the Curry-Howard isomorphism, weak 4.3 Preliminary remarks on the proof of the Church-Rosser Theorem . 25 and strong normalization, polymorphism, type inference, denotational se- mantics, complete partial orders, and the language PCF. 4.4 ProofoftheChurch-RosserTheorem. 27 4.5 Exercises .............................. 32 Contents 5 Combinatory algebras 34 5.1 Applicativestructures. 34 1 Introduction 1 5.2 Combinatorycompleteness . 35 1.1 Extensionalvs. intensionalviewoffunctions . ... 1 5.3 Combinatoryalgebras. 37 1.2 Thelambdacalculus ........................ 2 5.4 The failure of soundnessforcombinatoryalgebras . .... 38 1.3 Untypedvs.typedlambda-calculi . 3 5.5 Lambdaalgebras .......................... 40 1.4 Lambdacalculusandcomputability . 4 5.6 Extensionalcombinatoryalgebras . 44 1.5 Connectionstocomputerscience .
    [Show full text]
  • Lecture Notes on Types for Part II of the Computer Science Tripos
    Q Lecture Notes on Types for Part II of the Computer Science Tripos Prof. Andrew M. Pitts University of Cambridge Computer Laboratory c 2016 A. M. Pitts Contents Learning Guide i 1 Introduction 1 2 ML Polymorphism 6 2.1 Mini-ML type system . 6 2.2 Examples of type inference, by hand . 14 2.3 Principal type schemes . 16 2.4 A type inference algorithm . 18 3 Polymorphic Reference Types 25 3.1 The problem . 25 3.2 Restoring type soundness . 30 4 Polymorphic Lambda Calculus 33 4.1 From type schemes to polymorphic types . 33 4.2 The Polymorphic Lambda Calculus (PLC) type system . 37 4.3 PLC type inference . 42 4.4 Datatypes in PLC . 43 4.5 Existential types . 50 5 Dependent Types 53 5.1 Dependent functions . 53 5.2 Pure Type Systems . 57 5.3 System Fw .............................................. 63 6 Propositions as Types 67 6.1 Intuitionistic logics . 67 6.2 Curry-Howard correspondence . 69 6.3 Calculus of Constructions, lC ................................... 73 6.4 Inductive types . 76 7 Further Topics 81 References 84 Learning Guide These notes and slides are designed to accompany 12 lectures on type systems for Part II of the Cambridge University Computer Science Tripos. The course builds on the techniques intro- duced in the Part IB course on Semantics of Programming Languages for specifying type systems for programming languages and reasoning about their properties. The emphasis here is on type systems for functional languages and their connection to constructive logic. We pay par- ticular attention to the notion of parametric polymorphism (also known as generics), both because it has proven useful in practice and because its theory is quite subtle.
    [Show full text]
  • Category Theory and Lambda Calculus
    Category Theory and Lambda Calculus Mario Román García Trabajo Fin de Grado Doble Grado en Ingeniería Informática y Matemáticas Tutores Pedro A. García-Sánchez Manuel Bullejos Lorenzo Facultad de Ciencias E.T.S. Ingenierías Informática y de Telecomunicación Granada, a 18 de junio de 2018 Contents 1 Lambda calculus 13 1.1 Untyped λ-calculus . 13 1.1.1 Untyped λ-calculus . 14 1.1.2 Free and bound variables, substitution . 14 1.1.3 Alpha equivalence . 16 1.1.4 Beta reduction . 16 1.1.5 Eta reduction . 17 1.1.6 Confluence . 17 1.1.7 The Church-Rosser theorem . 18 1.1.8 Normalization . 20 1.1.9 Standardization and evaluation strategies . 21 1.1.10 SKI combinators . 22 1.1.11 Turing completeness . 24 1.2 Simply typed λ-calculus . 24 1.2.1 Simple types . 25 1.2.2 Typing rules for simply typed λ-calculus . 25 1.2.3 Curry-style types . 26 1.2.4 Unification and type inference . 27 1.2.5 Subject reduction and normalization . 29 1.3 The Curry-Howard correspondence . 31 1.3.1 Extending the simply typed λ-calculus . 31 1.3.2 Natural deduction . 32 1.3.3 Propositions as types . 34 1.4 Other type systems . 35 1.4.1 λ-cube..................................... 35 2 Mikrokosmos 38 2.1 Implementation of λ-expressions . 38 2.1.1 The Haskell programming language . 38 2.1.2 De Bruijn indexes . 40 2.1.3 Substitution . 41 2.1.4 De Bruijn-terms and λ-terms . 42 2.1.5 Evaluation .
    [Show full text]
  • 1 Simply-Typed Lambda Calculus
    CS 4110 – Programming Languages and Logics Lecture #18: Simply Typed λ-calculus A type is a collection of computational entities that share some common property. For example, the type int represents all expressions that evaluate to an integer, and the type int ! int represents all functions from integers to integers. The Pascal subrange type [1..100] represents all integers between 1 and 100. You can see types as a static approximation of the dynamic behaviors of terms and programs. Type systems are a lightweight formal method for reasoning about behavior of a program. Uses of type systems include: naming and organizing useful concepts; providing information (to the compiler or programmer) about data manipulated by a program; and ensuring that the run-time behavior of programs meet certain criteria. In this lecture, we’ll consider a type system for the lambda calculus that ensures that values are used correctly; for example, that a program never tries to add an integer to a function. The resulting language (lambda calculus plus the type system) is called the simply-typed lambda calculus (STLC). 1 Simply-typed lambda calculus The syntax of the simply-typed lambda calculus is similar to that of untyped lambda calculus, with the exception of abstractions. Since abstractions define functions tht take an argument, in the simply-typed lambda calculus, we explicitly state what the type of the argument is. That is, in an abstraction λx : τ: e, the τ is the expected type of the argument. The syntax of the simply-typed lambda calculus is as follows. It includes integer literals n, addition e1 + e2, and the unit value ¹º.
    [Show full text]
  • Lambda-Calculus Types and Models
    Jean-Louis Krivine LAMBDA-CALCULUS TYPES AND MODELS Translated from french by René Cori To my daughter Contents Introduction5 1 Substitution and beta-conversion7 Simple substitution ..............................8 Alpha-equivalence and substitution ..................... 12 Beta-conversion ................................ 18 Eta-conversion ................................. 24 2 Representation of recursive functions 29 Head normal forms .............................. 29 Representable functions ............................ 31 Fixed point combinators ........................... 34 The second fixed point theorem ....................... 37 3 Intersection type systems 41 System D­ ................................... 41 System D .................................... 50 Typings for normal terms ........................... 54 4 Normalization and standardization 61 Typings for normalizable terms ........................ 61 Strong normalization ............................. 68 ¯I-reduction ................................. 70 The ¸I-calculus ................................ 72 ¯´-reduction ................................. 74 The finite developments theorem ....................... 77 The standardization theorem ......................... 81 5 The Böhm theorem 87 3 4 CONTENTS 6 Combinatory logic 95 Combinatory algebras ............................. 95 Extensionality axioms ............................. 98 Curry’s equations ............................... 101 Translation of ¸-calculus ........................... 105 7 Models of lambda-calculus 111 Functional
    [Show full text]
  • The Lambda Calculus Bharat Jayaraman Department of Computer Science and Engineering University at Buffalo (SUNY) January 2010
    The Lambda Calculus Bharat Jayaraman Department of Computer Science and Engineering University at Buffalo (SUNY) January 2010 The lambda-calculus is of fundamental importance in the study of programming languages. It was founded by Alonzo Church in the 1930s well before programming languages or even electronic computers were invented. Church's thesis is that the computable functions are precisely those that are definable in the lambda-calculus. This is quite amazing, since the entire syntax of the lambda-calculus can be defined in just one line: T ::= V j λV:T j (TT ) The syntactic category T defines the set of well-formed expressions of the lambda-calculus, also referred to as lambda-terms. The syntactic category V stands for a variable; λV:T is called an abstraction, and (TT ) is called an application. We will examine these terms in more detail a little later, but, at the outset, it is remarkable that the lambda-calculus is capable of defining recursive functions even though there is no provision for writing recursive definitions. Indeed, there is no provision for writing named procedures or functions as we know them in conventional languages. Also absent are other familiar programming constructs such as control and data structures. We will therefore examine how the lambda-calculus can encode such constructs, and thereby gain some insight into the power of this little language. In the field of programming languages, John McCarthy was probably the first to rec- ognize the importance of the lambda-calculus, which inspired him in the 1950s to develop the Lisp programming language.
    [Show full text]
  • Brief Notes on the Category Theoretic Semantics of Simply Typed Lambda Calculus
    University of Cambridge 2017 MPhil ACS / CST Part III Category Theory and Logic (L108) Brief Notes on the Category Theoretic Semantics of Simply Typed Lambda Calculus Andrew Pitts Notation: comma-separated snoc lists When presenting logical systems and type theories, it is common to write finite lists of things using a comma to indicate the cons-operation and with the head of the list at the right. With this convention there is no common notation for the empty list; we will use the symbol “”. Thus ML-style list notation nil a :: nil b :: a :: nil etc becomes , a , a, b etc For non-empty lists, it is very common to leave the initial part “,” of the above notation implicit, for example just writing a, b instead of , a, b. Write X∗ for the set of such finite lists with elements from the set X. 1 Syntax of the simply typed l-calculus Fix a countably infinite set V whose elements are called variables and are typically written x, y, z,... The simple types (with product types) A over a set Gnd of ground types are given by the following grammar, where G ranges over Gnd: A ::= G j unit j A x A j A -> A Write ST(Gnd) for the set of simple types over Gnd. The syntax trees t of the simply typed l-calculus (STLC) over Gnd with constants drawn from a set Con are given by the following grammar, where c ranges over Con, x over V and A over ST(Gnd): t ::= c j x j () j (t , t) j fst t j snd t j lx : A.
    [Show full text]
  • Simply-Typed Lambda Calculus Static Semantics of F1 •Syntax: Notice :Τ • the Typing Judgment Λ Τ Terms E ::= X | X
    Homework Five Is Alive Simply-Typed • Ocaml now installed on dept Lambda Calculus linux/solaris machines in /usr/cs (e.g., /usr/cs/bin/ocamlc) • There will be no Number Six #1 #2 Back to School Lecture Schedule •Thu Oct 13 –Today • What is operational semantics? When would • Tue Oct 14 – Monomorphic Type Systems you use contextual (small-step) semantics? • Thu Oct 12 – Exceptions, Continuations, Rec Types • What is denotational semantics? • Tue Oct 17 – Subtyping – Homework 5 Due • What is axiomatic semantics? What is a •Thu Oct 19 –No Class verification condition? •Tue Oct 24 –2nd Order Types | Dependent Types – Double Lecture – Food? – Project Status Update Due •Thu Oct 26 –No Class • Tue Oct 31 – Theorem Proving, Proof Checking #3 #4 Today’s (Short?) Cunning Plan Why Typed Languages? • Type System Overview • Development • First-Order Type Systems – Type checking catches early many mistakes •Typing Rules – Reduced debugging time •Typing Derivations – Typed signatures are a powerful basis for design – Typed signatures enable separate compilation • Type Safety • Maintenance – Types act as checked specifications – Types can enforce abstraction •Execution – Static checking reduces the need for dynamic checking – Safe languages are easier to analyze statically • the compiler can generate better code #5 #6 1 Why Not Typed Languages? Safe Languages • There are typed languages that are not safe • Static type checking imposes constraints on the (“weakly typed languages”) programmer – Some valid programs might be rejected • All safe languages use types (static or dynamic) – But often they can be made well-typed easily Typed Untyped – Hard to step outside the language (e.g. OO programming Static Dynamic in a non-OO language, but cf.
    [Show full text]
  • Typed Lambda Calculus
    Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages Call-by-value Operational Semantics t ::= terms v ::= values x variable x. t abstraction values x. t abstraction t t application ( x. t12) v2 [x v2] t12 (E-AppAbs) t1 t’1 (E-APPL1) t1 t2 t’1 t2 t2 t’2 (E-APPL2) v1 t2 v1 t’2 Consistency of Function Application • Prevent runtime errors during evaluation • Reject inconsistent terms • What does ‘x x’ mean? • Cannot be always enforced – if <tricky computation> then true else (x. x) A Naïve Attempt • Add function type • Type rule x. t : – x. x : – If true then (x. x) else (x. y y) : • Too Coarse Simple Types T ::= types Bool type of Booleans T T type of functions T1 T2 T3 = T1 ( T2 T3) Explicit vs. Implicit Types • How to define the type of abstractions? – Explicit: defined by the programmer t ::= Type terms x variable x: T. t abstraction t t application – Implicit: Inferred by analyzing the body • The type checking problem: Determine if typed term is well typed • The type inference problem: Determine if there exists a type for (an untyped) term which makes it well typed Simple Typed Lambda Calculus t ::= terms x variable x: T. t abstraction t t application T::= types T T types of functions Typing Function Declarations x : T t : T 1 2 2 (T-ABS) (x : T1. t2 ): T1 T2 A typing context maps free variables into types , x : T t : T 1 2 2 (T-ABS) ( x : T1 . t2 ): T1 T2 Typing Free Variables x : T (T-VAR) x : T Typing Function Applications t1 : T11 T12 t2 : T11 (T-APP) t1 t2 : T12 Typing Conditionals t1 : Bool t2 : T t3 : T (T-IF) if t1 then t2 else t3 : T If true then (x: Bool.
    [Show full text]
  • Introduction to Type Theory
    Introduction to Type Theory Erik Barendsen version November 2005 selected from: Basic Course Type Theory Dutch Graduate School in Logic, June 1996 lecturers: Erik Barendsen (KUN/UU), Herman Geuvers (TUE) 1. Introduction In typing systems, types are assigned to objects (in some context). This leads to typing statements of the form a : τ, where a is an object and τ a type. Traditionally, there are two views on typing: the computational and the logical one. They differ in the interpretation of typing statements. In the computational setting, a is regarded as a program or algorithm, and τ as a specification of a (in some formalism). This gives a notion of partial correctness, namely well-typedness of programs. From the logical point of view, τ is a formula, and a is considered to be a proof of τ. Various logical systems have a type theoretic interpretation. This branch of type theory is related to proof theory because the constructive or algorithmic content of proofs plays an important role. The result is a realiz- ability interpretation: provability in the logical systems implies the existence of an ‘inhabitant’ in the corresponding type system. Church versus Curry Typing One can distinguish two different styles in typing, usually called after their inventors A. Church and H.B. Curry. The approach `ala Church assembles typed objects from typed components; i.e., the objects are constructed together with their types. We use type anno- tations to indicate the types of basic objects: if 0 denotes the type of natural numbers, then λx:0.x denotes the identity function on N, having type 0→0.
    [Show full text]
  • Simply Typed Lambda Calculus Most Programming Languages Used for Serious Software Development Are Typed Languages
    06-02552 Princ. of Progr. Languages (and “Extended”) The University of Birmingham Spring Semester 2019-20 School of Computer Science c Uday Reddy2019-20 Handout 4: Simply Typed Lambda Calculus Most programming languages used for serious software development are typed languages. The reason for their popularity is that types allow us to check mechanically whether we are plugging together compatible pieces of programs. When we plug together incompatible pieces either due to lack of understanding, confusion, or misunderstandings, we end up constructing programs that do not function correctly. Type checking allows us to catch such problems early and leads to more reliable software. In Mathematics, typed languages have been developed because their untyped variants have led to paradoxes. The most famous of them is the Russell’s paradox. If we don’t distinguish between the type of elements and the type of sets of such elements, then the language allows us to express “the set of all sets” which cannot be shown to exist or not exist. However, the typed languages are constraining. So, there is ever a temptation to devise untyped languages. Alonzo Church first developed a typed version of the lambda calculus which he called the “simple theory of types”. He later generalised it to the untyped lambda calculus in order to create a more expressive language. However, the untyped lambda calculus, if used as the foundation for logic, leads one back to paradoxes. Despite paradoxes, the untyped calculus was useful for some foundational purposes. In his Master’s thesis, Turing proved that the untyped lambda calculus and (what we now call) “Turing machines” were equally expressive.
    [Show full text]
  • Simply Easy! (An Implementation of a Dependently Typed Lambda
    Simply Easy! An Implementation of a Dependently Typed Lambda Calculus Andres Loh¨ Conor McBride Wouter Swierstra University of Bonn University of Nottingham [email protected] [email protected] [email protected] Abstract less than 150 lines of Haskell code, and can be generated directly We present an implementation in Haskell of a dependently-typed from this paper’s sources. lambda calculus that can be used as the core of a programming The full power of dependent types can only show if we add language. We show that a dependently-typed lambda calculus is no actual data types to the base calculus. Hence, we demonstrate in more difficult to implement than other typed lambda calculi. In fact, Section 4 how to extend our language with natural numbers and our implementation is almost as easy as an implementation of the vectors. More data types can be added using the principles ex- simply typed lambda calculus, which we emphasize by discussing plained in this section. Using the added data types, we write a few the modifications necessary to go from one to the other. We explain example programs that make use of dependent types, such as a vec- how to add data types and write simple programs in the core tor append operation that keeps track of the length of the vectors. language, and discuss the steps necessary to build a full-fledged Programming in λ5 directly is tedious due to the spartan nature programming language on top of our simple core. of the core calculus. In Section 5, we therefore sketch how to proceed if we want to construct a real programming language on top of our dependently-typed core.
    [Show full text]