Example
Given
(define (len lst)
(if (empty? lst)
0
(+ 1 (len (rest lst)))
)
)
(define (append lst1 lst2)
(if (empty? lst1)
lst2
(cons (first lst1)
(append (rest lst1) lst2)
)
)
)We prove via Proof by Structural Induction
Proof
- Define axioms and stuff we know
lst = ()lst = (cons lst1 lst2)(len '()) = 0by lines 2-3 of code(len (cons X Y)) = (+ 1 (len Y))by lines 2-4(append '() 12) = 12(append (cons X Y) 12) = (cons X (append Y 12))- Assume all built-ins perform correctly
- Want to prove
(len (append l1 l2)) = (+ (len l1) (len l2))(IH) - We want to prove this structural induction on
l1 - Case 1:
l1is'()- Then, we establish left side is equal to right side
(len (append l1 l2))= (len (append () l2))= (len l2)= (+ 0 (len l2))= (+ (len ()) (len l2))= (+ (len l1) (len l2))- Thus, we have shown our WTP
- Case 2:
l1is(cons X Y)- Assume IH
- Then, start with
(len (append l1 l2)) = (len (append (cons X Y) 12))by case= (len (cons X (append Y 12)))by 4= (+ 1 (len (append Y 12)))by 2= (+ (+ (len Y) (len l2)))by IH= (+ (+ 1 (len Y) (len l2))by arithmetic= (+ (len (cons X Y) (len l2))by 2= (+ (len l1) (len l2))by case