FSharpPlus


Monoid

Types with an associative binary operation that has an identity. ___

Minimal complete definition

static member get_Zero () :'Monoid
static member (+) (x:'Monoid, y:'Monoid) :'Monoid

Other operations

static member Sum (x:Seq<'Monoid>) :'Monoid

Rules

zero + x = x
x + zero = x
(x + y) + z = x + (y + z)
Seq.sum = Seq.fold (+) zero
sum = fold (+) zero (generic to all foldables)

Related Abstractions

Concrete implementations

From .Net/F#

From F#+

Suggest another concrete implementation

Examples

#r @"nuget: FSharpPlus"
open FSharpPlus
open FSharpPlus.Math.Generic
open FSharpPlus.Data


/// A monoid that represents results of comparisons
type Ordering = LT|EQ|GT with
    static member        Zero = EQ
    static member        (+) (x:Ordering, y) = 
        match x, y with
        | LT, _ -> LT
        | EQ, a -> a
        | GT, _ -> GT

let inline compare' x y =
    match compare x y with
    | a when a > 0 -> GT
    | a when a < 0 -> LT
    | _            -> EQ

let resGreater = compare' 7 6

/// A monoid of all numbers from 0 to 4
type Mod5 = Mod5 of uint32 with
    static member inline get_Zero() = Mod5 0u
    static member inline (+) (Mod5 x, Mod5 y) = Mod5 ( (x + y) % 5u)
let Mod5 x = Mod5 (x % 5u)


// Results of Monoid operations
let emptyLst:list<int> = zero
let zeroUint:Mod5   = zero
let res1 = zero ++ Mod5 11u
let res2  = sum <| map Mod5 [4u; 2u; 1u]
let res3  = sum [zero; Mod5 2G; Mod5 6G]
let res8n4 = [zero; [8;4]]
let res15 = Mult 15 ++ zero
let resTrue = sum [zero; Any true]
let resFalse = sum (map All [true;false])
let resHi = zero ++ "Hi"
let resGT = zero ++  GT
let resLT = sum [zero; LT ; EQ ;GT]
let res9823 = sum (map Dual [zero;"3";"2";"8";"9"])
let resBA = Dual "A" ++ Dual "B" 
let resEl00:list<int>*float = zero
let resS3P20     = (1G, Mult 5.0) ++  (2, Mult 4G)
let res230       = (zero,zero) ++ ([2],[3.0])
let res243       = ([2;4],[3]) ++ zero
let res23        = zero ++ ([2],"3")
let resLtDualGt  =  (LT,Dual GT) ++ zero
let res230hiSum2 = (zero, zero, 2) ++ ([2], ([3.0], "hi"), zero)
let res230hiS4P3 = (zero, zero   ) ++ ([2], ([3.0], "hi", 4, Mult (6 % 2)))
let tuple5 :string*(Any*string)*(All*All*All)*int*string = zero
module Seq from Microsoft.FSharp.Collections
<summary>Contains operations for working with values of type <see cref="T:Microsoft.FSharp.Collections.seq`1" />.</summary>
val sum: source: seq<'T> -> 'T (requires member (+) and member get_Zero)
<summary>Returns the sum of the elements in the sequence.</summary>
<remarks>The elements are summed using the <c>+</c> operator and <c>Zero</c> property associated with the generated type.</remarks>
<param name="source">The input sequence.</param>
<returns>The computed sum.</returns>
<example id="sum-1"><code lang="fsharp"> let input = [ 1; 5; 3; 2 ] input |&gt; Seq.sum </code> Evaluates to <c>11</c>. </example>
val fold: folder: ('State -> 'T -> 'State) -> state: 'State -> source: seq<'T> -> 'State
<summary>Applies a function to each element of the collection, threading an accumulator argument through the computation. If the input function is <c>f</c> and the elements are <c>i0...iN</c> then computes <c>f (... (f s i0)...) iN</c></summary>
<param name="folder">A function that updates the state with each element from the sequence.</param>
<param name="state">The initial state.</param>
<param name="source">The input sequence.</param>
<returns>The state object after the folding function is applied to each element of the sequence.</returns>
<exception cref="T:System.ArgumentNullException">Thrown when the input sequence is null.</exception>
<example id="fold-1"><code lang="fsharp"> type Charge = | In of int | Out of int let inputs = [In 1; Out 2; In 3] (0, inputs) ||&gt; Seq.fold (fun acc charge -&gt; match charge with | In i -&gt; acc + i | Out o -&gt; acc - o) </code> Evaluates to <c>2</c></example>
namespace FSharpPlus
namespace FSharpPlus.Math
module Generic from FSharpPlus.Math
<summary> Generic numbers, functions and operators. By opening this module some common operators become restricted, like (+) to 'T-&gt;'T-&gt;'T </summary>
namespace FSharpPlus.Data
union case Ordering.LT: Ordering
union case Ordering.EQ: Ordering
union case Ordering.GT: Ordering
val x: Ordering
type Ordering = | LT | EQ | GT static member (+) : x: Ordering * y: Ordering -> Ordering static member Zero: Ordering
 A monoid that represents results of comparisons
val y: Ordering
val a: Ordering
val compare': x: 'a -> y: 'a -> Ordering (requires comparison)
val x: 'a (requires comparison)
val y: 'a (requires comparison)
val compare: e1: 'T -> e2: 'T -> int (requires comparison)
<summary>Generic comparison.</summary>
<param name="e1">The first value.</param>
<param name="e2">The second value.</param>
<returns>The result of the comparison.</returns>
<example id="compare-example"><code lang="fsharp"> compare 1 2 // Evaluates to -1 compare [1;2;3] [1;2;4] // Evaluates to -1 compare 2 2 // Evaluates to 0 compare [1;2;3] [1;2;3] // Evaluates to 0 compare 2 1 // Evaluates to 1 compare [1;2;4] [1;2;3] // Evaluates to 1 </code></example>
val a: int
val resGreater: Ordering
Multiple items
union case Mod5.Mod5: uint32 -> Mod5

--------------------
type Mod5 = | Mod5 of uint32 static member (+) : Mod5 * Mod5 -> Mod5 static member get_Zero: unit -> Mod5
 A monoid of all numbers from 0 to 4
Multiple items
val uint32: value: 'T -> uint32 (requires member op_Explicit)
<summary>Converts the argument to unsigned 32-bit integer. This is a direct conversion for all primitive numeric types. For strings, the input is converted using <c>UInt32.Parse()</c> with InvariantCulture settings. Otherwise the operation requires an appropriate static conversion method on the input type.</summary>
<param name="value">The input value.</param>
<returns>The converted uint32</returns>
<example id="uint32-example"><code lang="fsharp"></code></example>


--------------------
[<Struct>] type uint32 = System.UInt32
<summary>An abbreviation for the CLI type <see cref="T:System.UInt32" />.</summary>
<category>Basic Types</category>


--------------------
type uint32<'Measure> = uint<'Measure>
<summary>The type of 32-bit unsigned integer numbers, annotated with a unit of measure. The unit of measure is erased in compiled code and when values of this type are analyzed using reflection. The type is representationally equivalent to <see cref="T:System.UInt32" />.</summary>
<category>Basic Types with Units of Measure</category>
val x: uint32
val y: uint32
Multiple items
val Mod5: x: uint32 -> Mod5

--------------------
type Mod5 = | Mod5 of uint32 static member (+) : Mod5 * Mod5 -> Mod5 static member get_Zero: unit -> Mod5
 A monoid of all numbers from 0 to 4
val emptyLst: int list
type 'T list = List<'T>
<summary>The type of immutable singly-linked lists. </summary>
<remarks>See the <see cref="T:Microsoft.FSharp.Collections.ListModule" /> module for further operations related to lists. Use the constructors <c>[]</c> and <c>::</c> (infix) to create values of this type, or the notation <c>[1; 2; 3]</c>. Use the values in the <c>List</c> module to manipulate values of this type, or pattern match against the values directly. See also <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/lists">F# Language Guide - Lists</a>. </remarks>
Multiple items
val int: value: 'T -> int (requires member op_Explicit)
<summary>Converts the argument to signed 32-bit integer. This is a direct conversion for all primitive numeric types. For strings, the input is converted using <c>Int32.Parse()</c> with InvariantCulture settings. Otherwise the operation requires an appropriate static conversion method on the input type.</summary>
<param name="value">The input value.</param>
<returns>The converted int</returns>
<example id="int-example"><code lang="fsharp"></code></example>


--------------------
[<Struct>] type int = int32
<summary>An abbreviation for the CLI type <see cref="T:System.Int32" />.</summary>
<category>Basic Types</category>


--------------------
type int<'Measure> = int
<summary>The type of 32-bit signed integer numbers, annotated with a unit of measure. The unit of measure is erased in compiled code and when values of this type are analyzed using reflection. The type is representationally equivalent to <see cref="T:System.Int32" />.</summary>
<category>Basic Types with Units of Measure</category>
val zero<'Monoid (requires member Zero)> : 'Monoid (requires member Zero)
<summary> A value that represents the 0 element of a Monoid. </summary>
<category index="4">Monoid</category>
val zeroUint: Mod5
val res1: Mod5
val res2: Mod5
val sum: x: 'Foldable<'Monoid> -> 'Monoid (requires member Fold and member ``+`` and member Zero)
<summary> Folds the sum of all monoid elements in the Foldable. </summary>
<category index="23">Additional Functions</category>
val map: f: ('T -> 'U) -> x: 'Functor<'T> -> 'Functor<'U> (requires member Map)
<summary>Lifts a function into a Functor.</summary>
<category index="1">Functor</category>
val res3: Mod5
val res8n4: int list list
val res15: Mult<int>
Multiple items
union case Mult.Mult: 'a -> Mult<'a>

--------------------
[<Struct>] type Mult<'a> = | Mult of 'a static member (+) : Mult<'n> * Mult<'n> -> Mult<'a2> (requires member ( * )) static member get_Zero: unit -> Mult<'a1> (requires member One)
<summary> Numeric wrapper for multiplication monoid (*, 1) </summary>
val resTrue: Any
Multiple items
union case Any.Any: bool -> Any

--------------------
[<Struct>] type Any = | Any of bool static member (+) : Any * Any -> Any static member Zero: Any
<summary> Boolean monoid under disjunction. </summary>
val resFalse: All
Multiple items
union case All.All: bool -> All

--------------------
[<Struct>] type All = | All of bool static member (+) : All * All -> All static member Zero: All
<summary> Boolean monoid under conjunction. </summary>
val resHi: string
val resGT: Ordering
val resLT: Ordering
val res9823: Dual<string>
Multiple items
union case Dual.Dual: 't -> Dual<'t>

--------------------
module Dual from FSharpPlus.Data
<summary> Basic operations on Dual </summary>

--------------------
[<Struct>] type Dual<'t> = | Dual of 't static member (+) : Dual<'T> * Dual<'T> -> Dual<'T> (requires member ``+``) static member get_Zero: unit -> Dual<'T> (requires member Zero)
<summary> The dual of a monoid, obtained by swapping the arguments of append. </summary>
val resBA: Dual<string>
val resEl00: int list * float
Multiple items
val float: value: 'T -> float (requires member op_Explicit)
<summary>Converts the argument to 64-bit float. This is a direct conversion for all primitive numeric types. For strings, the input is converted using <c>Double.Parse()</c> with InvariantCulture settings. Otherwise the operation requires an appropriate static conversion method on the input type.</summary>
<param name="value">The input value.</param>
<returns>The converted float</returns>
<example id="float-example"><code lang="fsharp"></code></example>


--------------------
[<Struct>] type float = System.Double
<summary>An abbreviation for the CLI type <see cref="T:System.Double" />.</summary>
<category>Basic Types</category>


--------------------
type float<'Measure> = float
<summary>The type of double-precision floating point numbers, annotated with a unit of measure. The unit of measure is erased in compiled code and when values of this type are analyzed using reflection. The type is representationally equivalent to <see cref="T:System.Double" />.</summary>
<category index="6">Basic Types with Units of Measure</category>
val resS3P20: int * Mult<float>
val res230: int list * float list
val res243: int list * int list
val res23: int list * string
val resLtDualGt: Ordering * Dual<Ordering>
val res230hiSum2: int list * (float list * string) * int
val res230hiS4P3: int list * (float list * string * int * Mult<int>)
val tuple5: string * (Any * string) * (All * All * All) * int * string
Multiple items
val string: value: 'T -> string
<summary>Converts the argument to a string using <c>ToString</c>.</summary>
<remarks>For standard integer and floating point values the and any type that implements <c>IFormattable</c><c>ToString</c> conversion uses <c>CultureInfo.InvariantCulture</c>. </remarks>
<param name="value">The input value.</param>
<returns>The converted string.</returns>
<example id="string-example"><code lang="fsharp"></code></example>


--------------------
type string = System.String
<summary>An abbreviation for the CLI type <see cref="T:System.String" />.</summary>
<category>Basic Types</category>