recursive
play

Recursive f(b) explicitly for each base case b R Functions - PowerPoint PPT Presentation

Recursive Function To define a function, f, on a recursively defined set R, define Recursive f(b) explicitly for each base case b R Functions f(c(x)) for each constructor, c, in terms of x and f(x) recursivef recursivefunctions.1


  1. Recursive Function To define a function, f, on a recursively defined set R, define Recursive • f(b) explicitly for each base case b ∈ R Functions • f(c(x)) for each constructor, c, in terms of x and f(x) recursivef recursivefunctions.1 Albert R Meyer, March 1, 2013 Albert R Meyer, March 1, 2013 unctions 2 N k n  recursive function on Recursive function on M expt(k, 0) ::= 1 Def. tree-depth(s) for s �� M expt(k, n+1) ::= k � expt(k,n) td( λ ) ::= 0 --uses recursive def of N : td( [ s ] t ) ::= • 0 ∈ � N 1 + max{td(s), td(t)} • if n ∈ � N , then n + 1 ∈ � N Albert R Meyer, March 1, 2013 recursivefunctions.3 Albert R Meyer, March 1, 2013 recursivefunctions.4 1

  2. Recursive Functions Length versus Depth Lemma: |r| + 2 ≤ 2 td(r)+1 summary: for all r �� M f: Data � Values Proof by Structural Induction f(b) def’d directly for base b Base case: [ r = λ ] f(cnstr(x)) def’d using f(x), x | λ |+2 = 0+2 = 2 = 2 0+1 = 2 td( λ )+1 OK! recursivefunctions.6 Albert R Meyer, March 1, 2013 recursivefunctions.5 Albert R Meyer, March 1, 2013 Size versus Depth Size versus Depth |r|+2 = | [ s ] t| + 2 def. of r Constructor case: [r = [ s ] t] = (|s|+|t| +2) + 2 def. of length by ind. hypothesis: = (|s|+2)+(|t|+2) ≤ 2 td(s)+1 + 2 td(t)+1 induction hyp. |s| + 2 ≤ 2 td(s)+1 ≤ 2 max(td(s),td(t))+1 + 2 max(td(s),td(t))+1 = 2·2 max(td(s),td(t))+1 ≤ 2·2 td(r) def. of d(r) |t| + 2 ≤ 2 td(t)+1 = 2 td(r)+1 QED! Albert R Meyer, March 1, 2013 recursivefunctions.7 Albert R Meyer, March 1, 2013 recursivefunctions.8 2

  3. positive powers of two log 2 of PP2 log 2 (2) ::= 1 2 ∈ PP2 log 2 (x � y) ::= log 2 (x) + log 2 (y) if x,y ∈ PP2, then x � y ∈ PP2 for x,y � � PP2 2, 2 � 2, 4 � 2, 4 � 4, 4 � 8, … log 2 (4) = log 2 (2 � 2) = 1 + 1 = 2 log 2 (8) = log 2 (2 � 4) = log 2 (2) + log 2 (4) 2 4 8 16 32 … ∈ � � PP2 = 1 + 2 = 3 Albert R Meyer, March 1, 2013 Albert R Meyer, March 1, 2013 recursivefunctions.25 recursivefunctions.24 loggy function on PP2 loggy function on PP2 loggy(2)::= 1 loggy(16) = loggy(8 � 2) = 9 loggy(x � y) ::= x + loggy(y) for x,y � � PP2 WAIT A SEC!: loggy(4) = loggy(2 � 2) = 2 + 1 = 3 loggy(16) = loggy(2 � 8) loggy(8) = loggy(2 � 4) = 2 + loggy(4) = 2 + loggy(8) = 2 + 5 = 2 + 3 = 5 loggy(16) = loggy(8 � 2) = 8 + loggy(2) = 7 = 8 + 1 = 9 Albert R Meyer, March 1, 2013 recursivefunctions.26 Albert R Meyer, March 1, 2013 recursivefunctions.27 3

  4. ambiguous constructors ambiguous recursive defs The Problem: more than one way to problem to watch out for: construct elements of PP2 from recursive function on datum, e, cnstrct(x,y) = x � y is defined according to what 16 = cnstrct(8,2) but also constructor created e. 16 = cnstrct(2,8) If 2 or more ways to construct e, ambiguous then which definition to use? Albert R Meyer, March 1, 2013 Albert R Meyer, March 1, 2013 recursivefunctions.29 recursivefunctions.28 4

  5. MIT OpenCourseWare http://ocw.mit.edu 6.042J / 18.062J Mathematics for Computer Science Spring 20 15 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend