Higman2.thy
author urbanc
Tue, 23 Aug 2011 12:36:43 +0000
changeset 209 300198795eb4
child 210 580e06329171
permissions -rw-r--r--
added test for Higman's lemma
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
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
     9
imports Closures
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
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   236
inductive substring ("_ \<preceq> _")
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   237
where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   238
  "[] \<preceq> y"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   239
| "x \<preceq> y \<Longrightarrow> c # x \<preceq> y"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   240
| "x \<preceq> y \<Longrightarrow> c # x \<preceq> c # y" 
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   241
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   242
lemma substring_refl:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   243
  "x \<preceq> x"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   244
apply(induct x)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   245
apply(auto intro: substring.intros)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   246
done
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   247
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   248
definition
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   249
 "SUBSEQ C \<equiv> {x. \<exists>y \<in> C. x \<preceq> y}"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   250
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   251
lemma
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   252
 "SUBSEQ (SUBSEQ C) = SUBSEQ C"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   253
unfolding SUBSEQ_def
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   254
apply(auto)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   255
apply(erule substring.induct)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   256
apply(rule_tac x="xb" in bexI)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   257
apply(rule substring.intros)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   258
apply(simp)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   259
apply(erule bexE)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   260
apply(rule_tac x="ya" in bexI)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   261
apply(rule substring.intros)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   262
apply(auto)[2]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   263
apply(erule bexE)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   264
apply(rule_tac x="ya" in bexI)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   265
apply(rule substring.intros)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   266
apply(auto)[2]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   267
apply(rule_tac x="x" in exI)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   268
apply(rule conjI)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   269
apply(rule_tac x="y" in bexI)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   270
apply(auto)[2]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   271
apply(rule substring_refl)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   272
done
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   273
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   274
lemma
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   275
  "x \<in> SUBSEQ C \<and> y \<preceq> x \<Longrightarrow> y \<in> SUBSEQ C"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   276
unfolding SUBSEQ_def
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   277
apply(auto)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   278
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   279
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   280
definition
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   281
 "CLOSED C \<equiv> C = SUBSEQ C"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   282
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   283
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   284
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   285
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   286
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   287
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   288
primrec
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   289
  is_prefix :: "'a list \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   290
where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   291
    "is_prefix [] f = True"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   292
  | "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
   293
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   294
theorem L_idx:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   295
  assumes L: "L w ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   296
  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
   297
proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   298
  case (L0 v ws)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   299
  hence "emb (f (length ws)) w" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   300
  moreover have "length ws < length (v # ws)" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   301
  ultimately show ?case by iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   302
next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   303
  case (L1 ws v)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   304
  then obtain i where emb: "emb (f i) w" and "i < length ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   305
    by simp iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   306
  hence "i < length (v # ws)" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   307
  with emb show ?case by iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   308
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   309
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   310
theorem good_idx:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   311
  assumes good: "good ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   312
  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
   313
proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   314
  case (good0 w ws)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   315
  hence "w = f (length ws)" and "is_prefix ws f" by simp_all
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   316
  with good0 show ?case by (iprover dest: L_idx)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   317
next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   318
  case (good1 ws w)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   319
  thus ?case by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   320
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   321
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   322
theorem bar_idx:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   323
  assumes bar: "bar ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   324
  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
   325
proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   326
  case (bar1 ws)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   327
  thus ?case by (rule good_idx)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   328
next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   329
  case (bar2 ws)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   330
  hence "is_prefix (f (length ws) # ws) f" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   331
  thus ?case by (rule bar2)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   332
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   333
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   334
text {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   335
Strong version: yields indices of words that can be embedded into each other.
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   336
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   337
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   338
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
   339
proof (rule bar_idx)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   340
  show "bar []" by (rule higman)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   341
  show "is_prefix [] f" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   342
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   343
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   344
text {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   345
Weak version: only yield sequence containing words
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   346
that can be embedded into each other.
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   347
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   348
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   349
theorem good_prefix_lemma:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   350
  assumes bar: "bar ws"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   351
  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
   352
proof induct
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   353
  case bar1
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   354
  thus ?case by iprover
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   355
next
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   356
  case (bar2 ws)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   357
  from bar2.prems have "is_prefix (f (length ws) # ws) f" by simp
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   358
  thus ?case by (iprover intro: bar2)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   359
qed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   360
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   361
theorem good_prefix: "\<exists>vs. is_prefix vs f \<and> good vs"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   362
  using higman
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   363
  by (rule good_prefix_lemma) simp+
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   364
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   365
subsection {* Extracting the program *}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   366
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   367
declare R.induct [ind_realizer]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   368
declare T.induct [ind_realizer]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   369
declare L.induct [ind_realizer]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   370
declare good.induct [ind_realizer]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   371
declare bar.induct [ind_realizer]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   372
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   373
extract higman_idx
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   374
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   375
text {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   376
  Program extracted from the proof of @{text higman_idx}:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   377
  @{thm [display] higman_idx_def [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   378
  Corresponding correctness theorem:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   379
  @{thm [display] higman_idx_correctness [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   380
  Program extracted from the proof of @{text higman}:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   381
  @{thm [display] higman_def [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   382
  Program extracted from the proof of @{text prop1}:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   383
  @{thm [display] prop1_def [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   384
  Program extracted from the proof of @{text prop2}:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   385
  @{thm [display] prop2_def [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   386
  Program extracted from the proof of @{text prop3}:
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   387
  @{thm [display] prop3_def [no_vars]}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   388
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   389
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   390
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   391
subsection {* Some examples *}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   392
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   393
instantiation LT and TT :: default
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   394
begin
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   395
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   396
definition "default = L0 [] []"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   397
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   398
definition "default = T0 A [] [] [] R0"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   399
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   400
instance ..
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   401
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   402
end
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   403
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   404
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
   405
  "mk_word_aux k = exec {
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   406
     i \<leftarrow> Random.range 10;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   407
     (if i > 7 \<and> k > 2 \<or> k > 1000 then Pair []
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   408
     else exec {
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   409
       let l = (if i mod 2 = 0 then A else B);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   410
       ls \<leftarrow> mk_word_aux (Suc k);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   411
       Pair (l # ls)
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   412
     })}"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   413
by pat_completeness auto
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   414
termination by (relation "measure ((op -) 1001)") auto
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   415
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   416
definition mk_word :: "Random.seed \<Rightarrow> letter list \<times> Random.seed" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   417
  "mk_word = mk_word_aux 0"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   418
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   419
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
   420
  "mk_word_s 0 = mk_word"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   421
  | "mk_word_s (Suc n) = exec {
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   422
       _ \<leftarrow> mk_word;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   423
       mk_word_s n
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   424
     }"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   425
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   426
definition g1 :: "nat \<Rightarrow> letter list" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   427
  "g1 s = fst (mk_word_s s (20000, 1))"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   428
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   429
definition g2 :: "nat \<Rightarrow> letter list" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   430
  "g2 s = fst (mk_word_s s (50000, 1))"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   431
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   432
fun f1 :: "nat \<Rightarrow> letter list" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   433
  "f1 0 = [A, A]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   434
  | "f1 (Suc 0) = [B]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   435
  | "f1 (Suc (Suc 0)) = [A, B]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   436
  | "f1 _ = []"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   437
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   438
fun f2 :: "nat \<Rightarrow> letter list" where
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   439
  "f2 0 = [A, A]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   440
  | "f2 (Suc 0) = [B]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   441
  | "f2 (Suc (Suc 0)) = [B, A]"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   442
  | "f2 _ = []"
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   443
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   444
ML {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   445
local
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   446
  val higman_idx = @{code higman_idx};
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   447
  val g1 = @{code g1};
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   448
  val g2 = @{code g2};
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   449
  val f1 = @{code f1};
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   450
  val f2 = @{code f2};
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   451
in
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   452
  val (i1, j1) = higman_idx g1;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   453
  val (v1, w1) = (g1 i1, g1 j1);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   454
  val (i2, j2) = higman_idx g2;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   455
  val (v2, w2) = (g2 i2, g2 j2);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   456
  val (i3, j3) = higman_idx f1;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   457
  val (v3, w3) = (f1 i3, f1 j3);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   458
  val (i4, j4) = higman_idx f2;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   459
  val (v4, w4) = (f2 i4, f2 j4);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   460
end;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   461
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   462
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   463
text {* The same story with the legacy SML code generator,
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   464
this can be removed once the code generator is removed. *}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   465
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   466
code_module Higman
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   467
contains
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   468
  higman = higman_idx
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   469
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   470
ML {*
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   471
local open Higman in
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   472
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   473
val a = 16807.0;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   474
val m = 2147483647.0;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   475
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   476
fun nextRand seed =
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   477
  let val t = a*seed
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   478
  in  t - m * real (Real.floor(t/m)) end;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   479
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   480
fun mk_word seed l =
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   481
  let
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   482
    val r = nextRand seed;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   483
    val i = Real.round (r / m * 10.0);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   484
  in if i > 7 andalso l > 2 then (r, []) else
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   485
    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
   486
  end;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   487
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   488
fun f s zero = mk_word s 0
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   489
  | f s (Suc n) = f (fst (mk_word s 0)) n;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   490
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   491
val g1 = snd o (f 20000.0);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   492
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   493
val g2 = snd o (f 50000.0);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   494
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   495
fun f1 zero = [A,A]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   496
  | f1 (Suc zero) = [B]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   497
  | f1 (Suc (Suc zero)) = [A,B]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   498
  | f1 _ = [];
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   499
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   500
fun f2 zero = [A,A]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   501
  | f2 (Suc zero) = [B]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   502
  | f2 (Suc (Suc zero)) = [B,A]
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   503
  | f2 _ = [];
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   504
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   505
val (i1, j1) = higman g1;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   506
val (v1, w1) = (g1 i1, g1 j1);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   507
val (i2, j2) = higman g2;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   508
val (v2, w2) = (g2 i2, g2 j2);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   509
val (i3, j3) = higman f1;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   510
val (v3, w3) = (f1 i3, f1 j3);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   511
val (i4, j4) = higman f2;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   512
val (v4, w4) = (f2 i4, f2 j4);
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   513
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   514
end;
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   515
*}
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   516
300198795eb4 added test for Higman's lemma
urbanc
parents:
diff changeset
   517
end