Structural induction in Haskell

≡放荡痞女 提交于 2019-11-29 04:29:22

You did not specify it, but I will assume :: means list concatention and use ++, since that is the operator used in Haskell. To prove this, we will perform induction on xs. First, we show that the statement holds for the base case (i.e. xs = [])

foldr f a (xs ++ ys) 
{- By definition of xs -}
= foldr f a ([] ++ ys)
{- By definition of ++ -}
= foldr f a ys

and

foldr f (foldr f a ys) xs
{- By definition of xs -}
= foldr f (foldr f a ys) []
{- By definition of foldr -}
= foldr f a ys

Now, we assume that the induction hypothesis foldr f a (xs ++ ys) = foldr f (foldr f a ys) xs holds for xs and show that it will hold for the list x:xs as well.

foldr f a (x:xs ++ ys)
{- By definition of ++ -}
= foldr f a (x:(xs ++ ys))
{- By definition of foldr -}
= x `f` foldr f a (xs ++ ys)
         ^------------------ call this k1
= x `f` k1

and

foldr f (foldr f a ys) (x:xs)
{- By definition of foldr -}
= x `f` foldr f (foldr f a ys) xs
         ^----------------------- call this k2
= x `f` k2

Now, by our induction hypothesis, we know that k1 and k2 are equal, therefore

x `f` k1 =  x `f` k2

Thus proving our hypothesis.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!