doc-src/Exercises/2002/a1/a1.thy
author kleing
Thu, 05 Dec 2002 17:12:07 +0100
changeset 13739 f5d0a66c8124
child 13844 44f741cdcea3
permissions -rw-r--r--
exercise collection
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13739
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     1
(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     2
theory a1 = Main:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     3
(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     4
subsection {* Lists *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     5
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     6
text {* Define a universal and an existential quantifier on lists.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     7
Expression @{term "alls P xs"} should be true iff @{term "P x"} holds
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     8
for every element @{term x} of @{term xs}, and @{term "exs P xs"}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     9
should be true iff @{term "P x"} holds for some element @{term x} of
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    10
@{term xs}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    11
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    12
consts 
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    13
  alls :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    14
  exs  :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    15
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    16
text {*
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    17
Prove or disprove (by counter example) the following theorems.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    18
You may have to prove some lemmas first.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    19
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    20
Use the @{text "[simp]"}-attribute only if the equation is truly a
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    21
simplification and is necessary for some later proof.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    22
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    23
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    24
lemma "alls (\<lambda>x. P x \<and> Q x) xs = (alls P xs \<and> alls Q xs)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    25
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    26
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    27
lemma "alls P (rev xs) = alls P xs"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    28
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    29
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    30
lemma "exs (\<lambda>x. P x \<and> Q x) xs = (exs P xs \<and> exs Q xs)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    31
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    32
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    33
lemma "exs P (map f xs) = exs (P o f) xs"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    34
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    35
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    36
lemma "exs P (rev xs) = exs P xs"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    37
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    38
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    39
text {* Find a term @{text Z} such that the following equation holds: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    40
lemma "exs (\<lambda>x. P x \<or> Q x) xs = Z"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    41
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    42
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    43
text {* Express the existential via the universal quantifier ---
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    44
@{text exs} should not occur on the right-hand side: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    45
lemma "exs P xs = Z"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    46
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    47
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    48
text {*
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    49
Define a function @{term "is_in x xs"} that checks if @{term x} occurs in
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    50
@{term xs} vorkommt. Now express @{text is_in} via @{term exs}:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    51
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    52
lemma "is_in a xs = Z"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    53
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    54
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    55
text {* Define a function @{term "nodups xs"} that is true iff
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    56
@{term xs} does not contain duplicates, and a function @{term "deldups
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    57
xs"} that removes all duplicates. Note that @{term "deldups[x,y,x]"}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    58
(where @{term x} and @{term y} are distinct) can be either
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    59
@{term "[x,y]"} or @{term "[y,x]"}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    60
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    61
Prove or disprove (by counter example) the following theorems.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    62
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    63
lemma "length (deldups xs) <= length xs"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    64
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    65
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    66
lemma "nodups (deldups xs)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    67
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    68
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    69
lemma "deldups (rev xs) = rev (deldups xs)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    70
(*<*)oops(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    71
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    72
(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    73
end
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    74
(*>*)