src/HOL/Extraction/Warshall.thy
author wenzelm
Fri, 25 Jan 2008 23:50:33 +0100
changeset 25976 11c6811f232c
parent 23373 ead82c82da9e
child 27982 2aaa4a5569a6
permissions -rw-r--r--
modernized primrec;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Extraction/Warshall.thy
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     2
    ID:         $Id$
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     4
*)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     5
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     6
header {* Warshall's algorithm *}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
     7
16761
99549528ce76 proof needed updating because of arith
nipkow
parents: 16417
diff changeset
     8
theory Warshall
99549528ce76 proof needed updating because of arith
nipkow
parents: 16417
diff changeset
     9
imports Main
99549528ce76 proof needed updating because of arith
nipkow
parents: 16417
diff changeset
    10
begin
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    11
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    12
text {*
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    13
  Derivation of Warshall's algorithm using program extraction,
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    14
  based on Berger, Schwichtenberg and Seisenberger \cite{Berger-JAR-2001}.
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    15
*}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    16
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    17
datatype b = T | F
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    18
25976
11c6811f232c modernized primrec;
wenzelm
parents: 23373
diff changeset
    19
primrec
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    20
  is_path' :: "('a \<Rightarrow> 'a \<Rightarrow> b) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a \<Rightarrow> bool"
25976
11c6811f232c modernized primrec;
wenzelm
parents: 23373
diff changeset
    21
where
11c6811f232c modernized primrec;
wenzelm
parents: 23373
diff changeset
    22
    "is_path' r x [] z = (r x z = T)"
11c6811f232c modernized primrec;
wenzelm
parents: 23373
diff changeset
    23
  | "is_path' r x (y # ys) z = (r x y = T \<and> is_path' r y ys z)"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    24
25976
11c6811f232c modernized primrec;
wenzelm
parents: 23373
diff changeset
    25
definition
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    26
  is_path :: "(nat \<Rightarrow> nat \<Rightarrow> b) \<Rightarrow> (nat * nat list * nat) \<Rightarrow>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    27
    nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool"
25976
11c6811f232c modernized primrec;
wenzelm
parents: 23373
diff changeset
    28
where
11c6811f232c modernized primrec;
wenzelm
parents: 23373
diff changeset
    29
  "is_path r p i j k \<longleftrightarrow> fst p = j \<and> snd (snd p) = k \<and>
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    30
     list_all (\<lambda>x. x < i) (fst (snd p)) \<and>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    31
     is_path' r (fst p) (fst (snd p)) (snd (snd p))"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    32
25976
11c6811f232c modernized primrec;
wenzelm
parents: 23373
diff changeset
    33
definition
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    34
  conc :: "('a * 'a list * 'a) \<Rightarrow> ('a * 'a list * 'a) \<Rightarrow> ('a * 'a list * 'a)"
25976
11c6811f232c modernized primrec;
wenzelm
parents: 23373
diff changeset
    35
where
11c6811f232c modernized primrec;
wenzelm
parents: 23373
diff changeset
    36
  "conc p q = (fst p, fst (snd p) @ fst q # fst (snd q), snd (snd q))"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    37
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    38
theorem is_path'_snoc [simp]:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    39
  "\<And>x. is_path' r x (ys @ [y]) z = (is_path' r x ys y \<and> r y z = T)"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    40
  by (induct ys) simp+
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    41
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    42
theorem list_all_scoc [simp]: "list_all P (xs @ [x]) = (P x \<and> list_all P xs)"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 16761
diff changeset
    43
  by (induct xs, simp+, iprover)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    44
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    45
theorem list_all_lemma: 
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    46
  "list_all P xs \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> list_all Q xs"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    47
proof -
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    48
  assume PQ: "\<And>x. P x \<Longrightarrow> Q x"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    49
  show "list_all P xs \<Longrightarrow> list_all Q xs"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    50
  proof (induct xs)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    51
    case Nil
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    52
    show ?case by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    53
  next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    54
    case (Cons y ys)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    55
    hence Py: "P y" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    56
    from Cons have Pys: "list_all P ys" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    57
    show ?case
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    58
      by simp (rule conjI PQ Py Cons Pys)+
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    59
  qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    60
qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    61
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    62
theorem lemma1: "\<And>p. is_path r p i j k \<Longrightarrow> is_path r p (Suc i) j k"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    63
  apply (unfold is_path_def)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    64
  apply (simp cong add: conj_cong add: split_paired_all)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    65
  apply (erule conjE)+
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    66
  apply (erule list_all_lemma)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    67
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    68
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    69
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    70
theorem lemma2: "\<And>p. is_path r p 0 j k \<Longrightarrow> r j k = T"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    71
  apply (unfold is_path_def)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    72
  apply (simp cong add: conj_cong add: split_paired_all)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    73
  apply (case_tac "aa")
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    74
  apply simp+
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    75
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    76
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    77
theorem is_path'_conc: "is_path' r j xs i \<Longrightarrow> is_path' r i ys k \<Longrightarrow>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    78
  is_path' r j (xs @ i # ys) k"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    79
proof -
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    80
  assume pys: "is_path' r i ys k"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    81
  show "\<And>j. is_path' r j xs i \<Longrightarrow> is_path' r j (xs @ i # ys) k"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    82
  proof (induct xs)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    83
    case (Nil j)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    84
    hence "r j i = T" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    85
    with pys show ?case by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    86
  next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    87
    case (Cons z zs j)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    88
    hence jzr: "r j z = T" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    89
    from Cons have pzs: "is_path' r z zs i" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    90
    show ?case
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    91
      by simp (rule conjI jzr Cons pzs)+
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    92
  qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    93
qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    94
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    95
theorem lemma3:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    96
  "\<And>p q. is_path r p i j i \<Longrightarrow> is_path r q i i k \<Longrightarrow>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    97
   is_path r (conc p q) (Suc i) j k"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    98
  apply (unfold is_path_def conc_def)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
    99
  apply (simp cong add: conj_cong add: split_paired_all)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   100
  apply (erule conjE)+
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   101
  apply (rule conjI)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   102
  apply (erule list_all_lemma)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   103
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   104
  apply (rule conjI)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   105
  apply (erule list_all_lemma)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   106
  apply simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   107
  apply (rule is_path'_conc)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   108
  apply assumption+
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   109
  done
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   110
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   111
theorem lemma5:
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   112
  "\<And>p. is_path r p (Suc i) j k \<Longrightarrow> ~ is_path r p i j k \<Longrightarrow>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   113
   (\<exists>q. is_path r q i j i) \<and> (\<exists>q'. is_path r q' i i k)"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   114
proof (simp cong add: conj_cong add: split_paired_all is_path_def, (erule conjE)+)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   115
  fix xs
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 17604
diff changeset
   116
  assume asms:
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 17604
diff changeset
   117
    "list_all (\<lambda>x. x < Suc i) xs"
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 17604
diff changeset
   118
    "is_path' r j xs k"
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 17604
diff changeset
   119
    "\<not> list_all (\<lambda>x. x < i) xs"
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   120
  show "(\<exists>ys. list_all (\<lambda>x. x < i) ys \<and> is_path' r j ys i) \<and>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   121
    (\<exists>ys. list_all (\<lambda>x. x < i) ys \<and> is_path' r i ys k)"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   122
  proof
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   123
    show "\<And>j. list_all (\<lambda>x. x < Suc i) xs \<Longrightarrow> is_path' r j xs k \<Longrightarrow>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   124
      \<not> list_all (\<lambda>x. x < i) xs \<Longrightarrow>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   125
    \<exists>ys. list_all (\<lambda>x. x < i) ys \<and> is_path' r j ys i" (is "PROP ?ih xs")
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   126
    proof (induct xs)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   127
      case Nil
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   128
      thus ?case by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   129
    next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   130
      case (Cons a as j)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   131
      show ?case
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   132
      proof (cases "a=i")
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   133
      	case True
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   134
      	show ?thesis
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   135
      	proof
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   136
	  from True and Cons have "r j i = T" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   137
	  thus "list_all (\<lambda>x. x < i) [] \<and> is_path' r j [] i" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   138
      	qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   139
      next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   140
      	case False
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   141
      	have "PROP ?ih as" by (rule Cons)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   142
      	then obtain ys where ys: "list_all (\<lambda>x. x < i) ys \<and> is_path' r a ys i"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   143
      	proof
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   144
	  from Cons show "list_all (\<lambda>x. x < Suc i) as" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   145
	  from Cons show "is_path' r a as k" by simp
16761
99549528ce76 proof needed updating because of arith
nipkow
parents: 16417
diff changeset
   146
	  from Cons and False show "\<not> list_all (\<lambda>x. x < i) as" by (simp)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   147
      	qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   148
      	show ?thesis
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   149
      	proof
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   150
	  from Cons False ys
16761
99549528ce76 proof needed updating because of arith
nipkow
parents: 16417
diff changeset
   151
	  show "list_all (\<lambda>x. x<i) (a#ys) \<and> is_path' r j (a#ys) i" by simp
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   152
      	qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   153
      qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   154
    qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   155
    show "\<And>k. list_all (\<lambda>x. x < Suc i) xs \<Longrightarrow> is_path' r j xs k \<Longrightarrow>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   156
      \<not> list_all (\<lambda>x. x < i) xs \<Longrightarrow>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   157
      \<exists>ys. list_all (\<lambda>x. x < i) ys \<and> is_path' r i ys k" (is "PROP ?ih xs")
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   158
    proof (induct xs rule: rev_induct)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   159
      case Nil
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   160
      thus ?case by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   161
    next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   162
      case (snoc a as k)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   163
      show ?case
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   164
      proof (cases "a=i")
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   165
      	case True
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   166
      	show ?thesis
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   167
      	proof
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   168
	  from True and snoc have "r i k = T" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   169
	  thus "list_all (\<lambda>x. x < i) [] \<and> is_path' r i [] k" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   170
      	qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   171
      next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   172
      	case False
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   173
      	have "PROP ?ih as" by (rule snoc)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   174
      	then obtain ys where ys: "list_all (\<lambda>x. x < i) ys \<and> is_path' r i ys a"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   175
      	proof
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   176
	  from snoc show "list_all (\<lambda>x. x < Suc i) as" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   177
	  from snoc show "is_path' r j as a" by simp
16761
99549528ce76 proof needed updating because of arith
nipkow
parents: 16417
diff changeset
   178
	  from snoc and False show "\<not> list_all (\<lambda>x. x < i) as" by simp
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   179
      	qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   180
      	show ?thesis
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   181
      	proof
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   182
	  from snoc False ys
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   183
	  show "list_all (\<lambda>x. x < i) (ys @ [a]) \<and> is_path' r i (ys @ [a]) k"
16761
99549528ce76 proof needed updating because of arith
nipkow
parents: 16417
diff changeset
   184
	    by simp
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   185
      	qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   186
      qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   187
    qed
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 17604
diff changeset
   188
  qed (rule asms)+
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   189
qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   190
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   191
theorem lemma5':
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   192
  "\<And>p. is_path r p (Suc i) j k \<Longrightarrow> \<not> is_path r p i j k \<Longrightarrow>
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   193
   \<not> (\<forall>q. \<not> is_path r q i j i) \<and> \<not> (\<forall>q'. \<not> is_path r q' i i k)"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 16761
diff changeset
   194
  by (iprover dest: lemma5)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   195
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   196
theorem warshall: 
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   197
  "\<And>j k. \<not> (\<exists>p. is_path r p i j k) \<or> (\<exists>p. is_path r p i j k)"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   198
proof (induct i)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   199
  case (0 j k)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   200
  show ?case
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   201
  proof (cases "r j k")
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   202
    assume "r j k = T"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   203
    hence "is_path r (j, [], k) 0 j k"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   204
      by (simp add: is_path_def)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   205
    hence "\<exists>p. is_path r p 0 j k" ..
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   206
    thus ?thesis ..
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   207
  next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   208
    assume "r j k = F"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   209
    hence "r j k ~= T" by simp
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   210
    hence "\<not> (\<exists>p. is_path r p 0 j k)"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 16761
diff changeset
   211
      by (iprover dest: lemma2)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   212
    thus ?thesis ..
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   213
  qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   214
next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   215
  case (Suc i j k)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   216
  thus ?case
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   217
  proof
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   218
    assume h1: "\<not> (\<exists>p. is_path r p i j k)"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   219
    from Suc show ?case
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   220
    proof
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   221
      assume "\<not> (\<exists>p. is_path r p i j i)"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   222
      with h1 have "\<not> (\<exists>p. is_path r p (Suc i) j k)"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 16761
diff changeset
   223
	by (iprover dest: lemma5')
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   224
      thus ?case ..
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   225
    next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   226
      assume "\<exists>p. is_path r p i j i"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   227
      then obtain p where h2: "is_path r p i j i" ..
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   228
      from Suc show ?case
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   229
      proof
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   230
	assume "\<not> (\<exists>p. is_path r p i i k)"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   231
	with h1 have "\<not> (\<exists>p. is_path r p (Suc i) j k)"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 16761
diff changeset
   232
	  by (iprover dest: lemma5')
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   233
	thus ?case ..
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   234
      next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   235
	assume "\<exists>q. is_path r q i i k"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   236
	then obtain q where "is_path r q i i k" ..
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   237
	with h2 have "is_path r (conc p q) (Suc i) j k" 
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   238
	  by (rule lemma3)
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   239
	hence "\<exists>pq. is_path r pq (Suc i) j k" ..
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   240
	thus ?case ..
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   241
      qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   242
    qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   243
  next
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   244
    assume "\<exists>p. is_path r p i j k"
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   245
    hence "\<exists>p. is_path r p (Suc i) j k"
17604
5f30179fbf44 rules -> iprover
nipkow
parents: 16761
diff changeset
   246
      by (iprover intro: lemma1)
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   247
    thus ?case ..
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   248
  qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   249
qed
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   250
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   251
extract warshall
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   252
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   253
text {*
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   254
  The program extracted from the above proof looks as follows
13671
eec2582923f6 Eta contraction is now switched off when printing extracted program.
berghofe
parents: 13471
diff changeset
   255
  @{thm [display, eta_contract=false] warshall_def [no_vars]}
13405
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   256
  The corresponding correctness theorem is
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   257
  @{thm [display] warshall_correctness [no_vars]}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   258
*}
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   259
d20a4e67afc8 Examples for program extraction in HOL.
berghofe
parents:
diff changeset
   260
end