282 |
282 |
283 |
283 |
284 subsection \<open>Longest Common Prefix\<close> |
284 subsection \<open>Longest Common Prefix\<close> |
285 |
285 |
286 definition Longest_common_prefix :: "'a list set \<Rightarrow> 'a list" where |
286 definition Longest_common_prefix :: "'a list set \<Rightarrow> 'a list" where |
287 "Longest_common_prefix L = (GREATEST ps WRT length. \<forall>xs \<in> L. prefix ps xs)" |
287 "Longest_common_prefix L = (ARG_MAX length ps. \<forall>xs \<in> L. prefix ps xs)" |
288 |
288 |
289 lemma Longest_common_prefix_ex: "L \<noteq> {} \<Longrightarrow> |
289 lemma Longest_common_prefix_ex: "L \<noteq> {} \<Longrightarrow> |
290 \<exists>ps. (\<forall>xs \<in> L. prefix ps xs) \<and> (\<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps)" |
290 \<exists>ps. (\<forall>xs \<in> L. prefix ps xs) \<and> (\<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps)" |
291 (is "_ \<Longrightarrow> \<exists>ps. ?P L ps") |
291 (is "_ \<Longrightarrow> \<exists>ps. ?P L ps") |
292 proof(induction "LEAST n. \<exists>xs \<in>L. n = length xs" arbitrary: L) |
292 proof(induction "LEAST n. \<exists>xs \<in>L. n = length xs" arbitrary: L) |
337 |
337 |
338 lemma Longest_common_prefix_eq: |
338 lemma Longest_common_prefix_eq: |
339 "\<lbrakk> L \<noteq> {}; \<forall>xs \<in> L. prefix ps xs; |
339 "\<lbrakk> L \<noteq> {}; \<forall>xs \<in> L. prefix ps xs; |
340 \<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps \<rbrakk> |
340 \<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps \<rbrakk> |
341 \<Longrightarrow> Longest_common_prefix L = ps" |
341 \<Longrightarrow> Longest_common_prefix L = ps" |
342 unfolding Longest_common_prefix_def GreatestM_def |
342 unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder |
343 by(rule some1_equality[OF Longest_common_prefix_unique]) auto |
343 by(rule some1_equality[OF Longest_common_prefix_unique]) auto |
344 |
344 |
345 lemma Longest_common_prefix_prefix: |
345 lemma Longest_common_prefix_prefix: |
346 "xs \<in> L \<Longrightarrow> prefix (Longest_common_prefix L) xs" |
346 "xs \<in> L \<Longrightarrow> prefix (Longest_common_prefix L) xs" |
347 unfolding Longest_common_prefix_def GreatestM_def |
347 unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder |
348 by(rule someI2_ex[OF Longest_common_prefix_ex]) auto |
348 by(rule someI2_ex[OF Longest_common_prefix_ex]) auto |
349 |
349 |
350 lemma Longest_common_prefix_longest: |
350 lemma Longest_common_prefix_longest: |
351 "L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> length ps \<le> length(Longest_common_prefix L)" |
351 "L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> length ps \<le> length(Longest_common_prefix L)" |
352 unfolding Longest_common_prefix_def GreatestM_def |
352 unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder |
353 by(rule someI2_ex[OF Longest_common_prefix_ex]) auto |
353 by(rule someI2_ex[OF Longest_common_prefix_ex]) auto |
354 |
354 |
355 lemma Longest_common_prefix_max_prefix: |
355 lemma Longest_common_prefix_max_prefix: |
356 "L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> prefix ps (Longest_common_prefix L)" |
356 "L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> prefix ps (Longest_common_prefix L)" |
357 by(metis Longest_common_prefix_prefix Longest_common_prefix_longest |
357 by(metis Longest_common_prefix_prefix Longest_common_prefix_longest |