1

Given n >= 0 and n < min (length xs) (length ys) show that (zip xs ys)!!n = (xs!!n, ys!!n) with structural Induction over xs.

Is it even possible to do this in a clean way? I cant find any spots where I can use the Induction Hypothesis.

jmply
  • 41
  • 6
  • I’m rusty, but I think you can prove `zip xs ys !! 0 = (xs !! 0, ys !! 0)` by equational reasoning and expanding the definitions of `zip` and `!!`, then prove that if `zip xs ys !! n = (xs !! n, ys !! n)` then `zip xs ys !! n + 1 = (xs !! n + 1, ys !! n + 1)`. – Jon Purdy Feb 06 '17 at 09:13
  • Thanks for the answer. I forgot to mention it should be structural induction over the length of the list. That is xs becomes (x:xs) in the induction step. – jmply Feb 06 '17 at 11:00

1 Answers1

1

First, I'll give definitions of zip and !!:

zip :: [a] -> [b] -> [(a,b)]
zip [] [] = []                             -- (zip-1)
zip (x:xs) (y:ys) = (x,y) : zip xs ys      -- (zip-2)
zip _ _ = []                               -- (zip-3)

(!!) :: [a] -> Int -> a
(x : _) !! 0 = x                           -- (!!-1)
(_ : xs) !! n = xs !! (n - 1)              -- (!!-2)

Let xs, ys and n arbitrary. Now, suppose that n >=0 and n < min (length xs) (length ys). We proceed by induction on xs.

  • Case xs = []. Now we do case analysis on ys. In both cases, we have that there's no n >=0 and n < min (length xs) (length ys). So, this case is trivially true.
  • Case xs = x : xs'. We proceed by case analysis on ys.
  • Case xs = x : xs' and ys = []. Again, we have the theorem trivially true since there's no n such that that n >=0 and n < min (length xs) (length ys).
  • Case xs = x : xs' and ys = y : ys'. Now we do case analysis on n.
  • Case xs = x : xs', ys = y : ys' and n = 0. We have that

    zip (x : xs') (y : ys') !! 0 = {by equation (zip-2)}
    (x,y) : zip xs' ys'     !! 0 = {by equation (!!-1)}
    (x,y)                        = {by equation (!!-1) - backwards}
    ((x : xs') !! 0, (y : ys') !! 0).
    
  • Case xs = x : xs', ys = y : ys' and n = n' + 1.

     zip (x : xs') (y : ys') !! (n + 1) = {by equation zip-2}
     (x,y) : zip xs' ys' !! (n + 1) = {by equation (!!-2)}
     zip xs' ys' !! n               = {by induction hypothesis}
     (xs' !! n , ys' !! n)          = {by equation (!!-2) backwards}
     ((x : xs') !! (n + 1), (y : ys') !! (n + 1))
    

    QED

Hope that this helps.

Rodrigo Ribeiro
  • 3,198
  • 1
  • 18
  • 26
  • Thank you for your answer! In the final case where you use the IH, wouldnt this be considered Induction over n? Also where did you get the function definitions? – jmply Feb 06 '17 at 11:25
  • By no means! The induction is over `xs`. The IH is forall ys. forall n. n >= 0 /\ n < min (length xs) (length ys) -> zip xs ys !! n = (xs !! n , ys !! n). The need for case analysis on `n` and `ys` is just to "massage" the goal to manipulate it easily using equational reasoning. – Rodrigo Ribeiro Feb 06 '17 at 11:31
  • If you think that the answer is fine, please mark it as an answer. – Rodrigo Ribeiro Feb 06 '17 at 11:34