lemma valid_index_list_all2_iff: "is ⊲ ds ⟷ list_all2 (<) is ds" by (metis list_all2_conv_all_nth list_all2_nthD valid_indexI valid_index_length valid_index_lt)
definition fixed_length_sublist::"'a list ==> nat ==> nat ==> 'a list"where "fixed_length_sublist xs l i = (take l (drop (l*i) xs))"
fun lookup_base::"nat list ==> 'a list ==> nat list ==> 'a"where
lookup_base_Nil: "lookup_base [] v [] = hd v" |
lookup_base_Cons: "lookup_base (d # ds) v (i # is) = lookup_base ds (fixed_length_sublist v (prod_list ds) i) is"
definition lookup::"'a tensor ==> nat list ==> 'a"where "lookup A = lookup_base (dims A) (vec A)"
fun tensor_vec_from_lookup::"nat list ==> (nat list ==> 'a) ==> 'a list"where
tensor_vec_from_lookup_Nil: "tensor_vec_from_lookup [] e = [e []]" |
tensor_vec_from_lookup_Cons: "tensor_vec_from_lookup (d # ds) e = concat (map (λi. tensor_vec_from_lookup ds (λis. e (i # is))) [0..<d])"
definition tensor_from_lookup::"nat list ==> (nat list ==> 'a) ==> 'a tensor"where "tensor_from_lookup ds e = tensor_from_vec ds (tensor_vec_from_lookup ds e)"
lemma concat_parts_leq: assumes"a * d ≤ length v" shows"concat (map (fixed_length_sublist v d) [0..<a]) = take (a*d) v" using assms proof (induction a) case0 thenshow ?caseby simp next case (Suc a) thenhave"concat (map (fixed_length_sublist v d) [0..<a]) = take (a * d) v"by auto thenhave"concat (map (fixed_length_sublist v d) [0..<Suc a]) = take (a * d) v @ fixed_length_sublist v d a"using fixed_length_sublist_def by auto thenshow ?caseusing Suc by (metis add.commute mult.commute mult_Suc take_add fixed_length_sublist_def) qed
lemma concat_parts_eq: assumes"a * d = length v" shows"concat (map (fixed_length_sublist v d) [0..<a]) = v" by (simp add: concat_parts_leq assms)
lemma tensor_lookup_base: assumes"length v = prod_list ds" and"∧is. is ⊲ ds ==> lookup_base ds v is = e is" shows"tensor_vec_from_lookup ds e = v" using assms proof (induction ds arbitrary:v e) case Nil thenshow ?caseunfolding tensor_vec_from_lookup.simps by (metis One_nat_def Tensor.lookup_base_Nil length_0_conv length_Suc_conv list.sel(1) prod_list.Nil valid_index.Nil) next case (Cons a ds) thenhave‹length v = prod_list ds * a›by auto
{ fix i assume‹i < a› thenhave‹Suc i ≤ a› by simp thenhave"prod_list ds * Suc i ≤ length v" using‹length v = prod_list ds * a› by (simp only: mult_le_mono2) have"∧is'. is' ⊲ ds ==> e (i # is') = lookup_base ds (fixed_length_sublist v (prod_list ds) i) is'" using‹i<a\›by (metis Cons.prems(2) Tensor.lookup_base_Cons valid_index.simps) thenhave"tensor_vec_from_lookup ds (λis'. e (i # is')) = fixed_length_sublist v (prod_list ds) i" using Cons using‹prod_list ds * Suc i ≤ length v›by (simp add: Cons.IH fixed_length_sublist_def)
} thenshow ?caseunfolding tensor_vec_from_lookup_Cons lookup_base_Cons using atLeastLessThan_iff map_eq_conv set_upt Cons concat_parts_eq prod_list.Cons by (metis (no_types, lifting)) qed
lemma tensor_lookup: assumes"∧is. is ⊲ dims A ==> lookup A is = e is" shows"tensor_from_lookup (dims A) e = A" using tensor_lookup_base lookup_def length_vec tensor_from_lookup_def by (metis assms tensor_from_vec_simp)
lemma concat_equal_length_map: assumes"∧i. i<a ==> length (f i) = d" shows"length (concat (map (λi. f i) [0..<a])) = a*d" using assms by (induction a;auto)
lemma concat_parts: assumes"∧xs. xs∈set xss ==> length xs = d"and"i<length xss" shows"fixed_length_sublist (concat xss) d i = xss ! i" using assms proof (induction xss arbitrary:i) case Nil thenshow ?caseby simp next case (Cons xs xss) thenhave"length (concat xss) = length xss * d"by (simp add: Cons.prems(1) concat_equal_length) show ?case proof (cases i) case0 thenhave"fixed_length_sublist (concat (xs # xss)) d i = xs" unfolding fixed_length_sublist_def by (simp add: Cons.prems(1)) thenshow ?thesis using0by auto next case (Suc i') thenhave"fixed_length_sublist (concat xss) d i' = xss ! i'"using Cons by auto thenshow ?thesis unfolding fixed_length_sublist_def using Suc Cons.prems(1) by auto qed qed
lemma concat_parts': assumes"∧i. i<a ==> length (f i) = d" and"i<a" shows"fixed_length_sublist (concat (map (λi. f i) [0..<a])) d i = f i" using assms proof (induction a) case0 thenshow ?caseby simp next case (Suc a) thenhave"(∧i. i < a ==> length (f i) = d)"by auto thenhave"length (concat (map f [0..<a])) = a*d"using concat_equal_length_map by auto show ?case proof (cases "i=a") assume"i=a" thenhave"fixed_length_sublist (concat (map f [0..<Suc a])) d i = f a" by (simp add: Suc.prems(1) ‹length (concat (map f [0..<a])) = a * d› fixed_length_sublist_def) thenshow ?caseusing‹i=a›by auto next assume"i≠a" thenhave"fixed_length_sublist (concat (map f [0..<a])) d i = f i" "concat (map f [0..<Suc a]) = concat (map f [0..<a]) @ f a"using Suc by auto show ?caseunfolding‹concat (map f [0..<Suc a]) = concat (map f [0..<a]) @ f a› unfolding fixed_length_sublist_def drop_append using‹length (concat (map f [0..<a])) = a * d›‹fixed_length_sublist (concat (map f [0..<a])) d i = f i› using append_assoc append_eq_conv_conj append_take_drop_id assms(1) assms(2) fixed_length_sublist_def by metis qed qed
lemma length_tensor_vec_from_lookup: "length (tensor_vec_from_lookup ds e) = prod_list ds" by (induction ds arbitrary:e; auto simp add: concat_equal_length_map)
lemma lookup_tensor_vec: assumes"is⊲ds" shows"lookup_base ds (tensor_vec_from_lookup ds e) is = e is" using assms proof (induction arbitrary:e rule:valid_index.induct) case Nil thenshow ?caseby simp next case (Cons "is" ds i d e) thenshow ?caseunfolding tensor_vec_from_lookup_Cons lookup_base_Cons by (simp add: length_tensor_vec_from_lookup concat_parts'[of d "λi. tensor_vec_from_lookup ds (λis. e (i # is))""prod_list ds" i] ‹i < d›) qed
lemma lookup_tensor_from_lookup: assumes"is⊲ds" shows"lookup (tensor_from_lookup ds e) is = e is" unfolding lookup_def tensor_from_lookup_def by (simp add: lookup_tensor_vec assms length_tensor_vec_from_lookup)
lemma tensor_lookup_eqI: assumes"dims A = dims B"and"∧is. is⊲(dims A) ==> lookup A is = lookup B is" shows"A = B"by (metis assms(1) assms(2) tensor_lookup)
end
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.1 Sekunden
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.