src/HOL/Library/Omega_Words_Fun.thy
author wenzelm
Mon Dec 28 01:28:28 2015 +0100 (2015-12-28)
changeset 61945 1135b8de26c3
parent 61585 a9599d3d7610
child 63040 eb4ddd18d635
permissions -rw-r--r--
more symbols;
wenzelm@61189
     1
(*
wenzelm@61189
     2
    Author:     Stefan Merz
wenzelm@61189
     3
    Author:     Salomon Sickert
wenzelm@61189
     4
    Author:     Julian Brunner
wenzelm@61189
     5
    Author:     Peter Lammich
lammich@61178
     6
*)
wenzelm@61189
     7
wenzelm@61189
     8
section \<open>$\omega$-words\<close>
wenzelm@61189
     9
lammich@61178
    10
theory Omega_Words_Fun
wenzelm@61189
    11
wenzelm@61189
    12
imports Infinite_Set
lammich@61178
    13
begin
lammich@61178
    14
wenzelm@61189
    15
text \<open>Note: This theory is based on Stefan Merz's work.\<close>
wenzelm@61189
    16
wenzelm@61189
    17
text \<open>
lammich@61178
    18
  Automata recognize languages, which are sets of words. For the
lammich@61178
    19
  theory of $\omega$-automata, we are mostly interested in
lammich@61178
    20
  $\omega$-words, but it is sometimes useful to reason about
lammich@61178
    21
  finite words, too. We are modeling finite words as lists; this
lammich@61178
    22
  lets us benefit from the existing library. Other formalizations
lammich@61178
    23
  could be investigated, such as representing words as functions
lammich@61178
    24
  whose domains are initial intervals of the natural numbers.
wenzelm@61189
    25
\<close>
wenzelm@61189
    26
lammich@61178
    27
wenzelm@61189
    28
subsection \<open>Type declaration and elementary operations\<close>
lammich@61178
    29
wenzelm@61189
    30
text \<open>
lammich@61178
    31
  We represent $\omega$-words as functions from the natural numbers
lammich@61178
    32
  to the alphabet type. Other possible formalizations include
lammich@61178
    33
  a coinductive definition or a uniform encoding of finite and
lammich@61178
    34
  infinite words, as studied by M\"uller et al.
wenzelm@61189
    35
\<close>
lammich@61178
    36
lammich@61178
    37
type_synonym
lammich@61178
    38
  'a word = "nat \<Rightarrow> 'a"
lammich@61178
    39
wenzelm@61189
    40
text \<open>
lammich@61178
    41
  We can prefix a finite word to an $\omega$-word, and a way
lammich@61178
    42
  to obtain an $\omega$-word from a finite, non-empty word is by
lammich@61178
    43
  $\omega$-iteration.
wenzelm@61189
    44
\<close>
lammich@61178
    45
lammich@61178
    46
definition
wenzelm@61384
    47
  conc :: "['a list, 'a word] \<Rightarrow> 'a word"  (infixr "\<frown>" 65)
wenzelm@61384
    48
  where "w \<frown> x == \<lambda>n. if n < length w then w!n else x (n - length w)"
lammich@61178
    49
lammich@61178
    50
definition
wenzelm@61384
    51
  iter :: "'a list \<Rightarrow> 'a word"  ("(_\<^sup>\<omega>)" [1000])
lammich@61178
    52
  where "iter w == if w = [] then undefined else (\<lambda>n. w!(n mod (length w)))"
lammich@61178
    53
lammich@61178
    54
lemma conc_empty[simp]: "[] \<frown> w = w"
lammich@61178
    55
  unfolding conc_def by auto
lammich@61178
    56
wenzelm@61189
    57
lemma conc_fst[simp]: "n < length w \<Longrightarrow> (w \<frown> x) n = w!n"
wenzelm@61189
    58
  by (simp add: conc_def)
lammich@61178
    59
wenzelm@61189
    60
lemma conc_snd[simp]: "\<not>(n < length w) \<Longrightarrow> (w \<frown> x) n = x (n - length w)"
wenzelm@61189
    61
  by (simp add: conc_def)
lammich@61178
    62
wenzelm@61189
    63
lemma iter_nth [simp]: "0 < length w \<Longrightarrow> w\<^sup>\<omega> n = w!(n mod (length w))"
wenzelm@61189
    64
  by (simp add: iter_def)
lammich@61178
    65
wenzelm@61189
    66
lemma conc_conc[simp]: "u \<frown> v \<frown> w = (u @ v) \<frown> w" (is "?lhs = ?rhs")
lammich@61178
    67
proof
lammich@61178
    68
  fix n
lammich@61178
    69
  have u: "n < length u \<Longrightarrow> ?lhs n = ?rhs n"
lammich@61178
    70
    by (simp add: conc_def nth_append)
lammich@61178
    71
  have v: "\<lbrakk> \<not>(n < length u); n < length u + length v \<rbrakk> \<Longrightarrow> ?lhs n = ?rhs n"
lammich@61178
    72
    by (simp add: conc_def nth_append, arith)
lammich@61178
    73
  have w: "\<not>(n < length u + length v) \<Longrightarrow> ?lhs n = ?rhs n"
lammich@61178
    74
    by (simp add: conc_def nth_append, arith)
lammich@61178
    75
  from u v w show "?lhs n = ?rhs n" by blast
lammich@61178
    76
qed
lammich@61178
    77
lammich@61178
    78
lemma range_conc[simp]: "range (w\<^sub>1 \<frown> w\<^sub>2) = set w\<^sub>1 \<union> range w\<^sub>2"
lammich@61178
    79
proof (intro equalityI subsetI)
wenzelm@61189
    80
  fix a
wenzelm@61189
    81
  assume "a \<in> range (w\<^sub>1 \<frown> w\<^sub>2)"
wenzelm@61189
    82
  then obtain i where 1: "a = (w\<^sub>1 \<frown> w\<^sub>2) i" by auto
wenzelm@61189
    83
  then show "a \<in> set w\<^sub>1 \<union> range w\<^sub>2"
wenzelm@61189
    84
    unfolding 1 by (cases "i < length w\<^sub>1") simp_all
lammich@61178
    85
next
wenzelm@61189
    86
  fix a
wenzelm@61189
    87
  assume a: "a \<in> set w\<^sub>1 \<union> range w\<^sub>2"
wenzelm@61189
    88
  then show "a \<in> range (w\<^sub>1 \<frown> w\<^sub>2)"
lammich@61178
    89
  proof
wenzelm@61189
    90
    assume "a \<in> set w\<^sub>1"
wenzelm@61189
    91
    then obtain i where 1: "i < length w\<^sub>1" "a = w\<^sub>1 ! i"
wenzelm@61189
    92
      using in_set_conv_nth by metis
lammich@61178
    93
    show ?thesis
lammich@61178
    94
    proof
lammich@61178
    95
      show "a = (w\<^sub>1 \<frown> w\<^sub>2) i" using 1 by auto
lammich@61178
    96
      show "i \<in> UNIV" by rule
lammich@61178
    97
    qed
lammich@61178
    98
  next
wenzelm@61189
    99
    assume "a \<in> range w\<^sub>2"
wenzelm@61189
   100
    then obtain i where 1: "a = w\<^sub>2 i" by auto
lammich@61178
   101
    show ?thesis
lammich@61178
   102
    proof
lammich@61178
   103
      show "a = (w\<^sub>1 \<frown> w\<^sub>2) (length w\<^sub>1 + i)" using 1 by simp
lammich@61178
   104
      show "length w\<^sub>1 + i \<in> UNIV" by rule
lammich@61178
   105
    qed
lammich@61178
   106
  qed
lammich@61178
   107
qed
lammich@61178
   108
lammich@61178
   109
wenzelm@61189
   110
lemma iter_unroll: "0 < length w \<Longrightarrow> w\<^sup>\<omega> = w \<frown> w\<^sup>\<omega>"
wenzelm@61189
   111
  by (rule ext) (simp add: conc_def mod_geq)
wenzelm@61189
   112
lammich@61178
   113
lammich@61178
   114
subsection \<open>Subsequence, Prefix, and Suffix\<close>
wenzelm@61189
   115
wenzelm@61189
   116
definition suffix :: "[nat, 'a word] \<Rightarrow> 'a word"
lammich@61178
   117
  where "suffix k x \<equiv> \<lambda>n. x (k+n)"
lammich@61178
   118
wenzelm@61189
   119
definition subsequence :: "'a word \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a list"  ("_ [_ \<rightarrow> _]" 900)
wenzelm@61189
   120
  where "subsequence w i j \<equiv> map w [i..<j]"
lammich@61178
   121
wenzelm@61189
   122
abbreviation prefix :: "nat \<Rightarrow> 'a word \<Rightarrow> 'a list"
wenzelm@61189
   123
  where "prefix n w \<equiv> subsequence w 0 n"
lammich@61178
   124
wenzelm@61189
   125
lemma suffix_nth [simp]: "(suffix k x) n = x (k+n)"
wenzelm@61189
   126
  by (simp add: suffix_def)
lammich@61178
   127
wenzelm@61189
   128
lemma suffix_0 [simp]: "suffix 0 x = x"
wenzelm@61189
   129
  by (simp add: suffix_def)
lammich@61178
   130
wenzelm@61189
   131
lemma suffix_suffix [simp]: "suffix m (suffix k x) = suffix (k+m) x"
wenzelm@61189
   132
  by (rule ext) (simp add: suffix_def add.assoc)
lammich@61178
   133
wenzelm@61189
   134
lemma subsequence_append: "prefix (i + j) w = prefix i w @ (w [i \<rightarrow> i + j])"
lammich@61178
   135
  unfolding map_append[symmetric] upt_add_eq_append[OF le0] subsequence_def ..
lammich@61178
   136
wenzelm@61189
   137
lemma subsequence_drop[simp]: "drop i (w [j \<rightarrow> k]) = w [j + i \<rightarrow> k]"
lammich@61178
   138
  by (simp add: subsequence_def drop_map)
lammich@61178
   139
wenzelm@61189
   140
lemma subsequence_empty[simp]: "w [i \<rightarrow> j] = [] \<longleftrightarrow> j \<le> i"
wenzelm@61189
   141
  by (auto simp add: subsequence_def)
lammich@61178
   142
wenzelm@61189
   143
lemma subsequence_length[simp]: "length (subsequence w i j) = j - i"
lammich@61178
   144
  by (simp add: subsequence_def)
lammich@61178
   145
wenzelm@61189
   146
lemma subsequence_nth[simp]: "k < j - i \<Longrightarrow> (w [i \<rightarrow> j]) ! k = w (i + k)"
lammich@61178
   147
  unfolding subsequence_def
lammich@61178
   148
  by auto
lammich@61178
   149
wenzelm@61189
   150
lemma subseq_to_zero[simp]: "w[i\<rightarrow>0] = []"
wenzelm@61189
   151
  by simp
wenzelm@61189
   152
wenzelm@61189
   153
lemma subseq_to_smaller[simp]: "i\<ge>j \<Longrightarrow> w[i\<rightarrow>j] = []"
wenzelm@61189
   154
  by simp
wenzelm@61189
   155
wenzelm@61189
   156
lemma subseq_to_Suc[simp]: "i\<le>j \<Longrightarrow> w [i \<rightarrow> Suc j] = w [ i \<rightarrow> j ] @ [w j]"
lammich@61178
   157
  by (auto simp: subsequence_def)
lammich@61178
   158
lammich@61178
   159
lemma subsequence_singleton[simp]: "w [i \<rightarrow> Suc i] = [w i]"
lammich@61178
   160
  by (auto simp: subsequence_def)
lammich@61178
   161
lammich@61178
   162
wenzelm@61189
   163
lemma subsequence_prefix_suffix: "prefix (j - i) (suffix i w) = w [i \<rightarrow> j]"
lammich@61178
   164
proof (cases "i \<le> j")
lammich@61178
   165
  case True
wenzelm@61189
   166
  have "w [i \<rightarrow> j] = map w (map (\<lambda>n. n + i) [0..<j - i])"
wenzelm@61189
   167
    unfolding map_add_upt subsequence_def
wenzelm@61189
   168
    using le_add_diff_inverse2[OF True] by force
wenzelm@61189
   169
  also
wenzelm@61189
   170
  have "\<dots> = map (\<lambda>n. w (n + i)) [0..<j - i]"
wenzelm@61189
   171
    unfolding map_map comp_def by blast
wenzelm@61189
   172
  finally
wenzelm@61189
   173
  show ?thesis
wenzelm@61189
   174
    unfolding subsequence_def suffix_def add.commute[of i] by simp
wenzelm@61189
   175
next
wenzelm@61189
   176
  case False
wenzelm@61189
   177
  then show ?thesis
wenzelm@61189
   178
    by (simp add: subsequence_def)
wenzelm@61189
   179
qed
lammich@61178
   180
wenzelm@61189
   181
lemma prefix_suffix: "x = prefix n x \<frown> (suffix n x)"
wenzelm@61189
   182
  by (rule ext) (simp add: subsequence_def conc_def)
lammich@61178
   183
lammich@61178
   184
declare prefix_suffix[symmetric, simp]
lammich@61178
   185
lammich@61178
   186
wenzelm@61189
   187
lemma word_split: obtains v\<^sub>1 v\<^sub>2 where "v = v\<^sub>1 \<frown> v\<^sub>2" "length v\<^sub>1 = k"
lammich@61178
   188
proof
wenzelm@61189
   189
  show "v = prefix k v \<frown> suffix k v"
wenzelm@61189
   190
    by (rule prefix_suffix)
wenzelm@61189
   191
  show "length (prefix k v) = k"
wenzelm@61189
   192
    by simp
lammich@61178
   193
qed
lammich@61178
   194
lammich@61178
   195
lammich@61178
   196
lemma set_subsequence[simp]: "set (w[i\<rightarrow>j]) = w`{i..<j}"
lammich@61178
   197
  unfolding subsequence_def by auto
lammich@61178
   198
wenzelm@61189
   199
lemma subsequence_take[simp]: "take i (w [j \<rightarrow> k]) = w [j \<rightarrow> min (j + i) k]"
lammich@61178
   200
  by (simp add: subsequence_def take_map min_def)
lammich@61178
   201
wenzelm@61189
   202
lemma subsequence_shift[simp]: "(suffix i w) [j \<rightarrow> k] = w [i + j \<rightarrow> i + k]"
lammich@61178
   203
  by (metis add_diff_cancel_left subsequence_prefix_suffix suffix_suffix)
lammich@61178
   204
lammich@61178
   205
lemma suffix_subseq_join[simp]: "i \<le> j \<Longrightarrow> v [i \<rightarrow> j] \<frown> suffix j v = suffix i v"
wenzelm@61189
   206
  by (metis (no_types, lifting) Nat.add_0_right le_add_diff_inverse prefix_suffix
lammich@61178
   207
    subsequence_shift suffix_suffix)
lammich@61178
   208
lammich@61178
   209
lemma prefix_conc_fst[simp]:
wenzelm@61189
   210
  assumes "j \<le> length w"
lammich@61178
   211
  shows "prefix j (w \<frown> w') = take j w"
lammich@61178
   212
proof -
lammich@61178
   213
  have "\<forall>i < j. (prefix j (w \<frown> w')) ! i = (take j w) ! i"
lammich@61178
   214
    using assms by (simp add: conc_fst subsequence_def)
lammich@61178
   215
  thus ?thesis
lammich@61178
   216
    by (simp add: assms list_eq_iff_nth_eq min.absorb2)
lammich@61178
   217
qed
wenzelm@61189
   218
lammich@61178
   219
lemma prefix_conc_snd[simp]:
lammich@61178
   220
  assumes "n \<ge> length u"
lammich@61178
   221
  shows "prefix n (u \<frown> v) = u @ prefix (n - length u) v"
lammich@61178
   222
proof (intro nth_equalityI allI impI)
wenzelm@61189
   223
  show "length (prefix n (u \<frown> v)) = length (u @ prefix (n - length u) v)"
wenzelm@61189
   224
    using assms by simp
wenzelm@61189
   225
  fix i
wenzelm@61189
   226
  assume "i < length (prefix n (u \<frown> v))"
wenzelm@61189
   227
  then show "prefix n (u \<frown> v) ! i = (u @ prefix (n - length u) v) ! i"
wenzelm@61189
   228
    by (cases "i < length u") (auto simp: nth_append)
lammich@61178
   229
qed
lammich@61178
   230
wenzelm@61189
   231
lemma prefix_conc_length[simp]: "prefix (length w) (w \<frown> w') = w"
lammich@61178
   232
  by simp
lammich@61178
   233
lammich@61178
   234
lemma suffix_conc_fst[simp]:
lammich@61178
   235
  assumes "n \<le> length u"
lammich@61178
   236
  shows "suffix n (u \<frown> v) = drop n u \<frown> v"
lammich@61178
   237
proof
wenzelm@61189
   238
  show "suffix n (u \<frown> v) i = (drop n u \<frown> v) i" for i
wenzelm@61189
   239
    using assms by (cases "n + i < length u") (auto simp: algebra_simps)
lammich@61178
   240
qed
lammich@61178
   241
lammich@61178
   242
lemma suffix_conc_snd[simp]:
lammich@61178
   243
  assumes "n \<ge> length u"
lammich@61178
   244
  shows "suffix n (u \<frown> v) = suffix (n - length u) v"
lammich@61178
   245
proof
wenzelm@61189
   246
  show "suffix n (u \<frown> v) i = suffix (n - length u) v i" for i
wenzelm@61189
   247
    using assms by simp
lammich@61178
   248
qed
lammich@61178
   249
wenzelm@61189
   250
lemma suffix_conc_length[simp]: "suffix (length w) (w \<frown> w') = w'"
lammich@61178
   251
  unfolding conc_def by force
lammich@61178
   252
lammich@61178
   253
lemma concat_eq[iff]:
lammich@61178
   254
  assumes "length v\<^sub>1 = length v\<^sub>2"
lammich@61178
   255
  shows "v\<^sub>1 \<frown> u\<^sub>1 = v\<^sub>2 \<frown> u\<^sub>2 \<longleftrightarrow> v\<^sub>1 = v\<^sub>2 \<and> u\<^sub>1 = u\<^sub>2"
wenzelm@61189
   256
  (is "?lhs \<longleftrightarrow> ?rhs")
lammich@61178
   257
proof
wenzelm@61189
   258
  assume ?lhs
wenzelm@61189
   259
  then have 1: "(v\<^sub>1 \<frown> u\<^sub>1) i = (v\<^sub>2 \<frown> u\<^sub>2) i" for i by auto
wenzelm@61189
   260
  show ?rhs
lammich@61178
   261
  proof (intro conjI ext nth_equalityI allI impI)
wenzelm@61189
   262
    show "length v\<^sub>1 = length v\<^sub>2" by (rule assms(1))
lammich@61178
   263
  next
lammich@61178
   264
    fix i
lammich@61178
   265
    assume 2: "i < length v\<^sub>1"
lammich@61178
   266
    have 3: "i < length v\<^sub>2" using assms(1) 2 by simp
lammich@61178
   267
    show "v\<^sub>1 ! i = v\<^sub>2 ! i" using 1[of i] 2 3 by simp
lammich@61178
   268
  next
wenzelm@61189
   269
    show "u\<^sub>1 i = u\<^sub>2 i" for i
wenzelm@61189
   270
      using 1[of "length v\<^sub>1 + i"] assms(1) by simp
lammich@61178
   271
  qed
lammich@61178
   272
next
wenzelm@61189
   273
  assume ?rhs
wenzelm@61189
   274
  then show ?lhs by simp
lammich@61178
   275
qed
wenzelm@61189
   276
wenzelm@61189
   277
lemma same_concat_eq[iff]: "u \<frown> v = u \<frown> w \<longleftrightarrow> v = w"
wenzelm@61189
   278
  by simp
lammich@61178
   279
lammich@61178
   280
lemma comp_concat[simp]: "f \<circ> u \<frown> v = map f u \<frown> (f \<circ> v)"
lammich@61178
   281
proof
lammich@61178
   282
  fix i
wenzelm@61189
   283
  show "(f \<circ> u \<frown> v) i = (map f u \<frown> (f \<circ> v)) i"
wenzelm@61189
   284
    by (cases "i < length u") simp_all
lammich@61178
   285
qed
lammich@61178
   286
wenzelm@61189
   287
lammich@61178
   288
subsection \<open>Prepending\<close>
lammich@61178
   289
wenzelm@61189
   290
primrec build :: "'a \<Rightarrow> 'a word \<Rightarrow> 'a word"  (infixr "##" 65)
lammich@61178
   291
  where "(a ## w) 0 = a" | "(a ## w) (Suc i) = w i"
lammich@61178
   292
lammich@61178
   293
lemma build_eq[iff]: "a\<^sub>1 ## w\<^sub>1 = a\<^sub>2 ## w\<^sub>2 \<longleftrightarrow> a\<^sub>1 = a\<^sub>2 \<and> w\<^sub>1 = w\<^sub>2"
lammich@61178
   294
proof
lammich@61178
   295
  assume 1: "a\<^sub>1 ## w\<^sub>1 = a\<^sub>2 ## w\<^sub>2"
wenzelm@61189
   296
  have 2: "(a\<^sub>1 ## w\<^sub>1) i = (a\<^sub>2 ## w\<^sub>2) i" for i
wenzelm@61189
   297
    using 1 by auto
lammich@61178
   298
  show "a\<^sub>1 = a\<^sub>2 \<and> w\<^sub>1 = w\<^sub>2"
lammich@61178
   299
  proof (intro conjI ext)
wenzelm@61189
   300
    show "a\<^sub>1 = a\<^sub>2"
wenzelm@61189
   301
      using 2[of "0"] by simp
wenzelm@61189
   302
    show "w\<^sub>1 i = w\<^sub>2 i" for i
wenzelm@61189
   303
      using 2[of "Suc i"] by simp
lammich@61178
   304
  qed
lammich@61178
   305
next
lammich@61178
   306
  assume 1: "a\<^sub>1 = a\<^sub>2 \<and> w\<^sub>1 = w\<^sub>2"
lammich@61178
   307
  show "a\<^sub>1 ## w\<^sub>1 = a\<^sub>2 ## w\<^sub>2" using 1 by simp
lammich@61178
   308
qed
wenzelm@61189
   309
lammich@61178
   310
lemma build_cons[simp]: "(a # u) \<frown> v = a ## u \<frown> v"
lammich@61178
   311
proof
lammich@61178
   312
  fix i
lammich@61178
   313
  show "((a # u) \<frown> v) i = (a ## u \<frown> v) i"
lammich@61178
   314
  proof (cases i)
lammich@61178
   315
    case 0
lammich@61178
   316
    show ?thesis unfolding 0 by simp
lammich@61178
   317
  next
lammich@61178
   318
    case (Suc j)
lammich@61178
   319
    show ?thesis unfolding Suc by (cases "j < length u", simp+)
lammich@61178
   320
  qed
lammich@61178
   321
qed
wenzelm@61189
   322
lammich@61178
   323
lemma build_append[simp]: "(w @ a # u) \<frown> v = w \<frown> a ## u \<frown> v"
lammich@61178
   324
  unfolding conc_conc[symmetric] by simp
wenzelm@61189
   325
lammich@61178
   326
lemma build_first[simp]: "w 0 ## suffix (Suc 0) w = w"
lammich@61178
   327
proof
wenzelm@61189
   328
  show "(w 0 ## suffix (Suc 0) w) i = w i" for i
wenzelm@61189
   329
    by (cases i) simp_all
lammich@61178
   330
qed
wenzelm@61189
   331
wenzelm@61189
   332
lemma build_split[intro]: "w = w 0 ## suffix 1 w"
wenzelm@61189
   333
  by simp
wenzelm@61189
   334
lammich@61178
   335
lemma build_range[simp]: "range (a ## w) = insert a (range w)"
lammich@61178
   336
proof safe
wenzelm@61189
   337
  show "(a ## w) i \<notin> range w \<Longrightarrow> (a ## w) i = a" for i
wenzelm@61189
   338
    by (cases i) auto
lammich@61178
   339
  show "a \<in> range (a ## w)"
lammich@61178
   340
  proof (rule range_eqI)
lammich@61178
   341
    show "a = (a ## w) 0" by simp
lammich@61178
   342
  qed
wenzelm@61189
   343
  show "w i \<in> range (a ## w)" for i
lammich@61178
   344
  proof (rule range_eqI)
lammich@61178
   345
    show "w i = (a ## w) (Suc i)" by simp
lammich@61178
   346
  qed
lammich@61178
   347
qed
lammich@61178
   348
lammich@61178
   349
lemma suffix_singleton_suffix[simp]: "w i ## suffix (Suc i) w = suffix i w"
wenzelm@61189
   350
  using suffix_subseq_join[of i "Suc i" w]
lammich@61178
   351
  by simp
lammich@61178
   352
lammich@61178
   353
text \<open>Find the first occurrence of a letter from a given set\<close>
lammich@61178
   354
lemma word_first_split_set:
lammich@61178
   355
  assumes "A \<inter> range w \<noteq> {}"
wenzelm@61189
   356
  obtains u a v where "w = u \<frown> [a] \<frown> v" "A \<inter> set u = {}" "a \<in> A"
lammich@61178
   357
proof -
lammich@61178
   358
  def i \<equiv> "LEAST i. w i \<in> A"
lammich@61178
   359
  show ?thesis
lammich@61178
   360
  proof
wenzelm@61189
   361
    show "w = prefix i w \<frown> [w i] \<frown> suffix (Suc i) w"
wenzelm@61189
   362
      by simp
lammich@61178
   363
    show "A \<inter> set (prefix i w) = {}"
wenzelm@61189
   364
      apply safe
wenzelm@61189
   365
      subgoal premises prems for a
wenzelm@61189
   366
      proof -
wenzelm@61189
   367
        from prems obtain k where 3: "k < i" "w k = a"
wenzelm@61189
   368
          by auto
wenzelm@61189
   369
        have 4: "w k \<notin> A"
wenzelm@61189
   370
          using not_less_Least 3(1) unfolding i_def .
wenzelm@61189
   371
        show ?thesis
wenzelm@61189
   372
          using prems(1) 3(2) 4 by auto
wenzelm@61189
   373
      qed
wenzelm@61189
   374
      done
wenzelm@61189
   375
    show "w i \<in> A"
wenzelm@61189
   376
      using LeastI assms(1) unfolding i_def by fast
lammich@61178
   377
  qed
lammich@61178
   378
qed
lammich@61178
   379
lammich@61178
   380
wenzelm@61189
   381
subsection \<open>The limit set of an $\omega$-word\<close>
lammich@61178
   382
wenzelm@61189
   383
text \<open>
lammich@61178
   384
  The limit set (also called infinity set) of an $\omega$-word
lammich@61178
   385
  is the set of letters that appear infinitely often in the word.
lammich@61178
   386
  This set plays an important role in defining acceptance conditions
lammich@61178
   387
  of $\omega$-automata.
wenzelm@61189
   388
\<close>
lammich@61178
   389
wenzelm@61189
   390
definition limit :: "'a word \<Rightarrow> 'a set"
wenzelm@61189
   391
  where "limit x \<equiv> {a . \<exists>\<^sub>\<infinity>n . x n = a}"
lammich@61178
   392
wenzelm@61189
   393
lemma limit_iff_frequent: "a \<in> limit x \<longleftrightarrow> (\<exists>\<^sub>\<infinity>n . x n = a)"
wenzelm@61189
   394
  by (simp add: limit_def)
lammich@61178
   395
wenzelm@61189
   396
text \<open>
lammich@61178
   397
  The following is a different way to define the limit,
lammich@61178
   398
  using the reverse image, making the laws about reverse
wenzelm@61189
   399
  image applicable to the limit set.
lammich@61178
   400
  (Might want to change the definition above?)
wenzelm@61189
   401
\<close>
lammich@61178
   402
wenzelm@61189
   403
lemma limit_vimage: "(a \<in> limit x) = infinite (x -` {a})"
wenzelm@61189
   404
  by (simp add: limit_def Inf_many_def vimage_def)
lammich@61178
   405
lammich@61178
   406
lemma two_in_limit_iff:
wenzelm@61189
   407
  "({a, b} \<subseteq> limit x) =
wenzelm@61189
   408
    ((\<exists>n. x n =a ) \<and> (\<forall>n. x n = a \<longrightarrow> (\<exists>m>n. x m = b)) \<and> (\<forall>m. x m = b \<longrightarrow> (\<exists>n>m. x n = a)))"
lammich@61178
   409
  (is "?lhs = (?r1 \<and> ?r2 \<and> ?r3)")
lammich@61178
   410
proof
lammich@61178
   411
  assume lhs: "?lhs"
lammich@61178
   412
  hence 1: "?r1" by (auto simp: limit_def elim: INFM_EX)
lammich@61178
   413
  from lhs have "\<forall>n. \<exists>m>n. x m = b" by (auto simp: limit_def INFM_nat)
lammich@61178
   414
  hence 2: "?r2" by simp
lammich@61178
   415
  from lhs have "\<forall>m. \<exists>n>m. x n = a" by (auto simp: limit_def INFM_nat)
lammich@61178
   416
  hence 3: "?r3" by simp
lammich@61178
   417
  from 1 2 3 show "?r1 \<and> ?r2 \<and> ?r3" by simp
lammich@61178
   418
next
lammich@61178
   419
  assume "?r1 \<and> ?r2 \<and> ?r3"
lammich@61178
   420
  hence 1: "?r1" and 2: "?r2" and 3: "?r3" by simp+
lammich@61178
   421
  have infa: "\<forall>m. \<exists>n\<ge>m. x n = a"
lammich@61178
   422
  proof
lammich@61178
   423
    fix m
lammich@61178
   424
    show "\<exists>n\<ge>m. x n = a" (is "?A m")
lammich@61178
   425
    proof (induct m)
lammich@61178
   426
      from 1 show "?A 0" by simp
lammich@61178
   427
    next
lammich@61178
   428
      fix m
lammich@61178
   429
      assume ih: "?A m"
lammich@61178
   430
      then obtain n where n: "n \<ge> m" "x n = a" by auto
lammich@61178
   431
      with 2 obtain k where k: "k>n" "x k = b" by auto
lammich@61178
   432
      with 3 obtain l where l: "l>k" "x l = a" by auto
lammich@61178
   433
      from n k l have "l \<ge> Suc m" by auto
lammich@61178
   434
      with l show "?A (Suc m)" by auto
lammich@61178
   435
    qed
lammich@61178
   436
  qed
lammich@61178
   437
  hence infa': "\<exists>\<^sub>\<infinity>n. x n = a" by (simp add: INFM_nat_le)
lammich@61178
   438
  have "\<forall>n. \<exists>m>n. x m = b"
lammich@61178
   439
  proof
lammich@61178
   440
    fix n
lammich@61178
   441
    from infa obtain k where k1: "k\<ge>n" and k2: "x k = a" by auto
lammich@61178
   442
    from 2 k2 obtain l where l1: "l>k" and l2: "x l = b" by auto
lammich@61178
   443
    from k1 l1 have "l > n" by auto
lammich@61178
   444
    with l2 show "\<exists>m>n. x m = b" by auto
lammich@61178
   445
  qed
lammich@61178
   446
  hence "\<exists>\<^sub>\<infinity>m. x m = b" by (simp add: INFM_nat)
lammich@61178
   447
  with infa' show "?lhs" by (auto simp: limit_def)
lammich@61178
   448
qed
lammich@61178
   449
wenzelm@61189
   450
text \<open>
lammich@61178
   451
  For $\omega$-words over a finite alphabet, the limit set is
lammich@61178
   452
  non-empty. Moreover, from some position onward, any such word
lammich@61178
   453
  contains only letters from its limit set.
wenzelm@61189
   454
\<close>
lammich@61178
   455
lammich@61178
   456
lemma limit_nonempty:
lammich@61178
   457
  assumes fin: "finite (range x)"
lammich@61178
   458
  shows "\<exists>a. a \<in> limit x"
lammich@61178
   459
proof -
lammich@61178
   460
  from fin obtain a where "a \<in> range x \<and> infinite (x -` {a})"
wenzelm@61189
   461
    by (rule inf_img_fin_domE) auto
lammich@61178
   462
  hence "a \<in> limit x"
lammich@61178
   463
    by (auto simp add: limit_vimage)
lammich@61178
   464
  thus ?thesis ..
lammich@61178
   465
qed
lammich@61178
   466
lammich@61178
   467
lemmas limit_nonemptyE = limit_nonempty[THEN exE]
lammich@61178
   468
lammich@61178
   469
lemma limit_inter_INF:
lammich@61178
   470
  assumes hyp: "limit w \<inter> S \<noteq> {}"
lammich@61178
   471
  shows "\<exists>\<^sub>\<infinity> n. w n \<in> S"
lammich@61178
   472
proof -
lammich@61178
   473
  from hyp obtain x where "\<exists>\<^sub>\<infinity> n. w n = x" and "x \<in> S"
lammich@61178
   474
    by (auto simp add: limit_def)
lammich@61178
   475
  thus ?thesis
lammich@61178
   476
    by (auto elim: INFM_mono)
lammich@61178
   477
qed
lammich@61178
   478
wenzelm@61189
   479
text \<open>
lammich@61178
   480
  The reverse implication is true only if $S$ is finite.
wenzelm@61189
   481
\<close>
lammich@61178
   482
lammich@61178
   483
lemma INF_limit_inter:
wenzelm@61189
   484
  assumes hyp: "\<exists>\<^sub>\<infinity> n. w n \<in>  S"
wenzelm@61189
   485
    and fin: "finite (S \<inter> range w)"
lammich@61178
   486
  shows  "\<exists>a. a \<in> limit w \<inter> S"
lammich@61178
   487
proof (rule ccontr)
lammich@61178
   488
  assume contra: "\<not>(\<exists>a. a \<in> limit w \<inter> S)"
lammich@61178
   489
  hence "\<forall>a\<in>S. finite {n. w n = a}"
lammich@61178
   490
    by (auto simp add: limit_def Inf_many_def)
lammich@61178
   491
  with fin have "finite (UN a:S \<inter> range w. {n. w n = a})"
lammich@61178
   492
    by auto
lammich@61178
   493
  moreover
lammich@61178
   494
  have "(UN a:S \<inter> range w. {n. w n = a}) = {n. w n \<in> S}"
lammich@61178
   495
    by auto
lammich@61178
   496
  moreover
lammich@61178
   497
  note hyp
lammich@61178
   498
  ultimately show "False"
lammich@61178
   499
    by (simp add: Inf_many_def)
lammich@61178
   500
qed
lammich@61178
   501
lammich@61178
   502
lemma fin_ex_inf_eq_limit: "finite A \<Longrightarrow> (\<exists>\<^sub>\<infinity>i. w i \<in> A) \<longleftrightarrow> limit w \<inter> A \<noteq> {}"
lammich@61178
   503
  by (metis INF_limit_inter equals0D finite_Int limit_inter_INF)
lammich@61178
   504
wenzelm@61189
   505
lemma limit_in_range_suffix: "limit x \<subseteq> range (suffix k x)"
lammich@61178
   506
proof
lammich@61178
   507
  fix a
lammich@61178
   508
  assume "a \<in> limit x"
lammich@61178
   509
  then obtain l where
lammich@61178
   510
    kl: "k < l" and xl: "x l = a"
lammich@61178
   511
    by (auto simp add: limit_def INFM_nat)
lammich@61178
   512
  from kl obtain m where "l = k+m"
lammich@61178
   513
    by (auto simp add:  less_iff_Suc_add)
lammich@61178
   514
  with xl show "a \<in> range (suffix k x)"
lammich@61178
   515
    by auto
lammich@61178
   516
qed
lammich@61178
   517
lammich@61178
   518
lemma limit_in_range: "limit r \<subseteq> range r"
lammich@61178
   519
  using limit_in_range_suffix[of r 0] by simp
lammich@61178
   520
lammich@61178
   521
lemmas limit_in_range_suffixD = limit_in_range_suffix[THEN subsetD]
lammich@61178
   522
wenzelm@61189
   523
lemma limit_subset: "limit f \<subseteq> f ` {n..}"
lammich@61178
   524
  using limit_in_range_suffix[of f n] unfolding suffix_def by auto
lammich@61178
   525
lammich@61178
   526
theorem limit_is_suffix:
lammich@61178
   527
  assumes fin: "finite (range x)"
lammich@61178
   528
  shows "\<exists>k. limit x = range (suffix k x)"
lammich@61178
   529
proof -
lammich@61178
   530
  have "\<exists>k. range (suffix k x) \<subseteq> limit x"
lammich@61178
   531
  proof -
wenzelm@61585
   532
    \<comment> "The set of letters that are not in the limit is certainly finite."
lammich@61178
   533
    from fin have "finite (range x - limit x)"
lammich@61178
   534
      by simp
wenzelm@61585
   535
    \<comment> "Moreover, any such letter occurs only finitely often"
lammich@61178
   536
    moreover
lammich@61178
   537
    have "\<forall>a \<in> range x - limit x. finite (x -` {a})"
lammich@61178
   538
      by (auto simp add: limit_vimage)
wenzelm@61585
   539
    \<comment> "Thus, there are only finitely many occurrences of such letters."
lammich@61178
   540
    ultimately have "finite (UN a : range x - limit x. x -` {a})"
lammich@61178
   541
      by (blast intro: finite_UN_I)
wenzelm@61585
   542
    \<comment> "Therefore these occurrences are within some initial interval."
lammich@61178
   543
    then obtain k where "(UN a : range x - limit x. x -` {a}) \<subseteq> {..<k}"
lammich@61178
   544
      by (blast dest: finite_nat_bounded)
wenzelm@61585
   545
    \<comment> "This is just the bound we are looking for."
lammich@61178
   546
    hence "\<forall>m. k \<le> m \<longrightarrow> x m \<in> limit x"
lammich@61178
   547
      by (auto simp add: limit_vimage)
lammich@61178
   548
    hence "range (suffix k x) \<subseteq> limit x"
lammich@61178
   549
      by auto
lammich@61178
   550
    thus ?thesis ..
lammich@61178
   551
  qed
lammich@61178
   552
  then obtain k where "range (suffix k x) \<subseteq> limit x" ..
lammich@61178
   553
  with limit_in_range_suffix
lammich@61178
   554
  have "limit x = range (suffix k x)"
lammich@61178
   555
    by (rule subset_antisym)
lammich@61178
   556
  thus ?thesis ..
lammich@61178
   557
qed
lammich@61178
   558
wenzelm@61337
   559
lemmas limit_is_suffixE = limit_is_suffix[THEN exE]
lammich@61178
   560
lammich@61178
   561
wenzelm@61189
   562
text \<open>
lammich@61178
   563
  The limit set enjoys some simple algebraic laws with respect
lammich@61178
   564
  to concatenation, suffixes, iteration, and renaming.
wenzelm@61189
   565
\<close>
lammich@61178
   566
wenzelm@61189
   567
theorem limit_conc [simp]: "limit (w \<frown> x) = limit x"
lammich@61178
   568
proof (auto)
lammich@61178
   569
  fix a assume a: "a \<in> limit (w \<frown> x)"
lammich@61178
   570
  have "\<forall>m. \<exists>n. m<n \<and> x n = a"
lammich@61178
   571
  proof
lammich@61178
   572
    fix m
lammich@61178
   573
    from a obtain n where "m + length w < n \<and> (w \<frown> x) n = a"
lammich@61178
   574
      by (auto simp add: limit_def Inf_many_def infinite_nat_iff_unbounded)
lammich@61178
   575
    hence "m < n - length w \<and> x (n - length w) = a"
lammich@61178
   576
      by (auto simp add: conc_def)
lammich@61178
   577
    thus "\<exists>n. m<n \<and> x n = a" ..
lammich@61178
   578
  qed
lammich@61178
   579
  hence "infinite {n . x n = a}"
lammich@61178
   580
    by (simp add: infinite_nat_iff_unbounded)
lammich@61178
   581
  thus "a \<in> limit x"
lammich@61178
   582
    by (simp add: limit_def Inf_many_def)
lammich@61178
   583
next
lammich@61178
   584
  fix a assume a: "a \<in> limit x"
lammich@61178
   585
  have "\<forall>m. length w < m \<longrightarrow> (\<exists>n. m<n \<and> (w \<frown> x) n = a)"
lammich@61178
   586
  proof (clarify)
lammich@61178
   587
    fix m
lammich@61178
   588
    assume m: "length w < m"
lammich@61178
   589
    with a obtain n where "m - length w < n \<and> x n = a"
lammich@61178
   590
      by (auto simp add: limit_def Inf_many_def infinite_nat_iff_unbounded)
lammich@61178
   591
    with m have "m < n + length w \<and> (w \<frown> x) (n + length w) = a"
lammich@61178
   592
      by (simp add: conc_def, arith)
lammich@61178
   593
    thus "\<exists>n. m<n \<and> (w \<frown> x) n = a" ..
lammich@61178
   594
  qed
lammich@61178
   595
  hence "infinite {n . (w \<frown> x) n = a}"
lammich@61178
   596
    by (simp add: unbounded_k_infinite)
lammich@61178
   597
  thus "a \<in> limit (w \<frown> x)"
lammich@61178
   598
    by (simp add: limit_def Inf_many_def)
lammich@61178
   599
qed
lammich@61178
   600
wenzelm@61189
   601
theorem limit_suffix [simp]: "limit (suffix n x) = limit x"
lammich@61178
   602
proof -
lammich@61178
   603
  have "x = (prefix n x) \<frown> (suffix n x)"
lammich@61178
   604
    by (simp add: prefix_suffix)
lammich@61178
   605
  hence "limit x = limit (prefix n x \<frown> suffix n x)"
lammich@61178
   606
    by simp
lammich@61178
   607
  also have "\<dots> = limit (suffix n x)"
lammich@61178
   608
    by (rule limit_conc)
lammich@61178
   609
  finally show ?thesis
lammich@61178
   610
    by (rule sym)
lammich@61178
   611
qed
lammich@61178
   612
lammich@61178
   613
theorem limit_iter [simp]:
lammich@61178
   614
  assumes nempty: "0 < length w"
lammich@61178
   615
  shows "limit w\<^sup>\<omega> = set w"
lammich@61178
   616
proof
lammich@61178
   617
  have "limit w\<^sup>\<omega> \<subseteq> range w\<^sup>\<omega>"
lammich@61178
   618
    by (auto simp add: limit_def dest: INFM_EX)
lammich@61178
   619
  also from nempty have "\<dots> \<subseteq> set w"
lammich@61178
   620
    by auto
lammich@61178
   621
  finally show "limit w\<^sup>\<omega> \<subseteq> set w" .
lammich@61178
   622
next
lammich@61178
   623
  {
lammich@61178
   624
    fix a assume a: "a \<in> set w"
lammich@61178
   625
    then obtain k where k: "k < length w \<and> w!k = a"
lammich@61178
   626
      by (auto simp add: set_conv_nth)
wenzelm@61585
   627
    \<comment> "the following bound is terrible, but it simplifies the proof"
wenzelm@61189
   628
    from nempty k have "\<forall>m. w\<^sup>\<omega> ((Suc m)*(length w) + k) = a"
lammich@61178
   629
      by (simp add: mod_add_left_eq)
lammich@61178
   630
    moreover
wenzelm@61585
   631
    \<comment> "why is the following so hard to prove??"
lammich@61178
   632
    have "\<forall>m. m < (Suc m)*(length w) + k"
lammich@61178
   633
    proof
lammich@61178
   634
      fix m
lammich@61178
   635
      from nempty have "1 \<le> length w" by arith
lammich@61178
   636
      hence "m*1 \<le> m*length w" by simp
lammich@61178
   637
      hence "m \<le> m*length w" by simp
lammich@61178
   638
      with nempty have "m < length w + (m*length w) + k" by arith
lammich@61178
   639
      thus "m < (Suc m)*(length w) + k" by simp
lammich@61178
   640
    qed
lammich@61178
   641
    moreover note nempty
lammich@61178
   642
    ultimately have "a \<in> limit w\<^sup>\<omega>"
lammich@61178
   643
      by (auto simp add: limit_iff_frequent INFM_nat)
lammich@61178
   644
  }
lammich@61178
   645
  then show "set w \<subseteq> limit w\<^sup>\<omega>" by auto
lammich@61178
   646
qed
lammich@61178
   647
lammich@61178
   648
lemma limit_o [simp]:
lammich@61178
   649
  assumes a: "a \<in> limit w"
lammich@61178
   650
  shows "f a \<in> limit (f \<circ> w)"
lammich@61178
   651
proof -
lammich@61178
   652
  from a
lammich@61178
   653
  have "\<exists>\<^sub>\<infinity>n. w n = a"
lammich@61178
   654
    by (simp add: limit_iff_frequent)
lammich@61178
   655
  hence "\<exists>\<^sub>\<infinity>n. f (w n) = f a"
lammich@61178
   656
    by (rule INFM_mono, simp)
lammich@61178
   657
  thus "f a \<in> limit (f \<circ> w)"
lammich@61178
   658
    by (simp add: limit_iff_frequent)
lammich@61178
   659
qed
lammich@61178
   660
wenzelm@61189
   661
text \<open>
lammich@61178
   662
  The converse relation is not true in general: $f(a)$ can be in the
lammich@61178
   663
  limit of $f \circ w$ even though $a$ is not in the limit of $w$.
wenzelm@61585
   664
  However, \<open>limit\<close> commutes with renaming if the function is
lammich@61178
   665
  injective. More generally, if $f(a)$ is the image of only finitely
lammich@61178
   666
  many elements, some of these must be in the limit of $w$.
wenzelm@61189
   667
\<close>
lammich@61178
   668
lammich@61178
   669
lemma limit_o_inv:
wenzelm@61189
   670
  assumes fin: "finite (f -` {x})"
wenzelm@61189
   671
    and x: "x \<in> limit (f \<circ> w)"
lammich@61178
   672
  shows "\<exists>a \<in> (f -` {x}). a \<in> limit w"
lammich@61178
   673
proof (rule ccontr)
wenzelm@61189
   674
  assume contra: "\<not> ?thesis"
wenzelm@61585
   675
  \<comment> "hence, every element in the pre-image occurs only finitely often"
lammich@61178
   676
  then have "\<forall>a \<in> (f -` {x}). finite {n. w n = a}"
lammich@61178
   677
    by (simp add: limit_def Inf_many_def)
wenzelm@61585
   678
  \<comment> "so there are only finitely many occurrences of any such element"
lammich@61178
   679
  with fin have "finite (\<Union> a \<in> (f -` {x}). {n. w n = a})"
lammich@61178
   680
    by auto
wenzelm@61585
   681
  \<comment> \<open>these are precisely those positions where $x$ occurs in $f \circ w$\<close>
lammich@61178
   682
  moreover
lammich@61178
   683
  have "(\<Union> a \<in> (f -` {x}). {n. w n = a}) = {n. f(w n) = x}"
lammich@61178
   684
    by auto
lammich@61178
   685
  ultimately
wenzelm@61585
   686
  \<comment> "so $x$ can occur only finitely often in the translated word"
lammich@61178
   687
  have "finite {n. f(w n) = x}"
lammich@61178
   688
    by simp
wenzelm@61585
   689
  \<comment> \<open>\ldots\ which yields a contradiction\<close>
lammich@61178
   690
  with x show "False"
lammich@61178
   691
    by (simp add: limit_def Inf_many_def)
lammich@61178
   692
qed
lammich@61178
   693
lammich@61178
   694
theorem limit_inj [simp]:
lammich@61178
   695
  assumes inj: "inj f"
lammich@61178
   696
  shows "limit (f \<circ> w) = f ` (limit w)"
lammich@61178
   697
proof
lammich@61178
   698
  show "f ` limit w \<subseteq> limit (f \<circ> w)"
lammich@61178
   699
    by auto
lammich@61178
   700
  show "limit (f \<circ> w) \<subseteq> f ` limit w"
lammich@61178
   701
  proof
lammich@61178
   702
    fix x
lammich@61178
   703
    assume x: "x \<in> limit (f \<circ> w)"
lammich@61178
   704
    from inj have "finite (f -` {x})"
lammich@61178
   705
      by (blast intro: finite_vimageI)
lammich@61178
   706
    with x obtain a where a: "a \<in> (f -` {x}) \<and> a \<in> limit w"
lammich@61178
   707
      by (blast dest: limit_o_inv)
lammich@61178
   708
    thus "x \<in> f ` (limit w)"
lammich@61178
   709
      by auto
lammich@61178
   710
  qed
lammich@61178
   711
qed
lammich@61178
   712
lammich@61178
   713
lemma limit_inter_empty:
lammich@61178
   714
  assumes fin: "finite (range w)"
lammich@61178
   715
  assumes hyp: "limit w \<inter> S = {}"
lammich@61178
   716
  shows "\<forall>\<^sub>\<infinity>n. w n \<notin> S"
lammich@61178
   717
proof -
lammich@61178
   718
  from fin obtain k where k_def: "limit w = range (suffix k w)"
lammich@61178
   719
    using limit_is_suffix by blast
wenzelm@61189
   720
  have "w (k + k') \<notin> S" for k'
lammich@61178
   721
    using hyp unfolding k_def suffix_def image_def by blast
lammich@61178
   722
  thus ?thesis
lammich@61178
   723
    unfolding MOST_nat_le using le_Suc_ex by blast
lammich@61178
   724
qed
lammich@61178
   725
lammich@61178
   726
text \<open>If the limit is the suffix of the sequence's range,
lammich@61178
   727
  we may increase the suffix index arbitrarily\<close>
lammich@61178
   728
lemma limit_range_suffix_incr:
lammich@61178
   729
  assumes "limit r = range (suffix i r)"
lammich@61178
   730
  assumes "j\<ge>i"
lammich@61178
   731
  shows "limit r = range (suffix j r)"
wenzelm@61189
   732
    (is "?lhs = ?rhs")
lammich@61178
   733
proof -
lammich@61178
   734
  have "?lhs = range (suffix i r)"
lammich@61178
   735
    using assms by simp
lammich@61178
   736
  moreover
lammich@61178
   737
  have "\<dots> \<supseteq> ?rhs" using \<open>j\<ge>i\<close>
wenzelm@61189
   738
    by (metis (mono_tags, lifting) assms(2)
wenzelm@61189
   739
        image_subsetI le_Suc_ex range_eqI suffix_def suffix_suffix)
lammich@61178
   740
  moreover
wenzelm@61189
   741
  have "\<dots> \<supseteq> ?lhs" by (rule limit_in_range_suffix)
lammich@61178
   742
  ultimately
lammich@61178
   743
  show "?lhs = ?rhs"
lammich@61178
   744
    by (metis antisym_conv limit_in_range_suffix)
lammich@61178
   745
qed
lammich@61178
   746
lammich@61178
   747
text \<open>For two finite sequences, we can find a common suffix index such
lammich@61178
   748
  that the limits can be represented as these suffixes' ranges.\<close>
lammich@61178
   749
lemma common_range_limit:
wenzelm@61189
   750
  assumes "finite (range x)"
wenzelm@61189
   751
    and "finite (range y)"
wenzelm@61189
   752
  obtains i where "limit x = range (suffix i x)"
lammich@61178
   753
    and "limit y = range (suffix i y)"
lammich@61178
   754
proof -
wenzelm@61189
   755
  obtain i j where 1: "limit x = range (suffix i x)"
lammich@61178
   756
    and 2: "limit y = range (suffix j y)"
lammich@61178
   757
    using assms limit_is_suffix by metis
wenzelm@61189
   758
  have "limit x = range (suffix (max i j) x)"
lammich@61178
   759
    and "limit y = range (suffix (max i j) y)"
lammich@61178
   760
    using limit_range_suffix_incr[OF 1] limit_range_suffix_incr[OF 2]
lammich@61178
   761
    by auto
lammich@61178
   762
  thus ?thesis
lammich@61178
   763
    using that by metis
lammich@61178
   764
qed
lammich@61178
   765
lammich@61178
   766
wenzelm@61189
   767
subsection \<open>Index sequences and piecewise definitions\<close>
lammich@61178
   768
wenzelm@61189
   769
text \<open>
lammich@61178
   770
  A word can be defined piecewise: given a sequence of words $w_0, w_1, \ldots$
lammich@61178
   771
  and a strictly increasing sequence of integers $i_0, i_1, \ldots$ where $i_0=0$,
lammich@61178
   772
  a single word is obtained by concatenating subwords of the $w_n$ as given by
lammich@61178
   773
  the integers: the resulting word is
lammich@61178
   774
  \[
lammich@61178
   775
    (w_0)_{i_0} \ldots (w_0)_{i_1-1} (w_1)_{i_1} \ldots (w_1)_{i_2-1} \ldots
lammich@61178
   776
  \]
wenzelm@61189
   777
  We prepare the field by proving some trivial facts about such sequences of
lammich@61178
   778
  indexes.
wenzelm@61189
   779
\<close>
lammich@61178
   780
wenzelm@61189
   781
definition idx_sequence :: "nat word \<Rightarrow> bool"
lammich@61178
   782
  where "idx_sequence idx \<equiv> (idx 0 = 0) \<and> (\<forall>n. idx n < idx (Suc n))"
lammich@61178
   783
lammich@61178
   784
lemma idx_sequence_less:
lammich@61178
   785
  assumes iseq: "idx_sequence idx"
lammich@61178
   786
  shows "idx n < idx (Suc(n+k))"
lammich@61178
   787
proof (induct k)
lammich@61178
   788
  from iseq show "idx n < idx (Suc (n + 0))"
lammich@61178
   789
    by (simp add: idx_sequence_def)
lammich@61178
   790
next
lammich@61178
   791
  fix k
lammich@61178
   792
  assume ih: "idx n < idx (Suc(n+k))"
lammich@61178
   793
  from iseq have "idx (Suc(n+k)) < idx (Suc(n + Suc k))"
lammich@61178
   794
    by (simp add: idx_sequence_def)
lammich@61178
   795
  with ih show "idx n < idx (Suc(n + Suc k))"
lammich@61178
   796
    by (rule less_trans)
lammich@61178
   797
qed
lammich@61178
   798
lammich@61178
   799
lemma idx_sequence_inj:
lammich@61178
   800
  assumes iseq: "idx_sequence idx"
wenzelm@61189
   801
    and eq: "idx m = idx n"
lammich@61178
   802
  shows "m = n"
lammich@61178
   803
proof (rule nat_less_cases)
lammich@61178
   804
  assume "n<m"
lammich@61178
   805
  then obtain k where "m = Suc(n+k)"
lammich@61178
   806
    by (auto simp add: less_iff_Suc_add)
lammich@61178
   807
  with iseq have "idx n < idx m"
lammich@61178
   808
    by (simp add: idx_sequence_less)
lammich@61178
   809
  with eq show ?thesis
lammich@61178
   810
    by simp
lammich@61178
   811
next
lammich@61178
   812
  assume "m<n"
lammich@61178
   813
  then obtain k where "n = Suc(m+k)"
lammich@61178
   814
    by (auto simp add: less_iff_Suc_add)
lammich@61178
   815
  with iseq have "idx m < idx n"
lammich@61178
   816
    by (simp add: idx_sequence_less)
lammich@61178
   817
  with eq show ?thesis
lammich@61178
   818
    by simp
lammich@61178
   819
qed (simp)
lammich@61178
   820
lammich@61178
   821
lemma idx_sequence_mono:
lammich@61178
   822
  assumes iseq: "idx_sequence idx"
wenzelm@61189
   823
    and m: "m \<le> n"
lammich@61178
   824
  shows "idx m \<le> idx n"
lammich@61178
   825
proof (cases "m=n")
lammich@61178
   826
  case True
lammich@61178
   827
  thus ?thesis by simp
lammich@61178
   828
next
lammich@61178
   829
  case False
lammich@61178
   830
  with m have "m < n" by simp
lammich@61178
   831
  then obtain k where "n = Suc(m+k)"
lammich@61178
   832
    by (auto simp add: less_iff_Suc_add)
lammich@61178
   833
  with iseq have "idx m < idx n"
lammich@61178
   834
    by (simp add: idx_sequence_less)
lammich@61178
   835
  thus ?thesis by simp
lammich@61178
   836
qed
lammich@61178
   837
wenzelm@61189
   838
text \<open>
lammich@61178
   839
  Given an index sequence, every natural number is contained in the
lammich@61178
   840
  interval defined by two adjacent indexes, and in fact this interval
lammich@61178
   841
  is determined uniquely.
wenzelm@61189
   842
\<close>
lammich@61178
   843
lammich@61178
   844
lemma idx_sequence_idx:
lammich@61178
   845
  assumes "idx_sequence idx"
lammich@61178
   846
  shows "idx k \<in> {idx k ..< idx (Suc k)}"
lammich@61178
   847
using assms by (auto simp add: idx_sequence_def)
lammich@61178
   848
lammich@61178
   849
lemma idx_sequence_interval:
lammich@61178
   850
  assumes iseq: "idx_sequence idx"
lammich@61178
   851
  shows "\<exists>k. n \<in> {idx k ..< idx (Suc k) }"
lammich@61178
   852
    (is "?P n" is "\<exists>k. ?in n k")
lammich@61178
   853
proof (induct n)
lammich@61178
   854
  from iseq have "0 = idx 0"
lammich@61178
   855
    by (simp add: idx_sequence_def)
lammich@61178
   856
  moreover
lammich@61178
   857
  from iseq have "idx 0 \<in> {idx 0 ..< idx (Suc 0) }"
lammich@61178
   858
    by (rule idx_sequence_idx)
lammich@61178
   859
  ultimately
lammich@61178
   860
  show "?P 0" by auto
lammich@61178
   861
next
lammich@61178
   862
  fix n
lammich@61178
   863
  assume "?P n"
lammich@61178
   864
  then obtain k where k: "?in n k" ..
lammich@61178
   865
  show "?P (Suc n)"
lammich@61178
   866
  proof (cases "Suc n < idx (Suc k)")
lammich@61178
   867
    case True
lammich@61178
   868
    with k have "?in (Suc n) k"
lammich@61178
   869
      by simp
lammich@61178
   870
    thus ?thesis ..
lammich@61178
   871
  next
lammich@61178
   872
    case False
lammich@61178
   873
    with k have "Suc n = idx (Suc k)"
lammich@61178
   874
      by auto
lammich@61178
   875
    with iseq have "?in (Suc n) (Suc k)"
lammich@61178
   876
      by (simp add: idx_sequence_def)
lammich@61178
   877
    thus ?thesis ..
lammich@61178
   878
  qed
lammich@61178
   879
qed
lammich@61178
   880
lammich@61178
   881
lemma idx_sequence_interval_unique:
lammich@61178
   882
  assumes iseq: "idx_sequence idx"
wenzelm@61189
   883
    and k: "n \<in> {idx k ..< idx (Suc k)}"
wenzelm@61189
   884
    and m: "n \<in> {idx m ..< idx (Suc m)}"
lammich@61178
   885
  shows "k = m"
lammich@61178
   886
proof (rule nat_less_cases)
lammich@61178
   887
  assume "k < m"
lammich@61178
   888
  hence "Suc k \<le> m" by simp
lammich@61178
   889
  with iseq have "idx (Suc k) \<le> idx m"
lammich@61178
   890
    by (rule idx_sequence_mono)
lammich@61178
   891
  with m have "idx (Suc k) \<le> n"
lammich@61178
   892
    by auto
lammich@61178
   893
  with k have "False"
lammich@61178
   894
    by simp
lammich@61178
   895
  thus ?thesis ..
lammich@61178
   896
next
lammich@61178
   897
  assume "m < k"
lammich@61178
   898
  hence "Suc m \<le> k" by simp
lammich@61178
   899
  with iseq have "idx (Suc m) \<le> idx k"
lammich@61178
   900
    by (rule idx_sequence_mono)
lammich@61178
   901
  with k have "idx (Suc m) \<le> n"
lammich@61178
   902
    by auto
lammich@61178
   903
  with m have "False"
lammich@61178
   904
    by simp
lammich@61178
   905
  thus ?thesis ..
lammich@61178
   906
qed (simp)
lammich@61178
   907
lammich@61178
   908
lemma idx_sequence_unique_interval:
lammich@61178
   909
  assumes iseq: "idx_sequence idx"
lammich@61178
   910
  shows "\<exists>! k. n \<in> {idx k ..< idx (Suc k) }"
lammich@61178
   911
proof (rule ex_ex1I)
lammich@61178
   912
  from iseq show "\<exists>k. n \<in> {idx k ..< idx (Suc k)}"
lammich@61178
   913
    by (rule idx_sequence_interval)
lammich@61178
   914
next
lammich@61178
   915
  fix k y
lammich@61178
   916
  assume "n \<in> {idx k..<idx (Suc k)}" and "n \<in> {idx y..<idx (Suc y)}"
lammich@61178
   917
  with iseq show "k = y" by (auto elim: idx_sequence_interval_unique)
lammich@61178
   918
qed
lammich@61178
   919
wenzelm@61189
   920
text \<open>
lammich@61178
   921
  Now we can define the piecewise construction of a word using
lammich@61178
   922
  an index sequence.
wenzelm@61189
   923
\<close>
lammich@61178
   924
wenzelm@61189
   925
definition merge :: "'a word word \<Rightarrow> nat word \<Rightarrow> 'a word"
wenzelm@61189
   926
  where "merge ws idx \<equiv> \<lambda>n. let i = THE i. n \<in> {idx i ..< idx (Suc i) } in ws i n"
lammich@61178
   927
lammich@61178
   928
lemma merge:
lammich@61178
   929
  assumes idx: "idx_sequence idx"
wenzelm@61189
   930
    and n: "n \<in> {idx i ..< idx (Suc i)}"
lammich@61178
   931
  shows "merge ws idx n = ws i n"
lammich@61178
   932
proof -
lammich@61178
   933
  from n have "(THE k. n \<in> {idx k ..< idx (Suc k) }) = i"
lammich@61178
   934
    by (rule the_equality[OF _ sym[OF idx_sequence_interval_unique[OF idx n]]]) simp
lammich@61178
   935
  thus ?thesis
lammich@61178
   936
    by (simp add: merge_def Let_def)
lammich@61178
   937
qed
lammich@61178
   938
lammich@61178
   939
lemma merge0:
lammich@61178
   940
  assumes idx: "idx_sequence idx"
lammich@61178
   941
  shows "merge ws idx 0 = ws 0 0"
lammich@61178
   942
proof (rule merge[OF idx])
lammich@61178
   943
  from idx have "idx 0 < idx (Suc 0)"
wenzelm@61189
   944
    unfolding idx_sequence_def by blast
lammich@61178
   945
  with idx show "0 \<in> {idx 0 ..< idx (Suc 0)}"
lammich@61178
   946
    by (simp add: idx_sequence_def)
lammich@61178
   947
qed
lammich@61178
   948
lammich@61178
   949
lemma merge_Suc:
lammich@61178
   950
  assumes idx: "idx_sequence idx"
wenzelm@61189
   951
    and n: "n \<in> {idx i ..< idx (Suc i)}"
wenzelm@61189
   952
  shows "merge ws idx (Suc n) = (if Suc n = idx (Suc i) then ws (Suc i) else ws i) (Suc n)"
wenzelm@61189
   953
proof auto
lammich@61178
   954
  assume eq: "Suc n = idx (Suc i)"
lammich@61178
   955
  from idx have "idx (Suc i) < idx (Suc(Suc i))"
wenzelm@61189
   956
    unfolding idx_sequence_def by blast
lammich@61178
   957
  with eq idx show "merge ws idx (idx (Suc i)) = ws (Suc i) (idx (Suc i))"
lammich@61178
   958
    by (simp add: merge)
lammich@61178
   959
next
lammich@61178
   960
  assume neq: "Suc n \<noteq> idx (Suc i)"
lammich@61178
   961
  with n have "Suc n \<in> {idx i ..< idx (Suc i) }"
lammich@61178
   962
    by auto
lammich@61178
   963
  with idx show "merge ws idx (Suc n) = ws i (Suc n)"
lammich@61178
   964
    by (rule merge)
lammich@61178
   965
qed
lammich@61178
   966
lammich@61178
   967
end