# HG changeset patch # User nipkow # Date 1676897982 -3600 # Node ID cc292dafc52735abf523713cbcf7d28b2cd27f2b # Parent aea11797247bac3cbb283159aaf16a6cca3400cc# Parent fa247805669d2c819e8d3fa3bf6f7cdae3dc9a97 merged diff -r aea11797247b -r cc292dafc527 NEWS --- a/NEWS Sun Feb 19 21:21:19 2023 +0000 +++ b/NEWS Mon Feb 20 13:59:42 2023 +0100 @@ -43,8 +43,6 @@ *** HOL *** -* Map.map_of and lemmas moved to List. - * Theory "HOL.Euclidean_Division" renamed to "HOL.Euclidean_Rings"; "euclidean division" typically denotes a particular division on integers. Minor INCOMPATIBILITY. diff -r aea11797247b -r cc292dafc527 src/Doc/Main/Main_Doc.thy --- a/src/Doc/Main/Main_Doc.thy Sun Feb 19 21:21:19 2023 +0000 +++ b/src/Doc/Main/Main_Doc.thy Mon Feb 20 13:59:42 2023 +0100 @@ -543,7 +543,6 @@ \<^const>\List.list_all2\ & \<^typeof>\List.list_all2\\\ \<^const>\List.list_update\ & \<^typeof>\List.list_update\\\ \<^const>\List.map\ & \<^typeof>\List.map\\\ -\<^const>\List.map_of\ & \<^typeof>\List.map_of\\\ \<^const>\List.measures\ & @{term_type_only List.measures "('a\nat)list\('a*'a)set"}\\ \<^const>\List.nth\ & \<^typeof>\List.nth\\\ \<^const>\List.nths\ & \<^typeof>\List.nths\\\ @@ -586,31 +585,32 @@ qualifier \q\<^sub>i\ is either a generator \mbox{\pat \ e\} or a guard, i.e.\ boolean expression. -%\section*{Map} -% -%Maps model partial functions and are often used as finite tables. However, -%the domain of a map may be infinite. -% -%\begin{tabular}{@ {} l @ {~::~} l @ {}} -%\\Map.empty\ & \\Map.empty\\\ -%\\Map.map_add\ & \\Map.map_add\\\ -%\\Map.map_comp\ & \\Map.map_comp\\\ -%\\Map.restrict_map\ & @ {term_type_only Map.restrict_map "('a\'b option)\'a set\('a\'b option)"}\\ -%\\Map.dom\ & @{term_type_only Map.dom "('a\'b option)\'a set"}\\ -%\\Map.ran\ & @{term_type_only Map.ran "('a\'b option)\'b set"}\\ -%\\Map.map_le\ & \\Map.map_le\\\ -%\\Map.map_upds\ & \\Map.map_upds\\\ -%\end{tabular} -% -%\subsubsection*{Syntax} -% -%\begin{tabular}{@ {} l @ {\quad$\equiv$\quad} l @ {}} -%\\Map.empty\ & @ {term[source] "\_. None"}\\ -%\\m(x:=Some y)\ & @ {term[source]"m(x:=Some y)"}\\ -%m(x1\y1,\,xn\yn) & @ {text[source]"m(x1\y1)\(xn\yn)"}\\ -%[x1\y1,\,xn\yn] & @ {text[source]"Map.empty(x1\y1,\,xn\yn)"}\\ -%\\map_upds m xs ys\ & @ {term[source]"map_upds m xs ys"}\\ -%\end{tabular} +\section*{Map} + +Maps model partial functions and are often used as finite tables. However, +the domain of a map may be infinite. + +\begin{tabular}{@ {} l @ {~::~} l @ {}} +\<^const>\Map.empty\ & \<^typeof>\Map.empty\\\ +\<^const>\Map.map_add\ & \<^typeof>\Map.map_add\\\ +\<^const>\Map.map_comp\ & \<^typeof>\Map.map_comp\\\ +\<^const>\Map.restrict_map\ & @{term_type_only Map.restrict_map "('a\'b option)\'a set\('a\'b option)"}\\ +\<^const>\Map.dom\ & @{term_type_only Map.dom "('a\'b option)\'a set"}\\ +\<^const>\Map.ran\ & @{term_type_only Map.ran "('a\'b option)\'b set"}\\ +\<^const>\Map.map_le\ & \<^typeof>\Map.map_le\\\ +\<^const>\Map.map_of\ & \<^typeof>\Map.map_of\\\ +\<^const>\Map.map_upds\ & \<^typeof>\Map.map_upds\\\ +\end{tabular} + +\subsubsection*{Syntax} + +\begin{tabular}{@ {} l @ {\quad$\equiv$\quad} l @ {}} +\<^term>\Map.empty\ & @{term[source] "\_. None"}\\ +\<^term>\m(x:=Some y)\ & @{term[source]"m(x:=Some y)"}\\ +\m(x\<^sub>1\y\<^sub>1,\,x\<^sub>n\y\<^sub>n)\ & @{text[source]"m(x\<^sub>1\y\<^sub>1)\(x\<^sub>n\y\<^sub>n)"}\\ +\[x\<^sub>1\y\<^sub>1,\,x\<^sub>n\y\<^sub>n]\ & @{text[source]"Map.empty(x\<^sub>1\y\<^sub>1,\,x\<^sub>n\y\<^sub>n)"}\\ +\<^term>\map_upds m xs ys\ & @{term[source]"map_upds m xs ys"}\\ +\end{tabular} \section*{Infix operators in Main} % \<^theory>\Main\ diff -r aea11797247b -r cc292dafc527 src/HOL/List.thy --- a/src/HOL/List.thy Sun Feb 19 21:21:19 2023 +0000 +++ b/src/HOL/List.thy Mon Feb 20 13:59:42 2023 +0100 @@ -243,11 +243,6 @@ abbreviation length :: "'a list \ nat" where "length \ size" -primrec map_of :: "('a \ 'b) list \ 'a \ 'b option" -where - "map_of [] = (\x. None)" -| "map_of (p # ps) = (map_of ps)(fst p := Some(snd p))" - definition enumerate :: "nat \ 'a list \ (nat \ 'a) list" where enumerate_eq_zip: "enumerate n xs = zip [n..\<^const>\map_of\\ - -lemma map_of_eq_None_iff: - "(map_of xys x = None) = (x \ fst ` (set xys))" -by (induct xys) simp_all - -lemma map_of_eq_Some_iff [simp]: - "distinct(map fst xys) \ (map_of xys x = Some y) = ((x,y) \ set xys)" -proof (induct xys) - case (Cons xy xys) - then show ?case - by (cases xy) (auto simp flip: map_of_eq_None_iff) -qed auto - -lemma Some_eq_map_of_iff [simp]: - "distinct(map fst xys) \ (Some y = map_of xys x) = ((x,y) \ set xys)" -by (auto simp del: map_of_eq_Some_iff simp: map_of_eq_Some_iff [symmetric]) - -lemma map_of_is_SomeI [simp]: - "\distinct(map fst xys); (x,y) \ set xys\ \ map_of xys x = Some y" - by simp - -lemma map_of_zip_is_None [simp]: - "length xs = length ys \ (map_of (zip xs ys) x = None) = (x \ set xs)" -by (induct rule: list_induct2) simp_all - -lemma map_of_zip_is_Some: - assumes "length xs = length ys" - shows "x \ set xs \ (\y. map_of (zip xs ys) x = Some y)" -using assms by (induct rule: list_induct2) simp_all - -lemma map_of_zip_upd: - fixes x :: 'a and xs :: "'a list" and ys zs :: "'b list" - assumes "length ys = length xs" - and "length zs = length xs" - and "x \ set xs" - and "(map_of (zip xs ys))(x := Some y) = (map_of (zip xs zs))(x := Some z)" - shows "map_of (zip xs ys) = map_of (zip xs zs)" -proof - fix x' :: 'a - show "map_of (zip xs ys) x' = map_of (zip xs zs) x'" - proof (cases "x = x'") - case True - from assms True map_of_zip_is_None [of xs ys x'] - have "map_of (zip xs ys) x' = None" by simp - moreover from assms True map_of_zip_is_None [of xs zs x'] - have "map_of (zip xs zs) x' = None" by simp - ultimately show ?thesis by simp - next - case False from assms - have "((map_of (zip xs ys))(x := Some y)) x' = ((map_of (zip xs zs))(x := Some z)) x'" by auto - with False show ?thesis by simp - qed -qed - -lemma map_of_zip_inject: - assumes "length ys = length xs" - and "length zs = length xs" - and dist: "distinct xs" - and map_of: "map_of (zip xs ys) = map_of (zip xs zs)" - shows "ys = zs" - using assms(1) assms(2)[symmetric] - using dist map_of -proof (induct ys xs zs rule: list_induct3) - case Nil show ?case by simp -next - case (Cons y ys x xs z zs) - from \map_of (zip (x#xs) (y#ys)) = map_of (zip (x#xs) (z#zs))\ - have map_of: "(map_of (zip xs ys))(x := Some y) = (map_of (zip xs zs))(x := Some z)" by simp - from Cons have "length ys = length xs" and "length zs = length xs" - and "x \ set xs" by simp_all - then have "map_of (zip xs ys) = map_of (zip xs zs)" using map_of by (rule map_of_zip_upd) - with Cons.hyps \distinct (x # xs)\ have "ys = zs" by simp - moreover have "y = z" using fun_upd_eqD[OF map_of] by simp - ultimately show ?case by simp -qed - -lemma map_of_zip_nth: - assumes "length xs = length ys" - assumes "distinct xs" - assumes "i < length ys" - shows "map_of (zip xs ys) (xs ! i) = Some (ys ! i)" -using assms proof (induct arbitrary: i rule: list_induct2) - case Nil - then show ?case by simp -next - case (Cons x xs y ys) - then show ?case - using less_Suc_eq_0_disj by auto -qed - -lemma map_of_zip_map: - "map_of (zip xs (map f xs)) = (\x. if x \ set xs then Some (f x) else None)" - by (induct xs) (simp_all add: fun_eq_iff) - -lemma map_of_SomeD: "map_of xs k = Some y \ (k, y) \ set xs" - by (induct xs) (auto split: if_splits) - -lemma map_of_mapk_SomeI: - "inj f \ map_of t k = Some x \ - map_of (map (case_prod (\k. Pair (f k))) t) (f k) = Some x" -by (induct t) (auto simp: inj_eq) - -lemma weak_map_of_SomeI: "(k, x) \ set l \ \x. map_of l k = Some x" -by (induct l) auto - -lemma map_of_filter_in: - "map_of xs k = Some z \ P k z \ map_of (filter (case_prod P) xs) k = Some z" -by (induct xs) auto - -lemma map_of_map: - "map_of (map (\(k, v). (k, f v)) xs) = map_option f \ map_of xs" - by (induct xs) (auto simp: fun_eq_iff) - -lemma map_of_Cons_code [code]: - "map_of [] k = None" - "map_of ((l, v) # ps) k = (if l = k then Some v else map_of ps k)" - by simp_all - - subsubsection \\<^const>\enumerate\\ lemma enumerate_simps [simp, code]: diff -r aea11797247b -r cc292dafc527 src/HOL/Map.thy --- a/src/HOL/Map.thy Sun Feb 19 21:21:19 2023 +0000 +++ b/src/HOL/Map.thy Mon Feb 20 13:59:42 2023 +0100 @@ -70,11 +70,21 @@ "_Map (_Maplets ms1 ms2)" \ "_MapUpd (_Map ms1) ms2" "_Maplets ms1 (_Maplets ms2 ms3)" \ "_Maplets (_Maplets ms1 ms2) ms3" +primrec map_of :: "('a \ 'b) list \ 'a \ 'b" +where + "map_of [] = empty" +| "map_of (p # ps) = (map_of ps)(fst p \ snd p)" + definition map_upds :: "('a \ 'b) \ 'a list \ 'b list \ 'a \ 'b" where "map_upds m xs ys = m ++ map_of (rev (zip xs ys))" translations "_MapUpd m (_maplets x y)" \ "CONST map_upds m x y" +lemma map_of_Cons_code [code]: + "map_of [] k = None" + "map_of ((l, v) # ps) k = (if l = k then Some v else map_of ps k)" + by simp_all + subsection \@{term [source] empty}\ @@ -132,6 +142,99 @@ "empty = map_of xys \ xys = []" by(subst eq_commute) simp +lemma map_of_eq_None_iff: + "(map_of xys x = None) = (x \ fst ` (set xys))" +by (induct xys) simp_all + +lemma map_of_eq_Some_iff [simp]: + "distinct(map fst xys) \ (map_of xys x = Some y) = ((x,y) \ set xys)" +proof (induct xys) + case (Cons xy xys) + then show ?case + by (cases xy) (auto simp flip: map_of_eq_None_iff) +qed auto + +lemma Some_eq_map_of_iff [simp]: + "distinct(map fst xys) \ (Some y = map_of xys x) = ((x,y) \ set xys)" +by (auto simp del: map_of_eq_Some_iff simp: map_of_eq_Some_iff [symmetric]) + +lemma map_of_is_SomeI [simp]: + "\distinct(map fst xys); (x,y) \ set xys\ \ map_of xys x = Some y" + by simp + +lemma map_of_zip_is_None [simp]: + "length xs = length ys \ (map_of (zip xs ys) x = None) = (x \ set xs)" +by (induct rule: list_induct2) simp_all + +lemma map_of_zip_is_Some: + assumes "length xs = length ys" + shows "x \ set xs \ (\y. map_of (zip xs ys) x = Some y)" +using assms by (induct rule: list_induct2) simp_all + +lemma map_of_zip_upd: + fixes x :: 'a and xs :: "'a list" and ys zs :: "'b list" + assumes "length ys = length xs" + and "length zs = length xs" + and "x \ set xs" + and "map_of (zip xs ys)(x \ y) = map_of (zip xs zs)(x \ z)" + shows "map_of (zip xs ys) = map_of (zip xs zs)" +proof + fix x' :: 'a + show "map_of (zip xs ys) x' = map_of (zip xs zs) x'" + proof (cases "x = x'") + case True + from assms True map_of_zip_is_None [of xs ys x'] + have "map_of (zip xs ys) x' = None" by simp + moreover from assms True map_of_zip_is_None [of xs zs x'] + have "map_of (zip xs zs) x' = None" by simp + ultimately show ?thesis by simp + next + case False from assms + have "(map_of (zip xs ys)(x \ y)) x' = (map_of (zip xs zs)(x \ z)) x'" by auto + with False show ?thesis by simp + qed +qed + +lemma map_of_zip_inject: + assumes "length ys = length xs" + and "length zs = length xs" + and dist: "distinct xs" + and map_of: "map_of (zip xs ys) = map_of (zip xs zs)" + shows "ys = zs" + using assms(1) assms(2)[symmetric] + using dist map_of +proof (induct ys xs zs rule: list_induct3) + case Nil show ?case by simp +next + case (Cons y ys x xs z zs) + from \map_of (zip (x#xs) (y#ys)) = map_of (zip (x#xs) (z#zs))\ + have map_of: "map_of (zip xs ys)(x \ y) = map_of (zip xs zs)(x \ z)" by simp + from Cons have "length ys = length xs" and "length zs = length xs" + and "x \ set xs" by simp_all + then have "map_of (zip xs ys) = map_of (zip xs zs)" using map_of by (rule map_of_zip_upd) + with Cons.hyps \distinct (x # xs)\ have "ys = zs" by simp + moreover from map_of have "y = z" by (rule map_upd_eqD1) + ultimately show ?case by simp +qed + +lemma map_of_zip_nth: + assumes "length xs = length ys" + assumes "distinct xs" + assumes "i < length ys" + shows "map_of (zip xs ys) (xs ! i) = Some (ys ! i)" +using assms proof (induct arbitrary: i rule: list_induct2) + case Nil + then show ?case by simp +next + case (Cons x xs y ys) + then show ?case + using less_Suc_eq_0_disj by auto +qed + +lemma map_of_zip_map: + "map_of (zip xs (map f xs)) = (\x. if x \ set xs then Some (f x) else None)" + by (induct xs) (simp_all add: fun_eq_iff) + lemma finite_range_map_of: "finite (range (map_of xys))" proof (induct xys) case (Cons a xys) @@ -139,6 +242,25 @@ using finite_range_updI by fastforce qed auto +lemma map_of_SomeD: "map_of xs k = Some y \ (k, y) \ set xs" + by (induct xs) (auto split: if_splits) + +lemma map_of_mapk_SomeI: + "inj f \ map_of t k = Some x \ + map_of (map (case_prod (\k. Pair (f k))) t) (f k) = Some x" +by (induct t) (auto simp: inj_eq) + +lemma weak_map_of_SomeI: "(k, x) \ set l \ \x. map_of l k = Some x" +by (induct l) auto + +lemma map_of_filter_in: + "map_of xs k = Some z \ P k z \ map_of (filter (case_prod P) xs) k = Some z" +by (induct xs) auto + +lemma map_of_map: + "map_of (map (\(k, v). (k, f v)) xs) = map_option f \ map_of xs" + by (induct xs) (auto simp: fun_eq_iff) + lemma dom_map_option: "dom (\k. map_option (f k) (m k)) = dom m" by (simp add: dom_def)