Skip to content

Instantly share code, notes, and snippets.

@voronoipotato
Last active November 20, 2017 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voronoipotato/d2162966603ba0a26da0231bc4e486c9 to your computer and use it in GitHub Desktop.
Save voronoipotato/d2162966603ba0a26da0231bc4e486c9 to your computer and use it in GitHub Desktop.
type Magma<'a> =
abstract member append : 'a -> 'a -> 'a
type Semigroup<'a> =
inherit Magma<'a>
type Monoid<'a> =
inherit Semigroup<'a>
abstract member empty : 'a
type addMonoid =
new () = {}
interface Monoid<int> with
member this.empty = 0
member this.append (a: int) (b: int) = a + b
type andMonoid =
new () = {}
interface Monoid<bool> with
member this.empty = true
member this.append (a: bool) (b: bool) = a && b
let appendElseEmpty (m: Monoid<'a>) condition (x :'a) (y: 'a) =
if condition
then m.append x y
else m.empty
printfn "%A" (appendElseEmpty (new addMonoid()) true 1 2)
printfn "%A" (appendElseEmpty (new andMonoid()) true true false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment