% 15-814 Fall 2021 % Lecture 3 % Recursion % Frank Pfenning % Booleans defn true = \x. \y. x defn false = \x. \y. y defn not = \b. \x. \y. b y x conv not true = false conv not false = true defn and = \b. \c. b c false conv and true true = true conv and false true = false conv and true false = false conv and false false = false % Natural numbers % without test cases defn zero = \s. \z. z defn succ = \n. \s. \z. s (n s z) norm one = succ zero norm two = succ one norm three = succ two defn plus = \n. \k. n succ k defn times = \n. \k. n (plus k) zero defn exp = \n. \k. k (times n) one defn pair = \x. \y. \k. k x y defn pred2 = \n. n (\p. p (\x. \y. pair (succ x) x)) (pair zero zero) defn pred = \n. pred2 n (\x. \y. y) %%% Lecture 3 on Recursion defn I = \x. x defn K = \x. \y. x defn Y = \h. (\x. h (x x)) (\x. h (x x)) (* if0 0 c d = c if0 (n+1) c d = d *) defn if0 = \n. \c. \d. n (K d) c (* fact = \n. if0 n 1 (n * fact (n-1)) fact = h_fact fact *) defn h_fact = \f. \n. if0 n (succ zero) (times n (f (pred n))) defn fact = Y h_fact norm _120 = fact _5 norm _720 = fact (succ _5)