theory Termin9 imports Main "~~/src/HOL/Library/Quotient_List" "~~/src/HOL/Library/Code_Target_Numeral" begin section \Typedef\ typedef 'a ne = "{xs :: 'a list . xs \ []}" by (rule exI[where x = "[undefined]"]) simp definition singleton :: "'a \ 'a ne" where "singleton x = Abs_ne [x]" definition append :: "'a ne \ 'a list \ 'a ne" where "append l1 l2 = Abs_ne (Rep_ne l1 @ l2)" definition head :: "'a ne \ 'a" where "head x = hd (Rep_ne x)" definition tail :: "'a ne \ 'a ne" where "tail x = Abs_ne (tl (Rep_ne x))" lemma "head (append l1 l2) = head l1" unfolding head_def append_def by (simp add: Abs_ne_inverse Rep_ne[simplified]) definition tail' :: "'a ne \ 'a list" where "tail' x = tl (Rep_ne x)" lemma "append (singleton (head l)) (tail' l) = l" unfolding head_def append_def tail'_def singleton_def by (simp add: Abs_ne_inverse Rep_ne_inverse Rep_ne[simplified]) section \Lifting: Merge-Sort (Blatt 4)\ hide_const sorted sort fun le :: "nat \ nat list \ bool" where "le n [] = True" | "le n (x#xs) = (n \ x \ le n xs)" fun sorted :: "nat list \ bool" where "sorted [] = True" | "sorted (x#xs) = (le x xs \ sorted xs)" fun merge :: "nat list \ nat list \ nat list" where "merge xs [] = xs" | "merge [] ys = ys" | "merge (x#xs) (y#ys) = (if (x \ y) then x#(merge xs (y#ys)) else y#(merge (x#xs) ys))" fun msort :: "nat list \ nat list" where "msort [] = []" | "msort [x] = [x]" | "msort xs = (let n = (length xs) div 2 in (merge (msort (take n xs)) (msort (drop n xs)) ))" lemma le_le: "\ le x xs; x' \ x \ \ le x' xs" by (induct xs) auto lemma le_merge: "\ le x xs; le x ys \ \ le x (merge xs ys)" by (induct xs ys rule:merge.induct) auto lemma sorted_merge_sorted: "\ sorted xs; sorted ys \ \ sorted(merge xs ys)" by(induct xs ys rule: merge.induct)(auto intro: le_merge simp add: le_le) theorem sorted_msort: "sorted (msort xs)" by (induct xs rule:msort.induct)(auto intro: sorted_merge_sorted) text \Diese Lemmas sind neu.\ lemma le_takeI [intro]: "le x xs \ le x (take n xs)" proof (induction n arbitrary: xs) case (Suc n xs) thus ?case by (cases xs) auto qed simp lemma sorted_takeI [intro]: "sorted xs \ sorted (take n xs)" proof (induction n arbitrary: xs) case (Suc n xs) thus ?case by (cases xs) auto qed simp lemma set_merge [simp]: "set (merge xs ys) = set xs \ set ys" by (induction xs ys rule: merge.induct) auto typedef slist = "{xs. sorted xs}" morphisms list_of as_sorted by (rule exI[where x = "[]"]) simp setup_lifting type_definition_slist lift_definition Singleton :: "nat \ slist" is "\x. [x]" by simp lift_definition set_of :: "slist \ nat set" is List.set . lift_definition take :: "nat \ slist \ slist" is List.take by (rule sorted_takeI) lift_definition smerge :: "slist \ slist \ slist" is merge by (rule sorted_merge_sorted) lift_definition sorted_list :: "nat list \ slist" is msort by (rule sorted_msort) lemma set_of_Singleton: "set_of (Singleton x) = {x}" by transfer simp lemma "list_of xs = a # b # ys \ a \ b" by transfer simp definition insert :: "nat \ slist \ slist" where "insert x xs = smerge xs (Singleton x)" lemma set_of_insert[simp]: "x \ set_of (insert x xs)" unfolding insert_def by transfer simp export_code insert take in Haskell section \Code generator\ definition rev_append :: "'a list \ 'a list \ 'a list" where "rev_append xs ys = ys @ xs" export_code rev_append in Haskell term the export_code the in Haskell datatype 'a my_type = K1 'a | K2 "'a list" fun dummy :: "'a my_type \ 'a my_type" where "dummy (K1 a) = K2 [a]" | "dummy k2 = k2" export_code dummy in Haskell inductive collatz :: "nat \ bool" where "collatz 1" | "\ n mod 2 = 0; collatz (n div 2) \ \ collatz n" | "\ n mod 2 = 1; collatz (3 * n + 1) \ \ collatz n" declare One_nat_def [simp del] lemma collatz_0: "\ collatz 0" by (rule notI, induction "0::nat" rule: collatz.induct) auto lemma collatz_greater_zero [elim]: assumes "collatz n" obtains "n > 0" using assms by (metis collatz_0 neq0_conv) lemma collatz_code [code]: "collatz n \ n \ 0 \ (n \ 1 \ (if (n mod 2 = 0) then collatz (n div 2) else collatz (3 * n + 1)))" by (auto elim: collatz.cases intro: collatz.intros) export_code collatz in Haskell lemma "collatz 1000000000123" by eval end