Theory ZF_examples
section‹Examples of Reasoning in ZF Set Theory›
theory ZF_examples imports ZFC begin
subsection ‹Binary Trees›
consts
  bt :: "i => i"
datatype "bt(A)" =
  Lf | Br ("a ∈ A", "t1 ∈ bt(A)", "t2 ∈ bt(A)")
declare bt.intros [simp]
text‹Induction via tactic emulation›
lemma Br_neq_left [rule_format]: "l ∈ bt(A) ==> ∀x r. Br(x, l, r) ≠ l"
  
  apply (induct_tac l)
  
  apply auto
  done
text‹The new induction method, which I don't understand›
lemma Br_neq_left': "l ∈ bt(A) ==> (!!x r. Br(x, l, r) ≠ l)"
  
  apply (induct set: bt)
  
  apply auto
  done
lemma Br_iff: "Br(a,l,r) = Br(a',l',r') <-> a=a' & l=l' & r=r'"
  
  by (blast elim!: bt.free_elims)
inductive_cases Br_in_bt: "Br(a,l,r) ∈ bt(A)"
  
text ‹
@{thm[display] Br_in_bt[no_vars]}
›
subsection‹Primitive recursion›
consts  n_nodes :: "i => i"
primrec
  "n_nodes(Lf) = 0"
  "n_nodes(Br(a,l,r)) = succ(n_nodes(l) #+ n_nodes(r))"
lemma n_nodes_type [simp]: "t ∈ bt(A) ==> n_nodes(t) ∈ nat"
  by (induct_tac t, auto) 
consts  n_nodes_aux :: "i => i"
primrec
  "n_nodes_aux(Lf) = (λk ∈ nat. k)"
  "n_nodes_aux(Br(a,l,r)) = 
      (λk ∈ nat. n_nodes_aux(r) `  (n_nodes_aux(l) ` succ(k)))"
lemma n_nodes_aux_eq [rule_format]:
     "t ∈ bt(A) ==> ∀k ∈ nat. n_nodes_aux(t)`k = n_nodes(t) #+ k"
  by (induct_tac t, simp_all) 
definition n_nodes_tail :: "i => i" where
   "n_nodes_tail(t) == n_nodes_aux(t) ` 0"
lemma "t ∈ bt(A) ==> n_nodes_tail(t) = n_nodes(t)"
 by (simp add: n_nodes_tail_def n_nodes_aux_eq) 
subsection ‹Inductive definitions›
consts  Fin       :: "i=>i"
inductive
  domains   "Fin(A)" ⊆ "Pow(A)"
  intros
    emptyI:  "0 ∈ Fin(A)"
    consI:   "[| a ∈ A;  b ∈ Fin(A) |] ==> cons(a,b) ∈ Fin(A)"
  type_intros  empty_subsetI cons_subsetI PowI
  type_elims   PowD [elim_format]
consts  acc :: "i => i"
inductive
  domains "acc(r)" ⊆ "field(r)"
  intros
    vimage:  "[| r-``{a}: Pow(acc(r)); a ∈ field(r) |] ==> a ∈ acc(r)"
  monos      Pow_mono
consts
  llist  :: "i=>i"
codatatype
  "llist(A)" = LNil | LCons ("a ∈ A", "l ∈ llist(A)")
consts
  lleq :: "i=>i"
coinductive
  domains "lleq(A)" ⊆ "llist(A) * llist(A)"
  intros
    LNil:  "<LNil, LNil> ∈ lleq(A)"
    LCons: "[| a ∈ A; <l,l'> ∈ lleq(A) |] 
            ==> <LCons(a,l), LCons(a,l')> ∈ lleq(A)"
  type_intros  llist.intros
subsection‹Powerset example›
lemma Pow_mono: "A⊆B  ==>  Pow(A) ⊆ Pow(B)"
apply (rule subsetI)
apply (rule PowI)
apply (drule PowD)
apply (erule subset_trans, assumption)
done
lemma "Pow(A Int B) = Pow(A) Int Pow(B)"
  
apply (rule equalityI)
  
apply (rule Int_greatest)
  
apply (rule Int_lower1 [THEN Pow_mono])
  
apply (rule Int_lower2 [THEN Pow_mono])
  
apply (rule subsetI)
  
apply (erule IntE)
  
apply (rule PowI)
  
apply (drule PowD)+
  
apply (rule Int_greatest)
  
apply (assumption+)
done
text‹Trying again from the beginning in order to use ‹blast››
lemma "Pow(A Int B) = Pow(A) Int Pow(B)"
by blast
lemma "C⊆D ==> Union(C) ⊆ Union(D)"
  
apply (rule subsetI)
  
apply (erule UnionE)
  
apply (rule UnionI)
  
apply (erule subsetD)
  
apply assumption 
  
apply assumption 
done
text‹A more abstract version of the same proof›
lemma "C⊆D ==> Union(C) ⊆ Union(D)"
  
apply (rule Union_least)
  
apply (rule Union_upper)
  
apply (erule subsetD, assumption)
done
lemma "[| a ∈ A;  f ∈ A->B;  g ∈ C->D;  A ∩ C = 0 |] ==> (f ∪ g)`a = f`a"
  
apply (rule apply_equality)
  
apply (rule UnI1)
  
apply (rule apply_Pair)
  
apply assumption 
  
apply assumption 
  
apply (rule fun_disjoint_Un)
  
apply assumption 
  
apply assumption 
  
apply assumption 
done
end