![]() ![]() Length (Unsynchronized.ref (Value a)) = Array.length a (Unsynchronized.ref Invalid) => raise AccessedOldVersion |įun length (Unsynchronized.ref Invalid) = raise AccessedOldVersion | Sub (Unsynchronized.ref (Value a), idx) = Array.sub (a,idx) Type 'a array = 'a Cell Unsynchronized.ref įun fromList l = Unsynchronized.ref (Value (omList l)) įun array (size, v) = Unsynchronized.ref (Value (Array.array (size,v))) įun tabulate (size, f) = Unsynchronized.ref (Value (Array.tabulate(size, f))) įun sub (Unsynchronized.ref Invalid, idx) = raise AccessedOldVersion | › code_printing code_module " STArray " ⇀ ( SML ) ‹ĭatatype 'a Cell = Invalid | Value of 'a array To be discontinued in Haskell! * ) code_printing code_module " Array " ⇀ ( Haskell ) ‹ module Array where › code_printing constant Array ⇀ ( Haskell ) " Array.array'_of'_list " code_printing constant new_array' ⇀ ( Haskell ) " Array.new'_array " code_printing constant array_length' ⇀ ( Haskell ) " Array.array'_length " code_printing constant array_get' ⇀ ( Haskell ) " Array.array'_get " code_printing constant array_set' ⇀ ( Haskell ) " Array.array'_set " code_printing constant array_of_list ⇀ ( Haskell ) " Array.array'_of'_list " code_printing constant array_grow' ⇀ ( Haskell ) " Array.array'_grow " code_printing constant array_shrink' ⇀ ( Haskell ) " Array.array'_shrink " subsubsection ‹ Code Generator Setup For SML › text ‹ We have the choice between single-threaded arrays, that raise an exception if an old version is accessed,Īnd truly functional arrays, that update the array in place, but store undo-information to restore * ) ( * TODO/FIXME: Using standard functional arrays here, as DiffArray seems the `if` is actually needed, because in Nat we have s > e -> e - s + 1 = 1Īrray_set :: ArrayType e -> Nat -> e -> ArrayType e Īrray_of_list xs = array_of_size (fromInteger (toInteger (length xs - 1))) xs Īrray_grow :: ArrayType e -> Nat -> e -> ArrayType e Īrray_grow a i x = let (s, e) = bounds a in Arr.listArray (s, e+i) (Arr.elems a ++ repeat x) Īrray_shrink :: ArrayType e -> Nat -> ArrayType e Īrray_shrink a sz = if sz > array_length a then undefined else array_of_size sz (Arr.elems a) ![]() New_array a n = array_of_size n (repeat a) Īrray_length a = let (s, e) = bounds a in if s > e then 0 else e - s + 1 ![]() and as we are in Nat, 0 is the smallest numberĪrray_of_size :: Nat -> -> ArrayType e › definition " array_get_oo x a i ≡ if i e for (s,e) = bounds They can be efficiently implemented in the target language by catchingĮxceptions. Value in case the index is out of bounds. ‹ array_get› and ‹ array_set›, which return a default Undefined if text "sz > array_length" } › primrec array_shrink :: " 'a array ⇒ nat ⇒ 'a array " where " array_shrink ( Array A ) sz = ( if ( sz > length A ) then undefined else Array ( take sz A ) ) " subsection ‹ Derived operations › text ‹ The following operations are total versions of › primrec array_grow :: " 'a array ⇒ nat ⇒ 'a ⇒ 'a array " where " array_grow ( Array A ) inc x = Array ( A replicate inc x ) " ― ‹ Shrinks array to new size text sz }. Theory Collections.Diff_Array section ‹ Arrays with in-place updates › theory Diff_Array imports Assoc_List Automatic_Refinement.Parametricity " HOL-Library.Code_Target_Numeral " begin datatype 'a array = Array " 'a list " subsection ‹ primitive operations › definition new_array :: " 'a ⇒ nat ⇒ 'a array " where " new_array a n = Array ( replicate n a ) " primrec array_length :: " 'a array ⇒ nat " where " array_length ( Array a ) = length a " primrec array_get :: " 'a array ⇒ nat ⇒ 'a " where " array_get ( Array a ) n = a ! n " primrec array_set :: " 'a array ⇒ nat ⇒ 'a ⇒ 'a array " where " array_set ( Array A ) n a = Array ( A ) " definition array_of_list :: " 'a list ⇒ 'a array " where " array_of_list = Array " ― ‹ Grows array by text inc } elements initialized to value text x }. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |