I am trying to prove the following statement by structural induction:
foldr f st (xs++yx) = f (foldr f st xs) (foldr f st ys) (foldr.3)
However I am not even sure how to define foldr, so I am stuck as no definitions have been provided to me. I now believe that foldr can be defined as
foldr f st [] = st (foldr.1)
foldr f st x:xs = f x (foldr f st xs) (foldr.2)
Now I want to start working on the base case passing the empty list to foldr. I have this, but I don't think it is correct.
foldr f st ([]++[]) = f (foldr f st []) (foldr f st [])
LHS:
foldr f st ([]++[]) = foldr f st [] by (++)
foldr f st [] = st by (foldr.1)
RHS:
f (foldr f st []) (foldr f st []) = f st st by (foldr.1)
= st by definition of identity, st = 0
LHS = RHS, therefore base case holds
Now this is what I have for my inductive step:
Assume that:
foldr f st (xs ++ ys) = f (foldr f st xs) (foldr f st ys) (ind. hyp)
Show that:
foldr f st (x:xs ++ ys) = f (foldr f st x:xs) (foldr f st ys) (inductive step)
LHS:
foldr f st (x:xs ++ ys) = f x (foldr f st xs) (foldr f st ys) (by foldr.2)
RHS:
f (foldr f st x:xs) (foldr f st ys) =
= f f x (foldr f st xs) (foldr f st ys) (by foldr.2)
= f x (foldr f st xs) (foldr f st ys)
LHS = RHS, therefore inductive step holds. End of proof.
I am not sure if this proof is valid. I need some help in determining if it correct and if not - what part of it is not.
First: you can find the definition for many basic Haskell functions via the API documentation, which is available on Hackage. The documentation for
base
is here.foldr
is exported inPrelude
, which has a link to its source code:It's defined like this for efficiency reasons; look up "worker-wrapper." It's equivalent to
Second: In your desired proof, the type of
f
must bea -> a -> a
, which is less general thana -> b -> b
.Let's work through the base case (
xs = ys = []
).This equation does not hold in general. To proceed with the proof, you'll have to assume that
st
is an identity forf
.You'll also have to assume, in the non-base case, that
f
is associative, I believe. These two assumptions, combined, indicate thatf
andst
form a monoid. Are you trying to prove something aboutfoldMap
?