
avigad@16908: (*  Title:      SetsAndFunctions.thy
avigad@16908:     Author:     Jeremy Avigad and Kevin Donnelly
avigad@16908: *)
avigad@16908: 
avigad@16908: header {* Operations on sets and functions *}
avigad@16908: 
avigad@16908: theory SetsAndFunctions
avigad@16908: imports Main
avigad@16908: begin
avigad@16908: 
avigad@16908: text {* 
avigad@16908: This library lifts operations like addition and muliplication to sets and
avigad@16908: functions of appropriate types. It was designed to support asymptotic
avigad@16908: calculations. See the comments at the top of BigO.thy
avigad@16908: *}
avigad@16908: 
avigad@16908: subsection {* Basic definitions *} 
avigad@16908: 
avigad@16908: instance set :: (plus)plus
avigad@16908: by intro_classes
avigad@16908: 
avigad@16908: instance fun :: (type,plus)plus
avigad@16908: by intro_classes
avigad@16908: 
avigad@16908: defs (overloaded)
avigad@16908:   func_plus: "f + g == (%x. f x + g x)"
avigad@16908:   set_plus: "A + B == {c. EX a:A. EX b:B. c = a + b}"
avigad@16908: 
avigad@16908: instance set :: (times)times
avigad@16908: by intro_classes
avigad@16908: 
avigad@16908: instance fun :: (type,times)times
avigad@16908: by intro_classes
avigad@16908: 
avigad@16908: defs (overloaded)
avigad@16908:   func_times: "f * g == (%x. f x * g x)" 
avigad@16908:   set_times:"A * B == {c. EX a:A. EX b:B. c = a * b}"
avigad@16908: 
avigad@16908: instance fun :: (type,minus)minus
avigad@16908: by intro_classes
avigad@16908: 
avigad@16908: defs (overloaded)
avigad@16908:   func_minus: "- f == (%x. - f x)"
avigad@16908:   func_diff: "f - g == %x. f x - g x"                 
avigad@16908: 
avigad@16908: instance fun :: (type,zero)zero
avigad@16908: by intro_classes
avigad@16908: 
avigad@16908: instance set :: (zero)zero
avigad@16908: by(intro_classes)
avigad@16908: 
avigad@16908: defs (overloaded)
avigad@16908:   func_zero: "0::(('a::type) => ('b::zero)) == %x. 0"
avigad@16908:   set_zero: "0::('a::zero)set == {0}"
avigad@16908: 
avigad@16908: instance fun :: (type,one)one
avigad@16908: by intro_classes
avigad@16908: 
avigad@16908: instance set :: (one)one
avigad@16908: by intro_classes
avigad@16908: 
avigad@16908: defs (overloaded)
avigad@16908:   func_one: "1::(('a::type) => ('b::one)) == %x. 1"
avigad@16908:   set_one: "1::('a::one)set == {1}"
avigad@16908: 
avigad@16908: constdefs 
avigad@16908:   elt_set_plus :: "'a::plus => 'a set => 'a set"    (infixl "+o" 70)
avigad@16908:   "a +o B == {c. EX b:B. c = a + b}"
avigad@16908: 
avigad@16908:   elt_set_times :: "'a::times => 'a set => 'a set"  (infixl "*o" 80)
avigad@16908:   "a *o B == {c. EX b:B. c = a * b}"
avigad@16908: 
avigad@16908: syntax
avigad@16908:   "elt_set_eq" :: "'a => 'a set => bool"      (infix "=o" 50)
avigad@16908: 
avigad@16908: translations
avigad@16908:   "x =o A" => "x : A"
avigad@16908: 
avigad@16908: instance fun :: (type,semigroup_add)semigroup_add
avigad@16908:   apply intro_classes
avigad@16908:   apply (auto simp add: func_plus add_assoc)
avigad@16908: done
avigad@16908: 
avigad@16908: instance fun :: (type,comm_monoid_add)comm_monoid_add
avigad@16908:   apply intro_classes
avigad@16908:   apply (auto simp add: func_zero func_plus add_ac)
avigad@16908: done
avigad@16908: 
avigad@16908: instance fun :: (type,ab_group_add)ab_group_add
avigad@16908:   apply intro_classes
avigad@16908:   apply (simp add: func_minus func_plus func_zero)
avigad@16908:   apply (simp add: func_minus func_plus func_diff diff_minus)
avigad@16908: done
avigad@16908: 
avigad@16908: instance fun :: (type,semigroup_mult)semigroup_mult
avigad@16908:   apply intro_classes
avigad@16908:   apply (auto simp add: func_times mult_assoc)
avigad@16908: done
avigad@16908: 
avigad@16908: instance fun :: (type,comm_monoid_mult)comm_monoid_mult
avigad@16908:   apply intro_classes
avigad@16908:   apply (auto simp add: func_one func_times mult_ac)
avigad@16908: done
avigad@16908: 
avigad@16908: instance fun :: (type,comm_ring_1)comm_ring_1
avigad@16908:   apply intro_classes
avigad@16908:   apply (auto simp add: func_plus func_times func_minus func_diff ext 
avigad@16908:     func_one func_zero ring_eq_simps) 
avigad@16908:   apply (drule fun_cong)
avigad@16908:   apply simp
avigad@16908: done
avigad@16908: 
avigad@16908: instance set :: (semigroup_add)semigroup_add
avigad@16908:   apply intro_classes
avigad@16908:   apply (unfold set_plus)
avigad@16908:   apply (force simp add: add_assoc)
avigad@16908: done
avigad@16908: 
avigad@16908: instance set :: (semigroup_mult)semigroup_mult
avigad@16908:   apply intro_classes
avigad@16908:   apply (unfold set_times)
avigad@16908:   apply (force simp add: mult_assoc)
avigad@16908: done
avigad@16908: 
avigad@16908: instance set :: (comm_monoid_add)comm_monoid_add
avigad@16908:   apply intro_classes
avigad@16908:   apply (unfold set_plus)
avigad@16908:   apply (force simp add: add_ac)
avigad@16908:   apply (unfold set_zero)
avigad@16908:   apply force
avigad@16908: done
avigad@16908: 
avigad@16908: instance set :: (comm_monoid_mult)comm_monoid_mult
avigad@16908:   apply intro_classes
avigad@16908:   apply (unfold set_times)
avigad@16908:   apply (force simp add: mult_ac)
avigad@16908:   apply (unfold set_one)
avigad@16908:   apply force
avigad@16908: done
avigad@16908: 
avigad@16908: subsection {* Basic properties *}
avigad@16908: 
avigad@16908: lemma set_plus_intro [intro]: "a : C ==> b : D ==> a + b : C + D" 
avigad@16908: by (auto simp add: set_plus)
avigad@16908: 
avigad@16908: lemma set_plus_intro2 [intro]: "b : C ==> a + b : a +o C"
avigad@16908: by (auto simp add: elt_set_plus_def)
avigad@16908: 
avigad@16908: lemma set_plus_rearrange: "((a::'a::comm_monoid_add) +o C) + 
avigad@16908:   (b +o D) = (a + b) +o (C + D)"
avigad@16908:   apply (auto simp add: elt_set_plus_def set_plus add_ac)
avigad@16908:   apply (rule_tac x = "ba + bb" in exI)
avigad@16908:   apply (auto simp add: add_ac)
avigad@16908:   apply (rule_tac x = "aa + a" in exI)
avigad@16908:   apply (auto simp add: add_ac)
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_plus_rearrange2: "(a::'a::semigroup_add) +o (b +o C) = 
avigad@16908:   (a + b) +o C"
avigad@16908: by (auto simp add: elt_set_plus_def add_assoc)
avigad@16908: 
avigad@16908: lemma set_plus_rearrange3: "((a::'a::semigroup_add) +o B) + C = 
avigad@16908:   a +o (B + C)"
avigad@16908:   apply (auto simp add: elt_set_plus_def set_plus)
avigad@16908:   apply (blast intro: add_ac)
avigad@16908:   apply (rule_tac x = "a + aa" in exI)
avigad@16908:   apply (rule conjI)
avigad@16908:   apply (rule_tac x = "aa" in bexI)
avigad@16908:   apply auto
avigad@16908:   apply (rule_tac x = "ba" in bexI)
avigad@16908:   apply (auto simp add: add_ac)
avigad@16908: done
avigad@16908: 
avigad@16908: theorem set_plus_rearrange4: "C + ((a::'a::comm_monoid_add) +o D) = 
avigad@16908:     a +o (C + D)" 
avigad@16908:   apply (auto intro!: subsetI simp add: elt_set_plus_def set_plus add_ac)
avigad@16908:   apply (rule_tac x = "aa + ba" in exI)
avigad@16908:   apply (auto simp add: add_ac)
avigad@16908: done
avigad@16908: 
avigad@16908: theorems set_plus_rearranges = set_plus_rearrange set_plus_rearrange2
avigad@16908:   set_plus_rearrange3 set_plus_rearrange4
avigad@16908: 
avigad@16908: lemma set_plus_mono [intro!]: "C <= D ==> a +o C <= a +o D"
avigad@16908: by (auto simp add: elt_set_plus_def)
avigad@16908: 
avigad@16908: lemma set_plus_mono2 [intro]: "(C::('a::plus) set) <= D ==> E <= F ==> 
avigad@16908:     C + E <= D + F"
avigad@16908: by (auto simp add: set_plus)
avigad@16908: 
avigad@16908: lemma set_plus_mono3 [intro]: "a : C ==> a +o D <= C + D"
avigad@16908: by (auto simp add: elt_set_plus_def set_plus)
avigad@16908: 
avigad@16908: lemma set_plus_mono4 [intro]: "(a::'a::comm_monoid_add) : C ==> 
avigad@16908:   a +o D <= D + C" 
avigad@16908: by (auto simp add: elt_set_plus_def set_plus add_ac)
avigad@16908: 
avigad@16908: lemma set_plus_mono5: "a:C ==> B <= D ==> a +o B <= C + D"
avigad@16908:   apply (subgoal_tac "a +o B <= a +o D")
avigad@16908:   apply (erule order_trans)
avigad@16908:   apply (erule set_plus_mono3)
avigad@16908:   apply (erule set_plus_mono)
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_plus_mono_b: "C <= D ==> x : a +o C 
avigad@16908:     ==> x : a +o D"
avigad@16908:   apply (frule set_plus_mono)
avigad@16908:   apply auto
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_plus_mono2_b: "C <= D ==> E <= F ==> x : C + E ==> 
avigad@16908:     x : D + F"
avigad@16908:   apply (frule set_plus_mono2)
avigad@16908:   prefer 2
avigad@16908:   apply force
avigad@16908:   apply assumption
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_plus_mono3_b: "a : C ==> x : a +o D ==> x : C + D"
avigad@16908:   apply (frule set_plus_mono3)
avigad@16908:   apply auto
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_plus_mono4_b: "(a::'a::comm_monoid_add) : C ==> 
avigad@16908:   x : a +o D ==> x : D + C" 
avigad@16908:   apply (frule set_plus_mono4)
avigad@16908:   apply auto
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_zero_plus [simp]: "(0::'a::comm_monoid_add) +o C = C"
avigad@16908: by (auto simp add: elt_set_plus_def)
avigad@16908: 
avigad@16908: lemma set_zero_plus2: "(0::'a::comm_monoid_add) : A ==> B <= A + B"
avigad@16908:   apply (auto intro!: subsetI simp add: set_plus)
avigad@16908:   apply (rule_tac x = 0 in bexI)
avigad@16908:   apply (rule_tac x = x in bexI)
avigad@16908:   apply (auto simp add: add_ac)
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_plus_imp_minus: "(a::'a::ab_group_add) : b +o C ==> (a - b) : C"
avigad@16908: by (auto simp add: elt_set_plus_def add_ac diff_minus)
avigad@16908: 
avigad@16908: lemma set_minus_imp_plus: "(a::'a::ab_group_add) - b : C ==> a : b +o C"
avigad@16908:   apply (auto simp add: elt_set_plus_def add_ac diff_minus)
avigad@16908:   apply (subgoal_tac "a = (a + - b) + b")
avigad@16908:   apply (rule bexI, assumption, assumption)
avigad@16908:   apply (auto simp add: add_ac)
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_minus_plus: "((a::'a::ab_group_add) - b : C) = (a : b +o C)"
avigad@16908: by (rule iffI, rule set_minus_imp_plus, assumption, rule set_plus_imp_minus, 
avigad@16908:     assumption)
avigad@16908: 
avigad@16908: lemma set_times_intro [intro]: "a : C ==> b : D ==> a * b : C * D" 
avigad@16908: by (auto simp add: set_times)
avigad@16908: 
avigad@16908: lemma set_times_intro2 [intro!]: "b : C ==> a * b : a *o C"
avigad@16908: by (auto simp add: elt_set_times_def)
avigad@16908: 
avigad@16908: lemma set_times_rearrange: "((a::'a::comm_monoid_mult) *o C) * 
avigad@16908:   (b *o D) = (a * b) *o (C * D)"
avigad@16908:   apply (auto simp add: elt_set_times_def set_times)
avigad@16908:   apply (rule_tac x = "ba * bb" in exI)
avigad@16908:   apply (auto simp add: mult_ac)
avigad@16908:   apply (rule_tac x = "aa * a" in exI)
avigad@16908:   apply (auto simp add: mult_ac)
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_times_rearrange2: "(a::'a::semigroup_mult) *o (b *o C) = 
avigad@16908:   (a * b) *o C"
avigad@16908: by (auto simp add: elt_set_times_def mult_assoc)
avigad@16908: 
avigad@16908: lemma set_times_rearrange3: "((a::'a::semigroup_mult) *o B) * C = 
avigad@16908:   a *o (B * C)"
avigad@16908:   apply (auto simp add: elt_set_times_def set_times)
avigad@16908:   apply (blast intro: mult_ac)
avigad@16908:   apply (rule_tac x = "a * aa" in exI)
avigad@16908:   apply (rule conjI)
avigad@16908:   apply (rule_tac x = "aa" in bexI)
avigad@16908:   apply auto
avigad@16908:   apply (rule_tac x = "ba" in bexI)
avigad@16908:   apply (auto simp add: mult_ac)
avigad@16908: done
avigad@16908: 
avigad@16908: theorem set_times_rearrange4: "C * ((a::'a::comm_monoid_mult) *o D) = 
avigad@16908:     a *o (C * D)" 
avigad@16908:   apply (auto intro!: subsetI simp add: elt_set_times_def set_times 
avigad@16908:     mult_ac)
avigad@16908:   apply (rule_tac x = "aa * ba" in exI)
avigad@16908:   apply (auto simp add: mult_ac)
avigad@16908: done
avigad@16908: 
avigad@16908: theorems set_times_rearranges = set_times_rearrange set_times_rearrange2
avigad@16908:   set_times_rearrange3 set_times_rearrange4
avigad@16908: 
avigad@16908: lemma set_times_mono [intro]: "C <= D ==> a *o C <= a *o D"
avigad@16908: by (auto simp add: elt_set_times_def)
avigad@16908: 
avigad@16908: lemma set_times_mono2 [intro]: "(C::('a::times) set) <= D ==> E <= F ==> 
avigad@16908:     C * E <= D * F"
avigad@16908: by (auto simp add: set_times)
avigad@16908: 
avigad@16908: lemma set_times_mono3 [intro]: "a : C ==> a *o D <= C * D"
avigad@16908: by (auto simp add: elt_set_times_def set_times)
avigad@16908: 
avigad@16908: lemma set_times_mono4 [intro]: "(a::'a::comm_monoid_mult) : C ==> 
avigad@16908:   a *o D <= D * C" 
avigad@16908: by (auto simp add: elt_set_times_def set_times mult_ac)
avigad@16908: 
avigad@16908: lemma set_times_mono5: "a:C ==> B <= D ==> a *o B <= C * D"
avigad@16908:   apply (subgoal_tac "a *o B <= a *o D")
avigad@16908:   apply (erule order_trans)
avigad@16908:   apply (erule set_times_mono3)
avigad@16908:   apply (erule set_times_mono)
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_times_mono_b: "C <= D ==> x : a *o C 
avigad@16908:     ==> x : a *o D"
avigad@16908:   apply (frule set_times_mono)
avigad@16908:   apply auto
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_times_mono2_b: "C <= D ==> E <= F ==> x : C * E ==> 
avigad@16908:     x : D * F"
avigad@16908:   apply (frule set_times_mono2)
avigad@16908:   prefer 2
avigad@16908:   apply force
avigad@16908:   apply assumption
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_times_mono3_b: "a : C ==> x : a *o D ==> x : C * D"
avigad@16908:   apply (frule set_times_mono3)
avigad@16908:   apply auto
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_times_mono4_b: "(a::'a::comm_monoid_mult) : C ==> 
avigad@16908:   x : a *o D ==> x : D * C" 
avigad@16908:   apply (frule set_times_mono4)
avigad@16908:   apply auto
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_one_times [simp]: "(1::'a::comm_monoid_mult) *o C = C"
avigad@16908: by (auto simp add: elt_set_times_def)
avigad@16908: 
avigad@16908: lemma set_times_plus_distrib: "(a::'a::semiring) *o (b +o C)= 
avigad@16908:   (a * b) +o (a *o C)"
avigad@16908: by (auto simp add: elt_set_plus_def elt_set_times_def ring_distrib)
avigad@16908: 
avigad@16908: lemma set_times_plus_distrib2: "(a::'a::semiring) *o (B + C) = 
avigad@16908:   (a *o B) + (a *o C)"
avigad@16908:   apply (auto simp add: set_plus elt_set_times_def ring_distrib)
avigad@16908:   apply blast
avigad@16908:   apply (rule_tac x = "b + bb" in exI)
avigad@16908:   apply (auto simp add: ring_distrib)
avigad@16908: done
avigad@16908: 
avigad@16908: lemma set_times_plus_distrib3: "((a::'a::semiring) +o C) * D <= 
avigad@16908:     a *o D + C * D"
avigad@16908:   apply (auto intro!: subsetI simp add: 
avigad@16908:     elt_set_plus_def elt_set_times_def set_times 
avigad@16908:     set_plus ring_distrib)
avigad@16908:   apply auto
avigad@16908: done
avigad@16908: 
avigad@16908: theorems set_times_plus_distribs = set_times_plus_distrib
avigad@16908:   set_times_plus_distrib2
avigad@16908: 
avigad@16908: lemma set_neg_intro: "(a::'a::ring_1) : (- 1) *o C ==> 
avigad@16908:     - a : C" 
avigad@16908: by (auto simp add: elt_set_times_def)
avigad@16908: 
avigad@16908: lemma set_neg_intro2: "(a::'a::ring_1) : C ==>
avigad@16908:     - a : (- 1) *o C"
avigad@16908: by (auto simp add: elt_set_times_def)
avigad@16908:   
avigad@16908: end




