src/HOL/Extraction/Util.thy
author wenzelm
Mon, 16 Mar 2009 18:24:30 +0100
changeset 30549 d2d7874648bd
parent 25421 1c5b8d54a339
child 32960 69916a850301
permissions -rw-r--r--
simplified method setup;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25421
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Extraction/Util.thy
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
     2
    ID:         $Id$
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
     4
*)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
     5
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
     6
header {* Auxiliary lemmas used in program extraction examples *}
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
     7
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
     8
theory Util
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
     9
imports Main
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    10
begin
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    11
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    12
text {*
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    13
Decidability of equality on natural numbers.
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    14
*}
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    15
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    16
lemma nat_eq_dec: "\<And>n::nat. m = n \<or> m \<noteq> n"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    17
  apply (induct m)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    18
  apply (case_tac n)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    19
  apply (case_tac [3] n)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    20
  apply (simp only: nat.simps, iprover?)+
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    21
  done
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    22
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    23
text {*
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    24
Well-founded induction on natural numbers, derived using the standard
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    25
structural induction rule.
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    26
*}
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    27
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    28
lemma nat_wf_ind:
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    29
  assumes R: "\<And>x::nat. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    30
  shows "P z"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    31
proof (rule R)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    32
  show "\<And>y. y < z \<Longrightarrow> P y"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    33
  proof (induct z)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    34
    case 0
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    35
    thus ?case by simp
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    36
  next
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    37
    case (Suc n y)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    38
    from nat_eq_dec show ?case
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    39
    proof
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    40
      assume ny: "n = y"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    41
      have "P n"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    42
	by (rule R) (rule Suc)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    43
      with ny show ?case by simp
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    44
    next
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    45
      assume "n \<noteq> y"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    46
      with Suc have "y < n" by simp
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    47
      thus ?case by (rule Suc)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    48
    qed
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    49
  qed
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    50
qed
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    51
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    52
text {*
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    53
Bounded search for a natural number satisfying a decidable predicate.
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    54
*}
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    55
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    56
lemma search:
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    57
  assumes dec: "\<And>x::nat. P x \<or> \<not> P x"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    58
  shows "(\<exists>x<y. P x) \<or> \<not> (\<exists>x<y. P x)"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    59
proof (induct y)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    60
  case 0 show ?case by simp
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    61
next
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    62
  case (Suc z)
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    63
  thus ?case
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    64
  proof
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    65
    assume "\<exists>x<z. P x"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    66
    then obtain x where le: "x < z" and P: "P x" by iprover
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    67
    from le have "x < Suc z" by simp
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    68
    with P show ?case by iprover
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    69
  next
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    70
    assume nex: "\<not> (\<exists>x<z. P x)"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    71
    from dec show ?case
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    72
    proof
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    73
      assume P: "P z"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    74
      have "z < Suc z" by simp
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    75
      with P show ?thesis by iprover
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    76
    next
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    77
      assume nP: "\<not> P z"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    78
      have "\<not> (\<exists>x<Suc z. P x)"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    79
      proof
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    80
	assume "\<exists>x<Suc z. P x"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    81
	then obtain x where le: "x < Suc z" and P: "P x" by iprover
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    82
	have "x < z"
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    83
	proof (cases "x = z")
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    84
	  case True
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    85
	  with nP and P show ?thesis by simp
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    86
	next
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    87
	  case False
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    88
	  with le show ?thesis by simp
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    89
	qed
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    90
	with P have "\<exists>x<z. P x" by iprover
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    91
	with nex show False ..
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    92
      qed
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    93
      thus ?case by iprover
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    94
    qed
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    95
  qed
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    96
qed
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    97
1c5b8d54a339 Moved auxiliary lemmas to separate theory.
berghofe
parents:
diff changeset
    98
end