From 5662ec063a1019518d5c1714d9c1ea42c7684737 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 2 Aug 2018 23:50:44 -0400 Subject: Replace the error-prone "n/range" calls with "indices". --- stdlib/source/lux/compiler/default/phase/analysis.lux | 10 ++++------ .../lux/compiler/default/phase/analysis/structure.lux | 2 +- .../default/phase/extension/analysis/host.jvm.lux | 6 +++--- .../lux/compiler/default/phase/synthesis/function.lux | 4 +++- .../default/phase/translation/scheme/function.jvm.lux | 2 +- stdlib/source/lux/compiler/default/repl/type.lux | 2 +- stdlib/source/lux/concurrency/actor.lux | 6 ++---- stdlib/source/lux/data/collection/array.lux | 8 ++++---- stdlib/source/lux/data/collection/dictionary.lux | 7 +------ stdlib/source/lux/data/collection/list.lux | 18 +++++++++--------- stdlib/source/lux/data/color.lux | 4 ++-- stdlib/source/lux/data/number/complex.lux | 2 +- stdlib/source/lux/macro/poly.lux | 2 +- stdlib/source/lux/macro/poly/equivalence.lux | 2 +- stdlib/source/lux/macro/poly/functor.lux | 4 ++-- stdlib/source/lux/macro/poly/json.lux | 4 ++-- stdlib/source/lux/math/random.lux | 2 +- stdlib/source/lux/type/implicit.lux | 6 +++--- stdlib/test/test/lux/data/collection/array.lux | 2 +- 19 files changed, 43 insertions(+), 50 deletions(-) diff --git a/stdlib/source/lux/compiler/default/phase/analysis.lux b/stdlib/source/lux/compiler/default/phase/analysis.lux index ccf46b873..b0776141a 100644 --- a/stdlib/source/lux/compiler/default/phase/analysis.lux +++ b/stdlib/source/lux/compiler/default/phase/analysis.lux @@ -116,11 +116,9 @@ (let [left (function.constant (|>> #.Left #Sum )) right (|>> #.Right #Sum )] (if (last? size tag) - (if (n/= 1 tag) - (right value) - (list/fold left - (right value) - (list.n/range 0 (n/- 2 tag)))) + (list/fold left + (right value) + (list.indices (dec tag))) (list/fold left (case value ( (#Sum _)) @@ -128,7 +126,7 @@ _ value) - (list.n/range 0 tag)))))] + (list.indices (inc tag))))))] [sum-analysis Analysis #Structure no-op] [sum-pattern Pattern #Complex id] diff --git a/stdlib/source/lux/compiler/default/phase/analysis/structure.lux b/stdlib/source/lux/compiler/default/phase/analysis/structure.lux index 2977eb777..1ef523c76 100644 --- a/stdlib/source/lux/compiler/default/phase/analysis/structure.lux +++ b/stdlib/source/lux/compiler/default/phase/analysis/structure.lux @@ -311,7 +311,7 @@ _ (if (n/= size-ts size-record) (wrap []) (///.throw record-size-mismatch [size-ts size-record recordT record])) - #let [tuple-range (list.n/range 0 (dec size-ts)) + #let [tuple-range (list.indices size-ts) tag->idx (dict.from-list name.Hash (list.zip2 tag-set tuple-range))] idx->val (monad.fold @ (function (_ [key val] idx->val) diff --git a/stdlib/source/lux/compiler/default/phase/extension/analysis/host.jvm.lux b/stdlib/source/lux/compiler/default/phase/extension/analysis/host.jvm.lux index 706d35e51..2713f59a2 100644 --- a/stdlib/source/lux/compiler/default/phase/extension/analysis/host.jvm.lux +++ b/stdlib/source/lux/compiler/default/phase/extension/analysis/host.jvm.lux @@ -604,7 +604,7 @@ (#.Primitive class-name (list)) arity - (|> (list.n/range 0 (dec arity)) + (|> (list.indices arity) list.reverse (list/map (|>> (n/* 2) inc #.Parameter)) (#.Primitive class-name) @@ -976,8 +976,8 @@ (-> Nat Nat (List Type)) (if (n/= 0 amount) (list) - (|> (list.n/range offset (|> amount dec (n/+ offset))) - (list/map idx-to-parameter)))) + (|> (list.indices amount) + (list/map (|>> (n/+ offset) idx-to-parameter))))) (def: (method-signature method-style method) (-> Method-Style Method (Operation Method-Signature)) diff --git a/stdlib/source/lux/compiler/default/phase/synthesis/function.lux b/stdlib/source/lux/compiler/default/phase/synthesis/function.lux index eadcd52b0..8a85b9873 100644 --- a/stdlib/source/lux/compiler/default/phase/synthesis/function.lux +++ b/stdlib/source/lux/compiler/default/phase/synthesis/function.lux @@ -107,7 +107,9 @@ (list) _ - (|> (list.size environment) dec (list.n/range 0) + (|> environment + list.size + list.indices (list/map (|>> #reference.Foreign))))) resolver' (if (and (nested? function-arity) direct?) diff --git a/stdlib/source/lux/compiler/default/phase/translation/scheme/function.jvm.lux b/stdlib/source/lux/compiler/default/phase/translation/scheme/function.jvm.lux index d4316a0b5..7eeb5a8ed 100644 --- a/stdlib/source/lux/compiler/default/phase/translation/scheme/function.jvm.lux +++ b/stdlib/source/lux/compiler/default/phase/translation/scheme/function.jvm.lux @@ -71,7 +71,7 @@ (_.let (list [@num-args (_.length/1 @curried)]) (<| (_.if (|> @num-args (_.=/2 arityO)) (<| (_.let (list [(reference.local' 0) @function])) - (_.let-values (list [[(|> (list.n/range 0 (dec arity)) + (_.let-values (list [[(|> (list.indices arity) (list/map ..input)) #.None] (_.apply/2 (_.global "apply") (_.global "values") @curried)])) diff --git a/stdlib/source/lux/compiler/default/repl/type.lux b/stdlib/source/lux/compiler/default/repl/type.lux index 212c5bcb9..7d3ac0d9c 100644 --- a/stdlib/source/lux/compiler/default/repl/type.lux +++ b/stdlib/source/lux/compiler/default/repl/type.lux @@ -110,7 +110,7 @@ (n/= num-tags (list.size casesR+)))] (wrap (function (_ variantV) (loop [cases-left (list.zip3 tags - (list.n/range 0 (dec num-tags)) + (list.indices num-tags) casesR+) variantV variantV] (case cases-left diff --git a/stdlib/source/lux/concurrency/actor.lux b/stdlib/source/lux/concurrency/actor.lux index f6a6fbe0d..a9154877e 100644 --- a/stdlib/source/lux/concurrency/actor.lux +++ b/stdlib/source/lux/concurrency/actor.lux @@ -334,12 +334,10 @@ g!actor-refs (: (List Code) (if (list.empty? actor-vars) (list) - (|> actor-vars list.size dec - (list.n/range 0) (list/map (|>> code.nat (~) ($) (`)))))) + (|> actor-vars list.size list.indices (list/map (|>> code.nat (~) ($) (`)))))) ref-replacements (|> (if (list.empty? actor-vars) (list) - (|> actor-vars list.size dec - (list.n/range 0) (list/map (|>> code.nat (~) ($) (`))))) + (|> actor-vars list.size list.indices (list/map (|>> code.nat (~) ($) (`))))) (: (List Code)) (list.zip2 g!all-vars) (: (List [Code Code]))) diff --git a/stdlib/source/lux/data/collection/array.lux b/stdlib/source/lux/data/collection/array.lux index 09fe50412..f19ad6547 100644 --- a/stdlib/source/lux/data/collection/array.lux +++ b/stdlib/source/lux/data/collection/array.lux @@ -62,7 +62,7 @@ (#.Some value) (write (n/+ offset dest-start) value target))) dest-array - (list.n/range 0 (dec length))))) + (list.indices length)))) (def: #export (occupied array) {#.doc "Finds out how many cells in an array are occupied."} @@ -82,7 +82,7 @@ (All [a] (-> (Array a) Nat)) (n/- (occupied array) (size array))) -(def: #export (filter p xs) +(def: #export (filter! p xs) (All [a] (-> (-> a Bit) (Array a) (Array a))) (list/fold (function (_ idx xs') @@ -184,7 +184,7 @@ _ #0))) #1 - (list.n/range 0 (dec sxs))))) + (list.indices sxs)))) )) (structure: #export Monoid (All [a] (Monoid (Array a))) @@ -210,7 +210,7 @@ (#.Some x) (write idx (f x) mb))) (new arr-size) - (list.n/range 0 (dec arr-size))) + (list.indices arr-size)) )))) (structure: #export _ (Fold Array) diff --git a/stdlib/source/lux/data/collection/dictionary.lux b/stdlib/source/lux/data/collection/dictionary.lux index 3d4e84207..f48554f55 100644 --- a/stdlib/source/lux/data/collection/dictionary.lux +++ b/stdlib/source/lux/data/collection/dictionary.lux @@ -146,11 +146,6 @@ (array.copy idx 0 array 0) (array.copy (n/- idx new-size) (inc idx) array idx)))) -## Given a top-limit for indices, produces all indices in [0, R). -(def: indices-for - (-> Nat (List Index)) - (|>> dec (list.n/range 0))) - ## Increases the level-shift by the branching-exponent, to explore ## levels further down the tree. (def: level-up @@ -240,7 +235,7 @@ ## When #Base nodes grow too large, they're promoted to #Hierarchy to ## add some depth to the tree and help keep it's balance. -(def: hierarchy-indices (List Index) (indices-for hierarchy-nodes-size)) +(def: hierarchy-indices (List Index) (list.indices hierarchy-nodes-size)) (def: (promote-base put' Hash level bitmap base) (All [k v] diff --git a/stdlib/source/lux/data/collection/list.lux b/stdlib/source/lux/data/collection/list.lux index bbc649fb9..d11f0a080 100644 --- a/stdlib/source/lux/data/collection/list.lux +++ b/stdlib/source/lux/data/collection/list.lux @@ -379,6 +379,13 @@ [tail (List a) xs' "For a list of size N, returns the N-1 elements after the first one."] ) +(def: #export (indices size) + {#.doc "Produces all the valid indices for a given size."} + (All [a] (-> Nat (List Nat))) + (if (n/= 0 size) + (list) + (|> size dec (n/range 0)))) + ## [Syntax] (def: (identifier$ name) (-> Text Code) @@ -415,7 +422,7 @@ (^ (list [_ (#.Nat num-lists)])) (if (n/> 0 num-lists) (let [(^open ".") Functor - indices (n/range 0 (dec num-lists)) + indices (..indices num-lists) type-vars (: (List Code) (map (|>> nat/encode identifier$) indices)) zip-type (` (All [(~+ type-vars)] (-> (~+ (map (: (-> Code Code) (function (_ var) (` (List (~ var))))) @@ -459,7 +466,7 @@ (^ (list [_ (#.Nat num-lists)])) (if (n/> 0 num-lists) (let [(^open ".") Functor - indices (n/range 0 (dec num-lists)) + indices (..indices num-lists) g!return-type (identifier$ "\treturn-type\t") g!func (identifier$ "\tfunc\t") type-vars (: (List Code) (map (|>> nat/encode identifier$) indices)) @@ -568,10 +575,3 @@ {#.doc "Pairs every element in the list with its index, starting at 0."} (All [a] (-> (List a) (List [Nat a]))) (enumerate' 0 xs)) - -(def: #export (indices size) - {#.doc "Produces all the valid indices for a given size."} - (All [a] (-> Nat (List Nat))) - (if (n/= 0 size) - (list) - (|> size dec (n/range 0)))) diff --git a/stdlib/source/lux/data/color.lux b/stdlib/source/lux/data/color.lux index e070ced40..335b937dd 100644 --- a/stdlib/source/lux/data/color.lux +++ b/stdlib/source/lux/data/color.lux @@ -291,7 +291,7 @@ (from-hsl [(|> idx .int int-to-frac (f/* slice) (f/+ hue) normalize) saturation luminance])) - (list.n/range 0 (dec results)))))) + (list.indices results))))) (def: #export (monochromatic results color) (-> Nat Color (List Color)) @@ -299,7 +299,7 @@ (list) (let [[hue saturation brightness] (to-hsb color) slice (|> +1.0 (f// (|> results .int int-to-frac)))] - (|> (list.n/range 0 (dec results)) + (|> (list.indices results) (list/map (|>> .int int-to-frac (f/* slice) (f/+ brightness) diff --git a/stdlib/source/lux/data/number/complex.lux b/stdlib/source/lux/data/number/complex.lux index cd785fb31..aeefa03d6 100644 --- a/stdlib/source/lux/data/number/complex.lux +++ b/stdlib/source/lux/data/number/complex.lux @@ -297,7 +297,7 @@ nth-root-of-abs (|> input abs (get@ #real) (math.pow (f// r-nth +1.0))) nth-phi (|> input argument (f// r-nth)) slice (|> math.pi (f/* +2.0) (f// r-nth))] - (|> (list.n/range 0 (dec nth)) + (|> (list.indices nth) (list/map (function (_ nth') (let [inner (|> nth' .int int-to-frac (f/* slice) diff --git a/stdlib/source/lux/macro/poly.lux b/stdlib/source/lux/macro/poly.lux index 135cad97e..72a56b81d 100644 --- a/stdlib/source/lux/macro/poly.lux +++ b/stdlib/source/lux/macro/poly.lux @@ -192,7 +192,7 @@ (let [partialI (|> current-arg (n/* 2) (n/+ funcI)) partial-varI (inc partialI) partial-varL (label partial-varI) - partialC (` ((~ funcL) (~+ (|> (list.n/range 0 (dec num-args)) + partialC (` ((~ funcL) (~+ (|> (list.indices num-args) (list/map (|>> (n/* 2) inc (n/+ funcI) label)) list.reverse))))] (recur (inc current-arg) diff --git a/stdlib/source/lux/macro/poly/equivalence.lux b/stdlib/source/lux/macro/poly/equivalence.lux index af714ed51..672817ee6 100644 --- a/stdlib/source/lux/macro/poly/equivalence.lux +++ b/stdlib/source/lux/macro/poly/equivalence.lux @@ -116,7 +116,7 @@ (do @ [g!eqs (poly.tuple (p.many Equivalence)) #let [g!_ (code.local-identifier "_____________") - indices (|> (list.size g!eqs) dec (list.n/range 0)) + indices (list.indices (list.size g!eqs)) g!lefts (list/map (|>> nat/encode (text/compose "left") code.local-identifier) indices) g!rights (list/map (|>> nat/encode (text/compose "right") code.local-identifier) indices)]] (wrap (` (: (~ (@Equivalence inputT)) diff --git a/stdlib/source/lux/macro/poly/functor.lux b/stdlib/source/lux/macro/poly/functor.lux index 205e78566..d866db45c 100644 --- a/stdlib/source/lux/macro/poly/functor.lux +++ b/stdlib/source/lux/macro/poly/functor.lux @@ -74,8 +74,8 @@ outL (code.local-identifier "____________outL")] [inT+ outC] (poly.function (p.many poly.any) (Arg outL)) - #let [inC+ (|> (list.size inT+) dec - (list.n/range 0) + #let [inC+ (|> (list.size inT+) + list.indices (list/map (|>> %n (format "____________inC") code.local-identifier)))]] (wrap (` (function ((~ g!) (~+ inC+)) (let [(~ outL) ((~ valueC) (~+ inC+))] diff --git a/stdlib/source/lux/macro/poly/json.lux b/stdlib/source/lux/macro/poly/json.lux index 9eabdc816..f1a5a840a 100644 --- a/stdlib/source/lux/macro/poly/json.lux +++ b/stdlib/source/lux/macro/poly/json.lux @@ -163,8 +163,8 @@ (do @ [g!encoders (poly.tuple (p.many Codec//encode)) #let [g!_ (code.local-identifier "_______") - g!members (|> (list.size g!encoders) dec - (list.n/range 0) + g!members (|> (list.size g!encoders) + list.indices (list/map (|>> nat/encode code.local-identifier)))]] (wrap (` (: (~ (@JSON//encode inputT)) (function ((~ g!_) [(~+ g!members)]) diff --git a/stdlib/source/lux/math/random.lux b/stdlib/source/lux/math/random.lux index 826846aac..f743b2d35 100644 --- a/stdlib/source/lux/math/random.lux +++ b/stdlib/source/lux/math/random.lux @@ -313,7 +313,7 @@ [rand nat] (wrap (swap idx (n/% _size rand) vec)))) row - (list.n/range 0 (dec _size)))] + (list.indices _size))] (|> _shuffle (run (pcg-32 [123 seed])) product.right))) diff --git a/stdlib/source/lux/type/implicit.lux b/stdlib/source/lux/type/implicit.lux index 6f26a01f8..42db42900 100644 --- a/stdlib/source/lux/type/implicit.lux +++ b/stdlib/source/lux/type/implicit.lux @@ -326,10 +326,10 @@ (::: eq.= x y) "(List Nat) equivalence" (::: = - (list.n/range 1 10) - (list.n/range 1 10)) + (list.indices 10) + (list.indices 10)) "(Functor List) map" - (::: map inc (list.n/range 0 9)) + (::: map inc (list.indices 10)) "Caveat emptor: You need to make sure to import the module of any structure you want to use." "Otherwise, this macro will not find it.")} (case args diff --git a/stdlib/test/test/lux/data/collection/array.lux b/stdlib/test/test/lux/data/collection/array.lux index ac3061464..f1421f7ea 100644 --- a/stdlib/test/test/lux/data/collection/array.lux +++ b/stdlib/test/test/lux/data/collection/array.lux @@ -73,7 +73,7 @@ (and (n/= size (@.occupied array)) (n/= 0 (@.vacant array)))) (test "Filtering mutates the array to remove invalid values." - (exec (@.filter n/even? array) + (exec (@.filter! n/even? array) (and (n/< size (@.occupied array)) (n/> 0 (@.vacant array)) (n/= size (n/+ (@.occupied array) -- cgit v1.2.3