From b48ea68a83d01903554c7696c77eedaaf1035680 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sun, 12 Jun 2022 00:38:20 -0400 Subject: De-sigil-ification: suffix : [Part 3] --- stdlib/source/unsafe/lux/data/binary.lux | 391 +++++------ stdlib/source/unsafe/lux/data/collection/array.lux | 750 +++++++++++---------- 2 files changed, 589 insertions(+), 552 deletions(-) (limited to 'stdlib/source/unsafe') diff --git a/stdlib/source/unsafe/lux/data/binary.lux b/stdlib/source/unsafe/lux/data/binary.lux index 6f51fde27..1dc9fc96c 100644 --- a/stdlib/source/unsafe/lux/data/binary.lux +++ b/stdlib/source/unsafe/lux/data/binary.lux @@ -60,57 +60,59 @@ (`` (with_expansions [ (.is .Nat size) (ffi.array byte ) (.is ..Binary )] - (template: .public (empty size) - [(is ..Binary - (for (~~ (.static @.old)) - (~~ (.static @.jvm)) - - (~~ (.static @.js)) - (.|> - .int - "lux i64 f64" - [] - ("js object new" ("js constant" "ArrayBuffer")) - [] - ("js object new" ("js constant" "Uint8Array")) - (.as ..Binary)) - - (~~ (.static @.python)) - (.|> - [] - ("python apply" (.as ffi.Function ("python constant" "bytearray"))) - (.as ..Binary)) - - (~~ (.static @.scheme)) - (..make-bytevector ) - - ... Default - (array.empty )))]))) - -(`` (with_expansions [ (.is ..Binary it) - (ffi.length )] - (template: .public (size it) - [(.is .Nat - (.for (~~ (.static @.old)) + (def: .public empty + (template (empty size) + [(is ..Binary + (for (~~ (.static @.old)) (~~ (.static @.jvm)) (~~ (.static @.js)) - (.|> - ("js object get" "length") - (.as .Frac) - "lux f64 i64" - .nat) + (.|> + .int + "lux i64 f64" + [] + ("js object new" ("js constant" "ArrayBuffer")) + [] + ("js object new" ("js constant" "Uint8Array")) + (.as ..Binary)) (~~ (.static @.python)) - (.|> - (.as (array.Array (.I64 .Any))) - "python array length") + (.|> + [] + ("python apply" (.as ffi.Function ("python constant" "bytearray"))) + (.as ..Binary)) (~~ (.static @.scheme)) - (..bytevector-length []) + (..make-bytevector ) ... Default - (array.size )))]))) + (array.empty )))])))) + +(`` (with_expansions [ (.is ..Binary it) + (ffi.length )] + (def: .public size + (template (size it) + [(.is .Nat + (.for (~~ (.static @.old)) + (~~ (.static @.jvm)) + + (~~ (.static @.js)) + (.|> + ("js object get" "length") + (.as .Frac) + "lux f64 i64" + .nat) + + (~~ (.static @.python)) + (.|> + (.as (array.Array (.I64 .Any))) + "python array length") + + (~~ (.static @.scheme)) + (..bytevector-length []) + + ... Default + (array.size )))])))) (def: byte_mask Nat @@ -124,67 +126,71 @@ (.|> (.as .I64) ("lux i64 and" ))] - (template: .public (bits_8 index it) - [(.<| (.as .I64) + (def: .public bits_8 + (template (bits_8 index it) + [(.<| (.as .I64) + (.is (.I64 .Any)) + (`` (.for (~~ (.static @.old)) (~~ ) + (~~ (.static @.jvm)) (~~ ) + + (~~ (.static @.js)) + (.|> + (.as (array.Array .Frac)) + ("js array read" ) + (.as .Frac) + "lux f64 i64" + .i64) + + (~~ (.static @.python)) + (.|> + (.as (array.Array .I64)) + ("python array read" )) + + (~~ (.static @.scheme)) + (..bytevector-u8-ref [ ]) + + ... Default + (.if (array.lacks? ) + (.i64 0) + (array.item )))))]))) + +(def: .public bits_16 + (template (bits_16 index' it') + [(.<| (.let [index (.is Nat index') + it (.is ..Binary it')]) + (.as .I64) (.is (.I64 .Any)) - (`` (.for (~~ (.static @.old)) (~~ ) - (~~ (.static @.jvm)) (~~ ) - - (~~ (.static @.js)) - (.|> - (.as (array.Array .Frac)) - ("js array read" ) - (.as .Frac) - "lux f64 i64" - .i64) - - (~~ (.static @.python)) - (.|> - (.as (array.Array .I64)) - ("python array read" )) - - (~~ (.static @.scheme)) - (..bytevector-u8-ref [ ]) - - ... Default - (.if (array.lacks? ) - (.i64 0) - (array.item )))))])) - -(template: .public (bits_16 index' it') - [(.<| (.let [index (.is Nat index') - it (.is ..Binary it')]) - (.as .I64) - (.is (.I64 .Any)) - (.all "lux i64 or" - ("lux i64 left-shift" 8 (..bits_8 index it)) - (..bits_8 ("lux i64 +" 1 index) it)))]) - -(template: .public (bits_32 index' it') - [(.<| (.let [index (.is Nat index') - it (.is ..Binary it')]) - (.as .I64) - (.is (.I64 .Any)) - (.all "lux i64 or" - ("lux i64 left-shift" 24 (..bits_8 index it)) - ("lux i64 left-shift" 16 (..bits_8 ("lux i64 +" 1 index) it)) - ("lux i64 left-shift" 8 (..bits_8 ("lux i64 +" 2 index) it)) - (..bits_8 ("lux i64 +" 3 index) it)))]) - -(template: .public (bits_64 index' it') - [(.<| (.let [index (.is Nat index') - it (.is ..Binary it')]) - (.as .I64) - (.is (.I64 .Any)) - (.all "lux i64 or" - ("lux i64 left-shift" 56 (..bits_8 index it)) - ("lux i64 left-shift" 48 (..bits_8 ("lux i64 +" 1 index) it)) - ("lux i64 left-shift" 40 (..bits_8 ("lux i64 +" 2 index) it)) - ("lux i64 left-shift" 32 (..bits_8 ("lux i64 +" 3 index) it)) - ("lux i64 left-shift" 24 (..bits_8 ("lux i64 +" 4 index) it)) - ("lux i64 left-shift" 16 (..bits_8 ("lux i64 +" 5 index) it)) - ("lux i64 left-shift" 8 (..bits_8 ("lux i64 +" 6 index) it)) - (..bits_8 ("lux i64 +" 7 index) it)))]) + (.all "lux i64 or" + ("lux i64 left-shift" 8 (..bits_8 index it)) + (..bits_8 ("lux i64 +" 1 index) it)))])) + +(def: .public bits_32 + (template (bits_32 index' it') + [(.<| (.let [index (.is Nat index') + it (.is ..Binary it')]) + (.as .I64) + (.is (.I64 .Any)) + (.all "lux i64 or" + ("lux i64 left-shift" 24 (..bits_8 index it)) + ("lux i64 left-shift" 16 (..bits_8 ("lux i64 +" 1 index) it)) + ("lux i64 left-shift" 8 (..bits_8 ("lux i64 +" 2 index) it)) + (..bits_8 ("lux i64 +" 3 index) it)))])) + +(def: .public bits_64 + (template (bits_64 index' it') + [(.<| (.let [index (.is Nat index') + it (.is ..Binary it')]) + (.as .I64) + (.is (.I64 .Any)) + (.all "lux i64 or" + ("lux i64 left-shift" 56 (..bits_8 index it)) + ("lux i64 left-shift" 48 (..bits_8 ("lux i64 +" 1 index) it)) + ("lux i64 left-shift" 40 (..bits_8 ("lux i64 +" 2 index) it)) + ("lux i64 left-shift" 32 (..bits_8 ("lux i64 +" 3 index) it)) + ("lux i64 left-shift" 24 (..bits_8 ("lux i64 +" 4 index) it)) + ("lux i64 left-shift" 16 (..bits_8 ("lux i64 +" 5 index) it)) + ("lux i64 left-shift" 8 (..bits_8 ("lux i64 +" 6 index) it)) + (..bits_8 ("lux i64 +" 7 index) it)))])) (with_expansions [ (hex "FF") (.is ..Binary it) @@ -199,99 +205,104 @@ (ffi.long_to_byte ) (ffi.write! )] - (`` (template: .public (has_8! index value it) - [(.is ..Binary - (.for (~~ (.static @.old)) - (~~ (.static @.jvm)) - - (~~ (.static @.js)) - (.|> - (.is ..Binary) - (.as (array.Array .Frac)) - ("js array write" - (.|> - .int - ("lux i64 and" (.int )) - "lux i64 f64" - .as_expected)) - (.as ..Binary)) - - (~~ (.static @.python)) - (.|> - (.is ..Binary) - (.as (array.Array (.I64 .Any))) - ("python array write" (.|> ("lux i64 and" ) (.is (.I64 .Any)))) - (.as ..Binary)) - - (~~ (.static @.scheme)) - (.let [it' ] - (.exec - (..bytevector-u8-set! [it' ]) - it')) - - ... Default - (array.has! (.|> .int ("lux i64 and" (.int ))) )))]))) - -(template: .public (has_16! index' value' it) - [(.let [index (.is .Nat index') - value (.is (.I64 .Any) value')] - (.|> it - (..has_8! index ("lux i64 right-shift" 8 value)) - (..has_8! ("lux i64 +" 1 index) value)))]) - -(template: .public (has_32! index' value' it) - [(.let [index (.is .Nat index') - value (.is (.I64 .Any) value')] - (.|> it - (..has_8! index ("lux i64 right-shift" 24 value)) - (..has_8! ("lux i64 +" 1 index) ("lux i64 right-shift" 16 value)) - (..has_8! ("lux i64 +" 2 index) ("lux i64 right-shift" 8 value)) - (..has_8! ("lux i64 +" 3 index) value)))]) - -(`` (template: .public (has_64! index' value' it) - [(.let [index (.is .Nat index') - value (.is (.I64 .Any) value')] - (.for (~~ (.static @.scheme)) (.let [write_high (.is (.-> ..Binary ..Binary) - (.|>> (..has_8! index ("lux i64 right-shift" 56 value)) - (..has_8! ("lux i64 +" 1 index) ("lux i64 right-shift" 48 value)) - (..has_8! ("lux i64 +" 2 index) ("lux i64 right-shift" 40 value)) - (..has_8! ("lux i64 +" 3 index) ("lux i64 right-shift" 32 value)))) - write_low (.is (.-> ..Binary ..Binary) - (.|>> (..has_8! ("lux i64 +" 4 index) ("lux i64 right-shift" 24 value)) - (..has_8! ("lux i64 +" 5 index) ("lux i64 right-shift" 16 value)) - (..has_8! ("lux i64 +" 6 index) ("lux i64 right-shift" 8 value)) - (..has_8! ("lux i64 +" 7 index) value)))] - (.|> it - write_high - write_low)) - (.|> it - (..has_8! index ("lux i64 right-shift" 56 value)) - (..has_8! ("lux i64 +" 1 index) ("lux i64 right-shift" 48 value)) - (..has_8! ("lux i64 +" 2 index) ("lux i64 right-shift" 40 value)) - (..has_8! ("lux i64 +" 3 index) ("lux i64 right-shift" 32 value)) - (..has_8! ("lux i64 +" 4 index) ("lux i64 right-shift" 24 value)) - (..has_8! ("lux i64 +" 5 index) ("lux i64 right-shift" 16 value)) - (..has_8! ("lux i64 +" 6 index) ("lux i64 right-shift" 8 value)) - (..has_8! ("lux i64 +" 7 index) value))))])) + (`` (def: .public has_8! + (template (has_8! index value it) + [(.is ..Binary + (.for (~~ (.static @.old)) + (~~ (.static @.jvm)) + + (~~ (.static @.js)) + (.|> + (.is ..Binary) + (.as (array.Array .Frac)) + ("js array write" + (.|> + .int + ("lux i64 and" (.int )) + "lux i64 f64" + .as_expected)) + (.as ..Binary)) + + (~~ (.static @.python)) + (.|> + (.is ..Binary) + (.as (array.Array (.I64 .Any))) + ("python array write" (.|> ("lux i64 and" ) (.is (.I64 .Any)))) + (.as ..Binary)) + + (~~ (.static @.scheme)) + (.let [it' ] + (.exec + (..bytevector-u8-set! [it' ]) + it')) + + ... Default + (array.has! (.|> .int ("lux i64 and" (.int ))) )))])))) + +(def: .public has_16! + (template (has_16! index' value' it) + [(.let [index (.is .Nat index') + value (.is (.I64 .Any) value')] + (.|> it + (..has_8! index ("lux i64 right-shift" 8 value)) + (..has_8! ("lux i64 +" 1 index) value)))])) + +(def: .public has_32! + (template (has_32! index' value' it) + [(.let [index (.is .Nat index') + value (.is (.I64 .Any) value')] + (.|> it + (..has_8! index ("lux i64 right-shift" 24 value)) + (..has_8! ("lux i64 +" 1 index) ("lux i64 right-shift" 16 value)) + (..has_8! ("lux i64 +" 2 index) ("lux i64 right-shift" 8 value)) + (..has_8! ("lux i64 +" 3 index) value)))])) + +(`` (def: .public has_64! + (template (has_64! index' value' it) + [(.let [index (.is .Nat index') + value (.is (.I64 .Any) value')] + (.for (~~ (.static @.scheme)) (.let [write_high (.is (.-> ..Binary ..Binary) + (.|>> (..has_8! index ("lux i64 right-shift" 56 value)) + (..has_8! ("lux i64 +" 1 index) ("lux i64 right-shift" 48 value)) + (..has_8! ("lux i64 +" 2 index) ("lux i64 right-shift" 40 value)) + (..has_8! ("lux i64 +" 3 index) ("lux i64 right-shift" 32 value)))) + write_low (.is (.-> ..Binary ..Binary) + (.|>> (..has_8! ("lux i64 +" 4 index) ("lux i64 right-shift" 24 value)) + (..has_8! ("lux i64 +" 5 index) ("lux i64 right-shift" 16 value)) + (..has_8! ("lux i64 +" 6 index) ("lux i64 right-shift" 8 value)) + (..has_8! ("lux i64 +" 7 index) value)))] + (.|> it + write_high + write_low)) + (.|> it + (..has_8! index ("lux i64 right-shift" 56 value)) + (..has_8! ("lux i64 +" 1 index) ("lux i64 right-shift" 48 value)) + (..has_8! ("lux i64 +" 2 index) ("lux i64 right-shift" 40 value)) + (..has_8! ("lux i64 +" 3 index) ("lux i64 right-shift" 32 value)) + (..has_8! ("lux i64 +" 4 index) ("lux i64 right-shift" 24 value)) + (..has_8! ("lux i64 +" 5 index) ("lux i64 right-shift" 16 value)) + (..has_8! ("lux i64 +" 6 index) ("lux i64 right-shift" 8 value)) + (..has_8! ("lux i64 +" 7 index) value))))]))) (with_expansions [ (.is ..Binary reference') (.is ..Binary sample') (java/util/Arrays::equals ) (ffi.of_boolean )] - (`` (template: .public (= reference' sample') - [(.for (~~ (.static @.old)) - (~~ (.static @.jvm)) - (.let [reference - sample - limit (..size reference)] - (.and ("lux i64 =" limit (..size sample)) - (.loop (again [index 0]) - (.if ("lux i64 =" limit index) - .true - (.and ("lux i64 =" - (..bits_8 index reference) - (..bits_8 index sample)) - (again ("lux i64 +" 1 index))))))))]))) + (`` (def: .public = + (template (= reference' sample') + [(.for (~~ (.static @.old)) + (~~ (.static @.jvm)) + (.let [reference + sample + limit (..size reference)] + (.and ("lux i64 =" limit (..size sample)) + (.loop (again [index 0]) + (.if ("lux i64 =" limit index) + .true + (.and ("lux i64 =" + (..bits_8 index reference) + (..bits_8 index sample)) + (again ("lux i64 +" 1 index))))))))])))) ... TODO: Turn into a template ASAP. (`` (inline: .public (copy! bytes source_offset source target_offset target) diff --git a/stdlib/source/unsafe/lux/data/collection/array.lux b/stdlib/source/unsafe/lux/data/collection/array.lux index fd43808c5..d3116b89b 100644 --- a/stdlib/source/unsafe/lux/data/collection/array.lux +++ b/stdlib/source/unsafe/lux/data/collection/array.lux @@ -13,7 +13,7 @@ (def: .public type "#Array") -(template [ ] +(with_template [ ] [(type: .public {.#Primitive ..type {.#Item {.#End}}})] @@ -23,390 +23,416 @@ (with_expansions [ (.Primitive "java.lang.Long") (.Primitive "java.lang.Object")] - (for @.jvm (template: (jvm_int value) - [(.|> value - (.as ) - "jvm object cast" - "jvm conversion long-to-int")]) + (for @.jvm (def: jvm_int + (template (jvm_int value) + [(.|> value + (.as ) + "jvm object cast" + "jvm conversion long-to-int")])) (these)) - (`` (template: .public (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))))) - )])) - - (`` (template: .public (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)))) - )])) - - (template: (lacks?' index array) - [( ( index array))]) - - (`` (template: .public (lacks? ) - [((.is (.All (_ r w) - (.-> .Nat (..Array' r w) .Bit)) - (.function (lacks? index array) - (.let [size (..size array)] - (.if ("lux i64 <" (.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)))) - )])) - - (template: .public (has? index array) - [(.not (..lacks? index array))]) - - (`` (template: .public (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))))) - )])) - - (`` (template: .public (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)))) - )])) - - (`` (template: .public (lacks! ) - [((.is (.All (_ r w) - (.-> .Nat (..Array' r w) (..Array' r w))) - (.function (lacks! index array) - (.let [size (..size array)] - (.if ("lux i64 <" (.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 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 ("lux i64 <" (.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 ("lux i64 <" (.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)))) + )]))) ) -(template: .public (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)))) - <$> )]) - -(template: .public (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))) - <$> )]) - -(template: .public (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 ("lux i64 <" (.int length) (.int offset)) - (.exec - (.if (..lacks? ("lux i64 +" offset src_start) src_array) - (..lacks! ("lux i64 +" offset dest_start) dest_array) - (..has! ("lux i64 +" offset dest_start) - (..item ("lux i64 +" offset src_start) src_array) - dest_array)) - (again ("lux i64 +" 1 offset))) - dest_array)))) - )]) - -(template [ ] - [(template: .public ( ) - [((.is (.All (_ r w) (.-> (..Array' r w) .Nat)) - (.function (occupancy array) - (.let [size (..size array)] - (.loop (again [index 0 - it 0]) - (.if ("lux i64 <" (.int size) (.int index)) - (.if (..lacks? index array) - (again ("lux i64 +" 1 index) ) - (again ("lux i64 +" 1 index) )) - it))))) - )])] +(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 ("lux i64 <" (.int length) (.int offset)) + (.exec + (.if (..lacks? ("lux i64 +" offset src_start) src_array) + (..lacks! ("lux i64 +" offset dest_start) dest_array) + (..has! ("lux i64 +" offset dest_start) + (..item ("lux i64 +" offset src_start) src_array) + dest_array)) + (again ("lux 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 ("lux i64 <" (.int size) (.int index)) + (.if (..lacks? index array) + (again ("lux i64 +" 1 index) ) + (again ("lux i64 +" 1 index) )) + it))))) + )]))] [occupancy it ("lux i64 +" 1 it)] [vacancy ("lux i64 +" 1 it) it] ) -(template: .public (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 ("lux i64 <" (.int size) (.int index)) - (.exec - (.if (..lacks? index it) - it - (.if (? (..item index 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 ("lux i64 <" (.int size) (.int index)) + (.exec + (.if (..lacks? index it) it - (..lacks! index it))) - (again ("lux i64 +" 1 index))) - it))))) - )]) - -(template [ ] - [(template: .public ( ) - [((.is (.All (_ r w) - (.-> (..Array' r w) (.Maybe ))) - (.function ( ? it) - (.let [size (..size it)] - (.loop (again [index 0]) - (.if ("lux i64 <" (.int size) (.int index)) - (.if (..lacks? index it) - (again ("lux i64 +" 1 index)) - (.let [it (..item index it)] - (.if - {.#Some } - (again ("lux i64 +" 1 index))))) - {.#None}))))) - )])] + (.if (? (..item index it)) + it + (..lacks! index it))) + (again ("lux 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 ("lux i64 <" (.int size) (.int index)) + (.if (..lacks? index it) + (again ("lux i64 +" 1 index)) + (.let [it (..item index it)] + (.if + {.#Some } + (again ("lux i64 +" 1 index))))) + {.#None}))))) + )]))] [example (.-> r .Bit) (? it) r it] [example' (.-> Nat r .Bit) (? index it) [Nat r] [index it]] ) -(template: .public (clone ) - [((.is (.All (_ a) (.-> (..Array a) (..Array a))) - (.function (clone it) - (.let [size (..size it)] - (..copy! size 0 it 0 (..empty size))))) - )]) - -(template: .public (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]) - (.case input - {.#End} - output - - {.#Item head tail} - (.exec - (..has! index head output) - (again ("lux i64 +" 1 index) tail))))))) - )]) +(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]) + (.case input + {.#End} + output + + {.#Item head tail} + (.exec + (..has! index head output) + (again ("lux i64 +" 1 index) tail))))))) + )])) (def: underflow Nat (-- 0)) -(`` (template: (list|-default ) - [((.is (.All (_ r w) (.-> (.List r) (..Array' r w) (.List r))) - (.function (list|-default empty array) - (.loop (again [index ("lux i64 -" 1 (..size array)) - output empty]) - (.if ("lux i64 =" (~~ (.static ..underflow)) index) - output - (again ("lux i64 -" 1 index) - (.if (..lacks? index array) - output - {.#Item (..item index array) output})))))) - )])) - -(`` (template: (list|+default ) - [((.is (.All (_ r w) (.-> r (..Array' r w) (.List r))) - (.function (list|+default default array) - (.loop (again [index ("lux i64 -" 1 (..size array)) - output (`` (.is (.List (~~ (.these (~~ (.type_of default))))) - {.#End}))]) - (.if ("lux i64 =" (~~ (.static ..underflow)) index) - output - (again ("lux i64 -" 1 index) - {.#Item (.if (..lacks? index array) - default - (..item index array)) - output}))))) - )])) - -(`` (template: .public (list ) - [((.is (.All (_ r w) (.-> (.Maybe r) (..Array' r w) (.List r))) - (.function (list default array) - (.case default - {.#Some default} - (~~ (..list|+default default array)) - - {.#None} - (~~ (..list|-default {.#End} array))))) - )])) - -(template: .public (= ) - [((.is (.All (_ r w0 w1) (.-> (.-> r r .Bit) (..Array' r w0) (..Array' r w1) .Bit)) - (.function (= //#= left/* right/*) - (.let [size (..size left/*)] - (.and ("lux i64 =" (..size right/*) size) - (.loop (again [index 0]) - (.if ("lux i64 <" (.int size) (.int index)) - (.if (..lacks? index left/*) - (..lacks? index right/*) - (.if (..lacks? index right/*) - .false - (.and (//#= (..item index left/*) - (..item index right/*)) - (again ("lux i64 +" 1 index))))) - true)))))) - )]) - -(template: .public (composite ) - [((.is (.All (_ a) (.-> (..Array' a .Nothing) (..Array' a .Nothing) (..Array a))) - (.function (composite left/* right/*) - (.let [|left| (..size left/*) - |right| (..size right/*)] - (.|> (..empty ("lux i64 +" |left| |right|)) - (..copy! |left| 0 left/* 0) - (..copy! |right| 0 right/* |left|))))) - )]) - -(template: .public (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 ("lux i64 <" (.int size) (.int index)) - (.if (..lacks? index it) - (again ("lux i64 +" 1 index) so_far) - (again ("lux i64 +" 1 index) ($ index (..item index it) so_far))) - so_far))))) - <$> )]) - -(template: .public (each <$> ) - [((.is (functor.Functor ..Array) - (.function (each $ input) - (..mix (.function (_ index item output) - (..has! index ($ item) output)) - (..empty (..size input)) - input))) - <$> )]) - -(template [ ] - [(template: .public ( ) - [((.is (.All (_ r w) - (.-> (.-> r .Bit) - (.-> (..Array' r w) .Bit))) - (.function ( ? it) - (.let [size (..size it)] - (.loop (again [index 0]) - (.if ("lux i64 <" (.int size) (.int index)) - (.if (..lacks? index it) - (again ("lux i64 +" 1 index)) - ( (? (..item index it)) - (again ("lux i64 +" 1 index)))) - ))))) - )])] +(`` (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 ("lux i64 -" 1 (..size array)) + output empty]) + (.if ("lux i64 =" (~~ (.static ..underflow)) index) + output + (again ("lux 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 ("lux i64 -" 1 (..size array)) + output (`` (.is (.List (~~ (.these (~~ (.type_of default))))) + {.#End}))]) + (.if ("lux i64 =" (~~ (.static ..underflow)) index) + output + (again ("lux 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) + (.case 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 ("lux i64 =" (..size right/*) size) + (.loop (again [index 0]) + (.if ("lux i64 <" (.int size) (.int index)) + (.if (..lacks? index left/*) + (..lacks? index right/*) + (.if (..lacks? index right/*) + .false + (.and (//#= (..item index left/*) + (..item index right/*)) + (again ("lux 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 ("lux 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 ("lux i64 <" (.int size) (.int index)) + (.if (..lacks? index it) + (again ("lux i64 +" 1 index) so_far) + (again ("lux 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 ("lux i64 <" (.int size) (.int index)) + (.if (..lacks? index it) + (again ("lux i64 +" 1 index)) + ( (? (..item index it)) + (again ("lux i64 +" 1 index)))) + ))))) + )]))] [every? .true and] [any? .false or] ) -(template: .public (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 ("lux i64 <" (.int size) (.int index)) - (with_expansions [ (again ("lux i64 +" 1 index))] - (.if (..lacks? index it) - - (.case (? (..item index it)) - {.#None} +(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 ("lux i64 <" (.int size) (.int index)) + (with_expansions [ (again ("lux i64 +" 1 index))] + (.if (..lacks? index it) - - output - output))) - {.#None}))))) - )]) + (.case (? (..item index it)) + {.#None} + + + output + output))) + {.#None}))))) + )])) -- cgit v1.2.3