(* Title: HOL/Library/IArray.thy Author: Tobias Nipkow, TU Muenchen Author: Jose Divasón <jose.divasonm at unirioja.es> Author: Jesús Aransay <jesus-maria.aransay at unirioja.es>
*)
section‹Immutable Arrays with Code Generation›
theory IArray imports Main begin
subsection‹Fundamental operations›
text‹Immutable arrays are lists wrapped up in an additional constructor.
There are no update operations. Hence code generation can safely implement
this type by efficient target language arrays. Currently only SML is
provided. Could be extended to other target languages and more operations.›
lemma [code]: "IArray.length as = nat_of_integer (IArray.length' as)" by simp
qualified definition exists_upto :: "('a \ bool) \ integer \ 'a iarray \ bool"where
[simp]: "exists_upto p k as \ (\l. 0 \ l \ l < k \ p (sub' (as, l)))"
lemma exists_upto_of_nat: "exists_upto p (of_nat n) as \ (\m
including integer.lifting by (simp, transfer)
(metis nat_int nat_less_iff of_nat_0_le_iff)
lemma [code]: "exists_upto p k as \ (if k \ 0 then False else let l = k - 1 in p (sub' (as, l)) \ exists_upto p l as)" proof (cases "k \ 1") case False thenhave‹k ≤ 0›
including integer.lifting by transfer simp thenshow ?thesis by simp next case True thenhave less: "k \ 0 \ False" by simp
define n where"n = nat_of_integer (k - 1)" with True have k: "k - 1 = of_nat n""k = of_nat (Suc n)" by simp_all show ?thesis unfolding less Let_def k(1) unfolding k(2) exists_upto_of_nat using less_Suc_eq by auto qed
lemma [code]: "IArray.exists p as \ exists_upto p (length' as) as"
including integer.lifting by (simp, transfer)
(auto, metis in_set_conv_nth less_imp_of_nat_less nat_int of_nat_0_le_iff)
end
subsection‹Code Generation for SML›
text‹Note that arrays cannot be printed directly but only by turning them into
lists first. Arrays could be converted back into lists for printing if they
were wrapped up in an additional constructor.›
text‹We map 🍋‹'a iarray\s in Isabelle/HOL to \Data.Array.IArray.array\ in Haskell. Performance mapping to‹Data.Array.Unboxed.Array›and ‹Data.Array.Array›is similar.›
newtype IArray e = IArray (Data.Array.IArray.Array Integer e);
tabulate :: (Integer, (Integer -> e)) -> IArray e;
tabulate (k, f) = IArray (Data.Array.IArray.array (0, k - 1) (map (\i -> let fi = f i in fi `seq` (i, fi)) [0..k - 1]));
of_list :: [e] -> IArray e;
of_list l = IArray (Data.Array.IArray.listArray (0, (toInteger . Prelude.length) l - 1) l);
sub :: (IArray e, Integer) -> e;
sub (IArray v, i) = v `Data.Array.Base.unsafeAt` fromInteger i;
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.