diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/library/lux/data/color.lux | 102 | ||||
-rw-r--r-- | stdlib/source/library/lux/data/color/named.lux | 13 | ||||
-rw-r--r-- | stdlib/source/library/lux/data/color/rgb.lux | 40 | ||||
-rw-r--r-- | stdlib/source/library/lux/ffi.lux | 2 | ||||
-rw-r--r-- | stdlib/source/library/lux/math/arithmetic/fixed_point.lux | 185 | ||||
-rw-r--r-- | stdlib/source/library/lux/math/arithmetic/infix.lux (renamed from stdlib/source/library/lux/math/infix.lux) | 0 | ||||
-rw-r--r-- | stdlib/source/library/lux/math/arithmetic/modular.lux (renamed from stdlib/source/library/lux/math/modular.lux) | 65 | ||||
-rw-r--r-- | stdlib/source/library/lux/web/css/value.lux | 13 | ||||
-rw-r--r-- | stdlib/source/library/lux/world/money.lux | 83 | ||||
-rw-r--r-- | stdlib/source/library/lux/world/money/currency.lux | 8 |
10 files changed, 375 insertions, 136 deletions
diff --git a/stdlib/source/library/lux/data/color.lux b/stdlib/source/library/lux/data/color.lux index a886acb79..2bfa5e5af 100644 --- a/stdlib/source/library/lux/data/color.lux +++ b/stdlib/source/library/lux/data/color.lux @@ -33,85 +33,19 @@ (def .public rgb (-> Color RGB) (|>> nominal.representation)) - - (def .public equivalence - (Equivalence Color) - (of equivalence.functor each ..rgb rgb.equivalence)) - - (def .public hash - (Hash Color) - (of hash.functor each ..rgb rgb.hash)) - - (with_template [<color> <rgb>] - [(def .public <color> - Color - (nominal.abstraction <rgb>))] - - [black rgb.black] - [white rgb.white] - ) - - (with_template [<color> <rgb>] - [(def .public <color> - (Monoid Color) - (implementation - (def identity - (nominal.abstraction - (of <rgb> identity))) - - (def (composite left right) - (nominal.abstraction - (of <rgb> composite - (nominal.representation left) - (nominal.representation right))))))] - - [addition rgb.addition] - [subtraction rgb.subtraction] - ) - - (def .public complement - (-> Color Color) - (|>> nominal.representation - rgb.complement - nominal.abstraction)) ) -(def (normal ratio) - (-> Frac Frac) - (cond (f.> +1.0 ratio) - (f.% +1.0 ratio) +(def (ratio it) + (-> Frac + Frac) + (cond (f.> +1.0 it) + (f.% +1.0 it) - (f.< +0.0 ratio) - (|> ratio (f.% +1.0) (f.+ +1.0)) + (f.< +0.0 it) + (|> it (f.% +1.0) (f.+ +1.0)) ... else - ratio)) - -(def .public (interpolated ratio end start) - (-> Frac Color Color Color) - (let [dS (..normal ratio) - dE (|> +1.0 (f.- dS)) - interpolated' (is (-> Nat Nat Nat) - (function (_ end start) - (|> (|> start .int int.frac (f.* dS)) - (f.+ (|> end .int int.frac (f.* dE))) - f.int - .nat))) - start (rgb start) - end (rgb end)] - (|> (rgb.rgb (interpolated' (rgb.red end) (rgb.red start)) - (interpolated' (rgb.green end) (rgb.green start)) - (interpolated' (rgb.blue end) (rgb.blue start))) - of_rgb))) - -(with_template [<name> <target>] - [(def .public (<name> ratio color) - (-> Frac Color Color) - (..interpolated ratio <target> color))] - - [darker ..black] - [brighter ..white] - ) + it)) (with_template [<op> <name>] [(def .public (<name> ratio it) @@ -120,7 +54,7 @@ (|> (hsl.hsl (hsl.hue it) (|> it hsl.saturation - (f.* (|> +1.0 (<op> (..normal ratio)))) + (f.* (|> +1.0 (<op> (..ratio ratio)))) (f.min +1.0)) (hsl.luminance it)) hsl.rgb @@ -147,12 +81,12 @@ saturation (hsl.saturation hsl) luminance (hsl.luminance hsl)] [color - (|> (hsl.hsl (|> hue (f.+ <1>) ..normal) + (|> (hsl.hsl (|> hue (f.+ <1>) ..ratio) saturation luminance) hsl.rgb of_rgb) - (|> (hsl.hsl (|> hue (f.+ <2>) ..normal) + (|> (hsl.hsl (|> hue (f.+ <2>) ..ratio) saturation luminance) hsl.rgb @@ -177,9 +111,9 @@ hsl.rgb ..of_rgb)))] [color - (|> hue (f.+ <1>) ..normal of_hue) - (|> hue (f.+ <2>) ..normal of_hue) - (|> hue (f.+ <3>) ..normal of_hue)])))] + (|> hue (f.+ <1>) ..ratio of_hue) + (|> hue (f.+ <2>) ..ratio of_hue) + (|> hue (f.+ <3>) ..ratio of_hue)])))] [square (|> +1.0 (f./ +4.0)) (|> +2.0 (f./ +4.0)) (|> +3.0 (f./ +4.0))] [tetradic (|> +2.0 (f./ +12.0)) (|> +6.0 (f./ +12.0)) (|> +8.0 (f./ +12.0))] @@ -197,9 +131,9 @@ hue (hsl.hue it) saturation (hsl.saturation it) luminance (hsl.luminance it) - spread (..normal spread)] + spread (..ratio spread)] (list#each (function (_ idx) - (|> (hsl.hsl (|> idx ++ .int int.frac (f.* spread) (f.+ hue) ..normal) + (|> (hsl.hsl (|> idx ++ .int int.frac (f.* spread) (f.+ hue) ..ratio) saturation luminance) hsl.rgb @@ -212,12 +146,12 @@ hue (hsb.hue it) saturation (hsb.saturation it) brightness (hsb.brightness it) - spread (..normal spread)] + spread (..ratio spread)] (|> (list.indices variations) (list#each (|>> ++ .int int.frac (f.* spread) (f.+ brightness) - ..normal + ..ratio (hsb.hsb hue saturation) hsb.rgb ..of_rgb))))) diff --git a/stdlib/source/library/lux/data/color/named.lux b/stdlib/source/library/lux/data/color/named.lux index 90c603977..a669b5940 100644 --- a/stdlib/source/library/lux/data/color/named.lux +++ b/stdlib/source/library/lux/data/color/named.lux @@ -3,17 +3,16 @@ [lux (.except) [math [number (.only hex)]]]] - ["[0]" // (.only Color) - ["[0]" rgb]]) + [// + ["//" rgb (.only RGB)]]) ... https://developer.mozilla.org/en-US/docs/Web/CSS/color_value (with_template [<red> <green> <blue> <name>] [(`` (def .public <name> - Color - (|> (rgb.rgb (hex <red>) - (hex <green>) - (hex <blue>)) - //.of_rgb)))] + RGB + (//.rgb (hex <red>) + (hex <green>) + (hex <blue>))))] ["F0" "F8" "FF" alice_blue] ["FA" "EB" "D7" antique_white] diff --git a/stdlib/source/library/lux/data/color/rgb.lux b/stdlib/source/library/lux/data/color/rgb.lux index 9f812baa9..deb97365f 100644 --- a/stdlib/source/library/lux/data/color/rgb.lux +++ b/stdlib/source/library/lux/data/color/rgb.lux @@ -14,6 +14,8 @@ [math [number ["n" nat] + ["i" int] + ["f" frac] ["[0]" i64]]] [meta [type @@ -145,3 +147,41 @@ [subtraction ..white n.min ..complement |>] ) ) + +(def (ratio it) + (-> Frac + Frac) + (cond (f.> +1.0 it) + (f.% +1.0 it) + + (f.< +0.0 it) + (|> it (f.% +1.0) (f.+ +1.0)) + + ... else + it)) + +(def .public (interpolated end ratio start) + (-> RGB Frac RGB + RGB) + (let [dS (..ratio ratio) + dE (|> +1.0 (f.- dS)) + interpolated' (is (-> Nat Nat + Nat) + (function (_ end start) + (|> (|> start .int i.frac (f.* dS)) + (f.+ (|> end .int i.frac (f.* dE))) + f.int + .nat)))] + (..rgb (interpolated' (..red end) (..red start)) + (interpolated' (..green end) (..green start)) + (interpolated' (..blue end) (..blue start))))) + +(with_template [<name> <target>] + [(def .public <name> + (-> Frac RGB + RGB) + (..interpolated <target>))] + + [darker ..black] + [brighter ..white] + ) diff --git a/stdlib/source/library/lux/ffi.lux b/stdlib/source/library/lux/ffi.lux index 70f47e5bf..0ad5a846f 100644 --- a/stdlib/source/library/lux/ffi.lux +++ b/stdlib/source/library/lux/ffi.lux @@ -679,7 +679,7 @@ (, (..imported class_name)))))))))] (` (def (, g!it) (syntax ((, g!it) [(, g!it) (<>.maybe <code>.any)]) - (.at meta.monad (,' in) + (.of meta.monad (,' in) (.list (`' (.exec (,* import!) ((,' ,) (when (, g!it) diff --git a/stdlib/source/library/lux/math/arithmetic/fixed_point.lux b/stdlib/source/library/lux/math/arithmetic/fixed_point.lux new file mode 100644 index 000000000..717d4261e --- /dev/null +++ b/stdlib/source/library/lux/math/arithmetic/fixed_point.lux @@ -0,0 +1,185 @@ +(.require + [library + [lux (.except) + [abstract + [equivalence (.only Equivalence)] + [order (.only Order)]] + [control + ["[0]" try (.only Try)] + ["[0]" exception (.only Exception)] + [function + ["[0]" inline]]] + [data + [text + ["%" \\format]]] + [math + [number + ["n" nat] + ["i" int] + ["[0]" i64]]] + [meta + [type + ["[0]" nominal]]]]] + [// (.only Arithmetic)]) + +(def .public maximum + Nat + (-- i64.width)) + +(nominal.def .public (Point @) + Nat + + (exception.def .public (point_exceeds_maximum it) + (Exception Nat) + (exception.report + (list ["Candidate" (%.nat it)] + ["Maximum" (%.nat maximum)]))) + + (def .public (point it) + (Ex (_ @) + (-> Nat + (Try (Point @)))) + (if (n.> maximum it) + (exception.except ..point_exceeds_maximum [it]) + {try.#Success (nominal.abstraction it)})) + + (def .public location + (All (_ @) + (-> (Point @) + Nat)) + (|>> nominal.representation)) + + (nominal.def .public (Fixed @) + Int + + (def sign + (-> Int + (I64 Any)) + (i64.and (i64.bit ..maximum))) + + (def of_units + (inline.inlined (_ @ it) + (All (_ @) + (-> (Point @) Int + (I64 Any))) + (|> it + .i64 + (i64.left_shifted (nominal.representation Point @)) + (i64.or (sign it))))) + + (def of_sub_units + (inline.inlined (_ @ it) + (All (_ @) + (-> (Point @) Rev + (I64 Any))) + (|> it + .i64 + (i.right_shifted (n.- (nominal.representation Point @) i64.width))))) + + (def .public (fixed @ units sub_units) + (All (_ @) + (-> (Point @) Int Rev + (Fixed @))) + (nominal.abstraction + (.int (i64.or (of_units @ units) + (of_sub_units @ sub_units))))) + + (with_template [<name> <type> <of>] + [(def .public (<name> @ it) + (All (_ @) + (-> (Point @) <type> + (Fixed @))) + (nominal.abstraction + (.int (<of> @ it))))] + + [of_int Int of_units] + [of_rev Rev of_sub_units] + ) + + (def .public (units @) + (All (_ @) + (-> (Point @) (Fixed @) + Int)) + (|>> nominal.representation + (i.right_shifted (nominal.representation Point @)))) + + (def .public (sub_units @) + (All (_ @) + (-> (Point @) (Fixed @) + Rev)) + (|>> nominal.representation + (i64.and (i64.mask (nominal.representation Point @))) + .rev)) + + (with_template [<composite_type> <post_processing> <fp> <int>] + [(def .public (<fp> _ parameter subject) + (All (_ @) + (-> (Point @) (Fixed @) (Fixed @) + <composite_type>)) + (<post_processing> + (<int> (nominal.representation parameter) + (nominal.representation subject))))] + + [(Fixed @) nominal.abstraction + i.+] + [(Fixed @) nominal.abstraction - i.-] + [(Fixed @) nominal.abstraction % i.%] + + [Bit |> = i.=] + [Bit |> < i.<] + [Bit |> <= i.<=] + [Bit |> > i.>] + [Bit |> >= i.>=] + ) + + (with_template [<fp> <u> <s>] + [(def .public (<fp> @ parameter subject) + (All (_ @) + (-> (Point @) (Fixed @) (Fixed @) + (Fixed @))) + (fixed @ + (<u> (units @ parameter) + (units @ subject)) + (.rev (<s> (.nat (sub_units @ parameter)) + (.nat (sub_units @ subject))))))] + + [* i.* n.*] + [/ i./ n./] + ) + ) + ) + +(def .public (equivalence @) + (All (_ @) + (-> (Point @) + (Equivalence (Fixed @)))) + (implementation + (def = (..= @)) + )) + +(def .public (order @) + (All (_ @) + (-> (Point @) + (Order (Fixed @)))) + (implementation + (def equivalence (..equivalence @)) + (def < (..< @)) + )) + +(def .public (arithmetic @) + (All (_ @) + (-> (Point @) + (Arithmetic (Fixed @)))) + (implementation + (def + (..+ @)) + (def - (..- @)) + (def * (..* @)) + (def / (../ @)) + (def % (..% @)) + )) + +(def .public (format @ it) + (All (_ @) + (-> (Point @) + (%.Format (Fixed @)))) + (%.format (%.int (..units @ it)) + (%.rev (..sub_units @ it)))) diff --git a/stdlib/source/library/lux/math/infix.lux b/stdlib/source/library/lux/math/arithmetic/infix.lux index 871f9c7f4..871f9c7f4 100644 --- a/stdlib/source/library/lux/math/infix.lux +++ b/stdlib/source/library/lux/math/arithmetic/infix.lux diff --git a/stdlib/source/library/lux/math/modular.lux b/stdlib/source/library/lux/math/arithmetic/modular.lux index e037e5c8c..12c3fb0bf 100644 --- a/stdlib/source/library/lux/math/modular.lux +++ b/stdlib/source/library/lux/math/arithmetic/modular.lux @@ -23,8 +23,8 @@ ["<[1]>" \\parser]] [type ["[0]" nominal (.except def)]]]]] - ["[0]" // - ["[1]" modulus (.only Modulus)]]) + [/// + ["/" modulus (.only Modulus)]]) (nominal.def .public (Mod m) (Record @@ -32,13 +32,17 @@ #value Int]) (def .public (modular modulus value) - (All (_ %) (-> (Modulus %) Int (Mod %))) + (All (_ %) + (-> (Modulus %) Int + (Mod %))) (abstraction [#modulus modulus - #value (i.mod (//.divisor modulus) value)])) + #value (i.mod (/.divisor modulus) value)])) (with_template [<name> <type> <side>] [(def .public <name> - (All (_ %) (-> (Mod %) <type>)) + (All (_ %) + (-> (Mod %) + <type>)) (|>> representation <side>))] [modulus (Modulus %) product.left] @@ -46,9 +50,10 @@ ) (exception.def .public (incorrect_modulus [modulus parsed]) - (All (_ %) (Exception [(Modulus %) Int])) + (All (_ %) + (Exception [(Modulus %) Int])) (exception.report - (list ["Expected" (i#encoded (//.divisor modulus))] + (list ["Expected" (i#encoded (/.divisor modulus))] ["Actual" (i#encoded parsed)]))) (def separator @@ -60,26 +65,30 @@ (<text>.and (<text>.one_of "-+") (<text>.many <text>.decimal)))) (def .public (codec expected) - (All (_ %) (-> (Modulus %) (Codec Text (Mod %)))) + (All (_ %) + (-> (Modulus %) + (Codec Text (Mod %)))) (implementation (def (encoded modular) (let [[_ value] (representation modular)] (all text#composite (i#encoded value) ..separator - (i#encoded (//.divisor expected))))) + (i#encoded (/.divisor expected))))) (def decoded (<text>.result (do <>.monad [[value _ actual] (all <>.and intL (<text>.this ..separator) intL) _ (<>.assertion (exception.error ..incorrect_modulus [expected actual]) - (i.= (//.divisor expected) actual))] + (i.= (/.divisor expected) actual))] (in (..modular expected value))))))) (with_template [<name> <op>] [(def .public (<name> reference subject) - (All (_ %) (-> (Mod %) (Mod %) Bit)) + (All (_ %) + (-> (Mod %) (Mod %) + Bit)) (let [[_ reference] (representation reference) [_ subject] (representation subject)] (<op> reference subject)))] @@ -92,25 +101,29 @@ ) (def .public equivalence - (All (_ %) (Equivalence (Mod %))) + (All (_ %) + (Equivalence (Mod %))) (implementation (def = ..=))) (def .public order - (All (_ %) (Order (Mod %))) + (All (_ %) + (Order (Mod %))) (implementation (def equivalence ..equivalence) (def < ..<))) (with_template [<name> <op>] [(def .public (<name> param subject) - (All (_ %) (-> (Mod %) (Mod %) (Mod %))) + (All (_ %) + (-> (Mod %) (Mod %) + (Mod %))) (let [[modulus param] (representation param) [_ subject] (representation subject)] (abstraction [#modulus modulus #value (|> subject (<op> param) - (i.mod (//.divisor modulus)))])))] + (i.mod (/.divisor modulus)))])))] [+ i.+] [- i.-] @@ -119,7 +132,9 @@ (with_template [<composition> <identity> <monoid>] [(def .public (<monoid> modulus) - (All (_ %) (-> (Modulus %) (Monoid (Mod %)))) + (All (_ %) + (-> (Modulus %) + (Monoid (Mod %)))) (implementation (def identity (..modular modulus <identity>)) @@ -131,25 +146,29 @@ ) (def .public (inverse modular) - (All (_ %) (-> (Mod %) (Maybe (Mod %)))) + (All (_ %) + (-> (Mod %) + (Maybe (Mod %)))) (let [[modulus value] (representation modular) - [[vk mk] gcd] (i.extended_gcd value (//.divisor modulus))] + [[vk mk] gcd] (i.extended_gcd value (/.divisor modulus))] (when gcd +1 {.#Some (..modular modulus vk)} _ {.#None}))) ) (exception.def .public (moduli_are_not_equal [reference subject]) - (All (_ r% s%) (Exception [(Modulus r%) (Modulus s%)])) + (All (_ r% s%) + (Exception [(Modulus r%) (Modulus s%)])) (exception.report - (list ["Reference" (i#encoded (//.divisor reference))] - ["Subject" (i#encoded (//.divisor subject))]))) + (list ["Reference" (i#encoded (/.divisor reference))] + ["Subject" (i#encoded (/.divisor subject))]))) (def .public (adapter reference subject) (All (_ r% s%) (-> (Modulus r%) (Modulus s%) - (Try (-> (Mod s%) (Mod r%))))) - (if (//.= reference subject) + (Try (-> (Mod s%) + (Mod r%))))) + (if (/.= reference subject) {try.#Success (|>> ..value (..modular reference))} (exception.except ..moduli_are_not_equal [reference subject]))) diff --git a/stdlib/source/library/lux/web/css/value.lux b/stdlib/source/library/lux/web/css/value.lux index 37c8580a0..c23a1fda0 100644 --- a/stdlib/source/library/lux/web/css/value.lux +++ b/stdlib/source/library/lux/web/css/value.lux @@ -5,9 +5,9 @@ ["[0]" maybe]] [data ["[0]" product] - ["[0]" color (.only) + [color [pigment (.only Pigment)] - ["[0]" rgb]] + ["[0]" rgb (.only RGB)]] ["[0]" text (.only) ["%" \\format (.only Format format)]] [collection @@ -830,12 +830,11 @@ (|>> abstraction)) (def .public (rgb color) - (-> color.Color + (-> RGB (Value Color)) - (let [color (color.rgb color)] - (..apply "rgb" (list (%.nat (rgb.red color)) - (%.nat (rgb.green color)) - (%.nat (rgb.blue color)))))) + (..apply "rgb" (list (%.nat (rgb.red color)) + (%.nat (rgb.green color)) + (%.nat (rgb.blue color))))) (def .public (rgba pigment) (-> Pigment diff --git a/stdlib/source/library/lux/world/money.lux b/stdlib/source/library/lux/world/money.lux index 80c5f5d09..cd0724459 100644 --- a/stdlib/source/library/lux/world/money.lux +++ b/stdlib/source/library/lux/world/money.lux @@ -2,7 +2,8 @@ [library [lux (.except) [abstract - ["[0]" equivalence (.only Equivalence)]] + [equivalence (.only Equivalence)] + ["[0]" order (.only Order)]] [data ["[0]" product] ["[0]" text (.only) @@ -30,6 +31,16 @@ [#currency currency #amount amount])) + (def .public (of_units currency it) + (All (_ currency) + (-> (/.Currency currency) Nat + (Money currency))) + (money currency + (n.* (/.sub_divisions currency) + it))) + + (def .public of_sub_units money) + (with_template [<name> <slot> <type>] [(def .public <name> (All (_ currency) @@ -42,15 +53,41 @@ [amount #amount Nat] ) + (def .public order + (All (_ currency) + (Order (Money currency))) + (of order.functor each + ..amount + n.order)) + + (def .public < + (All (_ currency) + (-> (Money currency) (Money currency) + Bit)) + (of ..order <)) + + (with_template [<name> <order>] + [(def .public <name> + (All (_ currency) + (-> (Money currency) (Money currency) + Bit)) + (<order> order))] + + [<= order.<=] + [> order.>] + [>= order.>=] + ) + (def .public equivalence - (All (_ of) - (Equivalence (Money of))) - (of equivalence.functor each - (|>> nominal.representation) - (all product.equivalence - /.equivalence - n.equivalence - ))) + (All (_ currency) + (Equivalence (Money currency))) + (of ..order equivalence)) + + (def .public = + (All (_ currency) + (-> (Money currency) (Money currency) + Bit)) + (of ..equivalence =)) (def .public (+ parameter subject) (All (_ currency) @@ -79,10 +116,36 @@ (All (_ currency) (%.Format (Money currency))) (let [[currency amount] (nominal.representation it) - [macro micro] (n./% (/.subdivisions currency) amount)] + [macro micro] (n./% (/.sub_divisions currency) amount)] (%.format (%.nat macro) (when micro 0 "" _ (%.format "." (%.nat micro))) " " (/.alphabetic_code currency)))) ) + +(with_template [<order> <name>] + [(def .public (<name> left right) + (All (_ currency) + (-> (Money currency) (Money currency) + (Money currency))) + (if (<order> (..amount left) + (..amount right)) + right + left))] + + [n.< min] + [n.> max] + ) + +(with_template [<*> <name>] + [(def .public (<name> it) + (All (_ currency) + (-> (Money currency) + Nat)) + (<*> (/.sub_divisions (..currency it)) + (..amount it)))] + + [n./ units] + [n.% sub_units] + ) diff --git a/stdlib/source/library/lux/world/money/currency.lux b/stdlib/source/library/lux/world/money/currency.lux index 7cefbb664..04292fa48 100644 --- a/stdlib/source/library/lux/world/money/currency.lux +++ b/stdlib/source/library/lux/world/money/currency.lux @@ -26,16 +26,16 @@ (Record [#alphabetic_code Text #numeric_code Nat - #subdivisions Nat]) + #sub_divisions Nat]) - (def .public (currency [alphabetic_code numeric_code subdivisions]) + (def .public (currency [alphabetic_code numeric_code sub_divisions]) (Ex (_ of) (-> [Text Nat Nat] (Currency of))) (nominal.abstraction [#alphabetic_code alphabetic_code #numeric_code numeric_code - #subdivisions subdivisions])) + #sub_divisions sub_divisions])) (with_template [<name> <slot> <type>] [(def .public <name> @@ -47,7 +47,7 @@ [alphabetic_code #alphabetic_code Text] [numeric_code #numeric_code Nat] - [subdivisions #subdivisions Nat] + [sub_divisions #sub_divisions Nat] ) (def .public equivalence |