introduction to
play

Introduction to Computer Science Page 1 of 184 Sanjiva - PowerPoint PPT Presentation

Home Page Title Page Introduction to Computer Science Page 1 of 184 Sanjiva Prasad Go Back sanjiva@cse.iitd.ernet.in Full Screen Department of Computer Science and Engineering I. I. T. Delhi, Hauz Khas, New Delhi


  1. A one element set Home Page prompt> ocaml Title Page Objective Caml version 3.12.0 ◭◭ ◮◮ # ();; ◭ ◮ - : unit = () Page 22 of 184 # Go Back ˆCInterrupted. Full Screen # ˆD Close prompt> Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  2. A two element set prompt> ocaml Home Page Objective Caml version 3.12.0 # true;; Title Page - : bool = true ◭◭ ◮◮ # false;; ◭ ◮ - : bool = false Page 23 of 184 # not true;; Go Back - : bool = false Full Screen # not false;; - : bool = true Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  3. And, Or Home Page # true & false;; Title Page - : bool = false # true & true;; ◭◭ ◮◮ - : bool = true # false & false;; ◭ ◮ - : bool = false Page 24 of 184 # true or false;; Go Back - : bool = true # true or true;; Full Screen - : bool = true # false or false;; Close - : bool = false Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  4. Home Page Computing Basics Title Page Algorithmic Computation: A finite specification of the ◭◭ ◮◮ solution to a given problem using the primitives of the computing tool. ◭ ◮ • It specifies a definite relationship between input and Page 25 of 184 output • It is unambiguous Go Back • It specifies a solution as a finite process, i.e. the Full Screen number of steps in the computation is finite Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  5. Writing Algorithms Home Page An algorithm will be written in a mixture of English and standard mathematical notation (usually called pseudo- Title Page code ). Usually, • algorithms written in a natural language are often ◭◭ ◮◮ ambiguous ◭ ◮ • mathematical notation is not ambiguous, but still cannot be understood by a machine Page 26 of 184 • algorithms written by us use various mathemati- Go Back cal properties. We know them, but the machine doesn’t. Full Screen • Even mathematical notation is often not quite pre- cise and certain intermediate objects or steps are Close left to the understanding of the reader (e.g. the use of “ · · · ” and “. . .” or “similarly”). Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  6. Programming Home Page Language Title Page ◭◭ ◮◮ • Require a way to communicate with a machine which has essentially no intelligence or understand- ◭ ◮ ing. • Translate the algorithm into a form that may be “un- Page 27 of 184 derstood” by a machine Go Back • This “form” is usually a program Program: An algorithm written in a programming lan- Full Screen guage. Close c.f. Wirth’s book “Algorithms+Data Structures=Programs” Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  7. Home Page Language Basics Title Page • Every programming language has a well defined vocabulary and a well defined grammar (called the ◭◭ ◮◮ syntax of the language). ◭ ◮ • Each program has to be written following rigid grammatical rules (syntactic rules). Page 28 of 184 • A programming language, and the computer (and the translator and system software in between) to- Go Back gether form our single computing tool Full Screen • Each program uses only the primitives of the com- puting tool Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  8. Home Page Programs Title Page Program: An algorithm written in the grammar of a pro- gramming language. ◭◭ ◮◮ A grammar is a set of rules for forming phrases or sen- ◭ ◮ tences in a language. Page 29 of 184 Each programming language also has its own vocab- ulary and grammar just as in the case of natural lan- Go Back guages. Full Screen We will learn the vocabulary and grammar of the lan- guage as we go along. Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  9. Home Page Programming Title Page The act of writing programs and validating (testing, rea- soning about) them is programming ◭◭ ◮◮ Even though most programming languages use essen- ◭ ◮ tially the same computing primitives, each program- Page 30 of 184 ming language needs to be learned. (cf “taught”) Programming languages differ from one another in Go Back terms of the convenience and facilities they offer even Full Screen though they usually are all “equally” “powerful” in terms of what they can (or can’t) actually compute. Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  10. Computing Models Home Page We consider mainly two models. Title Page • Value-oriented (Declarative or Functional): A pro- ◭◭ ◮◮ gram is specified simply as a mathematical expres- sion – focus on specifying what needs to be com- ◭ ◮ puted. Page 31 of 184 • State-oriented (Imperative: A program is specified by a sequence of commands to be executed – more Go Back attention on how to compute it. Full Screen Programming languages also come mainly in these two flavours. We will often identify the computing Close model with the programming language. Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  11. Home Page Primitives Title Page ◭◭ ◮◮ Every programming language offers the following ca- pabilities to define and use: ◭ ◮ • Primitive expressions and data Page 32 of 184 • Methods of combination of expressions and data • Methods of abstraction of both expressions and Go Back data Full Screen The functional model Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  12. Home Page Primitive expressions Title Page The simple objects and operations in the computing model. These include ◭◭ ◮◮ • basic data elements: numbers, characters, truth ◭ ◮ values etc. • basic operations on the data elements: addition, Page 33 of 184 subtraction, multiplication, division, boolean oper- ations, string operations etc. Go Back • a naming mechanism for various quantitities and Full Screen expressions to be used without repeating defini- tions Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  13. Home Page Title Page In Ocaml ◭◭ ◮◮ Simple values and their types; and simple operations. ◭ ◮ • (): unit Page 34 of 184 • true, false: bool • & , or , not Go Back • integers, floats (reals), strings, ... Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  14. Home Page Methods of Title Page combination ◭◭ ◮◮ Means of combining simple expressions and objects to obtain more complex expressions and objects. ◭ ◮ # (not true or false) & (not false or true);; Page 35 of 184 - : bool = false Go Back More advanced: composition of functions, inductive Full Screen definitions Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  15. Methods of abstraction • Means of naming Home Page • Means of grouping objects and expressions as a Title Page single unit Examples: functions, data structures, modules, ◭◭ ◮◮ classes etc. ◭ ◮ # let x = true;; Page 36 of 184 val x : bool = true # let bit b = if b then 1 else 0;; Go Back val bit : bool -> int = <fun> # bit true;; Full Screen - : int = 1 # bit false;; Close - : int = 0 # let imply(x,y) = not x or y;; Quit val imply : bool * bool -> bool = <fun> • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  16. The Functional Model Home Page The functional model is very convenient and easy to Title Page use: ◭◭ ◮◮ • Programs are written (more or less) in mathemati- cal notation ◭ ◮ • It is like using a hand-held calculator Page 37 of 184 • Interactive and so answers are immediately avail- able Go Back • The closeness to mathematics makes it convenient for developing, testing, proving and analysing algo- Full Screen rithms. Close OCaml Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  17. Boolean functions Home Page Title Page input x output : not x input x input y output : x & y true false true true true ◭◭ ◮◮ false true false true false true false false ◭ ◮ false false false Page 38 of 184 Go Back input x input y output : x or y true true true Full Screen false true true true false true Close false false false Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  18. User Types Can define arbitrary types Home Page # type digit = Zero | One | Two Title Page | Three | Four | Five | Six | Seven | Eight | Nine ;; ◭◭ ◮◮ type digit = ◭ ◮ Zero | One Page 39 of 184 | Two | Three Go Back | Four | Five Full Screen | Six | Seven Close | Eight | Nine Quit # • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  19. Digital Display Home Page Seven digital lines: Title Page ◭◭ ◮◮ ◭ ◮ Horizontal: htop, hmid, hbot Vertical: ltop, lbot, rtop, rbot. Page 40 of 184 A bool expression for display of a digit: Go Back Zero -> ltop & lbot & rtop & rbot Full Screen & htop & hbot & (not hmid);; Four -> ltop & hmid & rtop & rbot & Close (not htop) & (not hbot) & (not lbot);; Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  20. Home Page Title Page Factorial math ver 1 ◭◭ ◮◮ informally we may write it as ◭ ◮ � 1 Page 41 of 184 if ( n ≥ 0) and ( n < 1) n ! = 1 × 2 × . . . × n otherwise Go Back Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  21. Home Page Title Page Factorial math ver 2 ◭◭ ◮◮ Or more formally we use mathematical induction to de- ◭ ◮ fine it as Page 42 of 184 � 1 if ( n ≥ 0) and n < 1 n ! = n × ( n − 1)! otherwise Go Back Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  22. Home Page Title Page Factorial math ver 3 ◭◭ ◮◮ How about this definition? ◭ ◮ � 1 if n < 1 Page 43 of 184 n = (1) ( n + 1)! / ( n + 1) otherwise Go Back Mathematically correct since a definition implicitly de- fines a mathematical equality or identity. But . . . Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  23. Home Page Title Page Factorial functional ◭◭ ◮◮ program ◭ ◮ let rec fact n = if (n>= 0) & (n<1) Page 44 of 184 then 1 else n*(fact (n-1));; Go Back val fact : int -> int = <fun> Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  24. Home Page Factorial Computation Title Page # fact 4;; ◭◭ ◮◮ - : int = 24 # fact 8;; ◭ ◮ - : int = 40320 Page 45 of 184 # fact 12;; Go Back - : int = 479001600 # fact 13;; Full Screen - : int = -215430144 Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  25. Home Page OCaml Recap Title Page • type unit and value () . • type bool and values true , false ◭◭ ◮◮ • Boolean operations & , or , not ◭ ◮ • if then else to analyse bool values. Page 46 of 184 • Can build up expressions • Can give names to expressions using let Go Back • Can define new types using type Full Screen • Can define functions Close • Can define recursive functions with let rec . Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  26. Home Page Title Page Computational Model ◭◭ ◮◮ • Define values and functions ◭ ◮ • Write an expression using previously defined names, values, functions Page 47 of 184 • “Simplify” the expression by replacing the defined Go Back term by the rhs of the definition, substituting argu- ment values for the parameter variables. Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  27. Home Page Title Page Factorial program ◭◭ ◮◮ Assume n ≥ 0 . ◭ ◮ let rec fact n = if (n=0) Page 48 of 184 then 1 else n*(fact (n-1));; Go Back val fact : int -> int = <fun> Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  28. Tracing Factorial Home Page 3! Title Page = 3 × (3 − 1)! = 3 × 2! ◭◭ ◮◮ = 3 × (2 × (2 − 1)!) ◭ ◮ = 3 × (2 × 1!) = 3 × (2 × (1 × (1 − 1)!)) Page 49 of 184 = 3 × (2 × (1 × 0!)) = 3 × (2 × (1 × 1)) Go Back = 3 × (2 × 1) = 3 × 2 Full Screen = 6 Close The bracketing and association shown above are fol- lowed by the machine. Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  29. Home Page Title Page Algorithms vs Math ◭◭ ◮◮ Recall we said that ◭ ◮ � 1 if n = 0 n ! = (2) ( n + 1)! / ( n + 1) otherwise Page 50 of 184 is NOT an algorithm. Why not? Go Back Not an effective way to compute. Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  30. Tracing the above Home Page Since the system would execute “ blindly ” by a pro- Title Page cess of replacing every occurrence of k ! by the right hand-side of the definition (in this case it would be ◭◭ ◮◮ ( k + 1)! / ( k + 1) ). So for 3! we would get ◭ ◮ 3! = (3 + 1)! / (3 + 1) Page 51 of 184 = 4! / 4 = ((4 + 1)! / (4 + 1)) / 4 Go Back = (5! / 5) / 4 = . . . Full Screen = . . . Close which goes on forever! Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  31. Home Page Algorithms & Title Page Nontermination ◭◭ ◮◮ • An algorithm should guarantee that all its computations terminate . ◭ ◮ • In fact for all our algorithmic definitions we would have to prove guaranteed termination. Page 52 of 184 • NOTE: It is possible to write definitions in any program- Go Back ming language whose computations do not terminate for some legally valid arguments. Full Screen • Would like a method to prove termination, without trac- ing the behaviour for all inputs. Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  32. Home Page Title Page A Silly Example ◭◭ ◮◮ # let rec facb x = x * (facb (x-1));; ◭ ◮ val facb : int -> int = <fun> # facb 1;; Page 53 of 184 Stack overflow during evaluation (looping recursion?). Go Back No base case to stop regress. Also try let rec foo x = foo x Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  33. Another Example Home Page # let rec f x = if x = 0 then 1 Title Page else x - (f (x-1));; val f : int -> int = <fun> ◭◭ ◮◮ # f 0;; - : int = 1 ◭ ◮ # f 1;; - : int = 0 Page 54 of 184 # f 2;; - : int = 2 Go Back # f 3;; - : int = 1 Full Screen # f 4;; Close - : int = 3 # f 5;; Quit - : int = 2 • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  34. Example contd Home Page Title Page # f 6;; - : int = 4 ◭◭ ◮◮ # f 7;; - : int = 3 ◭ ◮ # f 8;; - : int = 5 Page 55 of 184 # f 9;; - : int = 4 Go Back # f 10;; - : int = 6 Full Screen # f 11;; Close - : int = 5 Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  35. Home Page Representational Title Page Limitations ◭◭ ◮◮ Recall also ◭ ◮ # fact 12;; - : int = 479001600 Page 56 of 184 # fact 13;; Go Back - : int = -215430144 The result “overflowed”. Full Screen Could not be represented correctly using OCaml type int . Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  36. Big Naturals Home Page Title Page Peano’s Axioms. ◭◭ ◮◮ Closure • 0 is a natural number. ◭ ◮ • For every natural number n , its successor s ( n ) is a Page 57 of 184 natural number. Equality is reflexive, symmetric, transitive. Also: Go Back • For every natural number n , s ( n ) = 0 is false Full Screen • For all natural numbers m and n , if s ( m ) = s ( n ) , then m = n . Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  37. Home Page The type nat Title Page # type nat = O | S of nat;; type nat = O | S of nat ◭◭ ◮◮ ◭ ◮ # let succ x = (S x);; Page 58 of 184 val succ : nat -> nat = <fun> # succ O;; Go Back - : nat = S O # succ (S O);; Full Screen - : nat = S (S O) Representation of n : n occurrences of S before O . Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  38. Home Page Induction Title Page Peano’s Induction Scheme ◭◭ ◮◮ If ϕ is a unary predicate such that: ◭ ◮ • ϕ ( O ) is true, and Page 59 of 184 • for every natural number n , if ϕ ( n ) is true, then ϕ ( S ( n )) is true, Go Back then ϕ ( n ) is true for every nat n . Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  39. Home Page Addition Title Page ◭◭ ◮◮ a + 0 = a ◭ ◮ a + s ( b ) = s ( a + b ) Page 60 of 184 # let rec add x y = match y with Go Back O -> x | (S y’) -> S (add x y’);; Full Screen val add : nat -> nat -> nat = <fun> Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  40. Comments Home Page • Pattern matching “ match y with ” Title Page – Case Analysis: case → expression . ◭◭ ◮◮ • Correctness : If x , y represent x, y , then add x y returns representation of x + y . ◭ ◮ • Correctness by induction on y . Page 61 of 184 • Termination proved “because induction” (on y ). Go Back So how do we prove the following? Full Screen • add O x = x • add x y = add y x Close • Associative Law for add Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  41. Home Page Multiplication Title Page ◭◭ ◮◮ a. 0 = 0 ◭ ◮ a.s ( b ) = a + a.b Page 62 of 184 # let rec mult x y = match y with Go Back O -> O | (S y’) -> add x (mult x y’);; Full Screen val mult : nat -> nat -> nat = <fun> Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  42. Comments Home Page • Again a recursive function Title Page • Correctness : If x , y represent x, y , then mult x y returns representation of x.y ◭◭ ◮◮ • Correctness by induction on y ◭ ◮ • Termination “because induction” on y Page 63 of 184 • mult defined in terms of add Go Back Can we prove Full Screen • mult O x = O • mult x y = mult y x Close • Associative law for mult Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  43. Comments Home Page • Can we prove mult a (add b c) = add (mult a b) (mult a c) ? Title Page • How many steps does it take to compute add x y ? ◭◭ ◮◮ • How much “space” (symbols) does it take to write the answer? ◭ ◮ • How many steps does it take to compute mult x y ? Page 64 of 184 • How much “space” (symbols) does it take to write Go Back this answer? • Can we easily define a program to compute x y ? Full Screen • How about a program for x = y ? Close • And for x > y ? Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  44. Home Page Recap: Sets and Types Title Page ◭◭ ◮◮ Math Programming Set Type ◭ ◮ 1 1 unit Element Value Page 65 of 184 · () Go Back Operations? None! Case analysis? trivially only one value... Full Screen 1 to any set A ? | A | = | A | 1 . How many functions from 1 Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  45. The Booleans Home Page Math Programming Title Page I B bool ◭◭ ◮◮ Elements Values true true ◭ ◮ false false BooleanFunctions Operations Page 66 of 184 ∧ , ∨ , ¬ & , or , not Go Back Example: # let imply(x,y) = not x or y;; val imply : bool * bool -> bool = <fun> Full Screen Case analysis: if b then e 1 else e 2 Close B to any set A ? | A | 2 . How many functions from I Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  46. User Types Home Page Keyword: type Example: Digits Title Page # type digit = Zero | One | Two ◭◭ ◮◮ | Three | Four | Five | Six | Seven | Eight | Nine ;; ◭ ◮ Math Programming Page 67 of 184 Digit digit Elements Values Go Back 0 Zero 1 One Full Screen . . . . . . Close 9 Nine Constants Constructors Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  47. Constructors (1) Home Page What are “Constructors”? Title Page First answer: Names (new) that represent elements of a set that you wish to define. ◭◭ ◮◮ Example: Zero , .. Nine . Here, the Constructors are the values of the set called ◭ ◮ digit . Given a type made of Constructors, what does one do Page 68 of 184 with them? Case analysis! Go Back In OCaml: match e with Zero -> e 0 Full Screen — . . . e 9 | Nine -> Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  48. Functions by cases Home Page let digval d = match d with Title Page Zero -> 0 | One -> 1 ◭◭ ◮◮ | Two -> 2 | Three -> 3 ◭ ◮ | Four -> 4 Page 69 of 184 | Five -> 5 | Six -> 6 Go Back | Seven -> 7 | Eight -> 8 Full Screen | Nine -> 9 ;; Close val digval : digit -> int = <fun> Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  49. Bits Lab Exercise: 1. Define a data type bit of “binary digits” (represent- Home Page ing 0 and 1 . Title Page 2. Map bool to bit and vice versa . 3. Define a function badd that given two bit s, returns ◭◭ ◮◮ the (lower) bit of the sum. ◭ ◮ 4. Define a function carryb that when adding two bit s, returns the carry bit Page 70 of 184 5. Combine the two functions into one function Go Back bit*bit -> bit*bit , returning a bitadd: pair of bit s representing the carry and the lower Full Screen bit s. b 1 b 2 cbit lb Close 0 0 0 0 1 0 0 1 Quit 0 1 0 1 1 1 1 0 • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  50. Bits... Home Page Lab Exercise (contd): 1. Refine the function to include a carry in bit , to de- Title Page fine a function baddc: bit*bit*bit -> bit * bit ◭◭ ◮◮ that given bit s b 1 , b 2 and carry bit c , returns the carry out and lower bit s. ◭ ◮ 2. If you had to define similar functions on the data Page 71 of 184 type digit , how many cases would you have? 3. How is adding digits used in adding two natural Go Back numbers? Full Screen 4. What is the initial carry in digit when adding two natural numbers? Close 5. What is the largest possible value of a carry out Quit digit? • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  51. Naturals Home Page Title Page I N = { 0 , 1 , 2 , . . . } ◭◭ ◮◮ The first infinite set. How do we write algorithms to work on an infinite set? ◭ ◮ Algorithms must be finite Want a finite way to characterize an infinite set: Peano- Page 72 of 184 Dedekind characterization. Start with base case zero , and closure under the suc- Go Back cessor operation. Define all standard arithmetic operations in terms of Full Screen these. Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  52. Home Page Analysing Naturals Title Page Analysis : Using Peano axioms Is a number zero? If it is non-zero,it is a successor of ◭◭ ◮◮ a number. What is that number? First tool: Case analysis of zero versus non-zero. ◭ ◮ More powerful tool: Induction . Base Case n = 0 Page 73 of 184 Induction Hypothesis . Assume property holds for n = k (Simple) or more generally, for all n ≤ k . Go Back Induction Step . Assuming IH, show that it holds for n = s ( k ) . Full Screen Conclude by Induction that the property holds for all Naturals. Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  53. The type nat # type nat = O | S of nat;; Home Page type nat = O | S of nat # O;; Title Page - : nat = O # S(O);; ◭◭ ◮◮ - : nat = S O ◭ ◮ Set I N Type nat Element value Page 74 of 184 0 O 1 = s (0) S ( O ) Go Back 2 = s ( s (0)) S ( S ( O )) . . . . . . Full Screen Representation: 0 represented by constructor O . Close If natural k ∈ I N represented by v: nat , the succes- sor of k represented by S(v) . Quit Constructors : O: nat and S , that given a nat creates a new value in nat . • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  54. Home Page Algorithms on nat (1) Title Page Basic analysis: is it zero or not? ◭◭ ◮◮ # let iszero n = match n with O -> true ◭ ◮ | S(n’) -> false ;; Page 75 of 184 val iszero : nat -> bool = <fun> Go Back Basic operation, adding one: Full Screen # let plus1 n = S(n);; val plus1 : nat -> nat = <fun> Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  55. Algorithms on nat (2) Home Page Converting between nat and OCaml’s int , A basic recursive function: Title Page # let rec nat2int n = match n with ◭◭ ◮◮ O -> 0 | (S n’) -> 1+(nat2int n’) ◭ ◮ ;; val nat2int : nat -> int = <fun> Page 76 of 184 And non-negative int s to nat ( i ≥ 0 ) Go Back # let rec int2nat i = if (i=0) Full Screen then O else S(int2nat (i-1)) Close ;; val int2nat : int -> nat = <fun> Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  56. nat vs int Home Page So, what are the differences between types nat and Title Page int ? ◭◭ ◮◮ 1. nat is user-defined; int is built-in 2. nat is built using constructors ◭ ◮ 3. values of nat are written O , S(O) , etc. whereas Page 77 of 184 those of int are written 0 , 1 , ... 4. nat is not size-restricted Go Back 5. int contains negative numbers Full Screen 6. So are there more int s or nat s? Close 7. int is more “efficient”. (What does that mean?) Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  57. nat Programs Home Page Lab Exercise (2) Title Page • Write a function nateq: nat * nat -> bool , ◭◭ ◮◮ that given nat s x, y returns true if they are iden- tical and false otherwise. ◭ ◮ • Write a function natless: nat * nat -> Page 78 of 184 bool , that returns true if nat x represents a smaller natural than does nat y , and false other- Go Back wise. • Write a function power: nat * nat -> nat , Full Screen that given nat s x, y representing natural numbers Close x, y ≥ 0 and not both representing 0 , returns a nat representing x y . Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  58. Functions: basics Home Page Title Page • A mapping f from set A (predomain) to B (codomain) is called a function if for each a ∈ A , ◭◭ ◮◮ f maps a to at most one b ∈ B . • A function f : A → B is called total if each a ∈ A is ◭ ◮ mapped to exactly one b ∈ B (written f ( a ) ). Page 79 of 184 • A function f : A → B is called onto (or surjective or epi-morphic ) if for each b ∈ B , there exists at least Go Back one a ∈ A such that f ( a ) = b . • A function f : A → B is clled 1-1 (or injective or Full Screen mono-morphic ) if whenever f ( a 1 ) = f ( a 2 )( ∈ B ) , Close then a 1 = a 2 ( ∈ A ) . Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  59. Closure properties Home Page • The identity map i A : A → A is a total, 1-1 and onto Title Page function. ◭◭ ◮◮ • If f : A → B and g : B → C are functions then so is f ; g : A → C , defined as the mapping from a ∈ A to ◭ ◮ g ( f ( a )) ∈ C . Page 80 of 184 • If f : A → B and g : B → C are total, then so is f ; g : A → C Go Back • If f : A → B and g : B → C are 1-1, then so is f ; g : A → C Full Screen • If f : A → B and g : B → C are onto, then so is Close f ; g : A → C Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  60. Size of Sets • The “size” of set A is no more than that of B , written Home Page | A | ≤ | B | if there is a total 1-1 function f : A → B . Title Page • Two sets A and B are equinumerous if | A | ≤ | B | and | B | ≤ | A | i.e., there are total 1-1 functions in ◭◭ ◮◮ both directions. • A set A is called denumerable (or countable if there ◭ ◮ is a total 1-1 function from A to a subset of I N . It is countably infinite if that subset of I N is not finite. Page 81 of 184 • There are as many odd naturals as even naturals Go Back • There are no more naturals than odd naturals Full Screen • | I N | ≤ | Z Z | • | Z Z | ≤ | I N | Close • | I N | ≤ | I N × I N | Quit • | I N × I N | ≤ | I N | • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  61. Home Page Title Page Counting ◭◭ ◮◮ Let Q Q represent the rationals. ◭ ◮ • | Z Z | ≤ | Q Q | Page 82 of 184 • | Q Q | ≤ | Z Z × I N | = | Z Z | Go Back • | Q Q | ≤ | Z Z | = | I N | Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  62. Home Page Countability Title Page ◭◭ ◮◮ Theorem (Cauchy): The denumerable union of denu- merable sets is denumerable . ◭ ◮ Formally: Let A i ( i ∈ J ) be a family of sets, Page 83 of 184 where | J | ≤ | I N | and | A i | ≤ | I N | for each i . Then | � i ∈ J A i | ≤ | I N | . Go Back Proof: By Cauchy’s first diagonal argument, and com- posing total 1-1 functions. Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  63. Home Page Title Page Uncountability ◭◭ ◮◮ The Reals I R are not countable. ◭ ◮ In fact, [0 , 1) is not countable. However | I R | = | [0 , 1) | . Page 84 of 184 Geometrically, a projection argument. Cantor’s Second Diagonal argument (“Diagonaliza- Go Back tion”). An exemplar proof by contradiction. Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  64. Home Page Recap: nat Title Page • Representing natural n requires proportional to n symbols (space) ◭◭ ◮◮ • Adding m and n requires space proportional to m + ◭ ◮ n and proportional to n elementary operations • Multiplying m and n requires space proportional to Page 85 of 184 m × n and proportional to n recursive calls, each involving about m elementary operations. Go Back • Even comparisons m = n or m > n require elemen- Full Screen tary operations proportional to at least the smaller of the two numbers. Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  65. Home Page Efficient Naturally Title Page Every natural ◭◭ ◮◮ n = Σ k i =0 d i × 10 i where 0 ≤ d i ≤ 9 . ◭ ◮ So n can be written as d k d k 1 . . . d 0 . Page 86 of 184 (Why not d 0 d 1 . . . d k ?) (Big-Endian vs Little-Endian) Is this representation unique? No! (Why not?) Go Back What if d k � = 0 when k > 0 ? Multiplication by 10: Stick a 0 at the end (shift left). Full Screen Divide by 10: Drop d 0 (shift right). Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  66. Generalizing... Home Page Instead of 10, work with an arbitrary base r > 1 Title Page ◭◭ ◮◮ n = Σ k i =0 d i × r i where 0 ≤ d i < r and d k � = 0 when k > 0 . ◭ ◮ Least significant digit: d 0 ; msd: d k . Page 87 of 184 Multiplication by r : Stick a 0 at the end (shift left). Sim- ilarly integral div by r Go Back How big a number with (at most) k digits? Full Screen 0 . . . ( r k − 1) Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  67. Home Page Binary Title Page Special case with r = 2 . ◭◭ ◮◮ 0 1 2 3 4 5 6 7 . . . ◭ ◮ 1 2 4 8 16 32 64 128 . . . 1 10 100 1000 10000 100000 1000000 10000000 . . . Page 88 of 184 Bit-wise operations Go Back • Table for addition of two bits with carry is small Full Screen • multiplying two bits is similar to logical “and” Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  68. Adding Specification Home Page Given: m represented as d k . . . d 0 n represented as d ′ k ′ . . . d ′ 0 Title Page Result: Bit sequence d ′′ k ′′ . . . d ′′ 0 , which represents m + n . ◭◭ ◮◮ Rough Idea: ◭ ◮ 1. Sum the bits d 0 and d ′ 0 with initial carry c 0 = 0 and generate lsb d ′′ 0 and carry c 1 . Page 89 of 184 2. Similarly sum the bits d 1 and d ′ 1 with carry c 1 , and generate d ′′ 1 and c 2 Go Back 3. ... until we run out at the left end of either the first Full Screen number ( k th position) or the second number ( k ′ th po- sition), or both k = k ′ . Close 4. If either number still has bits (eg when k ′ < k ) prop- Quit agate the carry bit (e.g. c k through the remaining bits. • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  69. Home Page Title Page Bits ◭◭ ◮◮ type bit = O | I;; ◭ ◮ type bit = O | I let flip b = match b with Page 90 of 184 O -> I | I -> O ;; Go Back val flip : bit -> bit = <fun> Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  70. Home Page Add with carry Title Page let addc (b1, b2, c) = match (b1, b2, c) with (O, O, O) -> (O,O) ◭◭ ◮◮ | (O, O, I) -> (O,I) ◭ ◮ | (O, I, O) -> (O,I) | (I, O, O) -> (O,I) Page 91 of 184 | (O, I, I) -> (I,O) | (I, O, I) -> (I,O) Go Back | (I, I, O) -> (I,O) | (I, I, I) -> (I,I) Full Screen ;; val addc : bit * bit * bit -> bit * bit = <fun> Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  71. Digression: Lists Home Page Title Page Ocaml lists: a built-in “ generic ” type construction. # [ ];; ◭◭ ◮◮ - : ’a list = [] I::[ ];; ◭ ◮ - : bit list = [I] # 24::23::22::[ ];; Page 92 of 184 - : int list = [24; 23; 22] # ["a"; "b"; "c" ];; Go Back - : string list = ["a"; "b"; "c"] Constructors are [ ] , the empty list Full Screen and , that takes :: an element of type α and an α list, returning an α list Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  72. Building lists Home Page # [];; Title Page - : ’a list = [] # 1::[];; ◭◭ ◮◮ - : int list = [1] # "a"::"b"::[];; ◭ ◮ - : string list = ["a"; "b"] Page 93 of 184 # let add x y = x+y;; val add : int -> int -> int = <fun> Go Back # let mult x y = x*y;; val mult : int -> int -> int = <fun> Full Screen # add::mult::[];; - : (int -> int -> int) list = [<fun>; <fun>] Close We can have lists of functions as well. Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  73. Basic list functions Home Page Is the list empty? Title Page let is_empty x = match x with ◭◭ ◮◮ [] -> true | _ -> false;; ◭ ◮ Return the first element (head) of the list Page 94 of 184 let head x = match x with b::bs -> b;; Go Back Return the tail of the list Full Screen let tail x = match x with b::bs -> bs;; What’s the problem with the last two? Non-exhaustive pat- Close tern matching. Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  74. Home Page Heads and Tails Title Page How do we deal with the empty list case? ◭◭ ◮◮ # let head x = match x with [] -> raise (Failure "empty list") ◭ ◮ | y::_ -> y;; Page 95 of 184 # let tail x = match x with [] -> raise (Failure "empty list") Go Back | _::ys -> ys;; Full Screen Functions can “raise” exceptions to signal that the input is illegal. Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  75. Home Page Inserting an element Title Page Inserting at the front is easy: ◭◭ ◮◮ # let insertf x l = x::l;; ◭ ◮ val insertf : ’a list -> ’a -> ’a list = <fun> Inserting at the back needs recursion: Page 96 of 184 let rec insertb x l = match l with Go Back [] -> x::[] | y::ys -> y::(insertb x ys);; Full Screen val insertb : ’a -> ’a list -> ’a list = <fun> Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  76. Concatenating two lists Home Page Given two lists l1 and l2 return a new list that has the elements of l1 followed by the elements of l2 . Title Page # let rec append1 l1 l2 = match l2 with [] -> l1 ◭◭ ◮◮ | x::xs -> append1 (insertb x l1) xs;; val append1 : ’a list -> ’a list -> ’a list = <fun> ◭ ◮ Page 97 of 184 # let rec append2 (l1,l2) = match (l1,l2) with ([],l2) -> l2 Go Back | (x::xs,l2) -> x::(append2(xs,l2));; val append2 : ’a list * ’a list -> ’a list = <fun> Full Screen Ocaml gives us a built-in append function: Close # [1;2] @ [3;4];; - : int list = [1; 2; 3; 4] Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  77. Home Page Title Page Reversing a list ◭◭ ◮◮ Given a list return a list with the same elements in ◭ ◮ reverse order. # let rec reverse l = match l with Page 98 of 184 [] -> [] Go Back | x::xs -> (reverse xs) @ x::[];; val reverse : ’a list -> ’a list = <fun> Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  78. Home Page The length of a list Title Page Given a list return the number of elements in the list. ◭◭ ◮◮ # let rec length l = match l with [] -> 0 ◭ ◮ | _::xs -> 1 + (length xs);; val length : ’a list -> int = <fun> Page 99 of 184 Simple extension: add the elements of the list Go Back # let rec addall l = match l with [] -> 0 Full Screen | x::xs -> x + (addall xs);; val addall : int list -> int = <fun> Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

  79. Home Page What does this list Title Page function do? ◭◭ ◮◮ ◭ ◮ # let rec f l = match l with Page 100 of 184 [] -> 0 | x::xs -> (length x) + (f xs);; Go Back val f : ’a list list -> int = <fun> End of digression. Full Screen Close Quit • First • Prev • Next • Last • Go Back • Full Screen • Close • Quit

Recommend


More recommend