@@ -426,67 +426,78 @@ lemma AList_restrict_in_dom: "AList.restrict (set (filter P (map fst xs))) xs =
426426lemma mk_functional_alist [ code ]:
427427 "mk_functional (set xs) = set (filter (\<lambda> (x,y). length (remdups (map snd (AList.restrict {x} xs))) = 1) xs)"
428428 by ( simp only : mk_functional_single_valued_dom rel_domres_alist single_valued_dom_alist AList_restrict_in_dom )
429-
429+
430430lemma rel_apply_set [ code ]:
431431 "rel_apply (set xs) k =
432432 (let ys = filter (\<lambda> (k', v). k = k') xs in
433433 if (length ys > 0 \<and> ys = replicate (length ys) (hd ys)) then snd (hd ys) else undefined)"
434- proof ( simp add : Let_unfold , safe )
435- let ?ys = "filter (\<lambda>(k', v). k = k') xs"
436- assume ys : "?ys \<noteq> []" "?ys = replicate (length ?ys) (hd ?ys)"
437- have kmem : "\<And> y. (k, y) \<in> set xs \<longleftrightarrow> (k, y) \<in> set ?ys"
438- by simp
439- from ys obtain v where v : "(k, v) \<in> set xs"
440- using hd_in_set by fastforce
441- hence ys' : "?ys = replicate (length ?ys) (k, v)"
442- by ( metis ( mono_tags ) case_prodI filter_set in_set_replicate member_filter ys ( 2 ))
443- hence "snd (hd ?ys) = v"
444- by ( metis hd_replicate replicate_0 snd_conv ys ( 1 ))
445- moreover have "(THE y. (k, y) \<in> set xs) = v"
446- by ( metis ( no_types , lifting ) v in_set_replicate kmem snd_conv the_equality ys' )
447- moreover have "(\<exists>!y. (k, y) \<in> set xs)"
448- by ( metis Pair_inject v in_set_replicate kmem ys' )
449- ultimately show "set xs(k)\<^sub>r = snd (hd ?ys)"
450- by ( simp add : rel_apply_def )
451- next
452- assume "filter (\<lambda>(k', v). k = k') xs = []"
453- hence "\<nexists>v. (k, v) \<in> set xs"
454- by ( metis ( mono_tags , lifting ) case_prodI filter_empty_conv )
455- thus "set xs(k)\<^sub>r = undefined"
456- by ( auto simp add : rel_apply_def )
457- next
434+ proof -
458435 let ?ys = "filter (\<lambda>(k', v). k = k') xs"
459- assume ys : "?ys \<noteq> replicate (length ?ys) (hd ?ys)"
460- have keys : "\<forall> (k', v') \<in> set ?ys. k' = k"
461- by auto
462- show "set xs(k)\<^sub>r = undefined"
463- proof ( cases "length ?ys = 0" )
464- case True
465- then show ?thesis
466- using ys by fastforce
467- next
468- case False
469- hence "length ?ys > 1"
470- by ( metis hd_in_set in_set_conv_nth length_0_conv less_one linorder_neqE_nat replicate_length_same ys )
471- have "fst (hd ?ys) = k"
472- using False hd_in_set by force
473- have "\<not>(\<forall> (k, v) \<in> set ?ys. v = snd (hd ?ys))"
474- proof
475- assume "(\<forall> (k, v) \<in> set ?ys. v = snd (hd ?ys))"
476- hence "(\<forall> p \<in> set ?ys. p = (k, snd (hd ?ys)))"
436+ have 1 : "\<lbrakk> ?ys \<noteq> []; ?ys = replicate (length ?ys) (hd ?ys) \<rbrakk> \<Longrightarrow>
437+ set xs(k)\<^sub>r = snd (hd ?ys)"
438+ proof -
439+ assume ys : "?ys \<noteq> []" "?ys = replicate (length ?ys) (hd ?ys)"
440+ have kmem : "\<And> y. (k, y) \<in> set xs \<longleftrightarrow> (k, y) \<in> set ?ys"
441+ by simp
442+ from ys obtain v where v : "(k, v) \<in> set xs"
443+ using hd_in_set by fastforce
444+ hence ys' : "?ys = replicate (length ?ys) (k, v)"
445+ by ( metis ( mono_tags ) case_prodI filter_set in_set_replicate member_filter ys ( 2 ))
446+ hence "snd (hd ?ys) = v"
447+ by ( metis hd_replicate replicate_0 snd_conv ys ( 1 ))
448+ moreover have "(THE y. (k, y) \<in> set xs) = v"
449+ by ( metis ( no_types , lifting ) v in_set_replicate kmem snd_conv the_equality ys' )
450+ moreover have "(\<exists>!y. (k, y) \<in> set xs)"
451+ by ( metis Pair_inject v in_set_replicate kmem ys' )
452+ ultimately show "set xs(k)\<^sub>r = snd (hd ?ys)"
453+ by ( simp add : rel_apply_def )
454+ qed
455+
456+ have 2 : "?ys = [] \<Longrightarrow> set xs(k)\<^sub>r = undefined"
457+ proof -
458+ assume "filter (\<lambda>(k', v). k = k') xs = []"
459+ hence "\<nexists>v. (k, v) \<in> set xs"
460+ by ( metis ( mono_tags , lifting ) case_prodI filter_empty_conv )
461+ thus "set xs(k)\<^sub>r = undefined"
462+ by ( auto simp add : rel_apply_def )
463+ qed
464+
465+ have 3 : "?ys \<noteq> replicate (length ?ys) (hd ?ys) \<Longrightarrow> set xs(k)\<^sub>r = undefined"
466+ proof -
467+ assume ys : "?ys \<noteq> replicate (length ?ys) (hd ?ys)"
468+ have keys : "\<forall> (k', v') \<in> set ?ys. k' = k"
469+ by auto
470+ show "set xs(k)\<^sub>r = undefined"
471+ proof ( cases "length ?ys = 0" )
472+ case True
473+ then show ?thesis
474+ using ys by fastforce
475+ next
476+ case False
477+ hence "length ?ys > 1"
478+ by ( metis hd_in_set in_set_conv_nth length_0_conv less_one linorder_neqE_nat replicate_length_same ys )
479+ have "fst (hd ?ys) = k"
480+ using False hd_in_set by force
481+ have "\<not>(\<forall> (k, v) \<in> set ?ys. v = snd (hd ?ys))"
482+ proof
483+ assume "(\<forall> (k, v) \<in> set ?ys. v = snd (hd ?ys))"
484+ hence "(\<forall> p \<in> set ?ys. p = (k, snd (hd ?ys)))"
485+ by fastforce
486+ hence "?ys = replicate (length ?ys) (hd ?ys)"
487+ by ( metis False length_0_conv list.set_sel ( 1 ) replicate_length_same )
488+ thus False
489+ using ys by blast
490+ qed
491+ then obtain v where "(k, v) \<in> set ?ys" "v \<noteq> snd (hd ?ys)"
477492 by fastforce
478- hence "?ys = replicate (length ?ys) (hd ?ys )"
479- by ( metis False length_0_conv list.set_sel ( 1 ) replicate_length_same )
480- thus False
481- using ys by blast
493+ hence "(\<not> (\<exists>!y. (k, y) \<in> set xs) )"
494+ using False list.set_sel ( 1 ) by fastforce
495+ then show ?thesis
496+ by ( simp add : rel_apply_def )
482497 qed
483- then obtain v where "(k, v) \<in> set ?ys" "v \<noteq> snd (hd ?ys)"
484- by fastforce
485- hence "(\<not> (\<exists>!y. (k, y) \<in> set xs))"
486- using False list.set_sel ( 1 ) by fastforce
487- then show ?thesis
488- by ( simp add : rel_apply_def )
489498 qed
499+ from 1 2 3 show ?thesis
500+ by ( simp add : Let_unfold )
490501qed
491502
492503end
0 commit comments