CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving

Size: px
Start display at page:

Download "CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving"

Transcription

1 CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving Course Notes Lent Term Term 2008 Sect. 4 The λ-calculus with Products and Conjunction Anton Setzer Dept. of Computer Science, Swansea University csetzer/lectures/ intertheo/07/index.html 4 (b) Currying 4 (d) Logic with Conjunction May 24, 2017 CS 336/CS M36/CS M46 Sect. 4 1/ 162 CS 336/CS M36/CS M46 Sect. 4 1a/ (b) Currying 4 (d) Logic with Conjunction One can expand the set of λ-types and λ-terms as follows: Types are defined as before, but we have additionally: If σ, τ are types, so is σ τ. CS 336/CS M36/CS M46 Sect. 4 (a) 1b/ 162 CS 336/CS M36/CS M46 Sect. 4 (a) 2/ 162

2 Example (Products) Example2 (Products) Assume we have some extra ground types Name := String Gender := {female, male} The exact definition of Gender and String in type theory will be given later (String will be a list of characters). Then we can define name-with-gender := String Gender Assume we have a type Term of terms, representing functions Int Int. The set of terms Term together with the function, they denote, is given as Term (Int Int) Then we have John, male : name-with-gender. If s : name-with-gender, then it s first projection is a name. CS 336/CS M36/CS M46 Sect. 4 (a) 3/ 162 Products CS 336/CS M36/CS M46 Sect. 4 (a) 4/ 162 Example The set of typed-λ-terms are defined as before but we have: If s : σ, t : τ then s, t : σ τ: Γ s : σ Γ t : τ (Pair) Γ s, t : σ τ If s : σ τ, then π 0 (s) : σ and π 1 (s) : τ: Γ s : σ τ (Proj0 ) Γ π 0 (s) : σ Γ s : σ τ (Proj1 ) Γ π 1 (s) : τ We show (λx (o o) (o o o).π 0 (x)) λy o.y, λz o.λv o.z : o o (λx (o o) (o o o). π 0 ( x }{{} :(o o) (o o o) } {{ } } {{ :o o } ((o o) (o o o)) o o )) λy o. y }{{} :o }{{} :o o, λz o. λv o. z }{{} :o }{{} o o } {{ } } o o o {{ } (o o) (o o o) } {{ } o o CS 336/CS M36/CS M46 Sect. 4 (a) 5/ 162 CS 336/CS M36/CS M46 Sect. 4 (a) 6/ 162

3 β-reduction for Pairs Example β-reduction for the pairs is the rule which allows to replace any subterm of the form π0 ( r 0, r 1 ) by r 0, any subterm of the form π1 ( r 0, r 1 ) by r 1. The subterms π i ( r 0, r 1 ) are called β-redexes of the term in question In addition we have the β-redexes (λx.t) s of the λ-calculus with. β-reduction for the typed λ-calculus with products includes both β-reduction for functions and β-reduction for pairs. (λx (o o) (o o o).π 0 (x)) λy o.y, λz o.λv o.z β π 0 ( λy o.y, λz o.λv o.z ) β λy o.y CS 336/CS M36/CS M46 Sect. 4 (a) 7/ 162 Products with many Components CS 336/CS M36/CS M46 Sect. 4 (a) 8/ 162 Products with many Components We write σ 0 σ n for ( ((σ 0 σ 1 ) σ 2 ) σ n ). Define for s 0 : σ 0,..., s n : σ n s 0,..., s n := s 0, s 1, s 2, s n : σ 0 σ n (Note by our convention that the type is equal to ( ((σ 0 σ 1 ) σ 2 ) σ n )) E.g. x, y, z := x, y, z. One can easily define corresponding projections πi n : (σ 0 σ n 1 ) σ i, s.t. π n i ( s 0,..., s n 1 ) = β s i. For instance in case n = 3 we need π 3 i ( s 0, s 1, s 2 ) = s i We obtain this by defining π0 3(x) := π 0(π 0 (x)) Then π 3 0( s 0, s 1, s 2 ) = π 0 (π 0 ( s 0, s 1, s 2 )) = π 0 ( s 0, s 1 ) = s 0 π 3 1 (x) := π 1 (π 0 (x)) Then π 3 1( s 0, s 1, s 2 ) = π 1 (π 0 ( s 0, s 1, s 2 )) = π 1 ( s 0, s 1 ) = s 1 π 3 i ( s 0, s 1, s 2 ) = π 3 i ( s 0, s 1, s 2 ) = s i CS 336/CS M36/CS M46 Sect. 4 (a) 9/ 162 CS 336/CS M36/CS M46 Sect. 4 (a) 10/ 162

4 Products with many Components η-expansion for Products π 3 i ( s 0, s 1, s 2 ) = s i π 3 2 (x) := π 1(x) Then π 3 2( s 0, s 1, s 2 ) = π 1 ( s 0, s 1, s 2 ) = s 2 If we have a product r : σ τ, then its projections are β-equal to the projections of π 0 (r), π 1 (r) : π 0 ( π 0 (r), π 1 (r) ) = β π 0 (r), π 1 ( π 0 (r), π 1 (r) ) = β π 1 (r). Therefore, similarly to functions, we would like to have that every term r : σ τ is equal to π 0 (r), π 1 (r). The η-rule expresses that subterms t : σ τ can be η-expanded to π 0 (t), π 1 (t) Details can be found on the next few slides, but won t be treated in the lecture. We jump over the rest of this Subsection and over SubSect. b. CS 336/CS M36/CS M46 Sect. 4 (a) 11/ 162 η-rule for Products CS 336/CS M36/CS M46 Sect. 4 (a) 12/ 162 η-rule for Products However, as for functions, we need to impose some restrictions, in order to avoid circularities: If t is of the form r0, r 1, and if we allowed then the reduction t π 0 (t), π 1 (t), we would get the following circular reduction: t π 0 (t), π 1 (t) π 0 ( r 0, r 1 ), π 1 ( r 0, r 1 ) β r 0, r 1 t If t occurs in the form πi (t), and if we then allowed to expand t, we would get π i (t) π i ( π 0 (t), π 1 (t) ) and would get the following circular reduction: π i (t) π i ( π 0 (t), π 1 (t) ) β π i (t), All other terms can be expanded without obtaining a new redex. CS 336/CS M36/CS M46 Sect. 4 (a) 13/ 162 CS 336/CS M36/CS M46 Sect. 4 (a) 14/ 162

5 η-expansion for Products Example η-expansion for products is the rule which allows to replace in a typed λ-term t one subterm s : σ τ, which is not of the form r0, r 1, and does not occur in the form π0 (s) or π 1 (s) by π 0 (s), π 1 (s). η-expansion for the typed λ-calculus with products includes both η-expansion for functions and for pairs. Assume g : (o o) o. (λf (o o) o.λx o o.f x) g β λx o o.g x η λx o o.g π 0 (x), π 1 (x) λx o o.g π 0 (x), π 1 (x) is therefore the β, η-normal form of (λf (o o) o.λx o o.f x) g CS 336/CS M36/CS M46 Sect. 4 (a) 15/ 162 Theorem CS 336/CS M36/CS M46 Sect. 4 (a) 16/ 162 η-rule The typed λ-calculus with products, β-reduction and with (or without) η-expansion is confluent and strongly normalising. We can introduce products as well for the untyped λ-calculus. Then we obtain a confluent (but of course non normalising) reduction system. With the η-rule we obtain now that if r : σ τ, then r = β,η π 0 (r), π 1 (r). If r : σ τ is of the form r0, r 1 then we have r = β π 0 (r), π 1 (r) : π 0 (r), π 1 (r) π 0 ( r 0, r 1 ), π 1 ( r 0, r 1 ) β r 0, r 1 Otherwise r η π 0 (r), π 1 (r). Therefore, every element of a product type is of the form something 0, something 1. Jump over Currying/Uncurrying r CS 336/CS M36/CS M46 Sect. 4 (a) 17/ 162 CS 336/CS M36/CS M46 Sect. 4 (a) 18/ 162

6 4 (b) Currying 4 (b) Currying 4 (b) Currying 4 (d) Logic with Conjunction 4 (b) Currying In the λ-calculus with products, there are two versions of a function f taking an integer and a floating point number and returning a string: f1 : (Int Float) String f2 : Int Float String. We say that f1 is in Uncurried form, and f2 is in Curried form. The name Curry honours Haskell Curry. The application of these two functions to arguments x and y is written as f 1 x, y, f 2 x y. CS 336/CS M36/CS M46 Sect. 4 (b) 18a/ (b) Currying Haskell Brooks Curry CS 336/CS M36/CS M46 Sect. 4 (b) 19/ (b) Currying Curried/Uncurried Functions Haskell Brooks Curry ( ) The above generalises to functions with arbitrarily (but finitely) many arguments of different type. The Curried version of a function f with arguments of types σ 0,..., σ n 1 and result type ρ is of type σ 0 σ n 1 ρ. Its Uncurried version has type (σ 0 σ n 1 ) ρ. CS 336/CS M36/CS M46 Sect. 4 (b) 20/ 162 CS 336/CS M36/CS M46 Sect. 4 (b) 21/ 162

7 Uncurrying 4 (b) Currying Currying 4 (b) Currying From a Curried function we can obtain an Uncurried function. This is called Uncurrying. Example: Assume f : Int Float String. Then λx Int Float.f π 0 (x) π 1 (x) : (Int Float) String is the Uncurried form of f. From a Uncurried function we can obtain an Curried function. This is called Currying. Example: Assume f : (Int Float) String. Then λx Int.λy Float.f x, y : Int Float String is the Curried form of f. On the next 2 slides follows a treatment of the general case. Jump over general case. CS 336/CS M36/CS M46 Sect. 4 (b) 22/ (b) Currying Uncurrying CS 336/CS M36/CS M46 Sect. 4 (b) 23/ (b) Currying Currying We can obtain from the Curried form f Curry of a function its Uncurried form f Uncurry by f Uncurry = λx.f Curry π n 0(x) π n n 1(x) where πi n : (σ 0 σ n 1 ) σ i are the projections. One can as well define a λ-term Uncurry : (σ 0 σ n 1 ρ) (σ 0 σ n 1 ) ρ Uncurry := λf, x.f π n 0 (x) πn n 1 (x) s.t. Uncurry f Curry β f Uncurry. This transformation is called Uncurrying. We can obtain from the Uncurried form f Uncurry of a function its Curried form f Curry by Again we can define f Curry = λx 0,..., x n 1.f Uncurry x 0,..., x n 1 Curry : ((σ 0 σ n 1 ) ρ) σ 0 σ n 1 ρ Curry := λf, x 0,..., x n 1.f x 0,..., x n 1 s.t. Curry f Uncurry β f Curry. This transformation is called Currying. It is an easy exercise to show Curry (Uncurry f ) = β,η f and Uncurry (Curry f ) = β,η f. CS 336/CS M36/CS M46 Sect. 4 (b) 24/ 162 CS 336/CS M36/CS M46 Sect. 4 (b) 25/ 162

8 4 (b) Currying (Un)Currying in Programming 4 (b) Currying (Un)Currying in Programming The Uncurried form of a function corresponds to the form functions are presented usually outside functional programming. There functions always need all arguments. 3+ is something which outside functional programming usually doesn t make much sense. In functional programming one often prefers the Curried form. This allows to apply a functional partially to its arguments. E.g. if we take + as usual in Curried form, then + 3 : Int Int is the function taking x and returning + 3 x which is 3 + x. Example: map ( + 3) [1, 2, 3] = [4, 5, 6] If we apply the function increasing every x by 3 to the list [1, 2, 3], we obtain the result of incrementing each list element by 3, i.e. [4, 5, 6]. CS 336/CS M36/CS M46 Sect. 4 (b) 26/ (b) Currying (Un)Currying in Programming CS 336/CS M36/CS M46 Sect. 4 (b) 27/ 162 One often avoids in functional programming (and as well in Agda) the formation of products (or record types). Especially for intermediate calculations. The packing and unpacking of products makes programming often harder. E.g. instead of defining a function f : σ (ρ τ) it is often better to form two functions f 1 : σ ρ and f 2 : σ τ, (which are often defined simultaneously). Only, when delivering the final program, the use of products is often better, because the result is more compact. 4 (b) Currying 4 (d) Logic with Conjunction CS 336/CS M36/CS M46 Sect. 4 (b) 28/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 28a/ 162

9 Records in Pascal In many languages there exists the notion of a Record type. In Pascal we can form for instance the type of Students In Agda, there are two ways of defining the product. The first one represents the product as a record type. Student = record begin StudentNumber : Integer; Name : String; end Elements of this type can be formed by determining their StudentNumber and Name. If x : Student, then x.studentnumber : Integer and x.name : String. CS 336/CS M36/CS M46 Sect. 4 (c) 29/ 162 Records in Java CS 336/CS M36/CS M46 Sect. 4 (c) 30/ 162 The Record Type in Agda Records correspond in Java to classes with public fields, no methods, and a standard constructor. E.g. the class Student is defined as follows: class Student{ Integer StudentNumber; String Name; Student(Integer StudentNumber, String Name){ this.studentnumber = StudentNumber; this.name = Name } } Assume we have introduced A, B : Set Then we can introduce the record type record AB : Set where field a : A b : B CS 336/CS M36/CS M46 Sect. 4 (c) 31/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 32/ 162

10 Name Clashes in the Record Type Name Clashes in the Record Type You are not allowed to use a and b, if the identifiers a and b have been introduced before. However, you can use the same record selector in different records. So causes an error. n : N n = Z record A : Set where field n : N However is accepted. record A : Set where field n : N record A : Set where field n : N N CS 336/CS M36/CS M46 Sect. 4 (c) 33/ 162 Longer Records CS 336/CS M36/CS M46 Sect. 4 (c) 34/ 162 The Product as a Record Type We can introduce longer records as well, e.g. record ABCD : Set where field a : A b : B c : C d : D Elements of a record type are introduced as follows: Assume we have a : A, b : B. Then we can introduce in the above situation ab : AB ab = record{a = a ; b = b } Note that, since a, b cannot be record selectors and separate identifiers at the same time, the ambiguous definition record{a = a; b = b} is not possible. CS 336/CS M36/CS M46 Sect. 4 (c) 35/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 36/ 162

11 The Product as a Record Type Projections Using a let expression we can get around this problem: ab : AB ab = let a : A a = a b : B b = b in record{a = a ; b = b } We recommend to avoid such definitions. If we have record AB : Set where field a : A b : B then Agda provides us with the following projection functions: AB.a : AB A AB.b : AB B CS 336/CS M36/CS M46 Sect. 4 (c) 37/ 162 Projections CS 336/CS M36/CS M46 Sect. 4 (c) 38/ 162 Records with Dependencies If we define then we obtain ab : AB ab = record{a = a ; b = b } AB.a ab = a AB.b ab = b We can define a generic product rprod A B depending on A : Set, B : Set (rprod stands for record-product): record rprod (A B : Set) : Set where field first : A second : B Here (A B : Set) stands for the two parameters (A : Set) (B : Set) The projections are denoted as follows: If ab : rprod A B, then rprod.first ab : A rprod.second ab : B CS 336/CS M36/CS M46 Sect. 4 (c) 39/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 40/ 162

12 Hidden Arguments Hidden Arguments When we use rprod.first : rprod A B A it is not always clear, which sets A and B one is referring to. In fact A and B are hidden arguments of rprod.first. In case one needs to make them explicit, this can be done as follows: rprod.first { A } { B } ab stands for rprod.first applied to ab, where ab : rprod A B. We can make any argument of a function hidden. For instance id : {A : Set} A A id a = a defines the identity function, which for any set A and a : A returns a. This function is used in the form without adding the parameter A. id a CS 336/CS M36/CS M46 Sect. 4 (c) 41/ 162 Hidden Arguments CS 336/CS M36/CS M46 Sect. 4 (c) 42/ 162 Hidden Arguments If we want to make the hidden parameter A explicit we can do so by writing id { A } a There is no deep theory about when arguments can be hidden or not. Any argument of a function can be declared to be hidden. If when type checking the code Agda cannot determine a hidden argument, then Agda will get unsolved hidden goals. CS 336/CS M36/CS M46 Sect. 4 (c) 43/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 44/ 162

13 Example Example Take the following code strange : {a : A} A strange {a} = a a : A a = strange Agda doesn t complain about the definition of strange. However, when checking the definition of a, it notices that it cannot figure out the hidden argument of strange. strange : {a : A} A strange {a} = a a : A a = strange It complains by Marking the word strange in yellow. Displaying a hidden goal in the buffer All Goals 184 : A [ at /home/csetzerlocal/test.agda:166,7-14 ] This means that for the missing hidden argument of strange a hidden goal has been introduced, which is of type A, and the position (line 166, column 7-14) is displayed. CS 336/CS M36/CS M46 Sect. 4 (c) 45/ 162 The Product using data CS 336/CS M36/CS M46 Sect. 4 (c) 46/ 162 The Product using data The data -product is introduced as follows (dprod stands for data-product): The second version of the product uses the more general data construct for defining so called algebraic types. With this construction we are leaving the so called logical framework. λ-terms and the record type form the logical framework, the basic types of Agda and of Martin-Löf type theory. The data-construct allows to introduce user-defined types. Here data dprod (A B : Set) : Set where p : A B dprod A B dprod A B depends on two sets A, B. p is the constructor of this set. The name (here p) is up to the user, we could have used any other valid Agda identifier. The idea is: The elements of Prod are exactly the terms p a b where a : A and b : B. CS 336/CS M36/CS M46 Sect. 4 (c) 47/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 48/ 162

14 Pattern Matching Pattern Matching In order to decompose an element of dprod A B in Agda, we can use pattern matching. This is best explained by an example. We postulate A, B : Set, and abbreviate dprod A B as AB: postulate A : Set postulate B : Set AB : Set AB = dprod A B postulate A : Set postulate B : Set AB : Set AB = dprod A B Assume we want to define the first projection s.t. This can be defined as follows: proj0 : AB A, proj0 (p a b) = a proj0 : AB A, proj0 (p a b) = a CS 336/CS M36/CS M46 Sect. 4 (c) 49/ 162 Pattern Matching CS 336/CS M36/CS M46 Sect. 4 (c) 50/ 162 Deep Pattern Matching postulate A : Set postulate B : Set AB : Set AB = dprod A B The second projection can be defined similarly: proj1 : AB B, proj1 (p a b) = b Note the parentheses around (p a b): proj1 p a b = b would read: proj1 applied to a variable p, a variable a and a variable b is equal to b. This causes an error, because proj1 only allows one argument. postulate A : Set postulate B : Set AB : Set AB = dprod A B Deeper pattern matching is as well possible: An element of dprod (dprod A B) B is of the form where a : A, b, b : B. We can define p (p a b ) b f : dprod (dprod A B) B A f (p (p a b) b ) = a CS 336/CS M36/CS M46 Sect. 4 (c) 51/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 52/ 162

15 Deep Pattern Matching Coverage Checker We are not allowed to use the same variable twice in a pattern (unless specially flagged flagged repeated variables occur only in advanced data types like the identity type). So causes an error. f : dprod (dprod A B) B A f (p (p a b) b) = a The coverage checker of Agda will make sure that the patterns cover all possible cases. So f : N N f Z = Z will not pass the coverage checker, because f (S n) is not defined. CS 336/CS M36/CS M46 Sect. 4 (c) 53/ 162 Hidden Arguments in dprod CS 336/CS M36/CS M46 Sect. 4 (c) 54/ 162 Hidden Arguments in dprod p in data dprod (A B : Set) : Set where p : A B dprod A B has hidden arguments {A : Set} and {B : Set}. In case one needs to make them explict, one can do so: c : dprod A B c = p {A} {B} a b If one wants to mention the first hidden argument, but not the second one, one simply omits the second one: c : dprod A B c = p {A} a b The following syntax allows to omit the first hidden argument, but to mention the second one: c : dprod A B c = p { } {B} a b In general, variables which are not used later can be written as. CS 336/CS M36/CS M46 Sect. 4 (c) 55/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 56/ 162

16 Decomposing Record Type Let D : Set D = rprod (dprod A B) C Assume we want to define f : D A which projects an element of D to the component A. Pattern matching is not possible for record types. What we can do is to use the with -construct We can abbreviate this by using: f : D A f d with rprod.first d f d p a b = a f : D A f d with rprod.first d... p a b = a Decomposing Record Type f : D A f d with rprod.first d f d p a b = a The above reads as follows: We define f d by looking at rprod.first d. We look at what happens when rprod.first d = p a b. In this case we define f d as a. CS 336/CS M36/CS M46 Sect. 4 (c) 57/ 162 Longer Example CS 336/CS M36/CS M46 Sect. 4 (c) 58/ 162 Longer Example As an example we want to define in Agda, depending on A, B, C, D : Set, ab : A B a-c : A C, b-d : B D an element f ab a-c b-d : C D. This means that f is a function which takes arguments a-c, b-d and ab as above and returns an element of C D. Therefore A, B, C, D : Set will be global assumptions (represented in Agda by postulates). So we have the following Agda code: postulate A : Set postulate B : Set postulate C : Set postulate D : Set f : (A B) (A C) (B D) (C D) CS 336/CS M36/CS M46 Sect. 4 (c) 59/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 60/ 162

17 Longer Example Longer Example Let AB and CD be names for A B and C D, respectively. Then we obtain the following code: record AB field a : A b : B record CD field c : C d : D : Set where : Set where The goal to be solved is as follows: f : AB (A C) (B D) CD f ab a-c b-d = {!!} CS 336/CS M36/CS M46 Sect. 4 (c) 61/ 162 Longer Example CS 336/CS M36/CS M46 Sect. 4 (c) 62/ 162 Longer Example The idea for this function is as follows: We first project ab : A B to elements a : A, b : B. Then we apply a-c : A C to a : A and obtain an element c : C. A diagram is as follows: ab : A B a-c a : A π 0 π 1 b : B b-d a-c A B f a-c b-d b-d And we apply b-d : B D to b : B and obtain an element d : D. Finally we form the pair c, d. c : C, c, d : C D d :D C D We will use let-expressions in order to compute the intermediate values a, b, c, d. CS 336/CS M36/CS M46 Sect. 4 (c) 63/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 64/ 162

18 Agda Code for the Above Remark on Previous Code f : AB (A C) (B D) CD f ab a-c b-d = let a : A a = AB.a ab b : B b = AB.b ab c : C c = a-c a d : D d = b-d b in record{c = c ; d = d } See exampleletexpressionrecord.agda. In the previous code we used in the let expression variables c and d instead of c and d. This is to avoid the ambiguity in record{c = c; d = d} Agda will interpret this example as intended, but it is not clear whether this will be always the case. CS 336/CS M36/CS M46 Sect. 4 (c) 65/ 162 Concrete Products CS 336/CS M36/CS M46 Sect. 4 (c) 66/ 162 Concrete Products When using the data-construct, it is often more convenient to introduce concrete products in a more direct way. Example: Assume we have defined a set Gender of genders, a set Name of names. The set of persons, given by a gender and a name, can then be defined as data Person : Set where person : Gender Name Person Then one can define customized projections using pattern matching, e.g. gender : Person Gender gender (person g n) = g CS 336/CS M36/CS M46 Sect. 4 (c) 67/ 162 CS 336/CS M36/CS M46 Sect. 4 (c) 68/ 162

19 4 (d) Logic with Conjunction 4 (d) Logic with Conjunction Constructive Meaning of 4 (b) Currying 4 (d) Logic with Conjunction A B is true, if A is true and B is true. Therefore a proof p : A B consists of a proof a : A and a proof b : B. So such a proof is a pair a, b s.t. a : A and b : B. Therefore A B is just the product A B of A and B. We can identify A B with A B. CS 336/CS M36/CS M46 Sect. 4 (d) 68a/ (d) Logic with Conjunction Conjunction in Agda CS 336/CS M36/CS M46 Sect. 4 (d) 69/ (d) Logic with Conjunction Conjunction in Agda Conjunction is represented as a product. There are two products in Agda, therefore as well two ways of representing conjunction: One using the record type: record r (A B : Set) : Set where field and1 : A and2 : B And one using the product formed using data. We use a more meaningful name for the constructor: data d (A B : Set) : Set where and : A B A d B See exampleproofproplogic3.agda The symbol can be introduced by typing in \wedge. CS 336/CS M36/CS M46 Sect. 4 (d) 70/ 162 CS 336/CS M36/CS M46 Sect. 4 (d) 71/ 162

20 4 (d) Logic with Conjunction Typing in Special Symbols 4 (d) Logic with Conjunction Typing in Special Symbols Typing in the special symbols (using the Emacs-package mule ) can be cumbersome. A more convenient way is to use the abbreviation mode: To activate the abbreviation mode, use under emacs M-x abbrev-mode Then one can let an arbitrary sequence of characters to be automatically replaced by an abbreviation. For instance if we want andd to expand to we do the following: We type in andd. We use the emacs command C-x ail We type in the mini buffer our intended expansion, namely (typed in as \wedge ). Now whenever we type in a space-like character (blanks and some punctuations) followed by andd followed by a space-like character, then andd is replaced by. You can edit the abbreviations you have defined by using M-x edit-abbrevs (when finished use C-c C-c in order to activate your definitions). CS 336/CS M36/CS M46 Sect. 4 (d) 72/ (d) Logic with Conjunction Abbreviation Mode CS 336/CS M36/CS M46 Sect. 4 (d) 73/ (d) Logic with Conjunction Customising Agda with Abbreviation Mode You can prevent the expansion of an an abbreviation by using C-q before adding any space-like character after andd. In order to load previous abbreviations and save the when exiting Agda you should add the following to your.emacs file: (read-abbrev-file " /.abbrev defs") However, for this to work you need first to create a file /.abbrev defs This is done by following the steps on the next slide CS 336/CS M36/CS M46 Sect. 4 (d) 74/ 162 CS 336/CS M36/CS M46 Sect. 4 (d) 75/ 162

21 4 (d) Logic with Conjunction Customising Agda with Abbreviation Mode 4 (d) Logic with Conjunction Customising Agda with Abbreviation Mode The creation of a file /.abbrev defs is done as follows (the steps need to be carried out only once): Define at least one abbreviation as above (you can change this abbreviation later by using M-x edit-abbrevs. For instance you can just type in fooo, type in C-x ail, and then type in the Mini-buffer foo, so that fooo is expanded to foo. Then execute M-x write-abbrev-file, and when asked for a file name, enter in the mini-buffer /.abbrev defs Now execute M-x read-abbrev-file, and when asked for a file name, enter in the mini-buffer /.abbrev defs If you now create a new abbreviation, and run C-x s which is the command for saving all buffers, it will ask as well whether you want to save the abbreviation file. CS 336/CS M36/CS M46 Sect. 4 (d) 75a/ (d) Logic with Conjunction Customising Agda with Abbreviation Mode CS 336/CS M36/CS M46 Sect. 4 (d) 75b/ (d) Logic with Conjunction Example In order to activate the abbreviation mode, whenever one enters an Agda file, add in your.emacs file after the line (load " /emacs/agdainstall") the following On the computer A A A and A B A will now be shown in Agda using both versions of. (add-hook agda2-mode-hook (lambda nil (abbrev-mode 1))) You need as well to run once write-abbrev-file. CS 336/CS M36/CS M46 Sect. 4 (d) 75c/ 162 CS 336/CS M36/CS M46 Sect. 4 (d) 76/ 162

22 4 (d) Logic with Conjunction Example (Conjunction) 4 (d) Logic with Conjunction Example (Conjunction) We prove A B B A (see exampleproofproplogic6.agda): Lemma : Set Lemma = A r B B r A lemma : Lemma lemma ab = record{and1 = r.and2 ab; and2 = r.and1 ab} Lemma : Set Lemma = A d B B d A lemma : Lemma lemma (and a b) = and b a CS 336/CS M36/CS M46 Sect. 4 (d) 77/ (d) Logic with Conjunction Conjunction with more Conjuncts CS 336/CS M36/CS M46 Sect. 4 (d) 78/ (d) Logic with Conjunction Conjunction with more Conjuncts If one has a conjunction with more than two conjuncts, e.g. A B C, one can always express it using the binary : As (A r B) r C or A r (B r C). If one adds infixl 30 r Especially when using the record version of it is more convenient to use a ternary version of conjunction (using one of the two versions of the product). Similarly one can introduce conjunctions of 4 or more conjuncts. Definition of the ternary and using a record: one can write for A r B r C (A r B) r C record And3r (A B C : Set) : Set where field and1 : A and2 : B and3 : C CS 336/CS M36/CS M46 Sect. 4 (d) 79/ 162 CS 336/CS M36/CS M46 Sect. 4 (d) 80/ 162

23 4 (d) Logic with Conjunction Conjunction with more Conjuncts 4 (b) Currying Definition of the ternary and using data : data And3d (A B C : Set) : Set where and3d : A B C And3d A B C See exampleproofproplogic5.agda 4 (d) Logic with Conjunction CS 336/CS M36/CS M46 Sect. 4 (d) 81/ 162 CS 336/CS M36/CS M46 Sect. 4 (e) 81a/ 162 λ-calculus and Term Rewriting One can combine the λ-calculus with term writing. This means that we have apart from the rules of the typed or untyped λ-calculus additional rules like x + 0 x. Then we obtain for instance λy.λz.y + 0 λy.λz.y. More details are given on the following slides, but will not be treated in this lecture. Jump over rest of this section. Consider the λ-calculus with terms using additional constants. Assume some term rewriting rules as before (which might involve some λ-terms). As in case of ordinary term rewriting, we form instantiations of the rules by replacing variables by arbitrary λ-terms (in the extended language). CS 336/CS M36/CS M46 Sect. 4 (e) 82/ 162 CS 336/CS M36/CS M46 Sect. 4 (e) 83/ 162

24 λ-calculus and Term Rewriting λ-calculus and Term Rewriting Assume for instance the rule Then s t, if s β-reduces (or η-expands, if one allows the η-rule) to t or there exists an instantiation s t s.t. s is a subterm of s and t is the result of replacing this subterm in s by t. s is called as usual a redex of s. Then we have double λx.x + x (λf.λx.f (f x)) double λx. double (double x) λx. double ((λx.x + x) x) λx. double (x + x) λx. (λx.x + x) (x + x) λx. (x + x) + (x + x) CS 336/CS M36/CS M46 Sect. 4 (e) 84/ 162 What does Subterm Mean? CS 336/CS M36/CS M46 Sect. 4 (e) 85/ 162 What does Subterm Mean? When referring to ordinary term rewriting rules, then for a term t to have subterm s meant essentially that there is a term t in which a new variable x occurs exactly once, and t = t [x := s]. Replacing this subterm by s means that we replace t by t [x := s ]. When referring to λ-terms, this is no longer the case: Assume for instance the rewrite rule x + 0 Rule x. λx.x + 0 has subterm x + 0, but there is no term t s.t. λx.x + 0 = t[y := x + 0]: If we substitute for instance in λx.y y by x + 0 we obtain λz.x + 0. The reason is that when matching a rewrite rule, free variables in the instantiation of the rule used might become bound. So we can apply x + 0 Rule x to λx.x + 0 and have therefore λx.x + 0 λx.x. Replacing a subterm by another subterm is to be understood verbally. CS 336/CS M36/CS M46 Sect. 4 (e) 86/ 162 CS 336/CS M36/CS M46 Sect. 4 (e) 87/ 162

25 Higher Order Rewrite Systems Reduction to Closed Terms The full definition of so called higher order term rewriting systems imposes more restrictions on the reduction rules. For our purposes the naive interpretation just presented suffices. Jump over next part. One can always replace term rewriting rules for the λ-calculus by one in which for all rules s Rule t we have that s, t are closed. This can be done in such a way that equality (modulo the rewriting rules, β and possibly η) in both systems coincide: Assume a rule s Rule t and let x 1,..., x n be the free variables in s. Then replace this rule by λx 1,..., x n.s Rule λx 1,..., x n.t. CS 336/CS M36/CS M46 Sect. 4 (e) 88/ 162 Proof CS 336/CS M36/CS M46 Sect. 4 (e) 89/ 162 Proof We write in the following x for x 1,..., x n. Assume a term r reduces using this rule in the original system to a term u: Then r contains a subterm of the form s where s is the result of substituting in s x i by some terms t i. Let t be the result of substituting in t x i by t i. Then u is the result of replacing s in r by t. Let then r be the result of replacing s by (λ x.s) t 1 t n, and u be the result of replacing in s s by (λ x.t) t 1 t n. Then we have r = β r Rule u = β u, so the reduction can be simulated in the second system. On the other hand, if r u by using in the second system the rule λ x.s Rule λ x.t, then r u in the previous system by using the rule s Rule t r contains a subterm equal to λ x.s and u is the result of substituting this subterm in r by λ x.t. But then r contains the subterm s and t is the result of substituting this subterm in r by t. CS 336/CS M36/CS M46 Sect. 4 (e) 90/ 162 CS 336/CS M36/CS M46 Sect. 4 (e) 91/ 162

26 Example Extended Typed λ-calculus We can replace the rewriting rules x + 0 x x + S y S (x + y) by λx.x + 0 λx.x That becomes in the new system λx, y.x + S y λx, y.s (x + y) S (0 + S 0) S (S (0 + 0)) S (S 0) Finally, we can combine the typed λ-calculus (with or without products, with or without η-expansion) with term rewriting rules. Essentially this means that we have additional constants with types and reduction rules for them. The details (which are given on the following slides) will not be treated in the lecture itself. S (0 + S 0) = β S ((λx, y.x + S y) 0 0) S ((λx, y.s(x + y)) 0 0) = β S (S (0 + 0)) = β S (S ((λx.x + 0) 0)) S (S ((λx.x) 0)) = β S (S 0) CS 336/CS M36/CS M46 Sect. 4 (e) 92/ 162 Extended Typed λ-calculus CS 336/CS M36/CS M46 Sect. 4 (e) 93/ 162 Example For introducing the new rewrite rules, we have to make the following modifications: We assign a type to each additional constant. The set of typed λ-terms is then introduced by the same rules as before, but we have as additional rule: If c is a constant of type σ, then we have Γ c : σ Assuming + : nat nat nat and writing as usual r + s for + r s we have the following derivation of λx nat.x + x : nat nat: x:nat + :nat nat nat x:nat x:nat (Ap) x:nat + x:nat nat x:nat x:nat x : nat + x x : nat (λx nat.x + x) : nat nat (Abs) The left most leaf in this derivation follows by the rule for the constant +. (Ap) CS 336/CS M36/CS M46 Sect. 4 (e) 94/ 162 CS 336/CS M36/CS M46 Sect. 4 (e) 95/ 162

27 Example Extended Typed λ-calculus Then we have (λf nat nat.λx nat.f (f x)) double λx nat. double (double x) λx nat. double ((λx nat.x + x) x) λx nat. double (x + x) λx nat. (λx nat.x + x) (x + x) λx nat. (x + x) + (x + x) Reduction rules should now be of the form Γ s Rule t : σ (instead of s Rule t) where we have Γ s : σ and Γ t : σ. As before, s shouldn t be a variable, and all variables in t should occur in s. Best guaranteed by demanding that all variables in Γ occur free in s. One usually omits Γ, σ, if it is clear from the context. Very often, the reduction rules will be of the form c Rule t : σ where c is a constant and therefore t a closed term. CS 336/CS M36/CS M46 Sect. 4 (e) 96/ 162 Extended Typed λ-calculus CS 336/CS M36/CS M46 Sect. 4 (e) 97/ 162 Example Instantiations of a rule Γ s Rule t : σ are now obtained by replacing variables x of type τ by terms r : τ (possibly depending on some context ). Reductions w.r.t. the rules are obtained by replacing subterms r : σ, which coincide with the left hand side of an instantiation of a rule r r : σ by the right hand side r. Assume ground type nat, constants + : nat nat nat (written infix, i.e. r + s for + r s), and double : nat nat. and the reduction rule double (λx nat.x + x) : nat nat. CS 336/CS M36/CS M46 Sect. 4 (e) 98/ 162 CS 336/CS M36/CS M46 Sect. 4 (e) 99/ 162

28 Example Then we have (λf nat nat.λx nat.f (f x)) double λx nat. double (double x) λx nat. double ((λx.x + x) x) λx nat. double (x + x) λx nat. (λx.x + x) (x + x) λx nat. (x + x) + (x + x) 4 (b) Currying 4 (d) Logic with Conjunction CS 336/CS M36/CS M46 Sect. 4 (e) 100/ 162 CS 336/CS M36/CS M46 Sect. 4 (f) 100a/ 162 The Type of Booleans We want to add types containing finitely many elements to the λ-calculus. We treat first the special case Bool (finite set with 2 elements) and then generalise this to general finite sets. We add a new type Bool to the set of ground types. We add constants tt : Bool, ff : Bool. Here tt stands for true, ff for false. CS 336/CS M36/CS M46 Sect. 4 (f) 101/ 162 CS 336/CS M36/CS M46 Sect. 4 (f) 102/ 162

29 Case σ Bool If then else Furthermore we add the principle of case distinction to the λ-calculus extended by Bool: Assume we have a type σ and case tt : σ case ff : σ Then we want to have that Case σ Bool case tt case ff tt = case tt Case σ Bool case tt case ff ff = case ff Case σ Bool case tt case ff b corresponds to If b then case tt else case ff : And we want that Case σ Bool case tt case ff : Bool σ Case σ Bool case tt case ff tt = case tt Case σ Bool case tt case ff ff = case ff In case b = tt, the if-then-else-term should be equal to casett, as it is the case for Case σ Bool case tt case ff b. In case b = ff, the if-then-else-term should be equal to case ff, as it is the case for Case σ Bool case tt case ff b. CS 336/CS M36/CS M46 Sect. 4 (f) 103/ 162 Type of Case σ Bool CS 336/CS M36/CS M46 Sect. 4 (f) 104/ 162 Example: Boolean Conjunction We don t need to have a complex rule for forming Case σ Bool case tt case ff. All we need to do is add a constant Case σ Bool of type Case σ Bool : σ σ Bool σ Then it follows that, whenever case tt : σ and case ff : σ, then Case σ Bool case tt case ff : Bool σ The equalities are achieved by adding reductions Case σ Bool case tt case ff tt case tt Case σ Bool case tt case ff ff case ff We define Boolean valued conjunction We write Bool : Bool Bool Bool. Bool for function symbol, Bool for the symbol, written infix, so b Bool c stands for Bool b c. Note that this will be an operation on Booleans. Above we introduced the operation on formulae, which takes two formulae A and B and forms the formula A B. b Bool c will form the Boolean value corresponding to the conjunction of b and c. CS 336/CS M36/CS M46 Sect. 4 (f) 105/ 162 CS 336/CS M36/CS M46 Sect. 4 (f) 106/ 162

30 Truth Table for Bool Example: Bool Bool has the following truth table: So we have Bool ff tt ff ff ff tt ff tt ff Bool b = ff tt Bool b = b Below we will see how to define for every Boolean value b : Bool a formula Atom b corresponding to this value. Then one can show that (Atom b) (Atom c) is equivalent to Atom (b Bool c). This means that b Bool c is true iff b is true and c is true. CS 336/CS M36/CS M46 Sect. 4 (f) 107/ 162 CS 336/CS M36/CS M46 Sect. 4 (f) 108/ 162 Example: Bool Example: Bool Bool : Bool Bool Bool. Bool will be introduced by λ-abstraction, so we get Bool = λ(b, c : Bool).Case Bool Bool e f b For conjunction we have: Bool = λ(b, c : Bool).t We have seen that tt Bool c = c for some term t. t will be defined by case distinction on b, and have result Bool, so we get Bool = λ(b, c : Bool).Case Bool Bool e f b for some e, f. So the if-case e above is c. Furthermore ff Bool c = ff So the else-case f above is ff. CS 336/CS M36/CS M46 Sect. 4 (f) 109/ 162 CS 336/CS M36/CS M46 Sect. 4 (f) 110/ 162

31 Example: Bool The Finite Sets In total we define therefore _ Bool _ = λ(b, c : Bool).Case Bool Bool c ff b : Bool Bool Bool We verify the correctness of this definition: tt Bool c = Bool tt c = Case Bool Bool c ff tt = c. as desired. ff Bool c = Bool ff c = Case Bool Bool c ff ff = ff. Correct as desired. Bool can be generalised to sets having n elements (n a fixed natural number): We add for every n N a new ground type Fin n. We add for every k N s.t. k < n a new constant Informally we will have have A n k : Fin n Fin n = {A n 0, A n 1,..., A n n 1} especially in the cases n = 2, 1, 0 we have Fin 2 = {A 2 0, A2 1 } Fin 1 = {A 1 0 } Fin 0 = CS 336/CS M36/CS M46 Sect. 4 (f) 111/ 162 The Finite Sets CS 336/CS M36/CS M46 Sect. 4 (f) 112/ 162 Rules for Fin n We have not made use of dependent types yet. n, k are external natural numbers. So we have for each n added one type Finn to the calculus. We have for each n and k < n added one constant A n k to the calculus. We add the principle of case distinction on Fin n : Assume n N, a type σ, and case i : σ for i = 0,..., n 1. Then we want Case σ n case 0 case n 1 : Fin n σ And we want Case σ n case 0 case n 1 A n i = case i CS 336/CS M36/CS M46 Sect. 4 (f) 113/ 162 CS 336/CS M36/CS M46 Sect. 4 (f) 114/ 162

32 Constants Case σ n Equality on Fin n As for Bool, this can be achieved by having constants Case σ n : σ} {{ σ} Fin n σ n times Then from casei : σ we obtain Case σ n case 0 case n 1 : Fin n σ Furthermore we add the reduction rules We can now define the Boolean valued function which determines for two elements of Fin n, whether they are equal: Define Eq n,bool : Fin n Fin n Bool s.t. Eq n,bool A i n A i n = tt Eq n,bool A i n A j n = ff for i j Case σ n case 0 case n 1 A n i case i Eq n,bool can be defined easily (for fixed n) by case distinction on its two arguments. CS 336/CS M36/CS M46 Sect. 4 (f) 115/ 162 Special Case Bool CS 336/CS M36/CS M46 Sect. 4 (f) 116/ 162 Rules for Bool can now be treated as the special case with n = 2: Fin n Bool := Fin 2 tt := A 2 0 : Bool ff := A 2 1 : Bool Case σ Bool := Case σ 2 : σ σ Bool σ (pronounced top ) is the special case for n = 1 (we write true for A 1 0 ): Fin n So we have a type := Fin1, true := A 1 0 :, (pronounced as top, typed in in Agda as \top) Case σ := Case σ 1 : σ σ. case σ a true a. CS 336/CS M36/CS M46 Sect. 4 (f) 117/ 162 CS 336/CS M36/CS M46 Sect. 4 (f) 118/ 162

33 as the True Formula Rules for Above we have seen that formulae can be identified with types for a formula to be true means to have an element of its type. has exactly one proof, and corresponds therefore to the always always true formula. That s why we call the element true, since it is the proof of the always true formula. Example: we have λx A.true : A (pronounced bottom, typed in in Agda as \bot) is the special case Fin n for n = 0: := Fin 0. has no element (Fin n has no element). Case distinction on Fin 0 is empty the number of cases is 0, so we get the empty case distinction. This means that we have Case σ : σ We have no reduction rules. CS 336/CS M36/CS M46 Sect. 4 (f) 119/ 162 CS 336/CS M36/CS M46 Sect. 4 (f) 120/ 162 has no elements. It is the formula, which is always false, since it has no proofs. Often called falsum or absurdity. Case σ expresses: from an element f of we obtain an element of any set. Correct, since there is no element of. Considered as a formula, Case σ means: from a proof of we obtain a proof of every other formula. I.e. it means implies everything. In logic this principle is called Ex falsum quodlibet (from the absurdity follows anything). E.g. A false formula like 0 = 1 or Swansea lies in Germany implies everything. For any formula A we have a proof of A: Case A : A. CS 336/CS M36/CS M46 Sect. 4 (f) 121/ 162 CS 336/CS M36/CS M46 Sect. 4 (f) 122/ 162

34 Negation The negation A of a formula A is true, iff A is false iff there is no proof of A. Now we can show that there is no proof of A iff A is true: If there is no proof of A, then from every proof of A we can obtain a proof of (since there is no proof of A); therefore A is true. On the other hand, if we A is true, i.e. has a proof, then there cannot be any proof of A, because from it we could get a proof of, which is the empty set. Therefore A is true iff A is true. Therefore we can identify A with A. 4 (b) Currying 4 (d) Logic with Conjunction CS 336/CS M36/CS M46 Sect. 4 (f) 123/ 162 CS 336/CS M36/CS M46 Sect. 4 (g) 123a/ 162 Pattern Matching We introduce Bool by listing its constructors data Bool : Set where tt : Bool ff : Bool We can use pattern matching in order to make case distinction on an argument of type Bool: Assume we want to define Bool : Bool Bool Bool tt = ff Bool ff = tt The above is already the Agda code defining Bool. examplenegbool.agda CS 336/CS M36/CS M46 Sect. 4 (g) 124/ 162 CS 336/CS M36/CS M46 Sect. 4 (g) 125/ 162

35 Finite Sets in Agda Finite Sets in Agda Finite sets can be introduced by giving one constructor for each element. E.g. data Colour : Set where blue : Colour red : Colour green : Colour Case distinction on finite sets in Agda can be done using pattern matching. In the Colour example above for instance, we can define is red : Colour Bool is red red = tt is red = ff The above has an overlapping case distinction: is red red matches both lines is red red = tt is red = ff CS 336/CS M36/CS M46 Sect. 4 (g) 126/ 162 Finite Sets in Agda CS 336/CS M36/CS M46 Sect. 4 (g) 127/ 162 in Agda is red : Colour Bool is red red = tt is red = ff The convention is that if there are overlapping patterns, then the first matching pattern is the one which is used. So is red red will be computed by having the first pattern, we get is red red = tt is red blue and is red green are computed using the second pattern, we get The definition of in Agda is straightforward: data : Set where true : We can define a function having an argument in by using pattern matching: g : Bool q true = tt is red blue = is red green = ff CS 336/CS M36/CS M46 Sect. 4 (g) 128/ 162 CS 336/CS M36/CS M46 Sect. 4 (g) 129/ 162

36 in Agda in Agda Alternatively, we can define in Agda as the empty record (note that there is no keyword field): record : Set where Then the element true of is defined as follows true : true = record{ } Agda has a builtin η-rule, which says that every x : is equal to record{ }. exampletrue.agda can be defined as the data -set with no constructors: If we want to define data : Set where g : Bool by pattern matching, we see that there is no element in, so there is no constructor case matching g x. CS 336/CS M36/CS M46 Sect. 4 (g) 130/ 162 in Agda CS 336/CS M36/CS M46 Sect. 4 (g) 131/ 162 in Agda We need to communicate this to Agda (this is needed in order to obtain decidability of pattern matching) by having the following code: g : Bool g () The code g () means: the argument at the position () is an element which matches no pattern, so this case is solved. examplefalse.agda Above we have shown why we can define A as A. Therefore negation can be defined in Agda as follows: : Set Set A = A CS 336/CS M36/CS M46 Sect. 4 (g) 132/ 162 CS 336/CS M36/CS M46 Sect. 4 (g) 133/ 162

37 Example for the Use of Example for the Use of Assume the type of trees: We can now define data Tree : Set where oak : Tree pine : Tree spruce : Tree IsConifer : Tree Set IsConifer oak = IsConifer = So IsConifer x is the false formula, if x = oak, and the true formula otherwise. If we want to define a function from trees, which are conifers, into another set, we can do so by requiring an additional argument IsConifer : f : (t : Tree) IsConifer t A f pine = {!!} f spruce = {!!} f oak () So we need to define f only for pine and spruce, the case where the first argument is oak cannot appear, since in that case the second argument is an element of the empty set, i.e. it matches no pattern. CS 336/CS M36/CS M46 Sect. 4 (g) 134/ 162 Example for the Use of CS 336/CS M36/CS M46 Sect. 4 (g) 135/ 162 Example 2 for the Use of Assume the type Stack of stacks of elements of N given by Note that we don t have to invent a result of f in case t is an oak tree. exampletree1.agda Jump over Example 2 (Stack) data Stack (A : Set) : Set where empty : Stack A push : A Stack A Stack A We can then introduce a predicate NonEmpty expressing that the stack is nonempty: NonEmpty : {A : Set} Stack A Set NonEmpty empty = NonEmpty (push ) = CS 336/CS M36/CS M46 Sect. 4 (g) 136/ 162 CS 336/CS M36/CS M46 Sect. 4 (g) 137/ 162

38 Example 2 for the Use of Atomic Formulae Now we can define top : {A : Set} (s : Stack A) NonEmpty s A top empty () top (push a ) = a (See examplestack.agda). Again we don t have to provide a result, in case s is empty (in general we couldn t provide such a result, since A might be empty). We will now show how to convert in Agda a Boolean value into a formula. Here we will leave the simply-typed λ-calculus, and move to dependent types. The operation which converts Boolean values into atomic formulae is Atom : Bool Set Atom tt = Atom ff = CS 336/CS M36/CS M46 Sect. 4 (g) 138/ 162 Atomic Formulae CS 336/CS M36/CS M46 Sect. 4 (g) 139/ 162 Example Atom : Bool Set Atom tt = Atom ff = So, in case b = tt, Atom b is the true formula, which is provable. In case b = ff, Atom b is the false formula, which is unprovable. exampleatom.agda Above we introduced the Boolean valued equality on Fin n, which for fixed n can be defined in Agda. Eq n,bool : Fin n Fin n Bool Eq n,bool A i n A i n = tt Eq n,bool = ff CS 336/CS M36/CS M46 Sect. 4 (g) 140/ 162 CS 336/CS M36/CS M46 Sect. 4 (g) 141/ 162

39 Example Example For instance in case of the set we define data Colour : Set where blue : Colour red : Colour green : Colour eqcolourbool : Colour Colour Bool eqcolourbool blue blue = tt eqcolourbool red red = tt eqcolourbool green green = tt eqcolourbool = ff We can now convert this equality into a formula as follows: == : Colour Colour Set c == c = Atom (eqcolourbool c c ) c == c is the formula expressing that c and c are the same colour. CS 336/CS M36/CS M46 Sect. 4 (g) 142/ 162 Example CS 336/CS M36/CS M46 Sect. 4 (g) 143/ 162 Example 2 Remember the definition of Boolean valued negation in Agda: == can be defined directly, by unfolding its definition. We obtain: examplecolourequality.agda == : Colour Colour Set blue == blue = red == red = green == green = == = We show Remember that we defined Bool : Bool Bool Bool tt = ff Bool ff = tt Atom ( Bool b) (Atom b) : Set Set A = A CS 336/CS M36/CS M46 Sect. 4 (g) 144/ 162 CS 336/CS M36/CS M46 Sect. 4 (g) 145/ 162

40 Example 2 Example 2 So our lemma is Lemma : Set Lemma = (b : Bool) Atom ( Bool b) (Atom b) Since A = A this is equivalent to Lemma = (b : Bool) Atom ( Bool b) Atom b We need to show lemma : Lemma lemma b p q = {!!} Lemma : Set Lemma = (b : Bool) Atom ( Bool b) Atom b lemma : Lemma lemma b p q = {!!} We need to make case distinction on b = tt and b = ff and replace the last line by the two cases: lemma : Lemma lemma tt p q = {!!} lemma ff p q = {!!} CS 336/CS M36/CS M46 Sect. 4 (g) 146/ 162 Example 2 CS 336/CS M36/CS M46 Sect. 4 (g) 147/ 162 Example 2 Lemma : Set Lemma = (b : Bool) Atom ( Bool b) Atom b lemma : Lemma lemma tt p q = {!!} lemma ff p q = {!!} In the first equation we have b = tt, therefore p : Atom ( Bool b) = Atom ff = So p matches no pattern, we can replace in this case p by (), and have solved this case. Lemma : Set Lemma = (b : Bool) Atom ( Bool b) Atom b lemma : Lemma lemma tt () q lemma ff p q = {!!} In the second case we have b = ff, so q : Atom b = Atom ff = So q matches no pattern, we can replace in this case q by (), and have solved this case as well CS 336/CS M36/CS M46 Sect. 4 (g) 148/ 162 CS 336/CS M36/CS M46 Sect. 4 (g) 149/ 162

41 Example 2 Example 3 Lemma : Set Lemma = (b : Bool) Atom ( Bool b) Atom b lemma : Lemma lemma tt () q lemma ff p () Note that it becomes increasingly complicated to guarantee that all cases are covered. Therefore it is important to check that the code has passed the coverage checker. examplenegbool2.agda Jump over Example 3 ( Bool) We introduce Boolean valued implication Bool : Bool Bool Bool and show that Atom (b Bool b ) implies Atom b Atom b. The other direction can be shown as well. CS 336/CS M36/CS M46 Sect. 4 (g) 150/ 162 Example 3 CS 336/CS M36/CS M46 Sect. 4 (g) 151/ 162 Example 3 Classically A B is true iff A is false or B is true. Something false implies everything, so A B is true if A is false. If A is true, then A B is true if B is true. So we have A B is false if A is true and B is false. In all other cases it is true. We can therefore define the Boolean valued implication as follows Bool : Bool Bool Bool tt Bool ff = ff Bool = tt We introduce the Lemma to be shown and the pattern for the proof: Lemma : Set Lemma = (b b : Bool) Atom (b Bool b ) Atom b Atom b lemma : Lemma lemma b b b b btrue = {!!} We try to make a case distinction which makes as often as possible one of the two proof objects b b : Atom (b Bool b ) or btrue : Atom b false. CS 336/CS M36/CS M46 Sect. 4 (g) 152/ 162 CS 336/CS M36/CS M46 Sect. 4 (g) 153/ 162

4. The λ-calc. with Prod. and Conj.

4. The λ-calc. with Prod. and Conj. 4. The λ-calc. with Prod. and Conj. (4. The λ-calculus with Products and Conjunction) (a) The typed λ-calculus with products. (b) Currying. (Omitted 2008). (c) The nondependent product in Agda. (d) Logic

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ August 17, 2007 Lambda Calculus and Type

More information

Exercise 1 (2+2+2 points)

Exercise 1 (2+2+2 points) 1 Exercise 1 (2+2+2 points) The following data structure represents binary trees only containing values in the inner nodes: data Tree a = Leaf Node (Tree a) a (Tree a) 1 Consider the tree t of integers

More information

λ calculus is inconsistent

λ calculus is inconsistent Content Rough timeline COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Gerwin Klein, June Andronick, Toby Murray λ Intro & motivation, getting started [1] Foundations & Principles

More information

B2. The Logical Framework

B2. The Logical Framework B2. The Logical Framework (a) Basic Form of Rules (b) The non-dependent function type and product. (c) The dependent function type and product. (d) Structural rules. Critical Systems, CS 411, Lentterm

More information

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER 2012 2013 MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS Time allowed TWO hours Candidates may complete the front

More information

CIS 500 Software Foundations Midterm I

CIS 500 Software Foundations Midterm I CIS 500 Software Foundations Midterm I October 11, 2006 Name: Student ID: Email: Status: Section: registered for the course not registered: sitting in to improve a previous grade not registered: just taking

More information

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods Course 2D1453, 2006-07 Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Some material from B. Pierce: TAPL + some from G. Klein, NICTA Alonzo Church, 1903-1995 Church-Turing thesis First

More information

Pure Lambda Calculus. Lecture 17

Pure Lambda Calculus. Lecture 17 Pure Lambda Calculus Lecture 17 Lambda Calculus Lambda Calculus (λ-calculus) is a functional notation introduced by Alonzo Church in the early 1930s to formalize the notion of computability. Pure λ-calculus

More information

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter 2.1-2.7 p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer

More information

CS 275 Automata and Formal Language Theory. First Problem of URMs. (a) Definition of the Turing Machine. III.3 (a) Definition of the Turing Machine

CS 275 Automata and Formal Language Theory. First Problem of URMs. (a) Definition of the Turing Machine. III.3 (a) Definition of the Turing Machine CS 275 Automata and Formal Language Theory Course Notes Part III: Limits of Computation Chapt. III.3: Turing Machines Anton Setzer http://www.cs.swan.ac.uk/ csetzer/lectures/ automataformallanguage/13/index.html

More information

1 Introduction. 3 Syntax

1 Introduction. 3 Syntax CS 6110 S18 Lecture 19 Typed λ-calculus 1 Introduction Type checking is a lightweight technique for proving simple properties of programs. Unlike theorem-proving techniques based on axiomatic semantics,

More information

CITS3211 FUNCTIONAL PROGRAMMING

CITS3211 FUNCTIONAL PROGRAMMING CITS3211 FUNCTIONAL PROGRAMMING 9. The λ calculus Summary: This lecture introduces the λ calculus. The λ calculus is the theoretical model underlying the semantics and implementation of functional programming

More information

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

More information

Exercise 1 ( = 22 points)

Exercise 1 ( = 22 points) 1 Exercise 1 (4 + 3 + 4 + 5 + 6 = 22 points) The following data structure represents polymorphic lists that can contain values of two types in arbitrary order: data DuoList a b = C a (DuoList a b) D b

More information

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008 CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008 Contents 1 Solution to the Exercise 1 1.1 Semantics for lambda calculus.......................

More information

MLW. Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky. March 26, Radboud University Nijmegen

MLW. Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky. March 26, Radboud University Nijmegen 1 MLW Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky Radboud University Nijmegen March 26, 2012 inductive types 2 3 inductive types = types consisting of closed terms built from constructors

More information

Higher-Order Logic. Specification and Verification with Higher-Order Logic

Higher-Order Logic. Specification and Verification with Higher-Order Logic Higher-Order Logic Specification and Verification with Higher-Order Logic Arnd Poetzsch-Heffter (Slides by Jens Brandt) Software Technology Group Fachbereich Informatik Technische Universität Kaiserslautern

More information

Exercise 1 ( = 24 points)

Exercise 1 ( = 24 points) 1 Exercise 1 (4 + 5 + 4 + 6 + 5 = 24 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a =

More information

On Agda JAIST/AIST WS CVS/AIST Yoshiki Kinoshita, Yoriyuki Yamagata. Agenda

On Agda JAIST/AIST WS CVS/AIST Yoshiki Kinoshita, Yoriyuki Yamagata. Agenda On Agda 2009.3.12 JAIST/AIST WS CVS/AIST Yoshiki Kinoshita, Yoriyuki Yamagata Agenda On Agda Agda as a programming language Agda as a proof system Further information. 2 1 Agenda On Agda Agda as a programming

More information

Programming with Dependent Types Interactive programs and Coalgebras

Programming with Dependent Types Interactive programs and Coalgebras Programming with Dependent Types Interactive programs and Coalgebras Anton Setzer Swansea University, Swansea, UK 14 August 2012 1/ 50 A Brief Introduction into ML Type Theory Interactive Programs in Dependent

More information

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011 CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011 1 Introduction Type checking is a lightweight technique for proving simple properties of programs. Unlike theorem-proving techniques based on axiomatic

More information

Concepts of programming languages

Concepts of programming languages Concepts of programming languages Lecture 5 Wouter Swierstra 1 Announcements Submit your project proposal to me by email on Friday; The presentation schedule in now online Exercise session after the lecture.

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ October 13, 2004 Lambda Calculus and Type

More information

Fundamentals and lambda calculus

Fundamentals and lambda calculus Fundamentals and lambda calculus Again: JavaScript functions JavaScript functions are first-class Syntax is a bit ugly/terse when you want to use functions as values; recall block scoping: (function ()

More information

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Toby Murray, June Andronick, Gerwin Klein λ 1 Last time... λ calculus syntax free variables, substitution β reduction α and η conversion

More information

Introduction to the Lambda Calculus. Chris Lomont

Introduction to the Lambda Calculus. Chris Lomont Introduction to the Lambda Calculus Chris Lomont 2010 2011 2012 www.lomont.org Leibniz (1646-1716) Create a universal language in which all possible problems can be stated Find a decision method to solve

More information

Lecture Notes on Data Representation

Lecture Notes on Data Representation Lecture Notes on Data Representation 15-814: Types and Programming Languages Frank Pfenning Lecture 9 Tuesday, October 2, 2018 1 Introduction In this lecture we ll see our type system in action. In particular

More information

Chapter 5: The Untyped Lambda Calculus

Chapter 5: The Untyped Lambda Calculus Chapter 5: The Untyped Lambda Calculus What is lambda calculus for? Basics: syntax and operational semantics Programming in the Lambda Calculus Formalities (formal definitions) What is Lambda calculus

More information

Programming Languages

Programming Languages CSE 230: Winter 2008 Principles of Programming Languages Ocaml/HW #3 Q-A Session Push deadline = Mar 10 Session Mon 3pm? Lecture 15: Type Systems Ranjit Jhala UC San Diego Why Typed Languages? Development

More information

Basic Foundations of Isabelle/HOL

Basic Foundations of Isabelle/HOL Basic Foundations of Isabelle/HOL Peter Wullinger May 16th 2007 1 / 29 1 Introduction into Isabelle s HOL Why Type Theory Basic Type Syntax 2 More HOL Typed λ Calculus HOL Rules 3 Example proof 2 / 29

More information

The Typed λ Calculus and Type Inferencing in ML

The Typed λ Calculus and Type Inferencing in ML Notes on Types S. Arun-Kumar Department of Computer Science and Engineering Indian Institute of Technology New Delhi, 110016 email: sak@cse.iitd.ernet.in April 14, 2002 2 Chapter 1 The Typed λ Calculus

More information

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

5. Introduction to the Lambda Calculus. Oscar Nierstrasz 5. Introduction to the Lambda Calculus Oscar Nierstrasz Roadmap > What is Computability? Church s Thesis > Lambda Calculus operational semantics > The Church-Rosser Property > Modelling basic programming

More information

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations Outline FP Foundations, Scheme In Text: Chapter 15 Mathematical foundations Functional programming λ-calculus LISP Scheme 2 Imperative Languages We have been discussing imperative languages C/C++, Java,

More information

Lecture 9: More Lambda Calculus / Types

Lecture 9: More Lambda Calculus / Types Lecture 9: More Lambda Calculus / Types CSC 131 Spring, 2019 Kim Bruce Pure Lambda Calculus Terms of pure lambda calculus - M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete

More information

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v. Pure Lambda Calculus Lecture 9: More Lambda Calculus / Types CSC 131 Spring, 2019 Kim Bruce Terms of pure lambda calculus - M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete

More information

CSE-321 Programming Languages 2012 Midterm

CSE-321 Programming Languages 2012 Midterm Name: Hemos ID: CSE-321 Programming Languages 2012 Midterm Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 14 15 29 20 7 15 100 There are six problems on 24 pages in this exam. The maximum score

More information

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics Overview A normal-order language Strictness Recursion Infinite data structures Direct denotational semantics Transition semantics Lazy (call-by-need) evaluation and its semantics A Normal-Order Language

More information

CSE-321 Programming Languages 2010 Midterm

CSE-321 Programming Languages 2010 Midterm Name: Hemos ID: CSE-321 Programming Languages 2010 Midterm Score Prob 1 Prob 2 Prob 3 Prob 4 Total Max 15 30 35 20 100 1 1 SML Programming [15 pts] Question 1. [5 pts] Give a tail recursive implementation

More information

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu, Lambda Calculus Type Systems, Lectures 3 Jevgeni Kabanov Tartu, 13.02.2006 PREVIOUSLY ON TYPE SYSTEMS Arithmetical expressions and Booleans Evaluation semantics Normal forms & Values Getting stuck Safety

More information

Overview. A Compact Introduction to Isabelle/HOL. Tobias Nipkow. System Architecture. Overview of Isabelle/HOL

Overview. A Compact Introduction to Isabelle/HOL. Tobias Nipkow. System Architecture. Overview of Isabelle/HOL Overview A Compact Introduction to Isabelle/HOL Tobias Nipkow TU München 1. Introduction 2. Datatypes 3. Logic 4. Sets p.1 p.2 System Architecture Overview of Isabelle/HOL ProofGeneral Isabelle/HOL Isabelle

More information

Typed Lambda Calculus

Typed Lambda Calculus Department of Linguistics Ohio State University Sept. 8, 2016 The Two Sides of A typed lambda calculus (TLC) can be viewed in two complementary ways: model-theoretically, as a system of notation for functions

More information

1 Scope, Bound and Free Occurrences, Closed Terms

1 Scope, Bound and Free Occurrences, Closed Terms CS 6110 S18 Lecture 2 The λ-calculus Last time we introduced the λ-calculus, a mathematical system for studying the interaction of functional abstraction and functional application. We discussed the syntax

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 18 Thursday, March 29, 2018 In abstract algebra, algebraic structures are defined by a set of elements and operations

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 27 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 21 November, 2011 1 / 19 1 2 3 4 2 / 19 Semantics for programming

More information

Logical Verification Course Notes. Femke van Raamsdonk Vrije Universiteit Amsterdam

Logical Verification Course Notes. Femke van Raamsdonk Vrije Universiteit Amsterdam Logical Verification Course Notes Femke van Raamsdonk femke@csvunl Vrije Universiteit Amsterdam autumn 2008 Contents 1 1st-order propositional logic 3 11 Formulas 3 12 Natural deduction for intuitionistic

More information

Introduction to dependent types in Coq

Introduction to dependent types in Coq October 24, 2008 basic use of the Coq system In Coq, you can play with simple values and functions. The basic command is called Check, to verify if an expression is well-formed and learn what is its type.

More information

Typed Lambda Calculus for Syntacticians

Typed Lambda Calculus for Syntacticians Department of Linguistics Ohio State University January 12, 2012 The Two Sides of Typed Lambda Calculus A typed lambda calculus (TLC) can be viewed in two complementary ways: model-theoretically, as a

More information

Inductive Definitions, continued

Inductive Definitions, continued 1 / 27 Inductive Definitions, continued Assia Mahboubi Jan 7th, 2016 2 / 27 Last lecture Introduction to Coq s inductive types: Introduction, elimination and computation rules; Twofold implementation :

More information

HOL DEFINING HIGHER ORDER LOGIC LAST TIME ON HOL CONTENT. Slide 3. Slide 1. Slide 4. Slide 2 WHAT IS HIGHER ORDER LOGIC? 2 LAST TIME ON HOL 1

HOL DEFINING HIGHER ORDER LOGIC LAST TIME ON HOL CONTENT. Slide 3. Slide 1. Slide 4. Slide 2 WHAT IS HIGHER ORDER LOGIC? 2 LAST TIME ON HOL 1 LAST TIME ON HOL Proof rules for propositional and predicate logic Safe and unsafe rules NICTA Advanced Course Forward Proof Slide 1 Theorem Proving Principles, Techniques, Applications Slide 3 The Epsilon

More information

Exercise 1 ( = 18 points)

Exercise 1 ( = 18 points) 1 Exercise 1 (4 + 5 + 4 + 5 = 18 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a = Leaf

More information

Typed Lambda Calculus and Exception Handling

Typed Lambda Calculus and Exception Handling Typed Lambda Calculus and Exception Handling Dan Zingaro zingard@mcmaster.ca McMaster University Typed Lambda Calculus and Exception Handling p. 1/2 Untyped Lambda Calculus Goal is to introduce typing

More information

Z Notation. June 21, 2018

Z Notation. June 21, 2018 Z Notation June 21, 2018 1 Definitions There are many different ways to introduce an object in a Z specification: declarations, abbreviations, axiomatic definitions, and free types. Keep in mind that the

More information

CS 6110 S14 Lecture 1 Introduction 24 January 2014

CS 6110 S14 Lecture 1 Introduction 24 January 2014 CS 6110 S14 Lecture 1 Introduction 24 January 2014 1 Introduction What is a program? Is it just something that tells the computer what to do? Yes, but there is much more to it than that. The basic expressions

More information

CSE 505: Concepts of Programming Languages

CSE 505: Concepts of Programming Languages CSE 505: Concepts of Programming Languages Dan Grossman Fall 2003 Lecture 6 Lambda Calculus Dan Grossman CSE505 Fall 2003, Lecture 6 1 Where we are Done: Modeling mutation and local control-flow Proving

More information

Lambda Calculus as a Programming Language

Lambda Calculus as a Programming Language Cristian Giumale / Lecture Notes 1 Lambda Calculus as a Programming Language The Lambda calculus can be considered as the machine code of a particular computer. Call it the Lambda machine. As in conventional

More information

Introduction to the Lambda Calculus

Introduction to the Lambda Calculus Introduction to the Lambda Calculus Overview: What is Computability? Church s Thesis The Lambda Calculus Scope and lexical address The Church-Rosser Property Recursion References: Daniel P. Friedman et

More information

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine. CMSC 330: Organization of Programming Languages Lambda Calculus Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying or tuples

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 1 Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying

More information

Lambda Calculus and Extensions as Foundation of Functional Programming

Lambda Calculus and Extensions as Foundation of Functional Programming Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauß 29. September 2015 Lehrerbildungsforum Informatik Last update: 30. September 2015 Overview

More information

An Introduction to Programming and Proving in Agda (incomplete draft)

An Introduction to Programming and Proving in Agda (incomplete draft) An Introduction to Programming and Proving in Agda (incomplete draft) Peter Dybjer January 29, 2018 1 A first Agda module Your first Agda-file is called BoolModule.agda. Its contents are module BoolModule

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010 Pure (Untyped) λ-calculus Andrey Kruglyak, 2010 1 Pure (untyped) λ-calculus An example of a simple formal language Invented by Alonzo Church (1936) Used as a core language (a calculus capturing the essential

More information

Lecture 5: The Untyped λ-calculus

Lecture 5: The Untyped λ-calculus Lecture 5: The Untyped λ-calculus Syntax and basic examples Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Static Analysis Pratikakis (CSD) Untyped λ-calculus I CS49040,

More information

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete Dr A Sahu Dept of Computer Science & Engineering IIT Guwahati Why study? Lambda calculus Syntax Evaluation Relationship to programming languages Church Rosser theorem Completeness of Lambda Calculus: Turing

More information

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham λ-calculus Lecture 1 Venanzio Capretta MGS 2018 - Nottingham Table of contents 1. History of λ-calculus 2. Definition of λ-calculus 3. Data Structures 1 History of λ-calculus Hilbert s Program David Hilbert

More information

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic 3.4 Deduction and Evaluation: Tools 3.4.1 Conditional-Equational Logic The general definition of a formal specification from above was based on the existence of a precisely defined semantics for the syntax

More information

Lambda Calculus. Lecture 4 CS /26/10

Lambda Calculus. Lecture 4 CS /26/10 Lambda Calculus Lecture 4 CS 565 10/26/10 Pure (Untyped) Lambda Calculus The only value is a function Variables denote functions Functions always take functions as arguments Functions always return functions

More information

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland COMP 1130 Lambda Calculus based on slides by Jeff Foster, U Maryland Motivation Commonly-used programming languages are large and complex ANSI C99 standard: 538 pages ANSI C++ standard: 714 pages Java

More information

Introduction to the λ-calculus

Introduction to the λ-calculus Announcements Prelim #2 issues: o Problem 5 grading guide out shortly o Problem 3 (hashing) issues Will be on final! Friday RDZ office hours are 11-12 not 1:30-2:30 1 Introduction to the λ-calculus Today

More information

Handout 10: Imperative programs and the Lambda Calculus

Handout 10: Imperative programs and the Lambda Calculus 06-02552 Princ of Progr Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 10: Imperative programs and the Lambda Calculus

More information

Lambda Calculus. Lambda Calculus

Lambda Calculus. Lambda Calculus Lambda Calculus Formalism to describe semantics of operations in functional PLs Variables are free or bound Function definition vs function abstraction Substitution rules for evaluating functions Normal

More information

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming

More information

Recursive Types and Subtyping

Recursive Types and Subtyping Recursive Types and Subtyping #1 One-Slide Summary Recall: Recursive types (e.g., τ list) make the typed lambda calculus as powerful as the untyped lambda calculus. If τ is a subtype of σ then any expression

More information

Types and Programming Languages. Lecture 5. Extensions of simple types

Types and Programming Languages. Lecture 5. Extensions of simple types Types and Programming Languages Lecture 5. Extensions of simple types Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao Tong University Fall, 2016 Coming soon Simply typed λ-calculus has enough structure

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Tuesday, February 19, 2013 The lambda calculus (or λ-calculus) was introduced by Alonzo Church and Stephen Cole Kleene in

More information

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

Lecture Notes on Program Equivalence

Lecture Notes on Program Equivalence Lecture Notes on Program Equivalence 15-312: Foundations of Programming Languages Frank Pfenning Lecture 24 November 30, 2004 When are two programs equal? Without much reflection one might say that two

More information

Formal Systems and their Applications

Formal Systems and their Applications Formal Systems and their Applications Dave Clarke (Dave.Clarke@cs.kuleuven.be) Acknowledgment: these slides are based in part on slides from Benjamin Pierce and Frank Piessens 1 Course Overview Introduction

More information

(Refer Slide Time: 4:00)

(Refer Slide Time: 4:00) Principles of Programming Languages Dr. S. Arun Kumar Department of Computer Science & Engineering Indian Institute of Technology, Delhi Lecture - 38 Meanings Let us look at abstracts namely functional

More information

Lambda Calculus as a Programming Language

Lambda Calculus as a Programming Language Cristian Giumale / Lecture Notes 1 Lambda Calculus as a Programming Language The Lambda calculus can be considered as the machine code of a particular computer. Call it the Lambda machine. As in conventional

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 1 Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Use currying

More information

Functional Programming. Overview. Topics. Recall λ-terms. Examples

Functional Programming. Overview. Topics. Recall λ-terms. Examples Topics Functional Programming Christian Sternagel Harald Zankl Evgeny Zuenko Department of Computer Science University of Innsbruck WS 2017/2018 abstract data types, algebraic data types, binary search

More information

Programming Languages Lecture 14: Sum, Product, Recursive Types

Programming Languages Lecture 14: Sum, Product, Recursive Types CSE 230: Winter 200 Principles of Programming Languages Lecture 4: Sum, Product, Recursive Types The end is nigh HW 3 No HW 4 (= Final) Project (Meeting + Talk) Ranjit Jhala UC San Diego Recap Goal: Relate

More information

CS4215 Programming Language Implementation. Martin Henz

CS4215 Programming Language Implementation. Martin Henz CS4215 Programming Language Implementation Martin Henz Thursday 26 January, 2012 2 Chapter 4 The Language simpl In this chapter, we are exting the language epl in order to provide a more powerful programming

More information

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Introduction to Lambda Calculus. Lecture 7 CS /08/09 Introduction to Lambda Calculus Lecture 7 CS 565 02/08/09 Lambda Calculus So far, we ve explored some simple but non-interesting languages language of arithmetic expressions IMP (arithmetic + while loops)

More information

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics Dan Grossman Spring 2011 Review e ::= λx. e x e e c v ::= λx. e c τ ::= int τ τ Γ ::= Γ, x : τ (λx. e) v e[v/x] e 1 e 1 e 1 e

More information

Introduction to Programming, Aug-Dec 2006

Introduction to Programming, Aug-Dec 2006 Introduction to Programming, Aug-Dec 2006 Lecture 3, Friday 11 Aug 2006 Lists... We can implicitly decompose a list into its head and tail by providing a pattern with two variables to denote the two components

More information

Lecture 14: Recursive Types

Lecture 14: Recursive Types Lecture 14: Recursive Types Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Programming Languages Pratikakis (CSD) Recursive Types CS546, 2018-2019 1 / 11 Motivation

More information

Functional Logic Programming: From Theory to Curry

Functional Logic Programming: From Theory to Curry Functional Logic Programming: From Theory to Curry Michael Hanus Institut für Informatik, CAU Kiel, D-24098 Kiel, Germany. mh@informatik.uni-kiel.de Abstract. Functional logic programming languages combine

More information

Reasoning About Imperative Programs. COS 441 Slides 10

Reasoning About Imperative Programs. COS 441 Slides 10 Reasoning About Imperative Programs COS 441 Slides 10 The last few weeks Agenda reasoning about functional programming It s very simple and very uniform: substitution of equal expressions for equal expressions

More information

A computer implemented philosophy of mathematics

A computer implemented philosophy of mathematics A computer implemented philosophy of mathematics M. Randall Holmes May 14, 2018 This paper presents a philosophical view of the basic foundations of mathematics, which is implemented in actual computer

More information

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides) Fundamentals and lambda calculus Deian Stefan (adopted from my & Edward Yang s CSE242 slides) Logistics Assignments: Programming assignment 1 is out Homework 1 will be released tomorrow night Podcasting:

More information

CSE-321 Programming Languages 2011 Final

CSE-321 Programming Languages 2011 Final Name: Hemos ID: CSE-321 Programming Languages 2011 Final Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 15 15 10 17 18 25 100 There are six problems on 18 pages in this exam, including one extracredit

More information

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus CMSC 330: Organization of Programming Languages Lambda Calculus 1 Turing Completeness Turing machines are the most powerful description of computation possible They define the Turing-computable functions

More information

Recursive Definitions, Fixed Points and the Combinator

Recursive Definitions, Fixed Points and the Combinator Recursive Definitions, Fixed Points and the Combinator Dr. Greg Lavender Department of Computer Sciences University of Texas at Austin Recursive Self-Reference Recursive self-reference occurs regularly

More information

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction CITS3211 FUNCTIONAL PROGRAMMING 14. Graph reduction Summary: This lecture discusses graph reduction, which is the basis of the most common compilation technique for lazy functional languages. CITS3211

More information

Programming Language Concepts: Lecture 14

Programming Language Concepts: Lecture 14 Programming Language Concepts: Lecture 14 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 14, 11 March 2009 Function programming

More information

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus CS 4110 Programming Languages & Logics Lecture 17 Programming in the λ-calculus 10 October 2014 Announcements 2 Foster Office Hours 11-12 Enjoy fall break! Review: Church Booleans 3 We can encode TRUE,

More information

Fundamental Concepts. Chapter 1

Fundamental Concepts. Chapter 1 Chapter 1 Fundamental Concepts This book is about the mathematical foundations of programming, with a special attention on computing with infinite objects. How can mathematics help in programming? There

More information