src/Doc/ProgProve/Bool_nat_list.thy
changeset 55317 834a84553e02
parent 54508 4bc48d713602
child 55318 908fd015cf2e
equal deleted inserted replaced
55316:885500f4aa6a 55317:834a84553e02
    10 
    10 
    11 These are the most important predefined types. We go through them one by one.
    11 These are the most important predefined types. We go through them one by one.
    12 Based on examples we learn how to define (possibly recursive) functions and
    12 Based on examples we learn how to define (possibly recursive) functions and
    13 prove theorems about them by induction and simplification.
    13 prove theorems about them by induction and simplification.
    14 
    14 
    15 \subsection{Type @{typ bool}}
    15 \subsection{Type \indexed{@{typ bool}}{bool}}
    16 
    16 
    17 The type of boolean values is a predefined datatype
    17 The type of boolean values is a predefined datatype
    18 @{datatype[display] bool}
    18 @{datatype[display] bool}
    19 with the two values @{const True} and @{const False} and
    19 with the two values \indexed{@{const True}}{True} and \indexed{@{const False}}{False} and
    20 with many predefined functions:  @{text "\<not>"}, @{text "\<and>"}, @{text "\<or>"}, @{text
    20 with many predefined functions:  @{text "\<not>"}, @{text "\<and>"}, @{text "\<or>"}, @{text
    21 "\<longrightarrow>"} etc. Here is how conjunction could be defined by pattern matching:
    21 "\<longrightarrow>"} etc. Here is how conjunction could be defined by pattern matching:
    22 *}
    22 *}
    23 
    23 
    24 fun conj :: "bool \<Rightarrow> bool \<Rightarrow> bool" where
    24 fun conj :: "bool \<Rightarrow> bool \<Rightarrow> bool" where
    26 "conj _ _ = False"
    26 "conj _ _ = False"
    27 
    27 
    28 text{* Both the datatype and function definitions roughly follow the syntax
    28 text{* Both the datatype and function definitions roughly follow the syntax
    29 of functional programming languages.
    29 of functional programming languages.
    30 
    30 
    31 \subsection{Type @{typ nat}}
    31 \subsection{Type \indexed{@{typ nat}}{nat}}
    32 
    32 
    33 Natural numbers are another predefined datatype:
    33 Natural numbers are another predefined datatype:
    34 @{datatype[display] nat}
    34 @{datatype[display] nat}\index{Suc@@{const Suc}}
    35 All values of type @{typ nat} are generated by the constructors
    35 All values of type @{typ nat} are generated by the constructors
    36 @{text 0} and @{const Suc}. Thus the values of type @{typ nat} are
    36 @{text 0} and @{const Suc}. Thus the values of type @{typ nat} are
    37 @{text 0}, @{term"Suc 0"}, @{term"Suc(Suc 0)"} etc.
    37 @{text 0}, @{term"Suc 0"}, @{term"Suc(Suc 0)"} etc.
    38 There are many predefined functions: @{text "+"}, @{text "*"}, @{text
    38 There are many predefined functions: @{text "+"}, @{text "*"}, @{text
    39 "\<le>"}, etc. Here is how you could define your own addition:
    39 "\<le>"}, etc. Here is how you could define your own addition:
   146 as informal text comprehensible to a reader familiar with traditional
   146 as informal text comprehensible to a reader familiar with traditional
   147 mathematical proofs. Later on we will introduce an Isabelle proof language
   147 mathematical proofs. Later on we will introduce an Isabelle proof language
   148 that is closer to traditional informal mathematical language and is often
   148 that is closer to traditional informal mathematical language and is often
   149 directly readable.
   149 directly readable.
   150 
   150 
   151 \subsection{Type @{text list}}
   151 \subsection{Type \indexed{@{text list}}{list}}
   152 
   152 
   153 Although lists are already predefined, we define our own copy just for
   153 Although lists are already predefined, we define our own copy just for
   154 demonstration purposes:
   154 demonstration purposes:
   155 *}
   155 *}
   156 (*<*)
   156 (*<*)
   182 "rev (Cons x xs) = app (rev xs) (Cons x Nil)"
   182 "rev (Cons x xs) = app (rev xs) (Cons x Nil)"
   183 
   183 
   184 text{* By default, variables @{text xs}, @{text ys} and @{text zs} are of
   184 text{* By default, variables @{text xs}, @{text ys} and @{text zs} are of
   185 @{text list} type.
   185 @{text list} type.
   186 
   186 
   187 Command \isacom{value} evaluates a term. For example, *}
   187 Command \isacom{value}\indexed{{\sf\textbf{value}}}{value} evaluates a term. For example, *}
   188 
   188 
   189 value "rev(Cons True (Cons False Nil))"
   189 value "rev(Cons True (Cons False Nil))"
   190 
   190 
   191 text{* yields the result @{value "rev(Cons True (Cons False Nil))"}. This works symbolically, too: *}
   191 text{* yields the result @{value "rev(Cons True (Cons False Nil))"}. This works symbolically, too: *}
   192 
   192 
   238 theorem rev_rev [simp]: "rev(rev xs) = xs"
   238 theorem rev_rev [simp]: "rev(rev xs) = xs"
   239 
   239 
   240 txt{* Commands \isacom{theorem} and \isacom{lemma} are
   240 txt{* Commands \isacom{theorem} and \isacom{lemma} are
   241 interchangeable and merely indicate the importance we attach to a
   241 interchangeable and merely indicate the importance we attach to a
   242 proposition. Via the bracketed attribute @{text simp} we also tell Isabelle
   242 proposition. Via the bracketed attribute @{text simp} we also tell Isabelle
   243 to make the eventual theorem a \concept{simplification rule}: future proofs
   243 to make the eventual theorem a \conceptnoidx{simplification rule}: future proofs
   244 involving simplification will replace occurrences of @{term"rev(rev xs)"} by
   244 involving simplification will replace occurrences of @{term"rev(rev xs)"} by
   245 @{term"xs"}. The proof is by induction: *}
   245 @{term"xs"}. The proof is by induction: *}
   246 
   246 
   247 apply(induction xs)
   247 apply(induction xs)
   248 
   248 
   381 \label{sec:predeflists}
   381 \label{sec:predeflists}
   382 
   382 
   383 Isabelle's predefined lists are the same as the ones above, but with
   383 Isabelle's predefined lists are the same as the ones above, but with
   384 more syntactic sugar:
   384 more syntactic sugar:
   385 \begin{itemize}
   385 \begin{itemize}
   386 \item @{text "[]"} is @{const Nil},
   386 \item @{text "[]"} is \indexed{@{const Nil}}{Nil},
   387 \item @{term"x # xs"} is @{term"Cons x xs"},
   387 \item @{term"x # xs"} is @{term"Cons x xs"}\index{Cons@@{const Cons}},
   388 \item @{text"[x\<^sub>1, \<dots>, x\<^sub>n]"} is @{text"x\<^sub>1 # \<dots> # x\<^sub>n # []"}, and
   388 \item @{text"[x\<^sub>1, \<dots>, x\<^sub>n]"} is @{text"x\<^sub>1 # \<dots> # x\<^sub>n # []"}, and
   389 \item @{term "xs @ ys"} is @{term"app xs ys"}.
   389 \item @{term "xs @ ys"} is @{term"app xs ys"}.
   390 \end{itemize}
   390 \end{itemize}
   391 There is also a large library of predefined functions.
   391 There is also a large library of predefined functions.
   392 The most important ones are the length function
   392 The most important ones are the length function
   393 @{text"length :: 'a list \<Rightarrow> nat"} (with the obvious definition),
   393 @{text"length :: 'a list \<Rightarrow> nat"}\index{length@@{const length}} (with the obvious definition),
   394 and the map function that applies a function to all elements of a list:
   394 and the \indexed{@{const map}}{map} function that applies a function to all elements of a list:
   395 \begin{isabelle}
   395 \begin{isabelle}
   396 \isacom{fun} @{const map} @{text"::"} @{typ[source] "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"}\\
   396 \isacom{fun} @{const map} @{text"::"} @{typ[source] "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"}\\
   397 @{text"\""}@{thm map.simps(1)}@{text"\" |"}\\
   397 @{text"\""}@{thm map.simps(1)}@{text"\" |"}\\
   398 @{text"\""}@{thm map.simps(2)}@{text"\""}
   398 @{text"\""}@{thm map.simps(2)}@{text"\""}
   399 \end{isabelle}
   399 \end{isabelle}
   400 
   400 
   401 \ifsem
   401 \ifsem
   402 Also useful are the \concept{head} of a list, its first element,
   402 Also useful are the \concept{head} of a list, its first element,
   403 and the \concept{tail}, the rest of the list:
   403 and the \concept{tail}, the rest of the list:
   404 \begin{isabelle}
   404 \begin{isabelle}\index{hd@@{const hd}}
   405 \isacom{fun} @{text"hd :: 'a list \<Rightarrow> 'a"}\\
   405 \isacom{fun} @{text"hd :: 'a list \<Rightarrow> 'a"}\\
   406 @{prop"hd(x#xs) = x"}
   406 @{prop"hd(x#xs) = x"}
   407 \end{isabelle}
   407 \end{isabelle}
   408 \begin{isabelle}
   408 \begin{isabelle}\index{tl@@{const tl}}
   409 \isacom{fun} @{text"tl :: 'a list \<Rightarrow> 'a list"}\\
   409 \isacom{fun} @{text"tl :: 'a list \<Rightarrow> 'a list"}\\
   410 @{prop"tl [] = []"} @{text"|"}\\
   410 @{prop"tl [] = []"} @{text"|"}\\
   411 @{prop"tl(x#xs) = xs"}
   411 @{prop"tl(x#xs) = xs"}
   412 \end{isabelle}
   412 \end{isabelle}
   413 Note that since HOL is a logic of total functions, @{term"hd []"} is defined,
   413 Note that since HOL is a logic of total functions, @{term"hd []"} is defined,