-2

I need to prove

f (g xs) == g (f xs)

whenver xs is a finite list of Ints.

Assume both f and g are of type [Int]->[Int]

user3358850
  • 129
  • 9

2 Answers2

5

Disproof by counterexample:

f xs = []
g xs = [1]

If you want this property to hold, you need more specific constraints on what f and g are.

You may be thinking of the law that

(map f . map g) == map (f . g)

which can indeed be proven.

amalloy
  • 89,153
  • 8
  • 140
  • 205
-1

Found this from somewhere

Theorem: (map f . map g) xs = map (f . g) xs
       - Proof is by induction on xs
    *** Base Case, xs = []
        - Left side: (map f . map g) [] = map f (map g []) = map f [] = []
        - Right side: map (f . g) [] = []
    *** Inductive Case, xs = k:ks
        - Inductive Hypothesis: (map f . map g) ks = map (f . g) ks
        - Left Side
          + (map f . map g) xs
          + map f (map g (k:ks))
          + map f ((g k) : (map g ks))
          + (f (g k)) : (map f (map g ks))
          + ((f . g) k) : ((map f . map g) ks)  i.e. change from bracket form back to point form
          + ((f . g) k) : (map (f . g) ks) by inductive hypothesis
          + map (f . g) (k:ks) by definition of map
          + map (f . g) xs 
user3358850
  • 129
  • 9
  • 2
    This is very different than saying `f (g xs) == g (f xs)` which, as others have pointed out, is false in general. – David Young Apr 25 '17 at 00:39