src/HOL/Predicate_Compile_Examples/Predicate_Compile_Quickcheck_Examples.thy
changeset 61145 497eb43a3064
parent 61140 78ece168f5b5
parent 61142 6d29eb7c5fb2
child 61424 c3658c18b7bc
equal deleted inserted replaced
61141:92858a52e45b 61145:497eb43a3064
   290 
   290 
   291 values [random_dseq 2, 2, 15] 6 "{(M::int list list, n, m). matrix M n m}"
   291 values [random_dseq 2, 2, 15] 6 "{(M::int list list, n, m). matrix M n m}"
   292 
   292 
   293 definition "scalar_product v w = (\<Sum> (x, y)\<leftarrow>zip v w. x * y)"
   293 definition "scalar_product v w = (\<Sum> (x, y)\<leftarrow>zip v w. x * y)"
   294 
   294 
   295 definition mv :: "('a \<Colon> semiring_0) list list \<Rightarrow> 'a list \<Rightarrow> 'a list"
   295 definition mv :: "('a :: semiring_0) list list \<Rightarrow> 'a list \<Rightarrow> 'a list"
   296   where [simp]: "mv M v = map (scalar_product v) M"
   296   where [simp]: "mv M v = map (scalar_product v) M"
   297 text {*
   297 text {*
   298   This defines the matrix vector multiplication. To work properly @{term
   298   This defines the matrix vector multiplication. To work properly @{term
   299 "matrix M m n \<and> length v = n"} must hold.
   299 "matrix M m n \<and> length v = n"} must hold.
   300 *}
   300 *}
   305 (*
   305 (*
   306 lemma sparsify_length: "(i, x) \<in> set (sparsify xs) \<Longrightarrow> i < length xs"
   306 lemma sparsify_length: "(i, x) \<in> set (sparsify xs) \<Longrightarrow> i < length xs"
   307   by (auto simp: sparsify_def set_zip)
   307   by (auto simp: sparsify_def set_zip)
   308 
   308 
   309 lemma listsum_sparsify[simp]:
   309 lemma listsum_sparsify[simp]:
   310   fixes v :: "('a \<Colon> semiring_0) list"
   310   fixes v :: "('a :: semiring_0) list"
   311   assumes "length w = length v"
   311   assumes "length w = length v"
   312   shows "(\<Sum>x\<leftarrow>sparsify w. (\<lambda>(i, x). v ! i) x * snd x) = scalar_product v w"
   312   shows "(\<Sum>x\<leftarrow>sparsify w. (\<lambda>(i, x). v ! i) x * snd x) = scalar_product v w"
   313     (is "(\<Sum>x\<leftarrow>_. ?f x) = _")
   313     (is "(\<Sum>x\<leftarrow>_. ?f x) = _")
   314   unfolding sparsify_def scalar_product_def
   314   unfolding sparsify_def scalar_product_def
   315   using assms listsum_map_filter[where f="?f" and P="\<lambda> i. snd i \<noteq> (0::'a)"]
   315   using assms listsum_map_filter[where f="?f" and P="\<lambda> i. snd i \<noteq> (0::'a)"]
   316   by (simp add: listsum_setsum)
   316   by (simp add: listsum_setsum)
   317 *)
   317 *)
   318 definition [simp]: "unzip w = (map fst w, map snd w)"
   318 definition [simp]: "unzip w = (map fst w, map snd w)"
   319 
   319 
   320 primrec insert :: "('a \<Rightarrow> 'b \<Colon> linorder) => 'a \<Rightarrow> 'a list => 'a list" where
   320 primrec insert :: "('a \<Rightarrow> 'b :: linorder) => 'a \<Rightarrow> 'a list => 'a list" where
   321   "insert f x [] = [x]" |
   321   "insert f x [] = [x]" |
   322   "insert f x (y # ys) = (if f y < f x then y # insert f x ys else x # y # ys)"
   322   "insert f x (y # ys) = (if f y < f x then y # insert f x ys else x # y # ys)"
   323 
   323 
   324 primrec sort :: "('a \<Rightarrow> 'b \<Colon> linorder) \<Rightarrow> 'a list => 'a list" where
   324 primrec sort :: "('a \<Rightarrow> 'b :: linorder) \<Rightarrow> 'a list => 'a list" where
   325   "sort f [] = []" |
   325   "sort f [] = []" |
   326   "sort f (x # xs) = insert f x (sort f xs)"
   326   "sort f (x # xs) = insert f x (sort f xs)"
   327 
   327 
   328 definition
   328 definition
   329   "length_permutate M = (unzip o sort (length o snd)) (zip [0 ..< length M] M)"
   329   "length_permutate M = (unzip o sort (length o snd)) (zip [0 ..< length M] M)"