(.require [library [lux (.except list type) [abstract ["[0]" functor]] [data [collection ["[0]" list]]] [meta [type ["[0]" variance]] [compiler ["@" target]]]]]) (def .public nominal "#Array") (with_template [ ] [(.type .public {.#Nominal ..nominal {.#Item {.#End}}})] [(variance.Mutable r w) (Array' r w)] [(variance.Mutable a a) (Array a)] ) (with_expansions [ (.Nominal "java.lang.Long") (.Nominal "java.lang.Object")] (for @.jvm (def jvm_int (template (jvm_int value) [(.|> value (.as ) .jvm_object_cast# .jvm_conversion_long_to_int#)])) (these)) (`` (def .public empty (template (empty ) [((.is (.All (_ a) (.-> .Nat (..Array a))) (.function (empty size) (.as_expected (.for (,, (.static @.old)) ("jvm anewarray" "(java.lang.Object )" size) (,, (.static @.jvm)) (|> (,, (..jvm_int size)) .jvm_array_new_object# (.is (..Array ))) (,, (.static @.js)) (.js_array_new# size) (,, (.static @.python)) (.python_array_new# size) (,, (.static @.lua)) (.lua_array_new# size) (,, (.static @.ruby)) (.ruby_array_new# size) (,, (.static @.php)) ("php array new" size) (,, (.static @.scheme)) ("scheme array new" size))))) )]))) (`` (def .public size (template (size ) [((.is (.All (_ r w) (.-> (..Array' r w) .Nat)) (.function (size array) (.for (,, (.static @.old)) ("jvm arraylength" array) (,, (.static @.jvm)) (.|> array .jvm_array_length_object# .jvm_conversion_int_to_long# .jvm_object_cast# (.is ) (.as .Nat)) (,, (.static @.js)) (.js_array_length# array) (,, (.static @.python)) (.python_array_length# array) (,, (.static @.lua)) (.lua_array_length# array) (,, (.static @.ruby)) (.ruby_array_length# array) (,, (.static @.php)) ("php array length" array) (,, (.static @.scheme)) ("scheme array length" array)))) )]))) (def lacks?' (template (lacks?' index array) [( ( index array))])) (`` (def .public lacks? (template (lacks? ) [((.is (.All (_ r w) (.-> .Nat (..Array' r w) .Bit)) (.function (lacks? index array) (.let [size (..size array)] (.if (.int_<# (.int size) (.int index)) (.for (,, (.static @.old)) ("jvm object null?" ("jvm aaload" array index)) (,, (.static @.jvm)) (.|> array (.jvm_array_read_object# (,, (jvm_int index))) .jvm_object_null?#) (,, (.static @.js)) (,, (lacks?' .js_array_read# .js_object_undefined?# index array)) (,, (.static @.python)) (,, (lacks?' .python_array_read# .python_object_none?# index array)) (,, (.static @.lua)) (,, (lacks?' .lua_array_read# .lua_object_nil?# index array)) (,, (.static @.ruby)) (,, (lacks?' .ruby_array_read# .ruby_object_nil?# index array)) (,, (.static @.php)) (,, (lacks?' "php array read" "php object null?" index array)) (,, (.static @.scheme)) (,, (lacks?' "scheme array read" "scheme object nil?" index array))) .true)))) )]))) (def .public has? (template (has? index array) [(.not (..lacks? index array))])) (`` (def .public item (template (item ) [((.is (.All (_ r w) (.-> .Nat (..Array' r w) r)) (.function (item index array) (.as_expected (.for (,, (.static @.old)) ("jvm aaload" array index) (,, (.static @.jvm)) (.jvm_array_read_object# (,, (jvm_int index)) array) (,, (.static @.js)) (.js_array_read# index array) (,, (.static @.python)) (.python_array_read# index array) (,, (.static @.lua)) (.lua_array_read# index array) (,, (.static @.ruby)) (.ruby_array_read# index array) (,, (.static @.php)) ("php array read" index array) (,, (.static @.scheme)) ("scheme array read" index array))))) )]))) (`` (def .public has! (template (has! ) [((.is (.All (_ r w) (.-> .Nat w (..Array' r w) (..Array' r w))) (.function (has! index value array) (.for (,, (.static @.old)) ("jvm aastore" array index value) (,, (.static @.jvm)) (.|> array (.jvm_array_write_object# (,, (jvm_int index)) value) .as_expected) (,, (.static @.js)) (.js_array_write# index (.as_expected value) array) (,, (.static @.python)) (.python_array_write# index (.as_expected value) array) (,, (.static @.lua)) (.lua_array_write# index (.as_expected value) array) (,, (.static @.ruby)) (.ruby_array_write# index (.as_expected value) array) (,, (.static @.php)) ("php array write" index (.as_expected value) array) (,, (.static @.scheme)) ("scheme array write" index (.as_expected value) array)))) )]))) (`` (def .public lacks! (template (lacks! ) [((.is (.All (_ r w) (.-> .Nat (..Array' r w) (..Array' r w))) (.function (lacks! index array) (.let [size (..size array)] (.if (.int_<# (.int size) (.int index)) (.for (,, (.static @.old)) (..has! index (.as_expected ("jvm object null")) array) (,, (.static @.jvm)) (..has! index (.as_expected (is (.jvm_object_null#))) array) (,, (.static @.js)) (.js_array_delete# index array) (,, (.static @.python)) (.python_array_delete# index array) (,, (.static @.lua)) (.lua_array_delete# index array) (,, (.static @.ruby)) (.ruby_array_delete# index array) (,, (.static @.php)) ("php array delete" index array) (,, (.static @.scheme)) ("scheme array delete" index array)) array)))) )]))) ) (def .public revised! (template (revised! <$> ) [((.is (.All (_ r w) (.-> .Nat (.-> r w) (..Array' r w) (..Array' r w))) (.function (revised! index $ array) (.if (..lacks? index array) array (..has! index ($ (..item index array)) array)))) <$> )])) (def .public upsert! (template (upsert! <$> ) [((.is (.All (_ r w) (.-> .Nat r (.-> r w) (..Array' r w) (..Array' r w))) (.function (upsert! index default $ array) (..has! index ($ (.if (..lacks? index array) default (..item index array))) array))) <$> )])) (def .public copy! (template (copy! ) [((.is (.All (_ r w) (.-> .Nat .Nat (..Array' w .Nothing) .Nat (..Array' r w) (..Array' r w))) (.function (copy! length src_start src_array dest_start dest_array) (.loop (again [offset 0]) (.if (.int_<# (.int length) (.int offset)) (.exec (.if (..lacks? (.i64_+# offset src_start) src_array) (..lacks! (.i64_+# offset dest_start) dest_array) (..has! (.i64_+# offset dest_start) (..item (.i64_+# offset src_start) src_array) dest_array)) (again (.i64_+# 1 offset))) dest_array)))) )])) (with_template [ ] [(def .public (template ( ) [((.is (.All (_ r w) (.-> (..Array' r w) .Nat)) (.function (occupancy array) (.let [size (..size array)] (.loop (again [index 0 it 0]) (.if (.int_<# (.int size) (.int index)) (.if (..lacks? index array) (again (.i64_+# 1 index) ) (again (.i64_+# 1 index) )) it))))) )]))] [occupancy it (.i64_+# 1 it)] [vacancy (.i64_+# 1 it) it] ) (def .public only! (template (only! ) [((.is (.All (_ r w) (.-> (.-> r .Bit) (..Array' r w) (..Array' r w))) (.function (only! ? it) (.let [size (..size it)] (.loop (again [index 0]) (.if (.int_<# (.int size) (.int index)) (.exec (.if (..lacks? index it) it (.if (? (..item index it)) it (..lacks! index it))) (again (.i64_+# 1 index))) it))))) )])) (with_template [ ] [(def .public (template ( ) [((.is (.All (_ r w) (.-> (..Array' r w) (.Maybe ))) (.function ( ? it) (.let [size (..size it)] (.loop (again [index 0]) (.if (.int_<# (.int size) (.int index)) (.if (..lacks? index it) (again (.i64_+# 1 index)) (.let [it (..item index it)] (.if {.#Some } (again (.i64_+# 1 index))))) {.#None}))))) )]))] [example (.-> r .Bit) (? it) r it] [example' (.-> Nat r .Bit) (? index it) [Nat r] [index it]] ) (def .public clone (template (clone ) [((.is (.All (_ a) (.-> (..Array a) (..Array a))) (.function (clone it) (.let [size (..size it)] (..copy! size 0 it 0 (..empty size))))) )])) (def .public of_list (template (of_list ) [((.is (.All (_ a) (.-> (.List a) (..Array a))) (.function (of_list input) (.let [size (list.size input) output (..empty size)] (.loop (again [index 0 input input]) (.when input {.#End} output {.#Item head tail} (.exec (..has! index head output) (again (.i64_+# 1 index) tail))))))) )])) (def underflow Nat (-- 0)) (`` (def list|-default (template (list|-default ) [((.is (.All (_ r w) (.-> (.List r) (..Array' r w) (.List r))) (.function (list|-default empty array) (.loop (again [index (.i64_-# 1 (..size array)) output empty]) (.if (.i64_=# (,, (.static ..underflow)) index) output (again (.i64_-# 1 index) (.if (..lacks? index array) output {.#Item (..item index array) output})))))) )]))) (`` (def list|+default (template (list|+default ) [((.is (.All (_ r w) (.-> r (..Array' r w) (.List r))) (.function (list|+default default array) (.loop (again [index (.i64_-# 1 (..size array)) output (`` (.is (.List (,, (.these (,, (.type_of default))))) {.#End}))]) (.if (.i64_=# (,, (.static ..underflow)) index) output (again (.i64_-# 1 index) {.#Item (.if (..lacks? index array) default (..item index array)) output}))))) )]))) (`` (def .public list (template (list ) [((.is (.All (_ r w) (.-> (.Maybe r) (..Array' r w) (.List r))) (.function (list default array) (.when default {.#Some default} (,, (..list|+default default array)) {.#None} (,, (..list|-default {.#End} array))))) )]))) (def .public = (template (= ) [((.is (.All (_ r w0 w1) (.-> (.-> r r .Bit) (..Array' r w0) (..Array' r w1) .Bit)) (.function (= //#= left/* right/*) (.let [size (..size left/*)] (.and (.i64_=# (..size right/*) size) (.loop (again [index 0]) (.if (.int_<# (.int size) (.int index)) (.if (..lacks? index left/*) (..lacks? index right/*) (.if (..lacks? index right/*) .false (.and (//#= (..item index left/*) (..item index right/*)) (again (.i64_+# 1 index))))) true)))))) )])) (def .public composite (template (composite ) [((.is (.All (_ a) (.-> (..Array' a .Nothing) (..Array' a .Nothing) (..Array a))) (.function (composite left/* right/*) (.let [|left| (..size left/*) |right| (..size right/*)] (.|> (..empty (.i64_+# |left| |right|)) (..copy! |left| 0 left/* 0) (..copy! |right| 0 right/* |left|))))) )])) (def .public mix (template (mix <$> ) [((.is (.All (_ r w s) (.-> (.-> Nat r s s) s (..Array' r w) s)) (.function (mix $ init it) (.let [size (..size it)] (.loop (again [index 0 so_far init]) (.if (.int_<# (.int size) (.int index)) (.if (..lacks? index it) (again (.i64_+# 1 index) so_far) (again (.i64_+# 1 index) ($ index (..item index it) so_far))) so_far))))) <$> )])) (def .public each (template (each <$> ) [((.is (functor.Functor ..Array) (.function (each $ input) (..mix (.function (_ index item output) (..has! index ($ item) output)) (..empty (..size input)) input))) <$> )])) (with_template [ ] [(def .public (template ( ) [((.is (.All (_ r w) (.-> (.-> r .Bit) (.-> (..Array' r w) .Bit))) (.function ( ? it) (.let [size (..size it)] (.loop (again [index 0]) (.if (.int_<# (.int size) (.int index)) (.if (..lacks? index it) (again (.i64_+# 1 index)) ( (? (..item index it)) (again (.i64_+# 1 index)))) ))))) )]))] [every? .true and] [any? .false or] ) (def .public one (template (one ) [((.is (.All (_ r r' w) (.-> (.-> r (.Maybe r')) (..Array' r w) (.Maybe r'))) (.function (one ? it) (.let [size (..size it)] (.loop (again [index 0]) (.if (.int_<# (.int size) (.int index)) (with_expansions [ (again (.i64_+# 1 index))] (.if (..lacks? index it) (.when (? (..item index it)) {.#None} output output))) {.#None}))))) )]))