[ 
https://issues.apache.org/jira/browse/GROOVY-12013?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King updated GROOVY-12013:
-------------------------------
    Description: 
h3. Summary

Adds two incubating annotations and an opt-in type-checking extension that
verify, at compile time, that the combiner passed to a parallel or stream
reduction is associative (and, for seeded forms, that the seed is an identity
element) — 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 and non-goals

* The checker runs only under {{@TypeChecked}}/{{@CompileStatic}} and treats
  the annotations/carrier types as asserted contracts (not proofs);
  sequential {{inject}} is deliberately never flagged.
* The inline-closure check is syntactic and assumes operators carry their
  conventional meaning. Resolving overloaded operators is a non-goal: in
  particular, overloading a normally-associative operator (e.g. {{+}}, {{*}})
  to be non-associative is poor style and is not detected by design. The
  declaration paths ({{@Associative}}/{{@Reducer}}, Monoid/Semigroup) are the
  reliable, overloading-immune way to assert the contract.


  was:
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 and non-goals

* The checker runs only under {{@TypeChecked}}/{{@CompileStatic}} and treats
  the annotations/carrier types as asserted contracts (not proofs);
  sequential {{inject}} is deliberately never flagged.
* The inline-closure check is syntactic and assumes operators carry their
  conventional meaning. Resolving overloaded operators is a non-goal: in
  particular, overloading a normally-associative operator (e.g. {{+}}, {{*}})
  to be non-associative is poor style and is not detected by design. The
  declaration paths ({{@Associative}}/{{@Reducer}}, Monoid/Semigroup) are the
  reliable, overloading-immune way to assert the contract.



> New optional type checking extension: 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
>            Assignee: Paul King
>            Priority: Major
>
> h3. Summary
> Adds two incubating annotations and an opt-in type-checking extension that
> verify, at compile time, that the combiner passed to a parallel or stream
> reduction is associative (and, for seeded forms, that the seed is an identity
> element) — 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 and non-goals
> * The checker runs only under {{@TypeChecked}}/{{@CompileStatic}} and treats
>   the annotations/carrier types as asserted contracts (not proofs);
>   sequential {{inject}} is deliberately never flagged.
> * The inline-closure check is syntactic and assumes operators carry their
>   conventional meaning. Resolving overloaded operators is a non-goal: in
>   particular, overloading a normally-associative operator (e.g. {{+}}, {{*}})
>   to be non-associative is poor style and is not detected by design. The
>   declaration paths ({{@Associative}}/{{@Reducer}}, Monoid/Semigroup) are the
>   reliable, overloading-immune way to assert the contract.



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

Reply via email to