section \Demo Theory of Session 12\ theory LiveDemo12 imports Main "HOL-Library.Code_Target_Numeral" begin subsection \Document Generation\ text \We import theory Main.\ text \We import theory @{theory Main}.\ text \ This theory (importing @{theory Main}) has two purposes: \<^enum> it constitutes, together with the corresponding @{file ROOT} file, an example of an Isabelle session, and \<^enum> illustrates the usage of document \antiquotations\ In order to produce a PDF run @{verbatim "isabelle build -v -D ."} in the session directory @{dir "."}. With plain text like this `@{text "Cons x xs"}' we can typeset arbitrary terms in Isabelle inner syntax. If you want to have Isar keywords treated in a special way use @{verbatim \@{theory_text "..."}\} instead. Consider, for example: \begin{center} @{theory_text "datatype term but also other words"} \end{center} \ subsection \Document Antiquotations\ text \ With \<^emph>\document antiquotations\ we can achieve many things. We can typeset: \<^item> terms (like @{term "[1,2,3,4]::nat list"}) \<^item> constants (like @{const Cons}) \<^item> types (like @{typeof "Cons"} and @{typ "nat set"}) \<^item> type constructors (like @{type "list"}) \<^item> theorems (like @{thm HOL.refl} and @{lemma "1 = 1" by (rule refl)}) \<^item> ... \<^item> There are several more antiquotations than those which are described here or in the slides. See a list via @{command print_antiquotations}. There are usually two version to type antiquotation, e.g., obviously \<^term>\(True, x)\ and @{term "(True, x)"} are identical. \ text \\include{my_tex_source.tex}\ lemma app_assoc: "xs @ (ys @ zs) = (xs @ ys) @ zs" by simp text \ We just proved associativity of @{const append} in @{command lemma} @{thm [source] app_assoc}. The exact statement of this property is: @{thm app_assoc}. By the way, did you know that the Isabelle knows the factorial function? The value of @{term "fact 5 :: int"} is @{value "fact 5 :: int"}.\ subsection \Type Classes\ text \ See also @{doc classes}. \ subsubsection \Transforming Arbitrary Values to Strings\ class Show = fixes "show" :: "'a \ string" declare [[show_sorts]] term "show" declare [[show_sorts=false]] term "show" text \ Characters are an instance of the @{class Show} class, that is, they can be transformed into @{typ string}s \ instantiation char :: Show begin definition show_char :: "char \ string" where "show_char c = [c]" instance .. end text \ Pairs are an instance of the @{class Show} class. \ instantiation prod :: (Show, Show) Show begin fun show_prod :: "('a \ 'b) \ string" where "show_prod (x, y) = CHR ''('' # show x @ CHR '','' # show y @ [CHR '')'']" instance .. end value "show (CHR ''c'', CHR ''d'')" text \ Lists are an instance of @{class Show}. \ instantiation list :: (Show) Show begin fun show_list :: "'a list \ string" where "show_list Nil = []" | "show_list (x#xs) = show x @ show_list xs" instance .. end value "show ''foo bar?''" subsubsection \Total Orders\ class partial_order = fixes LEQ :: "'a \ 'a \ bool" (infix "\<^bold>\" 50) assumes refl: "x \<^bold>\ x" and antisym: "x \<^bold>\ y \ y \<^bold>\ x \ x = y" and trans: "x \<^bold>\ y \ y \<^bold>\ z \ x \<^bold>\ z" begin definition LT :: "'a \ 'a \ bool" (infix "\<^bold><" 50) where "x \<^bold>< y \ x \<^bold>\ y \ x \ y" end thm antisym class total_order = partial_order + assumes total: "x \<^bold>< y \ y \<^bold>< x \ x = y" begin (* we can reason abstractly within the class *) lemma not_eq_imp_lt: "x \ y \ x \<^bold>< y \ y \<^bold>< x " using total[of x y] by auto end (* or outside the class *) lemma less_lt_trans: "x \<^bold>\ y \ y \<^bold>< z \ x \<^bold>< (z :: 'a :: partial_order)" by (metis LT_def partial_order_class.antisym partial_order_class.trans) instantiation nat :: partial_order begin fun LEQ_nat :: "nat \ nat \ bool" where "0 \<^bold>\ (y::nat) \ True" | "Suc x \<^bold>\ Suc y \ x \<^bold>\ y" | "x \<^bold>\ (y::nat) \ False" lemma LEQ_nat_def: fixes x y :: nat shows "x \<^bold>\ y \ (\n. y = x + n)" by (induction x y rule: LEQ_nat.induct) auto instance by (standard) (auto simp: LEQ_nat_def) end lemma LEQ_nat_total: fixes x y :: nat shows "x \<^bold>\ y \ y \<^bold>\ x" proof (induction x arbitrary: y) case (Suc x y) then show ?case by (cases y) auto qed simp instance nat :: total_order proof fix x y :: nat show "x \<^bold>< y \ y \<^bold>< x \ x = y" using LEQ_nat_total [of x y] unfolding LT_def by blast qed end