Higman2.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 27 Sep 2013 09:20:58 +0100
changeset 389 796de251332c
parent 210 580e06329171
permissions -rw-r--r--
added paper by Tobias
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
     1
(*  Title:      HOL/Proofs/Extraction/Higman.thy
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
     2
    Author:     Stefan Berghofer, TU Muenchen
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
     3
    Author:     Monika Seisenberger, LMU Muenchen
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
     4
*)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
     5
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
     6
header {* Higman's lemma *}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
     7
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
     8
theory Higman2
210
580e06329171 just test
urbanc
parents: 209
diff changeset
     9
imports Main
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    10
begin
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    11
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    12
text {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    13
  Formalization by Stefan Berghofer and Monika Seisenberger,
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    14
  based on Coquand and Fridlender \cite{Coquand93}.
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    15
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    16
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    17
datatype letter = A | B
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    18
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    19
inductive emb :: "letter list \<Rightarrow> letter list \<Rightarrow> bool"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    20
where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    21
   emb0 [Pure.intro]: "emb [] bs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    22
 | emb1 [Pure.intro]: "emb as bs \<Longrightarrow> emb as (b # bs)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    23
 | emb2 [Pure.intro]: "emb as bs \<Longrightarrow> emb (a # as) (a # bs)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    24
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    25
inductive L :: "letter list \<Rightarrow> letter list list \<Rightarrow> bool"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    26
  for v :: "letter list"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    27
where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    28
   L0 [Pure.intro]: "emb w v \<Longrightarrow> L v (w # ws)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    29
 | L1 [Pure.intro]: "L v ws \<Longrightarrow> L v (w # ws)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    30
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    31
inductive good :: "letter list list \<Rightarrow> bool"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    32
where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    33
    good0 [Pure.intro]: "L w ws \<Longrightarrow> good (w # ws)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    34
  | good1 [Pure.intro]: "good ws \<Longrightarrow> good (w # ws)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    35
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    36
inductive R :: "letter \<Rightarrow> letter list list \<Rightarrow> letter list list \<Rightarrow> bool"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    37
  for a :: letter
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    38
where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    39
    R0 [Pure.intro]: "R a [] []"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    40
  | R1 [Pure.intro]: "R a vs ws \<Longrightarrow> R a (w # vs) ((a # w) # ws)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    41
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    42
inductive T :: "letter \<Rightarrow> letter list list \<Rightarrow> letter list list \<Rightarrow> bool"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    43
  for a :: letter
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    44
where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    45
    T0 [Pure.intro]: "a \<noteq> b \<Longrightarrow> R b ws zs \<Longrightarrow> T a (w # zs) ((a # w) # zs)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    46
  | T1 [Pure.intro]: "T a ws zs \<Longrightarrow> T a (w # ws) ((a # w) # zs)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    47
  | T2 [Pure.intro]: "a \<noteq> b \<Longrightarrow> T a ws zs \<Longrightarrow> T a ws ((b # w) # zs)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    48
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    49
inductive bar :: "letter list list \<Rightarrow> bool"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    50
where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    51
    bar1 [Pure.intro]: "good ws \<Longrightarrow> bar ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    52
  | bar2 [Pure.intro]: "(\<And>w. bar (w # ws)) \<Longrightarrow> bar ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    53
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    54
theorem prop1: "bar ([] # ws)" by iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    55
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    56
theorem lemma1: "L as ws \<Longrightarrow> L (a # as) ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    57
  by (erule L.induct, iprover+)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    58
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    59
lemma lemma2': "R a vs ws \<Longrightarrow> L as vs \<Longrightarrow> L (a # as) ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    60
  apply (induct set: R)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    61
  apply (erule L.cases)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    62
  apply simp+
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    63
  apply (erule L.cases)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    64
  apply simp_all
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    65
  apply (rule L0)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    66
  apply (erule emb2)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    67
  apply (erule L1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    68
  done
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    69
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    70
lemma lemma2: "R a vs ws \<Longrightarrow> good vs \<Longrightarrow> good ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    71
  apply (induct set: R)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    72
  apply iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    73
  apply (erule good.cases)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    74
  apply simp_all
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    75
  apply (rule good0)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    76
  apply (erule lemma2')
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    77
  apply assumption
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    78
  apply (erule good1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    79
  done
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    80
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    81
lemma lemma3': "T a vs ws \<Longrightarrow> L as vs \<Longrightarrow> L (a # as) ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    82
  apply (induct set: T)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    83
  apply (erule L.cases)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    84
  apply simp_all
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    85
  apply (rule L0)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    86
  apply (erule emb2)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    87
  apply (rule L1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    88
  apply (erule lemma1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    89
  apply (erule L.cases)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    90
  apply simp_all
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    91
  apply iprover+
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    92
  done
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    93
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    94
lemma lemma3: "T a ws zs \<Longrightarrow> good ws \<Longrightarrow> good zs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    95
  apply (induct set: T)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    96
  apply (erule good.cases)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    97
  apply simp_all
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    98
  apply (rule good0)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
    99
  apply (erule lemma1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   100
  apply (erule good1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   101
  apply (erule good.cases)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   102
  apply simp_all
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   103
  apply (rule good0)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   104
  apply (erule lemma3')
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   105
  apply iprover+
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   106
  done
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   107
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   108
lemma lemma4: "R a ws zs \<Longrightarrow> ws \<noteq> [] \<Longrightarrow> T a ws zs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   109
  apply (induct set: R)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   110
  apply iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   111
  apply (case_tac vs)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   112
  apply (erule R.cases)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   113
  apply simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   114
  apply (case_tac a)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   115
  apply (rule_tac b=B in T0)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   116
  apply simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   117
  apply (rule R0)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   118
  apply (rule_tac b=A in T0)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   119
  apply simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   120
  apply (rule R0)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   121
  apply simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   122
  apply (rule T1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   123
  apply simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   124
  done
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   125
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   126
lemma letter_neq: "(a::letter) \<noteq> b \<Longrightarrow> c \<noteq> a \<Longrightarrow> c = b"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   127
  apply (case_tac a)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   128
  apply (case_tac b)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   129
  apply (case_tac c, simp, simp)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   130
  apply (case_tac c, simp, simp)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   131
  apply (case_tac b)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   132
  apply (case_tac c, simp, simp)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   133
  apply (case_tac c, simp, simp)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   134
  done
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   135
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   136
lemma letter_eq_dec: "(a::letter) = b \<or> a \<noteq> b"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   137
  apply (case_tac a)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   138
  apply (case_tac b)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   139
  apply simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   140
  apply simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   141
  apply (case_tac b)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   142
  apply simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   143
  apply simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   144
  done
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   145
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   146
theorem prop2:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   147
  assumes ab: "a \<noteq> b" and bar: "bar xs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   148
  shows "\<And>ys zs. bar ys \<Longrightarrow> T a xs zs \<Longrightarrow> T b ys zs \<Longrightarrow> bar zs" using bar
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   149
proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   150
  fix xs zs assume "T a xs zs" and "good xs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   151
  hence "good zs" by (rule lemma3)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   152
  then show "bar zs" by (rule bar1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   153
next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   154
  fix xs ys
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   155
  assume I: "\<And>w ys zs. bar ys \<Longrightarrow> T a (w # xs) zs \<Longrightarrow> T b ys zs \<Longrightarrow> bar zs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   156
  assume "bar ys"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   157
  thus "\<And>zs. T a xs zs \<Longrightarrow> T b ys zs \<Longrightarrow> bar zs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   158
  proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   159
    fix ys zs assume "T b ys zs" and "good ys"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   160
    then have "good zs" by (rule lemma3)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   161
    then show "bar zs" by (rule bar1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   162
  next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   163
    fix ys zs assume I': "\<And>w zs. T a xs zs \<Longrightarrow> T b (w # ys) zs \<Longrightarrow> bar zs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   164
    and ys: "\<And>w. bar (w # ys)" and Ta: "T a xs zs" and Tb: "T b ys zs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   165
    show "bar zs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   166
    proof (rule bar2)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   167
      fix w
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   168
      show "bar (w # zs)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   169
      proof (cases w)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   170
        case Nil
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   171
        thus ?thesis by simp (rule prop1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   172
      next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   173
        case (Cons c cs)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   174
        from letter_eq_dec show ?thesis
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   175
        proof
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   176
          assume ca: "c = a"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   177
          from ab have "bar ((a # cs) # zs)" by (iprover intro: I ys Ta Tb)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   178
          thus ?thesis by (simp add: Cons ca)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   179
        next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   180
          assume "c \<noteq> a"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   181
          with ab have cb: "c = b" by (rule letter_neq)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   182
          from ab have "bar ((b # cs) # zs)" by (iprover intro: I' Ta Tb)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   183
          thus ?thesis by (simp add: Cons cb)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   184
        qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   185
      qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   186
    qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   187
  qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   188
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   189
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   190
theorem prop3:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   191
  assumes bar: "bar xs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   192
  shows "\<And>zs. xs \<noteq> [] \<Longrightarrow> R a xs zs \<Longrightarrow> bar zs" using bar
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   193
proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   194
  fix xs zs
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   195
  assume "R a xs zs" and "good xs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   196
  then have "good zs" by (rule lemma2)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   197
  then show "bar zs" by (rule bar1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   198
next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   199
  fix xs zs
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   200
  assume I: "\<And>w zs. w # xs \<noteq> [] \<Longrightarrow> R a (w # xs) zs \<Longrightarrow> bar zs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   201
  and xsb: "\<And>w. bar (w # xs)" and xsn: "xs \<noteq> []" and R: "R a xs zs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   202
  show "bar zs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   203
  proof (rule bar2)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   204
    fix w
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   205
    show "bar (w # zs)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   206
    proof (induct w)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   207
      case Nil
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   208
      show ?case by (rule prop1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   209
    next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   210
      case (Cons c cs)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   211
      from letter_eq_dec show ?case
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   212
      proof
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   213
        assume "c = a"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   214
        thus ?thesis by (iprover intro: I [simplified] R)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   215
      next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   216
        from R xsn have T: "T a xs zs" by (rule lemma4)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   217
        assume "c \<noteq> a"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   218
        thus ?thesis by (iprover intro: prop2 Cons xsb xsn R T)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   219
      qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   220
    qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   221
  qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   222
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   223
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   224
theorem higman: "bar []"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   225
proof (rule bar2)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   226
  fix w
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   227
  show "bar [w]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   228
  proof (induct w)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   229
    show "bar [[]]" by (rule prop1)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   230
  next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   231
    fix c cs assume "bar [cs]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   232
    thus "bar [c # cs]" by (rule prop3) (simp, iprover)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   233
  qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   234
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   235
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   236
notation
580e06329171 just test
urbanc
parents: 209
diff changeset
   237
  emb ("_ \<preceq> _")
580e06329171 just test
urbanc
parents: 209
diff changeset
   238
580e06329171 just test
urbanc
parents: 209
diff changeset
   239
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   240
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   241
lemma substring_refl:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   242
  "x \<preceq> x"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   243
apply(induct x)
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   244
apply(auto intro: emb.intros)
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   245
done
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   246
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   247
lemma substring_trans:
580e06329171 just test
urbanc
parents: 209
diff changeset
   248
  assumes a: "x1 \<preceq> x2" and b: "x2 \<preceq> x3"
580e06329171 just test
urbanc
parents: 209
diff changeset
   249
  shows "x1 \<preceq> x3"
580e06329171 just test
urbanc
parents: 209
diff changeset
   250
using a b
580e06329171 just test
urbanc
parents: 209
diff changeset
   251
apply(induct arbitrary: x3)
580e06329171 just test
urbanc
parents: 209
diff changeset
   252
apply(auto intro: emb.intros)
580e06329171 just test
urbanc
parents: 209
diff changeset
   253
apply(rotate_tac 2)
580e06329171 just test
urbanc
parents: 209
diff changeset
   254
apply(erule emb.cases)
580e06329171 just test
urbanc
parents: 209
diff changeset
   255
apply(simp_all)
580e06329171 just test
urbanc
parents: 209
diff changeset
   256
sorry 
580e06329171 just test
urbanc
parents: 209
diff changeset
   257
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   258
definition
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   259
 "SUBSEQ C \<equiv> {x. \<exists>y \<in> C. x \<preceq> y}"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   260
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   261
lemma
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   262
 "SUBSEQ (SUBSEQ C) = SUBSEQ C"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   263
unfolding SUBSEQ_def
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   264
apply(auto)
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   265
apply(erule emb.induct)
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   266
apply(rule_tac x="xb" in bexI)
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   267
apply(rule emb.intros)
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   268
apply(simp)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   269
apply(erule bexE)
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   270
apply(rule_tac x="y" in bexI)
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   271
apply(auto)[2]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   272
apply(erule bexE)
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   273
sorry
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   274
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   275
lemma substring_closed:
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   276
  "x \<in> SUBSEQ C \<and> y \<preceq> x \<Longrightarrow> y \<in> SUBSEQ C"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   277
unfolding SUBSEQ_def
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   278
apply(auto)
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   279
apply(rule_tac x="xa" in bexI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   280
apply(rule substring_trans)
580e06329171 just test
urbanc
parents: 209
diff changeset
   281
apply(auto)
580e06329171 just test
urbanc
parents: 209
diff changeset
   282
done
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   283
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   284
lemma "SUBSEQ C \<subseteq> UNIV"
580e06329171 just test
urbanc
parents: 209
diff changeset
   285
unfolding SUBSEQ_def
580e06329171 just test
urbanc
parents: 209
diff changeset
   286
apply(auto)
580e06329171 just test
urbanc
parents: 209
diff changeset
   287
done
580e06329171 just test
urbanc
parents: 209
diff changeset
   288
580e06329171 just test
urbanc
parents: 209
diff changeset
   289
580e06329171 just test
urbanc
parents: 209
diff changeset
   290
580e06329171 just test
urbanc
parents: 209
diff changeset
   291
ML {*
580e06329171 just test
urbanc
parents: 209
diff changeset
   292
@{term "UNIV - (C::string set)"}
580e06329171 just test
urbanc
parents: 209
diff changeset
   293
*}
580e06329171 just test
urbanc
parents: 209
diff changeset
   294
580e06329171 just test
urbanc
parents: 209
diff changeset
   295
lemma
580e06329171 just test
urbanc
parents: 209
diff changeset
   296
  assumes "finite S"
580e06329171 just test
urbanc
parents: 209
diff changeset
   297
  shows "finite (UNIV - {y. \<forall>z \<in> S. \<not>(z \<preceq> y)})"
580e06329171 just test
urbanc
parents: 209
diff changeset
   298
oops
580e06329171 just test
urbanc
parents: 209
diff changeset
   299
 
580e06329171 just test
urbanc
parents: 209
diff changeset
   300
580e06329171 just test
urbanc
parents: 209
diff changeset
   301
580e06329171 just test
urbanc
parents: 209
diff changeset
   302
lemma a: "\<forall>x \<in> SUBSEQ C. \<exists>y \<in> C. x \<preceq> y"
580e06329171 just test
urbanc
parents: 209
diff changeset
   303
unfolding SUBSEQ_def 
580e06329171 just test
urbanc
parents: 209
diff changeset
   304
apply(auto)
580e06329171 just test
urbanc
parents: 209
diff changeset
   305
done
580e06329171 just test
urbanc
parents: 209
diff changeset
   306
580e06329171 just test
urbanc
parents: 209
diff changeset
   307
lemma b:
580e06329171 just test
urbanc
parents: 209
diff changeset
   308
  shows "\<exists>S \<subseteq> SUBSEQ C. S \<noteq>{} \<and> (y \<in> C \<longleftrightarrow> (\<forall>z \<in> S. \<not>(z \<preceq> y)))"
580e06329171 just test
urbanc
parents: 209
diff changeset
   309
sorry
580e06329171 just test
urbanc
parents: 209
diff changeset
   310
580e06329171 just test
urbanc
parents: 209
diff changeset
   311
lemma "False"
580e06329171 just test
urbanc
parents: 209
diff changeset
   312
using b a
580e06329171 just test
urbanc
parents: 209
diff changeset
   313
apply(blast)
580e06329171 just test
urbanc
parents: 209
diff changeset
   314
done
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   315
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   316
definition
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   317
 "CLOSED C \<equiv> C = SUBSEQ C"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   318
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   319
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   320
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   321
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   322
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   323
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   324
primrec
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   325
  is_prefix :: "'a list \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   326
where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   327
    "is_prefix [] f = True"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   328
  | "is_prefix (x # xs) f = (x = f (length xs) \<and> is_prefix xs f)"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   329
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   330
theorem L_idx:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   331
  assumes L: "L w ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   332
  shows "is_prefix ws f \<Longrightarrow> \<exists>i. emb (f i) w \<and> i < length ws" using L
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   333
proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   334
  case (L0 v ws)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   335
  hence "emb (f (length ws)) w" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   336
  moreover have "length ws < length (v # ws)" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   337
  ultimately show ?case by iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   338
next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   339
  case (L1 ws v)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   340
  then obtain i where emb: "emb (f i) w" and "i < length ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   341
    by simp iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   342
  hence "i < length (v # ws)" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   343
  with emb show ?case by iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   344
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   345
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   346
theorem good_idx:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   347
  assumes good: "good ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   348
  shows "is_prefix ws f \<Longrightarrow> \<exists>i j. emb (f i) (f j) \<and> i < j" using good
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   349
proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   350
  case (good0 w ws)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   351
  hence "w = f (length ws)" and "is_prefix ws f" by simp_all
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   352
  with good0 show ?case by (iprover dest: L_idx)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   353
next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   354
  case (good1 ws w)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   355
  thus ?case by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   356
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   357
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   358
theorem bar_idx:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   359
  assumes bar: "bar ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   360
  shows "is_prefix ws f \<Longrightarrow> \<exists>i j. emb (f i) (f j) \<and> i < j" using bar
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   361
proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   362
  case (bar1 ws)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   363
  thus ?case by (rule good_idx)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   364
next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   365
  case (bar2 ws)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   366
  hence "is_prefix (f (length ws) # ws) f" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   367
  thus ?case by (rule bar2)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   368
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   369
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   370
text {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   371
Strong version: yields indices of words that can be embedded into each other.
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   372
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   373
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   374
theorem higman_idx: "\<exists>(i::nat) j. emb (f i) (f j) \<and> i < j"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   375
proof (rule bar_idx)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   376
  show "bar []" by (rule higman)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   377
  show "is_prefix [] f" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   378
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   379
210
580e06329171 just test
urbanc
parents: 209
diff changeset
   380
definition
580e06329171 just test
urbanc
parents: 209
diff changeset
   381
  myeq ("~~")
580e06329171 just test
urbanc
parents: 209
diff changeset
   382
where
580e06329171 just test
urbanc
parents: 209
diff changeset
   383
  "~~ \<equiv> {(x, y). x \<preceq> y \<and> y \<preceq> x}"
580e06329171 just test
urbanc
parents: 209
diff changeset
   384
580e06329171 just test
urbanc
parents: 209
diff changeset
   385
abbreviation
580e06329171 just test
urbanc
parents: 209
diff changeset
   386
  myeq_applied ("_ ~~~ _")
580e06329171 just test
urbanc
parents: 209
diff changeset
   387
where
580e06329171 just test
urbanc
parents: 209
diff changeset
   388
  "x ~~~ y \<equiv> (x, y) \<in> ~~"
580e06329171 just test
urbanc
parents: 209
diff changeset
   389
580e06329171 just test
urbanc
parents: 209
diff changeset
   390
580e06329171 just test
urbanc
parents: 209
diff changeset
   391
definition
580e06329171 just test
urbanc
parents: 209
diff changeset
   392
 "minimal x Y \<equiv> (x \<in> Y \<and> (\<forall>y \<in> Y. y \<preceq> x \<longrightarrow> x \<preceq> y))"
580e06329171 just test
urbanc
parents: 209
diff changeset
   393
580e06329171 just test
urbanc
parents: 209
diff changeset
   394
definition
580e06329171 just test
urbanc
parents: 209
diff changeset
   395
 "downclosed Y \<equiv> (\<forall>x \<in> Y. \<forall>y. y \<preceq> x \<longrightarrow> y \<in> Y)" 
580e06329171 just test
urbanc
parents: 209
diff changeset
   396
580e06329171 just test
urbanc
parents: 209
diff changeset
   397
580e06329171 just test
urbanc
parents: 209
diff changeset
   398
lemma g:
580e06329171 just test
urbanc
parents: 209
diff changeset
   399
  assumes "minimal x Y" "y ~~~ x" "downclosed Y"
580e06329171 just test
urbanc
parents: 209
diff changeset
   400
  shows "minimal y Y"
580e06329171 just test
urbanc
parents: 209
diff changeset
   401
using assms
580e06329171 just test
urbanc
parents: 209
diff changeset
   402
apply(simp add: minimal_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   403
apply(rule conjI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   404
apply(simp add: downclosed_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   405
apply(simp add: myeq_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   406
apply(auto)[1]
580e06329171 just test
urbanc
parents: 209
diff changeset
   407
apply(rule ballI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   408
apply(rule impI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   409
apply(simp add: downclosed_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   410
apply(simp add: myeq_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   411
apply(erule conjE)
580e06329171 just test
urbanc
parents: 209
diff changeset
   412
apply(rotate_tac 5)
580e06329171 just test
urbanc
parents: 209
diff changeset
   413
apply(drule_tac x="ya" in bspec)
580e06329171 just test
urbanc
parents: 209
diff changeset
   414
apply(auto)[1]
580e06329171 just test
urbanc
parents: 209
diff changeset
   415
apply(drule mp)
580e06329171 just test
urbanc
parents: 209
diff changeset
   416
apply(erule conjE)
580e06329171 just test
urbanc
parents: 209
diff changeset
   417
apply(rule substring_trans)
580e06329171 just test
urbanc
parents: 209
diff changeset
   418
apply(auto)[2]
580e06329171 just test
urbanc
parents: 209
diff changeset
   419
apply(rule substring_trans)
580e06329171 just test
urbanc
parents: 209
diff changeset
   420
apply(auto)[2]
580e06329171 just test
urbanc
parents: 209
diff changeset
   421
done
580e06329171 just test
urbanc
parents: 209
diff changeset
   422
580e06329171 just test
urbanc
parents: 209
diff changeset
   423
thm Least_le
580e06329171 just test
urbanc
parents: 209
diff changeset
   424
580e06329171 just test
urbanc
parents: 209
diff changeset
   425
lemma
580e06329171 just test
urbanc
parents: 209
diff changeset
   426
  assumes a: "\<exists>(i::nat) j. (f i) \<preceq> (f j) \<and> i < j"
580e06329171 just test
urbanc
parents: 209
diff changeset
   427
  and "downclosed Y"
580e06329171 just test
urbanc
parents: 209
diff changeset
   428
  shows "\<exists>S. finite S \<and> (\<forall>x \<in> Y. \<exists>y \<in> S. \<not> (y \<preceq> x))"
580e06329171 just test
urbanc
parents: 209
diff changeset
   429
proof -
580e06329171 just test
urbanc
parents: 209
diff changeset
   430
  def Ymin \<equiv> "{x. minimal x Y}"
580e06329171 just test
urbanc
parents: 209
diff changeset
   431
  have "downclosed Ymin"
580e06329171 just test
urbanc
parents: 209
diff changeset
   432
  unfolding Ymin_def downclosed_def
580e06329171 just test
urbanc
parents: 209
diff changeset
   433
  apply(auto)
580e06329171 just test
urbanc
parents: 209
diff changeset
   434
  apply(simp add: minimal_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   435
  apply(rule conjI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   436
  using assms(2)
580e06329171 just test
urbanc
parents: 209
diff changeset
   437
  apply(simp add: downclosed_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   438
  apply(auto)[1]
580e06329171 just test
urbanc
parents: 209
diff changeset
   439
  apply(rule ballI) 
580e06329171 just test
urbanc
parents: 209
diff changeset
   440
  apply(rule impI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   441
  apply(erule conjE)
580e06329171 just test
urbanc
parents: 209
diff changeset
   442
  apply(drule_tac x="ya" in bspec)
580e06329171 just test
urbanc
parents: 209
diff changeset
   443
  apply(simp)
580e06329171 just test
urbanc
parents: 209
diff changeset
   444
  apply(drule mp)
580e06329171 just test
urbanc
parents: 209
diff changeset
   445
  apply(rule substring_trans)
580e06329171 just test
urbanc
parents: 209
diff changeset
   446
  apply(auto)[2]
580e06329171 just test
urbanc
parents: 209
diff changeset
   447
  apply(rule substring_trans)
580e06329171 just test
urbanc
parents: 209
diff changeset
   448
  apply(auto)[2]
580e06329171 just test
urbanc
parents: 209
diff changeset
   449
  done
580e06329171 just test
urbanc
parents: 209
diff changeset
   450
  def Yeq \<equiv> "Ymin // ~~"
580e06329171 just test
urbanc
parents: 209
diff changeset
   451
  def Ypick \<equiv> "(\<lambda>X. SOME x. x \<in> X) ` Yeq" 
580e06329171 just test
urbanc
parents: 209
diff changeset
   452
  have "finite Ypick" sorry
580e06329171 just test
urbanc
parents: 209
diff changeset
   453
  moreover
580e06329171 just test
urbanc
parents: 209
diff changeset
   454
  thm LeastI_ex
580e06329171 just test
urbanc
parents: 209
diff changeset
   455
  have "(\<forall>x \<in> Y. \<exists>y \<in> Ypick. (\<not> (y \<preceq> x)))"
580e06329171 just test
urbanc
parents: 209
diff changeset
   456
  apply(rule ballI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   457
  apply(subgoal_tac "\<exists>y. y \<in> Ypick")
580e06329171 just test
urbanc
parents: 209
diff changeset
   458
  apply(erule exE)
580e06329171 just test
urbanc
parents: 209
diff changeset
   459
  apply(rule_tac x="y" in bexI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   460
  apply(subgoal_tac "y \<in> Ymin")
580e06329171 just test
urbanc
parents: 209
diff changeset
   461
  apply(simp add: Ymin_def minimal_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   462
  apply(subgoal_tac "~~ `` {y} \<in> Yeq")
580e06329171 just test
urbanc
parents: 209
diff changeset
   463
  apply(simp add: Yeq_def quotient_def Image_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   464
  apply(erule bexE)
580e06329171 just test
urbanc
parents: 209
diff changeset
   465
  apply(simp add: Ymin_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   466
  apply(subgoal_tac "y ~~~ xa")
580e06329171 just test
urbanc
parents: 209
diff changeset
   467
  apply(drule g)
580e06329171 just test
urbanc
parents: 209
diff changeset
   468
  apply(assumption)
580e06329171 just test
urbanc
parents: 209
diff changeset
   469
  apply(rule assms(2))
580e06329171 just test
urbanc
parents: 209
diff changeset
   470
  apply(simp add: minimal_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   471
  apply(erule conjE)
580e06329171 just test
urbanc
parents: 209
diff changeset
   472
  apply(drule_tac x="x" in bspec)
580e06329171 just test
urbanc
parents: 209
diff changeset
   473
  apply(assumption)
580e06329171 just test
urbanc
parents: 209
diff changeset
   474
  
580e06329171 just test
urbanc
parents: 209
diff changeset
   475
lemma
580e06329171 just test
urbanc
parents: 209
diff changeset
   476
  assumes a: "\<exists>(i::nat) j. (f i) \<preceq> (f j) \<and> i < j"
580e06329171 just test
urbanc
parents: 209
diff changeset
   477
  and b: "downclosed Y" 
580e06329171 just test
urbanc
parents: 209
diff changeset
   478
  and c: "Y \<noteq> {}"
580e06329171 just test
urbanc
parents: 209
diff changeset
   479
  shows "\<exists>S. finite S \<and> (Y = {y. (\<forall>z \<in> S. \<not>(z \<preceq> y))})"
580e06329171 just test
urbanc
parents: 209
diff changeset
   480
proof -
580e06329171 just test
urbanc
parents: 209
diff changeset
   481
  def Ybar \<equiv> "- Y"
580e06329171 just test
urbanc
parents: 209
diff changeset
   482
  def M \<equiv> "{x \<in> Ybar. minimal x Ybar}"
580e06329171 just test
urbanc
parents: 209
diff changeset
   483
  def Cpre \<equiv> "M // ~~"
580e06329171 just test
urbanc
parents: 209
diff changeset
   484
  def C \<equiv> "(\<lambda>X. SOME x. x \<in> X) ` Cpre"
580e06329171 just test
urbanc
parents: 209
diff changeset
   485
  have "finite C" sorry
580e06329171 just test
urbanc
parents: 209
diff changeset
   486
  moreover
580e06329171 just test
urbanc
parents: 209
diff changeset
   487
  have "\<forall>x \<in> Y. \<exists>y \<in> C.  y \<preceq> x" sorry
580e06329171 just test
urbanc
parents: 209
diff changeset
   488
  then have "\<forall>x. (x \<in> Ybar) \<longleftrightarrow> (\<exists>z \<in> C. z \<preceq> x)"
580e06329171 just test
urbanc
parents: 209
diff changeset
   489
  apply(auto simp add: Ybar_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   490
    apply(rule allI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   491
    apply(rule iffI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   492
    prefer 2
580e06329171 just test
urbanc
parents: 209
diff changeset
   493
    apply(erule bexE)
580e06329171 just test
urbanc
parents: 209
diff changeset
   494
    apply(case_tac "x \<in> Y")
580e06329171 just test
urbanc
parents: 209
diff changeset
   495
    prefer 2
580e06329171 just test
urbanc
parents: 209
diff changeset
   496
    apply(simp add: Ybar_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   497
    apply(subgoal_tac "z \<in> Y")
580e06329171 just test
urbanc
parents: 209
diff changeset
   498
    apply(simp add: C_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   499
    apply(simp add: Cpre_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   500
    apply(simp add: M_def Ybar_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   501
    apply(simp add: quotient_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   502
    apply(simp add: myeq_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   503
    apply(simp add: image_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   504
    apply(rule_tac x="x" in exI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   505
    apply(simp)
580e06329171 just test
urbanc
parents: 209
diff changeset
   506
    apply(rule conjI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   507
    apply(simp add: minimal_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   508
    apply(rule ballI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   509
    apply(simp)
580e06329171 just test
urbanc
parents: 209
diff changeset
   510
    apply(rule impI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   511
    prefer 3
580e06329171 just test
urbanc
parents: 209
diff changeset
   512
    apply(simp add: Ybar_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   513
    apply(rule notI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   514
    apply(simp add: C_def Cpre_def M_def Ybar_def quotient_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   515
580e06329171 just test
urbanc
parents: 209
diff changeset
   516
    prefer 2
580e06329171 just test
urbanc
parents: 209
diff changeset
   517
    apply(rule someI2_ex)
580e06329171 just test
urbanc
parents: 209
diff changeset
   518
    apply(rule_tac x="x" in exI) 
580e06329171 just test
urbanc
parents: 209
diff changeset
   519
    apply(simp add: substring_refl)
580e06329171 just test
urbanc
parents: 209
diff changeset
   520
    apply(auto)[1]
580e06329171 just test
urbanc
parents: 209
diff changeset
   521
    using b
580e06329171 just test
urbanc
parents: 209
diff changeset
   522
    apply -
580e06329171 just test
urbanc
parents: 209
diff changeset
   523
    sorry
580e06329171 just test
urbanc
parents: 209
diff changeset
   524
  ultimately
580e06329171 just test
urbanc
parents: 209
diff changeset
   525
  have "\<exists>S. finite S \<and> (\<forall>y. y \<in> Y  = (\<forall>z \<in> S. \<not>(z \<preceq> y)))"
580e06329171 just test
urbanc
parents: 209
diff changeset
   526
    apply -
580e06329171 just test
urbanc
parents: 209
diff changeset
   527
    apply(rule_tac x="C" in exI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   528
    apply(simp)
580e06329171 just test
urbanc
parents: 209
diff changeset
   529
    apply(rule allI)
580e06329171 just test
urbanc
parents: 209
diff changeset
   530
    apply(rule iffI) 
580e06329171 just test
urbanc
parents: 209
diff changeset
   531
    apply(drule_tac x="y" in spec)
580e06329171 just test
urbanc
parents: 209
diff changeset
   532
    apply(simp add: Ybar_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   533
    apply(simp add: Ybar_def)
580e06329171 just test
urbanc
parents: 209
diff changeset
   534
    apply(case_tac "y \<in> Y")
580e06329171 just test
urbanc
parents: 209
diff changeset
   535
    apply(simp)
580e06329171 just test
urbanc
parents: 209
diff changeset
   536
    apply(drule_tac x="y" in spec)
580e06329171 just test
urbanc
parents: 209
diff changeset
   537
    apply(simp)
580e06329171 just test
urbanc
parents: 209
diff changeset
   538
    done
580e06329171 just test
urbanc
parents: 209
diff changeset
   539
    then show ?thesis
580e06329171 just test
urbanc
parents: 209
diff changeset
   540
    by (auto)
580e06329171 just test
urbanc
parents: 209
diff changeset
   541
qed      
580e06329171 just test
urbanc
parents: 209
diff changeset
   542
580e06329171 just test
urbanc
parents: 209
diff changeset
   543
580e06329171 just test
urbanc
parents: 209
diff changeset
   544
thm higman_idx
580e06329171 just test
urbanc
parents: 209
diff changeset
   545
209
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   546
text {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   547
Weak version: only yield sequence containing words
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   548
that can be embedded into each other.
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   549
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   550
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   551
theorem good_prefix_lemma:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   552
  assumes bar: "bar ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   553
  shows "is_prefix ws f \<Longrightarrow> \<exists>vs. is_prefix vs f \<and> good vs" using bar
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   554
proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   555
  case bar1
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   556
  thus ?case by iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   557
next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   558
  case (bar2 ws)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   559
  from bar2.prems have "is_prefix (f (length ws) # ws) f" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   560
  thus ?case by (iprover intro: bar2)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   561
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   562
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   563
theorem good_prefix: "\<exists>vs. is_prefix vs f \<and> good vs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   564
  using higman
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   565
  by (rule good_prefix_lemma) simp+
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   566
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   567
subsection {* Extracting the program *}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   568
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   569
declare R.induct [ind_realizer]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   570
declare T.induct [ind_realizer]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   571
declare L.induct [ind_realizer]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   572
declare good.induct [ind_realizer]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   573
declare bar.induct [ind_realizer]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   574
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   575
extract higman_idx
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   576
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   577
text {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   578
  Program extracted from the proof of @{text higman_idx}:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   579
  @{thm [display] higman_idx_def [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   580
  Corresponding correctness theorem:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   581
  @{thm [display] higman_idx_correctness [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   582
  Program extracted from the proof of @{text higman}:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   583
  @{thm [display] higman_def [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   584
  Program extracted from the proof of @{text prop1}:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   585
  @{thm [display] prop1_def [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   586
  Program extracted from the proof of @{text prop2}:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   587
  @{thm [display] prop2_def [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   588
  Program extracted from the proof of @{text prop3}:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   589
  @{thm [display] prop3_def [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   590
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   591
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   592
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   593
subsection {* Some examples *}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   594
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   595
instantiation LT and TT :: default
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   596
begin
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   597
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   598
definition "default = L0 [] []"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   599
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   600
definition "default = T0 A [] [] [] R0"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   601
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   602
instance ..
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   603
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   604
end
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   605
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   606
function mk_word_aux :: "nat \<Rightarrow> Random.seed \<Rightarrow> letter list \<times> Random.seed" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   607
  "mk_word_aux k = exec {
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   608
     i \<leftarrow> Random.range 10;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   609
     (if i > 7 \<and> k > 2 \<or> k > 1000 then Pair []
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   610
     else exec {
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   611
       let l = (if i mod 2 = 0 then A else B);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   612
       ls \<leftarrow> mk_word_aux (Suc k);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   613
       Pair (l # ls)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   614
     })}"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   615
by pat_completeness auto
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   616
termination by (relation "measure ((op -) 1001)") auto
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   617
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   618
definition mk_word :: "Random.seed \<Rightarrow> letter list \<times> Random.seed" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   619
  "mk_word = mk_word_aux 0"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   620
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   621
primrec mk_word_s :: "nat \<Rightarrow> Random.seed \<Rightarrow> letter list \<times> Random.seed" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   622
  "mk_word_s 0 = mk_word"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   623
  | "mk_word_s (Suc n) = exec {
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   624
       _ \<leftarrow> mk_word;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   625
       mk_word_s n
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   626
     }"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   627
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   628
definition g1 :: "nat \<Rightarrow> letter list" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   629
  "g1 s = fst (mk_word_s s (20000, 1))"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   630
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   631
definition g2 :: "nat \<Rightarrow> letter list" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   632
  "g2 s = fst (mk_word_s s (50000, 1))"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   633
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   634
fun f1 :: "nat \<Rightarrow> letter list" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   635
  "f1 0 = [A, A]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   636
  | "f1 (Suc 0) = [B]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   637
  | "f1 (Suc (Suc 0)) = [A, B]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   638
  | "f1 _ = []"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   639
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   640
fun f2 :: "nat \<Rightarrow> letter list" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   641
  "f2 0 = [A, A]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   642
  | "f2 (Suc 0) = [B]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   643
  | "f2 (Suc (Suc 0)) = [B, A]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   644
  | "f2 _ = []"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   645
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   646
ML {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   647
local
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   648
  val higman_idx = @{code higman_idx};
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   649
  val g1 = @{code g1};
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   650
  val g2 = @{code g2};
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   651
  val f1 = @{code f1};
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   652
  val f2 = @{code f2};
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   653
in
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   654
  val (i1, j1) = higman_idx g1;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   655
  val (v1, w1) = (g1 i1, g1 j1);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   656
  val (i2, j2) = higman_idx g2;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   657
  val (v2, w2) = (g2 i2, g2 j2);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   658
  val (i3, j3) = higman_idx f1;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   659
  val (v3, w3) = (f1 i3, f1 j3);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   660
  val (i4, j4) = higman_idx f2;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   661
  val (v4, w4) = (f2 i4, f2 j4);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   662
end;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   663
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   664
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   665
text {* The same story with the legacy SML code generator,
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   666
this can be removed once the code generator is removed. *}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   667
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   668
code_module Higman
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   669
contains
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   670
  higman = higman_idx
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   671
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   672
ML {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   673
local open Higman in
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   674
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   675
val a = 16807.0;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   676
val m = 2147483647.0;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   677
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   678
fun nextRand seed =
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   679
  let val t = a*seed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   680
  in  t - m * real (Real.floor(t/m)) end;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   681
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   682
fun mk_word seed l =
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   683
  let
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   684
    val r = nextRand seed;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   685
    val i = Real.round (r / m * 10.0);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   686
  in if i > 7 andalso l > 2 then (r, []) else
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   687
    apsnd (cons (if i mod 2 = 0 then A else B)) (mk_word r (l+1))
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   688
  end;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   689
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   690
fun f s zero = mk_word s 0
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   691
  | f s (Suc n) = f (fst (mk_word s 0)) n;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   692
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   693
val g1 = snd o (f 20000.0);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   694
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   695
val g2 = snd o (f 50000.0);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   696
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   697
fun f1 zero = [A,A]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   698
  | f1 (Suc zero) = [B]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   699
  | f1 (Suc (Suc zero)) = [A,B]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   700
  | f1 _ = [];
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   701
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   702
fun f2 zero = [A,A]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   703
  | f2 (Suc zero) = [B]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   704
  | f2 (Suc (Suc zero)) = [B,A]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   705
  | f2 _ = [];
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   706
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   707
val (i1, j1) = higman g1;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   708
val (v1, w1) = (g1 i1, g1 j1);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   709
val (i2, j2) = higman g2;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   710
val (v2, w2) = (g2 i2, g2 j2);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   711
val (i3, j3) = higman f1;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   712
val (v3, w3) = (f1 i3, f1 j3);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   713
val (i4, j4) = higman f2;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   714
val (v4, w4) = (f2 i4, f2 j4);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   715
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   716
end;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   717
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   718
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   719
end