
wenzelm@16932: (*  Title:      HOL/Library/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
haftmann@30738: imports Main
avigad@16908: begin
avigad@16908: 
wenzelm@19736: 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
wenzelm@17161: calculations. See the comments at the top of theory @{text BigO}.
avigad@16908: *}
avigad@16908: 
wenzelm@19736: subsection {* Basic definitions *}
avigad@16908: 
haftmann@25594: definition
berghofe@26814:   set_plus :: "('a::plus) set => 'a set => 'a set"  (infixl "\<oplus>" 65) where
berghofe@26814:   "A \<oplus> B == {c. EX a:A. EX b:B. c = a + b}"
haftmann@25594: 
haftmann@25594: instantiation "fun" :: (type, plus) plus
haftmann@25594: begin
avigad@16908: 
haftmann@25594: definition
haftmann@25594:   func_plus: "f + g == (%x. f x + g x)"
haftmann@25594: 
haftmann@25594: instance ..
haftmann@25594: 
haftmann@25594: end
haftmann@25594: 
haftmann@25594: definition
berghofe@26814:   set_times :: "('a::times) set => 'a set => 'a set"  (infixl "\<otimes>" 70) where
berghofe@26814:   "A \<otimes> B == {c. EX a:A. EX b:B. c = a * b}"
haftmann@25594: 
haftmann@25594: instantiation "fun" :: (type, times) times
haftmann@25594: begin
haftmann@25594: 
haftmann@25594: definition
haftmann@25594:   func_times: "f * g == (%x. f x * g x)"
avigad@16908: 
haftmann@25594: instance ..
haftmann@25594: 
haftmann@25594: end
haftmann@25594: 
haftmann@25594: 
haftmann@25594: instantiation "fun" :: (type, zero) zero
haftmann@25594: begin
haftmann@25594: 
haftmann@25594: definition
haftmann@25594:   func_zero: "0::(('a::type) => ('b::zero)) == %x. 0"
haftmann@25594: 
haftmann@25594: instance ..
haftmann@25594: 
haftmann@25594: end
haftmann@25594: 
haftmann@25594: instantiation "fun" :: (type, one) one
haftmann@25594: begin
haftmann@25594: 
haftmann@25594: definition
avigad@16908:   func_one: "1::(('a::type) => ('b::one)) == %x. 1"
haftmann@25594: 
haftmann@25594: instance ..
haftmann@25594: 
haftmann@25594: end
avigad@16908: 
wenzelm@19736: definition
wenzelm@21404:   elt_set_plus :: "'a::plus => 'a set => 'a set"  (infixl "+o" 70) where
wenzelm@19736:   "a +o B = {c. EX b:B. c = a + b}"
avigad@16908: 
wenzelm@21404: definition
wenzelm@21404:   elt_set_times :: "'a::times => 'a set => 'a set"  (infixl "*o" 80) where
wenzelm@19736:   "a *o B = {c. EX b:B. c = a * b}"
avigad@16908: 
wenzelm@19656: abbreviation (input)
wenzelm@21404:   elt_set_eq :: "'a => 'a set => bool"  (infix "=o" 50) where
wenzelm@19380:   "x =o A == x : A"
avigad@16908: 
krauss@20523: instance "fun" :: (type,semigroup_add)semigroup_add
wenzelm@19380:   by default (auto simp add: func_plus add_assoc)
avigad@16908: 
krauss@20523: instance "fun" :: (type,comm_monoid_add)comm_monoid_add
wenzelm@19380:   by default (auto simp add: func_zero func_plus add_ac)
avigad@16908: 
krauss@20523: instance "fun" :: (type,ab_group_add)ab_group_add
wenzelm@19736:   apply default
berghofe@26814:    apply (simp add: fun_Compl_def func_plus func_zero)
berghofe@26814:   apply (simp add: fun_Compl_def func_plus fun_diff_def diff_minus)
wenzelm@19736:   done
avigad@16908: 
krauss@20523: instance "fun" :: (type,semigroup_mult)semigroup_mult
wenzelm@19736:   apply default
avigad@16908:   apply (auto simp add: func_times mult_assoc)
wenzelm@19736:   done
avigad@16908: 
krauss@20523: instance "fun" :: (type,comm_monoid_mult)comm_monoid_mult
wenzelm@19736:   apply default
wenzelm@19736:    apply (auto simp add: func_one func_times mult_ac)
wenzelm@19736:   done
avigad@16908: 
krauss@20523: instance "fun" :: (type,comm_ring_1)comm_ring_1
wenzelm@19736:   apply default
nipkow@29667:    apply (auto simp add: func_plus func_times fun_Compl_def fun_diff_def
nipkow@29667:      func_one func_zero algebra_simps)
avigad@16908:   apply (drule fun_cong)
avigad@16908:   apply simp
wenzelm@19736:   done
avigad@16908: 
haftmann@29509: interpretation set_semigroup_add!: semigroup_add "op \<oplus> :: ('a::semigroup_add) set => 'a set => 'a set"
wenzelm@19736:   apply default
berghofe@26814:   apply (unfold set_plus_def)
avigad@16908:   apply (force simp add: add_assoc)
wenzelm@19736:   done
avigad@16908: 
haftmann@29509: interpretation set_semigroup_mult!: semigroup_mult "op \<otimes> :: ('a::semigroup_mult) set => 'a set => 'a set"
wenzelm@19736:   apply default
berghofe@26814:   apply (unfold set_times_def)
avigad@16908:   apply (force simp add: mult_assoc)
wenzelm@19736:   done
avigad@16908: 
haftmann@29509: interpretation set_comm_monoid_add!: comm_monoid_add "{0}" "op \<oplus> :: ('a::comm_monoid_add) set => 'a set => 'a set"
wenzelm@19736:   apply default
berghofe@26814:    apply (unfold set_plus_def)
wenzelm@19736:    apply (force simp add: add_ac)
avigad@16908:   apply force
wenzelm@19736:   done
avigad@16908: 
haftmann@29509: interpretation set_comm_monoid_mult!: comm_monoid_mult "{1}" "op \<otimes> :: ('a::comm_monoid_mult) set => 'a set => 'a set"
wenzelm@19736:   apply default
berghofe@26814:    apply (unfold set_times_def)
wenzelm@19736:    apply (force simp add: mult_ac)
avigad@16908:   apply force
wenzelm@19736:   done
wenzelm@19736: 
avigad@16908: 
avigad@16908: subsection {* Basic properties *}
avigad@16908: 
berghofe@26814: lemma set_plus_intro [intro]: "a : C ==> b : D ==> a + b : C \<oplus> D"
berghofe@26814:   by (auto simp add: set_plus_def)
avigad@16908: 
avigad@16908: lemma set_plus_intro2 [intro]: "b : C ==> a + b : a +o C"
wenzelm@19736:   by (auto simp add: elt_set_plus_def)
avigad@16908: 
berghofe@26814: lemma set_plus_rearrange: "((a::'a::comm_monoid_add) +o C) \<oplus>
berghofe@26814:     (b +o D) = (a + b) +o (C \<oplus> D)"
berghofe@26814:   apply (auto simp add: elt_set_plus_def set_plus_def add_ac)
wenzelm@19736:    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)
wenzelm@19736:   done
avigad@16908: 
wenzelm@19736: lemma set_plus_rearrange2: "(a::'a::semigroup_add) +o (b +o C) =
wenzelm@19736:     (a + b) +o C"
wenzelm@19736:   by (auto simp add: elt_set_plus_def add_assoc)
avigad@16908: 
berghofe@26814: lemma set_plus_rearrange3: "((a::'a::semigroup_add) +o B) \<oplus> C =
berghofe@26814:     a +o (B \<oplus> C)"
berghofe@26814:   apply (auto simp add: elt_set_plus_def set_plus_def)
wenzelm@19736:    apply (blast intro: add_ac)
avigad@16908:   apply (rule_tac x = "a + aa" in exI)
avigad@16908:   apply (rule conjI)
wenzelm@19736:    apply (rule_tac x = "aa" in bexI)
wenzelm@19736:     apply auto
avigad@16908:   apply (rule_tac x = "ba" in bexI)
wenzelm@19736:    apply (auto simp add: add_ac)
wenzelm@19736:   done
avigad@16908: 
berghofe@26814: theorem set_plus_rearrange4: "C \<oplus> ((a::'a::comm_monoid_add) +o D) =
berghofe@26814:     a +o (C \<oplus> D)"
berghofe@26814:   apply (auto intro!: subsetI simp add: elt_set_plus_def set_plus_def add_ac)
wenzelm@19736:    apply (rule_tac x = "aa + ba" in exI)
wenzelm@19736:    apply (auto simp add: add_ac)
wenzelm@19736:   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"
wenzelm@19736:   by (auto simp add: elt_set_plus_def)
avigad@16908: 
wenzelm@19736: lemma set_plus_mono2 [intro]: "(C::('a::plus) set) <= D ==> E <= F ==>
berghofe@26814:     C \<oplus> E <= D \<oplus> F"
berghofe@26814:   by (auto simp add: set_plus_def)
avigad@16908: 
berghofe@26814: lemma set_plus_mono3 [intro]: "a : C ==> a +o D <= C \<oplus> D"
berghofe@26814:   by (auto simp add: elt_set_plus_def set_plus_def)
avigad@16908: 
wenzelm@19736: lemma set_plus_mono4 [intro]: "(a::'a::comm_monoid_add) : C ==>
berghofe@26814:     a +o D <= D \<oplus> C"
berghofe@26814:   by (auto simp add: elt_set_plus_def set_plus_def add_ac)
avigad@16908: 
berghofe@26814: lemma set_plus_mono5: "a:C ==> B <= D ==> a +o B <= C \<oplus> D"
avigad@16908:   apply (subgoal_tac "a +o B <= a +o D")
wenzelm@19736:    apply (erule order_trans)
wenzelm@19736:    apply (erule set_plus_mono3)
avigad@16908:   apply (erule set_plus_mono)
wenzelm@19736:   done
avigad@16908: 
wenzelm@19736: 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
wenzelm@19736:   done
avigad@16908: 
berghofe@26814: lemma set_plus_mono2_b: "C <= D ==> E <= F ==> x : C \<oplus> E ==>
berghofe@26814:     x : D \<oplus> F"
avigad@16908:   apply (frule set_plus_mono2)
wenzelm@19736:    prefer 2
wenzelm@19736:    apply force
avigad@16908:   apply assumption
wenzelm@19736:   done
avigad@16908: 
berghofe@26814: lemma set_plus_mono3_b: "a : C ==> x : a +o D ==> x : C \<oplus> D"
avigad@16908:   apply (frule set_plus_mono3)
avigad@16908:   apply auto
wenzelm@19736:   done
avigad@16908: 
wenzelm@19736: lemma set_plus_mono4_b: "(a::'a::comm_monoid_add) : C ==>
berghofe@26814:     x : a +o D ==> x : D \<oplus> C"
avigad@16908:   apply (frule set_plus_mono4)
avigad@16908:   apply auto
wenzelm@19736:   done
avigad@16908: 
avigad@16908: lemma set_zero_plus [simp]: "(0::'a::comm_monoid_add) +o C = C"
wenzelm@19736:   by (auto simp add: elt_set_plus_def)
avigad@16908: 
berghofe@26814: lemma set_zero_plus2: "(0::'a::comm_monoid_add) : A ==> B <= A \<oplus> B"
berghofe@26814:   apply (auto intro!: subsetI simp add: set_plus_def)
avigad@16908:   apply (rule_tac x = 0 in bexI)
wenzelm@19736:    apply (rule_tac x = x in bexI)
wenzelm@19736:     apply (auto simp add: add_ac)
wenzelm@19736:   done
avigad@16908: 
avigad@16908: lemma set_plus_imp_minus: "(a::'a::ab_group_add) : b +o C ==> (a - b) : C"
wenzelm@19736:   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")
wenzelm@19736:    apply (rule bexI, assumption, assumption)
avigad@16908:   apply (auto simp add: add_ac)
wenzelm@19736:   done
avigad@16908: 
avigad@16908: lemma set_minus_plus: "((a::'a::ab_group_add) - b : C) = (a : b +o C)"
wenzelm@19736:   by (rule iffI, rule set_minus_imp_plus, assumption, rule set_plus_imp_minus,
avigad@16908:     assumption)
avigad@16908: 
berghofe@26814: lemma set_times_intro [intro]: "a : C ==> b : D ==> a * b : C \<otimes> D"
berghofe@26814:   by (auto simp add: set_times_def)
avigad@16908: 
avigad@16908: lemma set_times_intro2 [intro!]: "b : C ==> a * b : a *o C"
wenzelm@19736:   by (auto simp add: elt_set_times_def)
avigad@16908: 
berghofe@26814: lemma set_times_rearrange: "((a::'a::comm_monoid_mult) *o C) \<otimes>
berghofe@26814:     (b *o D) = (a * b) *o (C \<otimes> D)"
berghofe@26814:   apply (auto simp add: elt_set_times_def set_times_def)
wenzelm@19736:    apply (rule_tac x = "ba * bb" in exI)
wenzelm@19736:    apply (auto simp add: mult_ac)
avigad@16908:   apply (rule_tac x = "aa * a" in exI)
avigad@16908:   apply (auto simp add: mult_ac)
wenzelm@19736:   done
avigad@16908: 
wenzelm@19736: lemma set_times_rearrange2: "(a::'a::semigroup_mult) *o (b *o C) =
wenzelm@19736:     (a * b) *o C"
wenzelm@19736:   by (auto simp add: elt_set_times_def mult_assoc)
avigad@16908: 
berghofe@26814: lemma set_times_rearrange3: "((a::'a::semigroup_mult) *o B) \<otimes> C =
berghofe@26814:     a *o (B \<otimes> C)"
berghofe@26814:   apply (auto simp add: elt_set_times_def set_times_def)
wenzelm@19736:    apply (blast intro: mult_ac)
avigad@16908:   apply (rule_tac x = "a * aa" in exI)
avigad@16908:   apply (rule conjI)
wenzelm@19736:    apply (rule_tac x = "aa" in bexI)
wenzelm@19736:     apply auto
avigad@16908:   apply (rule_tac x = "ba" in bexI)
wenzelm@19736:    apply (auto simp add: mult_ac)
wenzelm@19736:   done
avigad@16908: 
berghofe@26814: theorem set_times_rearrange4: "C \<otimes> ((a::'a::comm_monoid_mult) *o D) =
berghofe@26814:     a *o (C \<otimes> D)"
berghofe@26814:   apply (auto intro!: subsetI simp add: elt_set_times_def set_times_def
avigad@16908:     mult_ac)
wenzelm@19736:    apply (rule_tac x = "aa * ba" in exI)
wenzelm@19736:    apply (auto simp add: mult_ac)
wenzelm@19736:   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"
wenzelm@19736:   by (auto simp add: elt_set_times_def)
avigad@16908: 
wenzelm@19736: lemma set_times_mono2 [intro]: "(C::('a::times) set) <= D ==> E <= F ==>
berghofe@26814:     C \<otimes> E <= D \<otimes> F"
berghofe@26814:   by (auto simp add: set_times_def)
avigad@16908: 
berghofe@26814: lemma set_times_mono3 [intro]: "a : C ==> a *o D <= C \<otimes> D"
berghofe@26814:   by (auto simp add: elt_set_times_def set_times_def)
avigad@16908: 
wenzelm@19736: lemma set_times_mono4 [intro]: "(a::'a::comm_monoid_mult) : C ==>
berghofe@26814:     a *o D <= D \<otimes> C"
berghofe@26814:   by (auto simp add: elt_set_times_def set_times_def mult_ac)
avigad@16908: 
berghofe@26814: lemma set_times_mono5: "a:C ==> B <= D ==> a *o B <= C \<otimes> D"
avigad@16908:   apply (subgoal_tac "a *o B <= a *o D")
wenzelm@19736:    apply (erule order_trans)
wenzelm@19736:    apply (erule set_times_mono3)
avigad@16908:   apply (erule set_times_mono)
wenzelm@19736:   done
avigad@16908: 
wenzelm@19736: 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
wenzelm@19736:   done
avigad@16908: 
berghofe@26814: lemma set_times_mono2_b: "C <= D ==> E <= F ==> x : C \<otimes> E ==>
berghofe@26814:     x : D \<otimes> F"
avigad@16908:   apply (frule set_times_mono2)
wenzelm@19736:    prefer 2
wenzelm@19736:    apply force
avigad@16908:   apply assumption
wenzelm@19736:   done
avigad@16908: 
berghofe@26814: lemma set_times_mono3_b: "a : C ==> x : a *o D ==> x : C \<otimes> D"
avigad@16908:   apply (frule set_times_mono3)
avigad@16908:   apply auto
wenzelm@19736:   done
avigad@16908: 
wenzelm@19736: lemma set_times_mono4_b: "(a::'a::comm_monoid_mult) : C ==>
berghofe@26814:     x : a *o D ==> x : D \<otimes> C"
avigad@16908:   apply (frule set_times_mono4)
avigad@16908:   apply auto
wenzelm@19736:   done
avigad@16908: 
avigad@16908: lemma set_one_times [simp]: "(1::'a::comm_monoid_mult) *o C = C"
wenzelm@19736:   by (auto simp add: elt_set_times_def)
avigad@16908: 
wenzelm@19736: lemma set_times_plus_distrib: "(a::'a::semiring) *o (b +o C)=
wenzelm@19736:     (a * b) +o (a *o C)"
nipkow@23477:   by (auto simp add: elt_set_plus_def elt_set_times_def ring_distribs)
avigad@16908: 
berghofe@26814: lemma set_times_plus_distrib2: "(a::'a::semiring) *o (B \<oplus> C) =
berghofe@26814:     (a *o B) \<oplus> (a *o C)"
berghofe@26814:   apply (auto simp add: set_plus_def elt_set_times_def ring_distribs)
wenzelm@19736:    apply blast
avigad@16908:   apply (rule_tac x = "b + bb" in exI)
nipkow@23477:   apply (auto simp add: ring_distribs)
wenzelm@19736:   done
avigad@16908: 
berghofe@26814: lemma set_times_plus_distrib3: "((a::'a::semiring) +o C) \<otimes> D <=
berghofe@26814:     a *o D \<oplus> C \<otimes> D"
wenzelm@19736:   apply (auto intro!: subsetI simp add:
berghofe@26814:     elt_set_plus_def elt_set_times_def set_times_def
berghofe@26814:     set_plus_def ring_distribs)
avigad@16908:   apply auto
wenzelm@19736:   done
avigad@16908: 
wenzelm@19380: theorems set_times_plus_distribs =
wenzelm@19380:   set_times_plus_distrib
avigad@16908:   set_times_plus_distrib2
avigad@16908: 
wenzelm@19736: lemma set_neg_intro: "(a::'a::ring_1) : (- 1) *o C ==>
wenzelm@19736:     - a : C"
wenzelm@19736:   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"
wenzelm@19736:   by (auto simp add: elt_set_times_def)
wenzelm@19736: 
avigad@16908: end




