如何使用精益消除代数表达式中的括号

时间:2018-10-24 22:05:50

标签: theorem-proving lean

我正在尝试使用精益证明一个代数定理。我的代码是

 import algebra.group
import algebra.ring
open algebra

variable {A : Type}

variables [s : ring A] (a b c : A)
include s

theorem clown (a b c d e : A) : 
(a + b  + e) * ( c + d) =   a * c + (b * c + e* c) + (a * d + b * d + e * d)   :=
calc

(a + b  + e) * ( c + d) = (a + (b + e))* (c +d)   : !add.assoc
                    ... = (a + (b + e)) * c + (a + (b + e)) * d   : by rewrite  left_distrib
                    ... =  a * c + (b + e) * c + (a + ( b + e)) * d : by rewrite right_distrib
                    ... =  a * c + (b * c + e* c) + (a + (b + e)) * d : by rewrite right_distrib
                    ... =  a * c + (b * c + e* c) + (a * d + (b + e) * d) : by rewrite right_distrib
                    ... =  a * c + (b * c + e* c) + (a * d + (b * d + e * d) ) : by rewrite right_distrib
                    ... =  a * c + (b * c + e* c) + (a * d + b * d + e * d ) :  !add.assoc

 check clown

请让我知道如何消除最后的括号。就是说,我只想获得

a * c + b * c + e * c + a * d + b * d + e * d

非常感谢。

3 个答案:

答案 0 :(得分:2)

这看起来像精益2语法。除非您专门将Lean 2用于同伦类型理论模式,否则我强烈建议您升级到Lean 3,该方法自2017年初就已经发布。

默认情况下,操作+和*关联到左侧。也就是说,a * c + b * c + e* c + a * d + b * d + e * d(((((a * c + b * c) + e* c) + a * d) + b * d) + e * d)相同。您可以使用add.assoc(在精益3中重命名为add_assoc)的足够应用来证明这一最终相等性。在精益3中,您可以使用by simpby simp only [add_assoc]进行证明。

答案 1 :(得分:1)

如果您不介意假设您的戒指是可交换的,则也可以使用ring策略。

import tactic.ring

variables {A : Type} [comm_ring A]

theorem clown (a b c d e : A) : 
  (a + b + e) * (c + d) = a * c + (b * c + e * c) + (a * d + b * d + e * d) :=
by ring

答案 2 :(得分:0)

使用以下代码可获得可能的解决方案

 import algebra.group
import algebra.ring
open algebra

variable {A : Type}

variables [s : ring A] (a b c : A)
include s

theorem clown (a b c d e : A)  : 
(a + b  + e) * ( c + d) =  a * c + a * d + b*c + b*d +e*c+e*d :=
calc

(a + b  + e) * ( c + d) = a*(c + d) + b*(c + d) + e*(c + d)   : by rewrite distrib_three_right
                   ...  = a * c + a * d + b*(c+d)+e*(c+d) : by rewrite *left_distrib
                   ...  = a * c + a* d + (b*c + b*d) +e*(c+d) : by rewrite *left_distrib
                   ... =  a * c + a* d + (b*c + b*d) +(e*c+e*d): by rewrite left_distrib
                   ... = a * c + a* d + b*c + b*d + (e*c+e*d) :  !add.assoc
                   ... = a * c + a* d + b*c + b*d + e*c+e*d :  !add.assoc

check clown

其他示例

 import algebra.group
import algebra.ring
open algebra

variable {A : Type}

variables [s : ring A] (a b c : A)
include s

theorem clown (a b c d e f: A)  : 
(a + b  + e + f) * ( c + d) =  a * c + a * d + b*c + b*d +e*c+e*d  + f * c + f * d:=
calc

(a + b  + e + f) * ( c + d) = a*(c + d) + b*(c + d) + e*(c + d) + f*(c +d)  : by rewrite *right_distrib
                   ...  = a * c + a * d + b*(c+d)+e*(c+d) + f * (c + d): by rewrite *left_distrib
                   ...  = a * c + a* d + (b*c + b*d) +e*(c+d) + f*(c+d): by rewrite *left_distrib
                   ... =  a * c + a* d + (b*c + b*d) +(e*c+e*d)+ f*(c+d): by rewrite left_distrib
                   ... =  a * c + a* d + (b*c + b*d) +(e*c+e*d)+ (f*c+ f*d): by rewrite left_distrib
                   ... = a * c + a* d + b*c + b*d + (e*c+e*d)+ (f*c+f*d) :  !add.assoc
                   ... = a * c + a* d + b*c + b*d + e*c+e*d + (f*c + f*d):  !add.assoc
                   ... = a * c + a* d + b*c + b*d + e*c+e*d + f*c + f*d  :  !add.assoc

check clown

同一示例,但减少了

    variable {A : Type}

variables [s : ring A] 
include s

    theorem clown (a b c d e f: A)  : 
    (a + b  + e + f) * ( c + d) =  a * c + a * d + b*c + b*d +e*c+e*d  + f * c + f * d:=
    calc

    (a + b  + e + f) * ( c + d) = a*(c + d) + b*(c + d) + e*(c + d) + f*(c +d)  : by rewrite *right_distrib

                       ... =  a * c + a* d + (b*c + b*d) +(e*c+e*d)+ (f*c+ f*d): by rewrite *left_distrib

                       ... = a * c + a* d + b*c + b*d + e*c+e*d + f*c + f*d  :  by rewrite *add.assoc

    check clown

其他示例

 import algebra.ring
open algebra
check mul_sub_left_distrib 
check add.assoc
variable {A : Type}

variables [s : ring A] 
include s

theorem clown (a b c d : A)  : 
    (a + b ) * ( c - d) =  a*c-a*d+ b*c- b*d:=
    calc

    (a + b) * ( c  -d) = a*(c-d) +b*(c-d) : by rewrite *right_distrib
                   ... = a*(c + -d) + b*(c-d) : rfl
                   ... = a*c  + a*-d+b*(c-d) : by rewrite left_distrib
                   ... = a*c + a*-d + (b*c - b*d): by rewrite mul_sub_left_distrib 
                   ... = a*c + a*-d + b*c - b*d : add.assoc
                   ... = a*c + -(a*d)+  b*c - b*d : by rewrite mul_neg_eq_neg_mul_symm
                   ... = a*c - a*d + b*c - b*d : rfl
 check clown  

其他示例

import algebra.group
import algebra.ring
open algebra
variable {A : Type}

variables [s : ring A] 
include s

theorem clown (a b c d e : A)  : 
    (a + b + e ) * ( c - d) =  a*c -a*d + b*c - b*d + e*c - e*d:=
    calc
(a + b + e) * ( c - d) = a*(c-d) +b*(c-d) + e*(c-d) : by rewrite *right_distrib
                   ... = a*(c + -d) + b*(c+ -d) + e*(c-d): rfl
                   ... = a*c  + a*-d+(b*c +b*-d) + e*(c-d) : by rewrite *left_distrib
                   ... = a*c + a*-d + (b*c +b*-d)+ (e*c -e*d) : by rewrite *mul_sub_left_distrib 
                   ... = a*c + a*-d  + b*c + b*-d + (e*c - e*d) : !add.assoc
                   ... = a*c + a*-d + b*c + b*-d + e*c - e*d : !add.assoc
                   ... = a*c + -(a*d) + b*c +-(b*d) + e*c - e*d : by rewrite *mul_neg_eq_neg_mul_symm
                   ... = a*c - a*d + b*c - b*d + e*c - e*d : rfl
 check clown

其他示例

import algebra.group
import algebra.ring
open algebra
variable {A : Type}

variables [s : ring A] 
include s

theorem clown (a b c d e f  : A)  : 
    (a + b + e + f) * ( c - d) =  a*c -a*d + b*c - b*d + e*c - e*d + f*c - f*d:=
    calc
(a + b + e + f) * ( c - d) = a*(c-d) +b*(c-d) + e*(c-d) + f*(c - d) : by rewrite *right_distrib
                   ... = a*(c + -d) + b*(c+ -d) + e*(c + -d) + f *(c-d): rfl
                   ... = a*c  + a*-d+(b*c +b*-d) + (e*c + e*-d)+ f*(c-d) : by rewrite *left_distrib
                   ... = a*c + a*-d + (b*c +b*-d)+ (e*c + e*-d) + (f*c - f*d) : by rewrite *mul_sub_left_distrib 
                   ... = a*c + a*-d  + b*c + b*-d + (e*c + e*-d) + (f*c -f*d): !add.assoc
                   ... = a*c + a*-d + b*c + b*-d + e*c + e*-d  + (f*c - f*d): !add.assoc
                   ... = a*c + a*-d + b*c + b*-d + e*c + e*-d  + f*c - f*d: !add.assoc
                   ... = a*c + -(a*d) + b*c +-(b*d) + e*c + - (e*d) + f*c - f*d : by rewrite *mul_neg_eq_neg_mul_symm
                   ... = a*c - a*d + b*c - b*d + e*c - e*d  + f*c - f*d : rfl
 check clown 

其他示例

    import algebra.group
import algebra.ring
open algebra
variable {A : Type}

variables [s : ring A] 
include s

theorem clown (a b c d e f  : A)  : 
    (a + b - e  - f) * ( c - d) =  a*c -a*d + b*c -b*d -e*c + e*d - f*c + f*d :=
    calc
(a + b - e - f) * ( c - d) =(a + b + -e + -f)*(c-d) : rfl
                   ... = a*(c-d) +b*(c-d) + -e*(c-d) + -f*(c - d) : by rewrite *right_distrib
                   ... = a*(c + -d) + b*(c+ -d) + -e*(c + -d) + -f *(c-d): rfl
                   ... = a*c  + a*-d+(b*c +b*-d) + (-e*c + -e*-d)+ -f*(c-d) : by rewrite *left_distrib
                   ... = a*c + a*-d + (b*c +b*-d)+ (-e*c + -e*-d) + (-f*c - -f*d) : by rewrite *mul_sub_left_distrib 
                   ... = a*c + a*-d  + b*c + b*-d + (-e*c + -e*-d) + (-f*c - -f*d): !add.assoc
                   ... = a*c + a*-d + b*c + b*-d + -e*c + -e*-d  + (-f*c - -f*d): !add.assoc
                   ... = a*c + a*-d + b*c + b*-d + -e*c + -e*-d  + -f*c - -f*d: !add.assoc
                   ... = a*c + -(a*d) + b*c +-(b*d) + -e*c + - (-e*d) + -f*c - -f*d : by rewrite *mul_neg_eq_neg_mul_symm
                   ... = a*c - a*d + b*c -b*d + -e*c + - (-e*d) + -f*c  - -f*d : rfl
                   ... =a*c - a*d + b*c -b*d + -(e*c) + - -(e*d) + -(f*c)  - -(f*d) : by rewrite  *neg_mul_eq_neg_mul_symm
                   ... =a*c - a*d + b*c -b*d - e*c + - -(e*d) - f*c  - -(f*d) : rfl
                   ... = a*c - a*d + b*c -b*d - e*c + (e*d) - f*c  - -(f*d) : by rewrite neg_neg
                   ... = a*c - a*d + b*c -b*d - e*c + e*d - f*c +  - -(f*d) : rfl
                   ... = a*c - a*d + b*c -b*d - e*c + e*d - f*c +  (f*d) : by rewrite neg_neg
                   ... = a*c - a*d + b*c -b*d - e*c + e*d - f*c +  f*d : rfl

 check clown