Paul King created GROOVY-12013:
----------------------------------

             Summary: Add CombinerChecker to verify associative combiners in 
injectParallel/sumParallel/Stream.reduce
                 Key: GROOVY-12013
                 URL: https://issues.apache.org/jira/browse/GROOVY-12013
             Project: Groovy
          Issue Type: New Feature
            Reporter: Paul King


h3. Summary

Adds two incubating annotations and an opt-in type-checking extension that
verify, at compile time, that the combining function passed to an
order-independent reduction is associative — the contract such reductions
silently require but cannot otherwise enforce.

h3. Annotations ({{groovy.transform}})

* {{@Associative}} — declares a two-argument combiner is associative:
  {{combine(a, combine(b, c)) == combine(combine(a, b), c)}}.
* {{@Reducer}} — associative *and* has an identity element, named via the
  {{zero()}} member (e.g. {{@Reducer(zero = '0')}}).

h3. CombinerChecker ({{groovy.typecheckers}})

A type-checking extension enabled with
{{@TypeChecked(extensions='groovy.typecheckers.CombinerChecker')}}.

It inspects the combiner of:
* Groovy's parallel collection reductions {{injectParallel}} and
  {{sumParallel}};
* the JDK stream reductions {{Stream}}, {{IntStream}}, {{LongStream}} and
  {{DoubleStream}} {{reduce}} (all overloads).

The combiner is accepted when it is:
* a method reference to an {{@Associative}}/{{@Reducer}} method; or
* a method reference to, or a thin delegating closure over, a {{Monoid}}
  or {{Semigroup}} from any library (matched by simple type name —
  Functional Java, Palatable Lambda, Purefun, etc. — configurable via the
  {{monoids}}/{{semigroups}} options, no dependency added).

It reports a compile-time error when:
* an inline closure applies a non-associative operator ({{-}}, {{/}}, {{%}})
  directly to its two arguments;
* the seed of a seeded reduction contradicts a {{@Reducer}}'s declared
  identity;
* a {{Semigroup}} (which has no identity) is used with a seeded reduction
  that requires one.

Two modes: the default (lenient) flags only high-confidence problems; *strict*
({{CombinerChecker(mode: 'strict')}}) additionally requires the combiner to
carry a declared, verifiable contract.

h3. Notes

* The checker runs only under {{@TypeChecked}}/{{@CompileStatic}} and treats
  the annotations/carrier types as asserted contracts (not proofs);
  sequential {{inject}} is deliberately never flagged.




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to