src/HOL/Extraction/Higman.thy
author berghofe
Mon, 28 Apr 2003 10:33:42 +0200
changeset 13930 562fd03b266d
parent 13711 5ace1cccb612
child 13969 3aa8c0bb3080
permissions -rw-r--r--
Converted main proofs to Isar.
berghofe@13405
     1
(*  Title:      HOL/Extraction/Higman.thy
berghofe@13405
     2
    ID:         $Id$
berghofe@13405
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@13405
     4
                Monika Seisenberger, LMU Muenchen
berghofe@13405
     5
*)
berghofe@13405
     6
berghofe@13405
     7
header {* Higman's lemma *}
berghofe@13405
     8
berghofe@13405
     9
theory Higman = Main:
berghofe@13405
    10
berghofe@13405
    11
text {*
berghofe@13405
    12
  Formalization by Stefan Berghofer and Monika Seisenberger,
berghofe@13405
    13
  based on Coquand and Fridlender \cite{Coquand93}.
berghofe@13405
    14
*}
berghofe@13405
    15
berghofe@13405
    16
datatype letter = A | B
berghofe@13405
    17
berghofe@13405
    18
consts
berghofe@13405
    19
  emb :: "(letter list \<times> letter list) set"
berghofe@13405
    20
berghofe@13405
    21
inductive emb
berghofe@13405
    22
intros
berghofe@13405
    23
  emb0 [CPure.intro]: "([], bs) \<in> emb"
berghofe@13405
    24
  emb1 [CPure.intro]: "(as, bs) \<in> emb \<Longrightarrow> (as, b # bs) \<in> emb"
berghofe@13405
    25
  emb2 [CPure.intro]: "(as, bs) \<in> emb \<Longrightarrow> (a # as, a # bs) \<in> emb"
berghofe@13405
    26
berghofe@13405
    27
consts
berghofe@13405
    28
  L :: "letter list \<Rightarrow> letter list list set"
berghofe@13405
    29
berghofe@13405
    30
inductive "L y"
berghofe@13405
    31
intros
berghofe@13405
    32
  L0 [CPure.intro]: "(w, y) \<in> emb \<Longrightarrow> w # ws \<in> L y"
berghofe@13405
    33
  L1 [CPure.intro]: "ws \<in> L y \<Longrightarrow> w # ws \<in> L y"
berghofe@13405
    34
berghofe@13405
    35
consts
berghofe@13405
    36
  good :: "letter list list set"
berghofe@13405
    37
berghofe@13405
    38
inductive good
berghofe@13405
    39
intros
berghofe@13405
    40
  good0 [CPure.intro]: "ws \<in> L w \<Longrightarrow> w # ws \<in> good"
berghofe@13405
    41
  good1 [CPure.intro]: "ws \<in> good \<Longrightarrow> w # ws \<in> good"
berghofe@13405
    42
berghofe@13405
    43
consts
berghofe@13405
    44
  R :: "letter \<Rightarrow> (letter list list \<times> letter list list) set"
berghofe@13405
    45
berghofe@13405
    46
inductive "R a"
berghofe@13405
    47
intros
berghofe@13405
    48
  R0 [CPure.intro]: "([], []) \<in> R a"
berghofe@13405
    49
  R1 [CPure.intro]: "(vs, ws) \<in> R a \<Longrightarrow> (w # vs, (a # w) # ws) \<in> R a"
berghofe@13405
    50
berghofe@13405
    51
consts
berghofe@13405
    52
  T :: "letter \<Rightarrow> (letter list list \<times> letter list list) set"
berghofe@13405
    53
berghofe@13405
    54
inductive "T a"
berghofe@13405
    55
intros
berghofe@13405
    56
  T0 [CPure.intro]: "a \<noteq> b \<Longrightarrow> (ws, zs) \<in> R b \<Longrightarrow> (w # zs, (a # w) # zs) \<in> T a"
berghofe@13405
    57
  T1 [CPure.intro]: "(ws, zs) \<in> T a \<Longrightarrow> (w # ws, (a # w) # zs) \<in> T a"
berghofe@13405
    58
  T2 [CPure.intro]: "a \<noteq> b \<Longrightarrow> (ws, zs) \<in> T a \<Longrightarrow> (ws, (b # w) # zs) \<in> T a"
berghofe@13405
    59
berghofe@13405
    60
consts
berghofe@13405
    61
  bar :: "letter list list set"
berghofe@13405
    62
berghofe@13405
    63
inductive bar
berghofe@13405
    64
intros
berghofe@13405
    65
  bar1 [CPure.intro]: "ws \<in> good \<Longrightarrow> ws \<in> bar"
berghofe@13930
    66
  bar2 [CPure.intro]: "(\<And>w. w # ws \<in> bar) \<Longrightarrow> ws \<in> bar"
berghofe@13405
    67
berghofe@13405
    68
theorem prop1: "([] # ws) \<in> bar" by rules
berghofe@13405
    69
berghofe@13405
    70
theorem lemma1: "ws \<in> L as \<Longrightarrow> ws \<in> L (a # as)"
berghofe@13405
    71
  by (erule L.induct, rules+)
berghofe@13405
    72
berghofe@13405
    73
theorem lemma2' [rule_format]: "(vs, ws) \<in> R a \<Longrightarrow> vs \<in> L as \<longrightarrow> ws \<in> L (a # as)"
berghofe@13405
    74
  apply (erule R.induct)
berghofe@13405
    75
  apply (rule impI)
berghofe@13405
    76
  apply (erule L.elims)
berghofe@13405
    77
  apply simp+
berghofe@13405
    78
  apply (rule impI)
berghofe@13405
    79
  apply (erule L.elims)
berghofe@13405
    80
  apply simp_all
berghofe@13405
    81
  apply (rule L0)
berghofe@13405
    82
  apply (erule emb2)
berghofe@13405
    83
  apply (erule L1)
berghofe@13405
    84
  done
berghofe@13405
    85
 
berghofe@13405
    86
theorem lemma2 [rule_format]: "(vs, ws) \<in> R a \<Longrightarrow> vs \<in> good \<longrightarrow> ws \<in> good"
berghofe@13405
    87
  apply (erule R.induct)
berghofe@13405
    88
  apply rules
berghofe@13405
    89
  apply (rule impI)
berghofe@13405
    90
  apply (erule good.elims)
berghofe@13405
    91
  apply simp_all
berghofe@13405
    92
  apply (rule good0)
berghofe@13405
    93
  apply (erule lemma2')
berghofe@13405
    94
  apply assumption
berghofe@13405
    95
  apply (erule good1)
berghofe@13405
    96
  done
berghofe@13405
    97
berghofe@13405
    98
theorem lemma3' [rule_format]:
berghofe@13405
    99
    "(vs, ws) \<in> T a \<Longrightarrow> vs \<in> L as \<longrightarrow> ws \<in> L (a # as)"
berghofe@13405
   100
  apply (erule T.induct)
berghofe@13405
   101
  apply (rule impI)
berghofe@13405
   102
  apply (erule L.elims)
berghofe@13405
   103
  apply simp_all
berghofe@13405
   104
  apply (rule L0)
berghofe@13405
   105
  apply (erule emb2)
berghofe@13405
   106
  apply (rule L1)
berghofe@13405
   107
  apply (erule lemma1)
berghofe@13405
   108
  apply (rule impI)
berghofe@13405
   109
  apply (erule L.elims)
berghofe@13405
   110
  apply simp_all
berghofe@13405
   111
  apply rules+
berghofe@13405
   112
  done
berghofe@13405
   113
berghofe@13405
   114
theorem lemma3 [rule_format]: "(ws, zs) \<in> T a \<Longrightarrow> ws \<in> good \<longrightarrow> zs \<in> good"
berghofe@13405
   115
  apply (erule T.induct)
berghofe@13405
   116
  apply (rule impI)
berghofe@13405
   117
  apply (erule good.elims)
berghofe@13405
   118
  apply simp_all
berghofe@13405
   119
  apply (rule good0)
berghofe@13405
   120
  apply (erule lemma1)
berghofe@13405
   121
  apply (erule good1)
berghofe@13405
   122
  apply (rule impI)
berghofe@13405
   123
  apply (erule good.elims)
berghofe@13405
   124
  apply simp_all
berghofe@13405
   125
  apply (rule good0)
berghofe@13405
   126
  apply (erule lemma3')
berghofe@13405
   127
  apply rules+
berghofe@13405
   128
  done
berghofe@13405
   129
berghofe@13405
   130
theorem lemma4 [rule_format]: "(ws, zs) \<in> R a \<Longrightarrow> ws \<noteq> [] \<longrightarrow> (ws, zs) \<in> T a"
berghofe@13405
   131
  apply (erule R.induct)
berghofe@13405
   132
  apply rules
berghofe@13405
   133
  apply (rule impI)
berghofe@13405
   134
  apply (case_tac vs)
berghofe@13405
   135
  apply (erule R.elims)
berghofe@13405
   136
  apply simp
berghofe@13405
   137
  apply (case_tac a)
berghofe@13405
   138
  apply (rule_tac b=B in T0)
berghofe@13405
   139
  apply simp
berghofe@13405
   140
  apply (rule R0)
berghofe@13405
   141
  apply (rule_tac b=A in T0)
berghofe@13405
   142
  apply simp
berghofe@13405
   143
  apply (rule R0)
berghofe@13405
   144
  apply simp
berghofe@13405
   145
  apply (rule T1)
berghofe@13405
   146
  apply (erule mp)
berghofe@13405
   147
  apply simp
berghofe@13405
   148
  done
berghofe@13405
   149
berghofe@13930
   150
lemma letter_neq: "(a::letter) \<noteq> b \<Longrightarrow> c \<noteq> a \<Longrightarrow> c = b"
berghofe@13930
   151
  apply (case_tac a)
berghofe@13930
   152
  apply (case_tac b)
berghofe@13930
   153
  apply (case_tac c, simp, simp)
berghofe@13930
   154
  apply (case_tac c, simp, simp)
berghofe@13930
   155
  apply (case_tac b)
berghofe@13930
   156
  apply (case_tac c, simp, simp)
berghofe@13930
   157
  apply (case_tac c, simp, simp)
berghofe@13930
   158
  done
berghofe@13405
   159
berghofe@13930
   160
lemma letter_eq_dec: "(a::letter) = b \<or> a \<noteq> b"
berghofe@13405
   161
  apply (case_tac a)
berghofe@13405
   162
  apply (case_tac b)
berghofe@13405
   163
  apply simp
berghofe@13405
   164
  apply simp
berghofe@13405
   165
  apply (case_tac b)
berghofe@13405
   166
  apply simp
berghofe@13405
   167
  apply simp
berghofe@13405
   168
  done
berghofe@13405
   169
berghofe@13930
   170
theorem prop2:
berghofe@13930
   171
  assumes ab: "a \<noteq> b" and bar: "xs \<in> bar"
berghofe@13930
   172
  shows "\<And>ys zs. ys \<in> bar \<Longrightarrow> (xs, zs) \<in> T a \<Longrightarrow> (ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar" using bar
berghofe@13930
   173
proof induct
berghofe@13930
   174
  fix xs zs assume "xs \<in> good" and "(xs, zs) \<in> T a"
berghofe@13930
   175
  show "zs \<in> bar" by (rule bar1) (rule lemma3)
berghofe@13930
   176
next
berghofe@13930
   177
  fix xs ys
berghofe@13930
   178
  assume I: "\<And>w ys zs. ys \<in> bar \<Longrightarrow> (w # xs, zs) \<in> T a \<Longrightarrow> (ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar"
berghofe@13930
   179
  assume "ys \<in> bar"
berghofe@13930
   180
  thus "\<And>zs. (xs, zs) \<in> T a \<Longrightarrow> (ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar"
berghofe@13930
   181
  proof induct
berghofe@13930
   182
    fix ys zs assume "ys \<in> good" and "(ys, zs) \<in> T b"
berghofe@13930
   183
    show "zs \<in> bar" by (rule bar1) (rule lemma3)
berghofe@13930
   184
  next
berghofe@13930
   185
    fix ys zs assume I': "\<And>w zs. (xs, zs) \<in> T a \<Longrightarrow> (w # ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar"
berghofe@13930
   186
    and ys: "\<And>w. w # ys \<in> bar" and Ta: "(xs, zs) \<in> T a" and Tb: "(ys, zs) \<in> T b"
berghofe@13930
   187
    show "zs \<in> bar"
berghofe@13930
   188
    proof (rule bar2)
berghofe@13930
   189
      fix w
berghofe@13930
   190
      show "w # zs \<in> bar"
berghofe@13930
   191
      proof (cases w)
berghofe@13930
   192
	case Nil
berghofe@13930
   193
	thus ?thesis by simp (rule prop1)
berghofe@13930
   194
      next
berghofe@13930
   195
	case (Cons c cs)
berghofe@13930
   196
	from letter_eq_dec show ?thesis
berghofe@13930
   197
	proof
berghofe@13930
   198
	  assume ca: "c = a"
berghofe@13930
   199
	  from ab have "(a # cs) # zs \<in> bar" by (rules intro: I ys Ta Tb)+
berghofe@13930
   200
	  thus ?thesis by (simp add: Cons ca)
berghofe@13930
   201
	next
berghofe@13930
   202
	  assume "c \<noteq> a"
berghofe@13930
   203
	  with ab have cb: "c = b" by (rule letter_neq)
berghofe@13930
   204
	  from ab have "(b # cs) # zs \<in> bar" by (rules intro: I' Ta Tb)+
berghofe@13930
   205
	  thus ?thesis by (simp add: Cons cb)
berghofe@13930
   206
	qed
berghofe@13930
   207
      qed
berghofe@13930
   208
    qed
berghofe@13930
   209
  qed
berghofe@13930
   210
qed
berghofe@13405
   211
berghofe@13930
   212
theorem prop3:
berghofe@13930
   213
  assumes bar: "xs \<in> bar"
berghofe@13930
   214
  shows "\<And>zs. xs \<noteq> [] \<Longrightarrow> (xs, zs) \<in> R a \<Longrightarrow> zs \<in> bar" using bar
berghofe@13930
   215
proof induct
berghofe@13930
   216
  fix xs zs
berghofe@13930
   217
  assume "xs \<in> good" and "(xs, zs) \<in> R a"
berghofe@13930
   218
  show "zs \<in> bar" by (rule bar1) (rule lemma2)
berghofe@13930
   219
next
berghofe@13930
   220
  fix xs zs
berghofe@13930
   221
  assume I: "\<And>w zs. w # xs \<noteq> [] \<Longrightarrow> (w # xs, zs) \<in> R a \<Longrightarrow> zs \<in> bar"
berghofe@13930
   222
  and xsb: "\<And>w. w # xs \<in> bar" and xsn: "xs \<noteq> []" and R: "(xs, zs) \<in> R a"
berghofe@13930
   223
  show "zs \<in> bar"
berghofe@13930
   224
  proof (rule bar2)
berghofe@13930
   225
    fix w
berghofe@13930
   226
    show "w # zs \<in> bar"
berghofe@13930
   227
    proof (induct w)
berghofe@13930
   228
      case Nil
berghofe@13930
   229
      show ?case by (rule prop1)
berghofe@13930
   230
    next
berghofe@13930
   231
      case (Cons c cs)
berghofe@13930
   232
      from letter_eq_dec show ?case
berghofe@13930
   233
      proof
berghofe@13930
   234
	assume "c = a"
berghofe@13930
   235
	thus ?thesis by (rules intro: I [simplified] R)
berghofe@13930
   236
      next
berghofe@13930
   237
	from R xsn have T: "(xs, zs) \<in> T a" by (rule lemma4)
berghofe@13930
   238
	assume "c \<noteq> a"
berghofe@13930
   239
	thus ?thesis by (rules intro: prop2 Cons xsb xsn R T)
berghofe@13930
   240
      qed
berghofe@13930
   241
    qed
berghofe@13930
   242
  qed
berghofe@13930
   243
qed
berghofe@13405
   244
berghofe@13405
   245
theorem higman: "[] \<in> bar"
berghofe@13930
   246
proof (rule bar2)
berghofe@13930
   247
  fix w
berghofe@13930
   248
  show "[w] \<in> bar"
berghofe@13930
   249
  proof (induct w)
berghofe@13930
   250
    show "[[]] \<in> bar" by (rule prop1)
berghofe@13930
   251
  next
berghofe@13930
   252
    fix c cs assume "[cs] \<in> bar"
berghofe@13930
   253
    thus "[c # cs] \<in> bar" by (rule prop3) (simp, rules)
berghofe@13930
   254
  qed
berghofe@13930
   255
qed
berghofe@13405
   256
berghofe@13405
   257
consts
berghofe@13405
   258
  is_prefix :: "'a list \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool"
berghofe@13405
   259
berghofe@13405
   260
primrec
berghofe@13405
   261
  "is_prefix [] f = True"
berghofe@13405
   262
  "is_prefix (x # xs) f = (x = f (length xs) \<and> is_prefix xs f)"
berghofe@13405
   263
berghofe@13405
   264
theorem good_prefix_lemma:
berghofe@13930
   265
  assumes bar: "ws \<in> bar"
berghofe@13930
   266
  shows "is_prefix ws f \<Longrightarrow> \<exists>vs. is_prefix vs f \<and> vs \<in> good" using bar
berghofe@13930
   267
proof induct
berghofe@13930
   268
  case bar1
berghofe@13930
   269
  thus ?case by rules
berghofe@13930
   270
next
berghofe@13930
   271
  case (bar2 ws)
berghofe@13930
   272
  have "is_prefix (f (length ws) # ws) f" by simp
berghofe@13930
   273
  thus ?case by (rules intro: bar2)
berghofe@13930
   274
qed
berghofe@13405
   275
berghofe@13405
   276
theorem good_prefix: "\<exists>vs. is_prefix vs f \<and> vs \<in> good"
berghofe@13930
   277
  using higman
berghofe@13930
   278
  by (rule good_prefix_lemma) simp+
berghofe@13405
   279
berghofe@13711
   280
subsection {* Extracting the program *}
berghofe@13405
   281
berghofe@13711
   282
declare bar.induct [ind_realizer]
berghofe@13405
   283
berghofe@13405
   284
extract good_prefix
berghofe@13405
   285
berghofe@13405
   286
text {*
berghofe@13405
   287
  Program extracted from the proof of @{text good_prefix}:
berghofe@13405
   288
  @{thm [display] good_prefix_def [no_vars]}
berghofe@13405
   289
  Corresponding correctness theorem:
berghofe@13405
   290
  @{thm [display] good_prefix_correctness [no_vars]}
berghofe@13405
   291
  Program extracted from the proof of @{text good_prefix_lemma}:
berghofe@13405
   292
  @{thm [display] good_prefix_lemma_def [no_vars]}
berghofe@13405
   293
  Program extracted from the proof of @{text higman}:
berghofe@13405
   294
  @{thm [display] higman_def [no_vars]}
berghofe@13405
   295
  Program extracted from the proof of @{text prop1}:
berghofe@13405
   296
  @{thm [display] prop1_def [no_vars]}
berghofe@13405
   297
  Program extracted from the proof of @{text prop2}:
berghofe@13405
   298
  @{thm [display] prop2_def [no_vars]}
berghofe@13405
   299
  Program extracted from the proof of @{text prop3}:
berghofe@13405
   300
  @{thm [display] prop3_def [no_vars]}
berghofe@13405
   301
*}
berghofe@13405
   302
berghofe@13405
   303
generate_code
berghofe@13405
   304
  test = good_prefix
berghofe@13405
   305
berghofe@13405
   306
ML {*
berghofe@13405
   307
val a = 16807.0;
berghofe@13405
   308
val m = 2147483647.0;
berghofe@13405
   309
berghofe@13405
   310
fun nextRand seed =
berghofe@13405
   311
  let val t = a*seed
berghofe@13405
   312
  in  t - m * real (Real.floor(t/m)) end;
berghofe@13405
   313
berghofe@13405
   314
fun mk_word seed l =
berghofe@13405
   315
  let
berghofe@13405
   316
    val r = nextRand seed;
berghofe@13405
   317
    val i = Real.round (r / m * 10.0);
berghofe@13405
   318
  in if i > 7 andalso l > 2 then (r, []) else
berghofe@13405
   319
    apsnd (cons (if i mod 2 = 0 then A else B)) (mk_word r (l+1))
berghofe@13405
   320
  end;
berghofe@13405
   321
berghofe@13405
   322
fun f s id0 = mk_word s 0
berghofe@13405
   323
  | f s (Suc n) = f (fst (mk_word s 0)) n;
berghofe@13405
   324
berghofe@13405
   325
val g1 = snd o (f 20000.0);
berghofe@13405
   326
berghofe@13405
   327
val g2 = snd o (f 50000.0);
berghofe@13405
   328
berghofe@13405
   329
fun f1 id0 = [A,A]
berghofe@13405
   330
  | f1 (Suc id0) = [B]
berghofe@13405
   331
  | f1 (Suc (Suc id0)) = [A,B]
berghofe@13405
   332
  | f1 _ = [];
berghofe@13405
   333
berghofe@13405
   334
fun f2 id0 = [A,A]
berghofe@13405
   335
  | f2 (Suc id0) = [B]
berghofe@13405
   336
  | f2 (Suc (Suc id0)) = [B,A]
berghofe@13405
   337
  | f2 _ = [];
berghofe@13405
   338
berghofe@13405
   339
val xs1 = test g1;
berghofe@13405
   340
val xs2 = test g2;
berghofe@13405
   341
val xs3 = test f1;
berghofe@13405
   342
val xs4 = test f2;
berghofe@13405
   343
*}
berghofe@13405
   344
berghofe@13405
   345
end