Theory of Computation Lecture 4: Programs and Computable Functions II

Slides:



Advertisements
Similar presentations
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Advertisements

October 13, 2009Theory of Computation Lecture 11: A Universal Program III 1 Coding Programs by Numbers Gödel numbers are usually very large, even for small.
Introduction to C Programming
October 27, 2009Theory of Computation Lecture 12: A Universal Program IV 1Universality Then we append the following instruction: [C]IF K = Lt(Z) + 1 
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
September 17, 2009Theory of Computation Lecture 4: Programs and Computable Functions III 1 Macros Now we want to use macros of the form: W  f(V 1, …,
Introduction to C Programming
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Theory of Computation Lecture 5: Primitive Recursive Functions I
An introduction to functional programming using Haskell CENG242 –Recitation 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
24-Jun-16 Haskell Dealing with impurity. Purity Haskell is a “pure” functional programming language Functions have no side effects Input/output is a side.
Advanced Functional Programming 2010
Functional Programming
CSE202: Introduction to Formal Languages and Automata Theory
Applied Discrete Mathematics Week 2: Functions and Sequences
Types CSCE 314 Spring 2016.
ARITHMETIC IN C Operators.
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
Theory of Computation Lecture 12: A Universal Program III
Theory Of Computer Science
A lightening tour in 45 minutes
Debugging and Random Numbers
Revision Lecture
Functional Programming
Theory of Computation Lecture 23: Turing Machines IV
Theory of Computation Lecture 14: A Universal Program V
Theory of Computation Lecture 22: Turing Machines III
Great Theoretical Ideas in Computer Science
Control Structures – Selection
Theory of Computation Lecture 5: Programs and Computable Functions III
Theory of Computation Lecture 6: Primitive Recursive Functions I
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 30-Nov-18.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Theory Of Computer Science
Type & Typeclass Syntax in function
The Programming Language L
CSCE 314: Programming Languages Dr. Dylan Shell
Pattern Matching Pattern matching allows us to do things like this:
int [] scores = new int [10];
Fundamentals of Functional Programming
Lesson #6 Modular Programming and Functions.
Haskell Dealing with impurity 8-Apr-19.
PROGRAMMING IN HASKELL
Theory of Computation Lecture 21: Turing Machines II
Numerical Representation of Strings
Recursively Enumerable Sets
Programming Languages
Chapter 18 Recursion.
Functional Programming
Theory of Computation Lecture 5: Programs and Computable Functions III
The Programming Language L
Matlab Basics.
Haskell Dealing with impurity 29-May-19.
Theory of Computation Lecture 6: Primitive Recursive Functions I
Theory of Computation Lecture 22: Turing Machines II
Haskell Dealing with impurity 28-Jun-19.
Theory of Computation Lecture 13: A Universal Program V
Class code for pythonroom.com cchsp2cs
10.1 Sequences and Summation Notation
Theory of Computation Lecture 11: A Universal Program III
Theory of Computation Lecture 23: Turing Machines III
Presentation transcript:

Theory of Computation Lecture 4: Programs and Computable Functions II Recursion Since variables in Haskell are immutable, our only way of achieving iteration is through recursion. For example, the reverse function receives a list as its input and outputs the same list but with its elements in reverse order: reverse :: [a] -> [a] reverse [] = [] reverse (x:xs) = reverse xs ++ [x] February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II The $ Operator The $ operator is defined as follows: f $ x = f x It has the lowest precedence, and therefore, the value on its right is evaluated first before the function on its left is applied to it. As a consequence, it allows us to omit parentheses: negate (sum (map sqrt [1..10])) can be written as: negate $ sum $ map sqrt [1..10] February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Function Composition Similarly, we can use function composition to make our code more readable and to create new functions. As you know, in mathematics, function composition works like this: (f  g) (x) = f(g(x)) In Haskell, we use the “.” character instead: map (\xs -> negate (sum (tail xs))) [[1..5],[3..6],[1..7]] Can be written as: map (negate . sum . tail) [[1..5],[3..6],[1..7]] February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Data Types Data types can be declared as follows: data Bool = False | True data Shape = Circle Float Float Float | Rectangle Float Float Float Float deriving Show Then we can construct values of these types like this: x = Circle 3 4 5 The “deriving Show” line makes these values printable by simply using the show function (object-to-string conversion) the way it is defined for the individual objects (here: floats). February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Data Types We can use pattern matching on our custom data types: surface :: Shape -> Float surface (Circle _ _ r) = 3.1416 * r ^ 2 surface (Rectangle x1 y1 x2 y2) = (abs $ x2 - x1) * (abs $ y2 - y1) surface $ Circle 10 20 10 314.16 February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Records If we want to name the components of our data types, we can use records: data Car = Car {company :: [Char], model :: [Char], year :: Int} deriving Show myCar = Car {company="Ford", model="Mustang", year=1967} company myCar “Ford” February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Input/Output with “do” Purely functional code cannot perform user interactions such as input and output, because it would involve side effects. Therefore, we sometimes have to use impure functional code, which needs to be separated from the purely functional code in order to keep it (relatively) bug-safe. In Haskell, this is done by so-called Monads. To fully understand this concept, more in-depth study is necessary. However, in this course, we do not need to perform much input and output. We can use a simple wrapper (or “syntactic sugar”) for this – the “do” notation. February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Input/Output with “do” In a do-block, we can only use statements whose type is “tagged” IO so that they cannot be mixed with purely functional statements. Example for a program performing input and output: main = do putStrLn "Hello, what's your name?" name <- getLine putStrLn ("Hey " ++ name ++ ", you rock!") February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II The Syntax of L Now back to our programming language L… A computation of a program P is defined to be a sequence s1, s2, …, sk of snapshots of P such that si+1 is the successor of si for i = 1, 2, …, k – 1 and sk is terminal. February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II What does it exactly mean when we say that a program computes a function? We would like to find a precise definition for this. Let P be any program in the language L and let r1, …, rm be m given numbers. We form the state  of P which consists of the equations X1 = r1, X2 = r2, …, Xm = rm, Y = 0 together with the equations V = 0 for all other variables V in P. We call this the initial state. February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Moreover, we call the snapshot (1, ) the initial snapshot. When running the program, there are two possible outcomes: Case 1: There is a computation s1, s2, …, sk of P beginning with the initial snapshot. Then we write P(m)(r1, r2, …, rm) for the value of the variable Y at the terminal snapshot sk. February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Case 2: There is no such computation; i.e., there is an infinite sequence s1, s2, s3, …beginning with the initial snapshot. In this case, P(m)(r1, r2, …, rm) is undefined. February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Sample Computation (1, {X = r, Y = 0, Z = 0}), (4, {X = r, Y = 0, Z = 0}), (5, {X = r - 1, Y = 0, Z = 0}), (6, {X = r - 1, Y = 1, Z = 0}), (7, {X = r - 1, Y = 1, Z = 1}), (1, {X = r - 1, Y = 1, Z = 1}), . (1, {X = 0, Y = r, Z = r}), (2, {X = 0, Y = r, Z = r}), (3, {X = 0, Y = r, Z = r + 1}), (8, {X = 0, Y = r, Z = r + 1}). [A] IF X0 GOTO B (1) Z  Z+1 (2) IF Z0 GOTO E (3) [B] X  X-1 (4) Y  Y+1 (5) Z  Z+1 (6) IF Z0 GOTO A (7) We write: P(1)(r) = r. February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II For any program P and any positive integer m, the function P(m)(r1, r2, …, rm) is said to be computed by P. We allow any program to be run with any number of inputs. Consider the summation program from the previous lecture: P(2)(r1, r2) = r1 + r2 P(1)(r1) = r1 + 0 = r1 P(3)(r1, r2 , r3) = r1 + r2 February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II In general, a partial function f on a set Sm is a function whose domain is a subset of Sm. If a partial function on Sm has the domain Sm, then it is called total. February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II A given partial function g (of one or more variables) is said to be partially computable if it is computed by some program. This is the case if there is a program P such that g(r1, r2, …, rm) = P(m)(r1, r2, …, rm) for all r1, r2, …, rm. This means not only that both sides have the same value when they are defined, but also that when either side of the equation is undefined, the other one is as well. A function is said to be computable if it is both partially computable and total. February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Macros So far we have used macros in an informal way. Let us now develop a more precise definition of them. Let f(x1, x2, …, xn) be a partially computable function, and P be a program that computes f. Let us assume that the variables in P are named Y, X1, …, Xn, Z1, …, Zk, the labels in P are named E, A1, …, Al, and for each instruction of P of the form IF V0 GOTO Ai there is in P an instruction labeled Ai . February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Macros We can modify any program of the language L to comply with these assumptions. We write: P = P (Y, X1, …, Xn, Z1, …, Zk; E, A1, …, Al) In particular, we will use: Qm = P (Zm, Zm+1, …, Zm+n, Zm+n+1, …, Zm+n+k; Em, Am+1, …, Am+l) for a given value m. February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Macros Now we want to use macros of the form: W  f(V1, …, Vn) in our programs, where W, V1, …, Vn can be any variables; W could be among V1, …, Vn. We expand the macro as follows: Zm  0 Zm+1  V1 Zm+2  V2 : Zm+n  Vn Zm+n+1  0 Zm+n+2  0 Zm+n+k  0 Qm [Em] W  Zm February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Macros Whenever we expand a macro, the number m has to be chosen large enough so that none of the variables or labels in Qm occur in the main program. Note that in the expansion the output and local variables are set to zero, although at the start of the main program they are set to zero anyway. This is necessary because the macro expansion may be part of a loop in the main program. February 7, 2019 Theory of Computation Lecture 4: Programs and Computable Functions II