src/Doc/ProgProve/Bool_nat_list.thy
changeset 54121 4e7d71037bb6
parent 53015 a1119cf551e8
child 54436 0e1c576bbc19
equal deleted inserted replaced
54119:2c13cb4a057d 54121:4e7d71037bb6
   420 
   420 
   421 
   421 
   422 \subsection{Exercises}
   422 \subsection{Exercises}
   423 
   423 
   424 \begin{exercise}
   424 \begin{exercise}
       
   425 Use the \isacom{value} command to evaluate the following expressions:
       
   426 @{term[source] "1 + (2::nat)"}, @{term[source] "1 + (2::int)"},
       
   427 @{term[source] "1 - (2::nat)"} and @{term[source] "1 - (2::int)"}.
       
   428 \end{exercise}
       
   429 
       
   430 \begin{exercise}
   425 Start from the definition of @{const add} given above.
   431 Start from the definition of @{const add} given above.
   426 Prove it is associative (@{prop"add (add m n) p = add m (add n p)"})
   432 Prove that @{const add} it is associative and commutative.
   427 and commutative (@{prop"add m n = add n m"}). Define a recursive function
   433 Define a recursive function @{text double} @{text"::"} @{typ"nat \<Rightarrow> nat"}
   428 @{text double} @{text"::"} @{typ"nat \<Rightarrow> nat"} and prove that @{prop"double m = add m m"}.
   434 and prove @{prop"double m = add m m"}.
   429 \end{exercise}
   435 \end{exercise}
   430 
   436 
   431 \begin{exercise}
   437 \begin{exercise}
   432 Define a function @{text"count ::"} @{typ"'a \<Rightarrow> 'a list \<Rightarrow> nat"}
   438 Define a function @{text"count ::"} @{typ"'a \<Rightarrow> 'a list \<Rightarrow> nat"}
   433 that counts the number of occurrences of an element in a list. Prove
   439 that counts the number of occurrences of an element in a list. Prove
   434 @{prop"count x xs \<le> length xs"}.
   440 @{prop"count x xs \<le> length xs"}.
   435 \end{exercise}
   441 \end{exercise}
   436 
   442 
   437 \begin{exercise}
   443 \begin{exercise}
   438 Define a recursive function @{text "snoc ::"} @{typ"'a list \<Rightarrow> 'a \<Rightarrow> 'a list"}
   444 Define a recursive function @{text "snoc ::"} @{typ"'a list \<Rightarrow> 'a \<Rightarrow> 'a list"}
   439 that appends an element to the end of a list. Do not use the predefined append
   445 that appends an element to the end of a list. With the help of @{text snoc}
   440 operator @{text"@"}. With the help of @{text snoc} define a recursive function
   446 define a recursive function @{text "reverse ::"} @{typ"'a list \<Rightarrow> 'a list"}
   441 @{text "reverse ::"} @{typ"'a list \<Rightarrow> 'a list"} that reverses a list. Do not
   447 that reverses a list. Prove @{prop"reverse(reverse xs) = xs"}.
   442 use the predefined function @{const rev}.
       
   443 Prove @{prop"reverse(reverse xs) = xs"}.
       
   444 \end{exercise}
   448 \end{exercise}
       
   449 
       
   450 \begin{exercise}
       
   451 Define a recursive function @{text "sum ::"} @{typ"nat \<Rightarrow> nat"} such that
       
   452 \mbox{@{text"sum n"}} @{text"="} @{text"0 + ... + n"} and prove
       
   453 @{prop" sum(n::nat) = n * (n+1) div 2"}.
       
   454 \end{exercise}
   445 *}
   455 *}
   446 (*<*)
   456 (*<*)
   447 end
   457 end
   448 (*>*)
   458 (*>*)