Skip to content

Commit

Permalink
Can now do pure transduction in parallel.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoejp committed Apr 5, 2024
1 parent b300e9f commit a3e8782
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 110 deletions.
1 change: 1 addition & 0 deletions stdlib/source/library/lux/abstract/functor.lux
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[function
["//" polytypism]]]])

... https://en.wikipedia.org/wiki/Functor
(every .public (Functor !)
(Interface
(is (for_any (_ input output)
Expand Down
10 changes: 6 additions & 4 deletions stdlib/source/library/lux/abstract/functor/contravariant.lux
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
[library
[lux (.except)]])

(every .public (Functor !)
(every .public (Functor context)
(Interface
(is (for_any (_ @0 @1)
(-> (-> @1 @0)
(-> (! @0) (! @1))))
(is (for_any (_ before after)
(-> (-> after
before)
(-> (context before)
(context after))))
each)))
29 changes: 29 additions & 0 deletions stdlib/source/library/lux/data/collection/list.lux
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,32 @@
[(List it) (List it)]))
[(first position it)
(after position it)])

(the .public (sub size_of_complete_segment it)
(for_any (_ it)
(-> Natural (List it)
(List (List it))))
(when size_of_complete_segment
0 ..empty
_ (let [it (..reification it)
[complete partial] (n./% size_of_complete_segment (/.size it))
[segments size_of_last_segment] (when partial
0 [complete size_of_complete_segment]
_ [(++ complete) partial])
last_segment (-- segments)]
(|> (static segments
(function (_ current_segment)
[(++ current_segment)
(product.right
(static (if (n.= last_segment current_segment)
size_of_last_segment
size_of_complete_segment)
(function (_ position_in_current_segment)
[(++ position_in_current_segment)
(/.item (|> current_segment
(n.* size_of_complete_segment)
(n.+ position_in_current_segment))
it)])
0))])
0)
product.right))))
80 changes: 40 additions & 40 deletions stdlib/source/library/lux/data/collection/sequence.lux
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@
["[0]" code (.only)
["<[1]>" \\projection]]]]])

(every (Node a)
(Variant
{#Base (Array a)}
{#Hierarchy (Array (Node a))}))
(every Base
Array)

(every (Base a)
(Array a))
(every (Node it)
(Variant
{#Base (Base it)}
{#Hierarchy (Array (Node it))}))

(every (Hierarchy a)
(Array (Node a)))
(every (Hierarchy it)
(Array (Node it)))

(every Level
Natural)
Expand Down Expand Up @@ -282,11 +282,11 @@

(exception.the base_was_not_found)

(the .public (within_bounds? sequence idx)
(the .public (within_bounds? sequence)
(for_any (_ it)
(-> (Sequence it) Natural
Bit))
(n.< (its #size sequence) idx))
(-> (Sequence it)
(Predicate Natural)))
(n.< (its #size sequence)))

(the (base_for idx sequence)
(for_any (_ it)
Expand Down Expand Up @@ -461,17 +461,17 @@
(Sequence it)))
(list#mix ..suffix ..empty))

(the .public (member? equivalence sequence val)
(the .public (member? equivalence sequence)
(for_any (_ it)
(-> (Equivalence it) (Sequence it) it
Bit))
(list.member? equivalence (..as_list sequence) val))
(-> (Equivalence it) (Sequence it)
(Predicate it)))
(list.member? equivalence (..as_list sequence)))

(the .public empty?
(for_any (_ it)
(-> (Sequence it)
Bit))
(|>> (its #size) (n.= 0)))
(Predicate (Sequence it)))
(|>> (its #size)
(n.= 0)))

(the .public sequence
(syntax.macro (_ [elems (<>.some <code>.any)])
Expand Down Expand Up @@ -556,39 +556,39 @@
list.reversed
..of_list))

(template.with [<name> <array> <init> <op>]
(template.with [<name> ,for_array <init> <op>]
[(the .public <name>
(for_any (_ it)
(-> (Predicate it) (Sequence it)
Bit))
(let [help (is (for_any (_ a)
(-> (Predicate a) (Node a) Bit))
(function (help predicate node)
(when node
{#Base base}
(<array> predicate base)

{#Hierarchy hierarchy}
(<array> (help predicate) hierarchy))))]
(function (<name> predicate sequence)
(let [(open "_[0]") sequence]
(<op> (help predicate {#Hierarchy _#root})
(help predicate {#Base _#tail}))))))]
(-> (Predicate it)
(Predicate (Sequence it))))
(let [for_node (is (for_any (_ it)
(-> (Predicate it)
(Predicate (Node it))))
(function (for_node predicate node)
(when node
{#Base base}
(,for_array predicate base)

{#Hierarchy hierarchy}
(,for_array (for_node predicate) hierarchy))))]
(function (<name> predicate (open "_[0]"))
(<op> (,for_array (for_node predicate) _#root)
(,for_array predicate _#tail)))))]

[every? array.every? #1 and]
[any? array.any? #0 or]
)

(the .public (only when items)
(the .public (only when)
(for_any (_ it)
(-> (-> it Bit) (Sequence it)
(Sequence it)))
(-> (Predicate it)
(-> (Sequence it)
(Sequence it))))
(..mix (function (_ item output)
(if (when item)
(..suffix item output)
output))
..empty
items))
..empty))

(the (one|node check items)
(for_any (_ input output)
Expand Down
11 changes: 6 additions & 5 deletions stdlib/source/library/lux/function/polytypism/mix.lux
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
[library
[lux (.except)
[abstract
[monoid (.only Monoid)]]]]
[monoid (.only Monoid)]]
[transduction (.only Reduction)]]]
["[0]" //])

(every .public (Mix !)
(every .public (Mix context)
(Interface
(is (for_any (_ _0 _1)
(-> (-> _1 _0 _0) _0 (! _1)
_0))
(is (for_any (_ whole piece)
(-> (Reduction whole piece) whole (context piece)
whole))
mix)))

(the .public (with_monoid monoid mix value)
Expand Down
4 changes: 3 additions & 1 deletion stdlib/source/library/lux/test/property.lux
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@
(the .public coverage
(syntax.macro (_ [coverage <code>.any
condition <code>.any])
(in (list (` (by random.monad (,' in) (//.coverage (, coverage) (, condition))))))))
(in (list (` (by random.monad (,' in)
(//.coverage (, coverage)
(, condition))))))))

(the .public for
(syntax.macro (_ [coverage <code>.any
Expand Down
19 changes: 15 additions & 4 deletions stdlib/source/library/lux/transduction.lux
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@

(.using
[library
[lux (.except)]])
[lux (.except)
[abstract
[functor
["[0]" contravariant]]]]])

... https://clojure.org/reference/reducers
... https://clojure.org/news/2012/05/08/reducers
... https://clojure.org/news/2012/05/15/anatomy-of-reducer
(every .public (Reduction piece whole)
(every .public (Reduction whole piece)
(-> piece whole
whole))

(the .public functor
(for_any (_ whole)
(contravariant.Functor (Reduction whole)))
(implementation
(the (each value it)
(function (_ piece whole)
(it (value piece) whole)))))

... https://clojure.org/reference/transducers
... https://en.wikipedia.org/wiki/Transduction
(every .public (Transduction to_consume to_produce
outer_whole inner_whole)
(-> (Reduction to_produce inner_whole)
(Reduction to_consume outer_whole)))
(-> (Reduction inner_whole to_produce)
(Reduction outer_whole to_consume)))
99 changes: 77 additions & 22 deletions stdlib/source/library/lux/transduction/pure.lux
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
(.using
[library
[lux (.except only and)
[abstract
[functor (.only Functor)]
[monoid (.only Monoid)]]
["[0]" function (.only)
[predicate (.only Predicate)]
[polytypism
Expand All @@ -12,13 +15,33 @@
["[0]" try]]
[data
[collection
["[0]" dictionary (.only Dictionary)]]]]]
["[0]" list (.use "[1]#[0]" functor)]
["[0]" array
["[1]" \\unsafe]]
["[0]" dictionary (.only Dictionary)]]]
[concurrency
["[0]" future (.only Future) (.use "[1]#[0]" monad)]]
[math
[number
["n" natural]
["[0]" i64]]]
[control
[io (.only io)]]]]
["[0]" //])

(every .public (Transduction to_consume to_produce whole)
(//.Transduction to_consume to_produce
whole whole))

(the .public functor
(for_any (_ to_consume)
(Functor (Transduction to_consume)))
(implementation
(the (each value it)
(function (_ reduction)
(it (function (_ piece whole)
(reduction (value piece) whole)))))))

(the .public nothing
(for_any (_ it)
(Transduction it it))
Expand All @@ -34,32 +57,31 @@
(for_any (_ to_consume to_produce)
(-> (-> to_consume to_produce)
(Transduction to_consume to_produce)))
(function (_ mix part whole)
(mix (it part) whole)))
(by ..functor each it ..nothing))

(the .public (only ?)
(for_any (_ it)
(-> (Predicate it)
(Transduction it it)))
(function (_ mix part whole)
(if (? part)
(mix part whole)
(function (_ reduction piece whole)
(if (? piece)
(reduction piece whole)
whole)))

(the .public (many collection::mix)
(the .public (many mix)
(for_any (_ collection it)
(-> (Mix collection)
(Transduction (collection it) it)))
(function (_ mix part whole)
(collection::mix mix whole part)))
(function (_ reduction piece whole)
(mix reduction whole piece)))

(the .public maybe
(for_any (_ it)
(Transduction (Maybe it) it))
(function (_ mix part whole)
(when part
{.#Some part}
(mix part whole)
(function (_ reduction piece whole)
(when piece
{.#Some piece}
(reduction piece whole)

{.#None}
whole)))
Expand All @@ -68,18 +90,51 @@
(for_any (_ it)
(-> (Dictionary it it)
(Transduction it it)))
(function (_ mix part whole)
(mix (|> replacements
(dictionary.value part)
(try.else part))
whole)))
(function (_ reduction piece whole)
(reduction (|> replacements
(dictionary.value piece)
(try.else piece))
whole)))

(the .public (in_series mix initial
(the .public (in_series reduction initial
transduction
collection::mix collection)
mix collection)
(for_any (_ collection to_consume to_mix whole)
(-> (//.Reduction to_mix whole) whole
(-> (//.Reduction whole to_mix) whole
(Transduction to_consume to_mix)
(Mix collection) (collection to_consume)
whole))
(collection::mix (transduction mix) initial collection))
(mix (transduction reduction) initial collection))

(every .public Parallelism
Natural)

(the .public none
Parallelism
0)

(the .public suggestion
Parallelism
... 2^9 = 512
(i64.left_shifted 9 1))

(the .public (in_parallel parallelism monoid
transduction
it)
(for_any (_ to_consume whole)
(-> Parallelism (Monoid whole)
(Transduction to_consume whole)
(List to_consume)
(Future whole)))
(with monoid
(let [in_series (in_series composite identity transduction list.mix)]
(when parallelism
..none
(future#in (in_series it))

_
(|> it
(list.sub parallelism)
(list#each (|>> in_series io future.soon))
(list.all' future.monad)
(future#each (list.mix composite identity)))))))
Loading

0 comments on commit a3e8782

Please sign in to comment.