-1

I have the following problem.

Prove that for all finite lists xs of some type and predicates p and q on that type:

filter p (filter q xs) = filter q ( filter p xs)

Do I need some sample xs lists for solving this? Or how must this problem be solved? Thanks.

migea
  • 517
  • 4
  • 10
  • 23
  • Short answer: using induction. For example check [Structural induction in Haskell](http://stackoverflow.com/a/14960598/1308058) – phadej Jan 24 '15 at 20:45

1 Answers1

5

No, providing some xs samples is not what this exercise requires.

This exercise is asking you to prove for all possible values of p,q,xs, the the equation holds. Note that there are infinitely many possible values for p,q,xs, so it is unfeasible to brute force all the possible cases: a general mathematical proof must be provided, leveraging some logical principle.

Just to make a comparison, suppose you are asked to prove that 2*x+x = 3*x in an exercise. The expected solution is not "well, it holds on x=4 and x=10", neglecting all the other (infinitely many) values for x. A reasonable solution could be: "I have x=1*x, and so by the distributive law 2*x+x = 2*x + 1*x = (2+1)*x = 3*x", which works for all x.

In this kind of exercises, often one must proceed by induction on something. Here, xs looks as a good candidate for induction. So, to prove the equation true for all xs, it is necessary to prove

  1. the equation holds for xs=[]
  2. if the equation holds for xs=ys (ys being arbitrary), then it must hold for xs=y:ys (y being an arbitrary value)

If you prove 1. and 2. then you are done.

Just one extra hint: since y is arbitrary, you do not know if it satisfies predicates p and/or q. You can however check all the possible four cases there: both p,q hold, only p, only q, neither.

chi
  • 111,837
  • 3
  • 133
  • 218