简体   繁体   English

功能证明(Haskell)

[英]Functional proofs (Haskell)

I failed at reading RWH; 我在阅读RWH时失败了; and not one to quit, I ordered Haskell: The Craft of Functional Programming . 而不是一个退出,我命令Haskell:功能编程工艺 Now I'm curious about these functional proofs on page 146. Specifically I'm trying to prove 8.5.1 sum (reverse xs) = sum xs . 现在我对第146页的这些功能证明感到好奇。特别是我试图证明8.5.1 sum (reverse xs) = sum xs I can do some of the induction proof but then I get stuck.. 我可以做一些感应证明,但后来我卡住了..

HYP: HYP:

sum ( reverse xs ) = sum xs

BASE: 基础:

sum ( reverse [] ) = sum []

Left  = sum ( [] ) (reverse.1)
      = 0          (sum.1)

Right = 0          (sum.1)

INDUCTION: 感应:

sum ( reverse (x:xs) ) = sum (x:xs) 

Left = sum ( reverse xs ++ [x] )    (reverse.2)

Right = sum (x:xs)   
      = x + sum xs                  (sum.2)

So now I'm just trying ot prove that Left sum ( reverse xs ++ [x] ) is equal to Right x + sum xs , but that isn't too far off from where I started sum ( reverse (x:xs) ) = sum (x:xs) . 所以现在我只是试图证明Left sum ( reverse xs ++ [x] )等于Right x + sum xs ,但这与我开始sum ( reverse (x:xs) ) = sum (x:xs)位置相差不远sum ( reverse (x:xs) ) = sum (x:xs)

I'm not quite sure why this needs to be proved, it seems totally reasonable to use the symbolic proof of reverse x:y:z = z:y:x (by defn), and because + is commutative (arth) then reverse 1+2+3 = 3+2+1 , 我不太清楚为什么需要证明这一点,使用reverse x:y:z = z:y:x (通过defn)的符号证明似乎是完全合理的,因为+是可交换的(关节)然后reverse 1+2+3 = 3+2+1

sum (reverse [])     = sum []                     -- def reverse
sum (reverse (x:xs)) = sum (reverse xs ++ [x])    -- def reverse
                     = sum (reverse xs) + sum [x] -- sum lemma below
                     = sum (reverse xs) + x       -- def sum
                     = x + sum (reverse xs)       -- commutativity assumption!
                     = x + sum xs                 -- inductive hypothesis
                     = sum (x:xs)                 -- definition of sum

However, there are underlying assumptions of associativity and commutativity that are not strictly warranted and this will not work properly for a number of numerical types such as Float and Double where those assumptions are violated. 然而,存在关联性和可交换性的基本假设,这些假设并非严格保证,并且对于许多数值类型(例如FloatDouble ,其中违反这些假设),这将无法正常工作。

Lemma: sum (xs ++ ys) == sum xs + sum ys given the associativity of (+) 引理: sum (xs ++ ys) == sum xs + sum ys给定(+)的相关性

Proof: 证明:

sum ([] ++ ys)     = sum ys           -- def (++)
                   = 0 + sum ys       -- identity of addition
                   = sum [] ++ sum ys -- def sum

sum ((x:xs) ++ ys) = sum (x : (xs ++ ys))  -- def (++)
                   = x + sum (xs ++ ys)    -- def sum 
                   = x + (sum xs + sum ys) -- inductive hypothesis
                   = (x + sum xs) + sum ys -- associativity assumption!
                   = sum (x:xs) + sum ys   -- def sum

Basically you need to show that 基本上你需要证明这一点

sum (reverse xs ++ [x]) = sum (reverse xs) + sum [x]

which then easily leads to 然后很容易导致

                        = x + sum (reverse xs)
                        = x + sum xs  -- by inductive hyp.

The problem is to show that sum distributes over list concatenation. 问题是显示sum分配在列表连接上。

Use the definition of a sum to break up (sum reverse xs ++[x]) into x + sum(reverse(xs)), and using your inductive hypothesis you know sum(reverse(xs)) = sum(xs). 使用和的定义来分解(反向xs ++ [x])到x + sum(反向(xs)),并使用归纳假设你知道sum(reverse(xs))= sum(xs)。 But I agree, induction is overkill for a problem like this. 但我同意,对于像这样的问题,归纳是过度的。

Here's where I think you're stuck. 这就是我认为你被困住的地方。 You need to prove a lemma that says 你需要证明一个引理说明

sum (xs ++ ys) == sum xs + sum ys

To prove this law you will have to assume that addition is associative, which is true only for integers and rationals. 为证明这一定律,你必须假设加法是相关的,这对整数和有理数都是正确的。

Then, you will also need to assume that addition is commutative, which is true for integers and rationals but also for floats. 然后,您还需要假设加法是可交换的,对于整数和有理数,对于浮点数也是如此。


Digression: The style of your proofs looks very strange to me. 题外话:你的证明风格对我来说很奇怪。 I think you will have an easier time writing these kinds of proofs if you use the style in Graham Hutton's book . 如果你使用Graham Hutton的书中的风格,我想你会更容易写出这些证据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM