0

I am trying to prove the following:

filter p . filter q = filter (and p q)

where 

and p q x = p x && q x

here is my try, but i am not sure if it is correct, can anyone give me some ideas?

Induction of list xs
Base case xs = []
(filter p . filter q) xs 
= {definition of .}
filter p ( filter q xs )
= {xs = []}
filter p ( filter q [] )
= {definition of filter}
filter p []
= {definition of filter}
[]
= {xs = []}
filter (and p q) []
= {definition of filter}
= []

Inductive case:

(filter p . filter q) xs
= {definition of .}
filter p ( filter q xs)
= {definition of filter}
filter p [ x | x<-xs, q x]
= {definition of filter}
[ y | y <- [ x | x<-xs, q x], p y]
= {definition of list} -- i am not sure if it is correct as well
[ x | <- xs, (p x) && (q x)]
= {definition of and p q x = p x && q x}
[ x | x<-xs, and p q x ]
= {definition of filter}
filter (and p q) xs

The inductive case is the point i struggled, filter can be implemented by foldr and also list comprehension, but if i use the list comprehension, it is ok to be a inductive case?

freestyle
  • 3,692
  • 11
  • 21
Edison Lo
  • 476
  • 8
  • 25
  • 1
    List comprehensions are defined in terms of `filter` so you probably can't use them in the proof without resirting to circular reasoning. Where did your get your version of the definition? Try the "official" one found in Hackage. – n. m. could be an AI May 04 '17 at 16:13
  • 1
    Here is the [source code for filter](http://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.List.html#filter). – basile-henry May 04 '17 at 16:25
  • 2
    In order to reason about a function like `filter`, you must perform another case split in your inductive case - `filter p (x:xs)` becomes split into `(p x => filter p (x:xs) = x : filter p xs) /\ (not (p x) => filter p (x:xs) = filter p xs)`. Since you have `filter p . filter q` you will need to split this into four cases - then you will see the four cases correspond exactly to the logic table for `and`, and this is the crux of the proof (everything else is boring fluff). Reasoning about a list comprehension version of `filter` is much harder than just doing the case split. – user2407038 May 04 '17 at 16:47
  • Thank you for the source code link and the way to do the induction – Edison Lo May 04 '17 at 17:02

0 Answers0