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