No matching definitions.

tur/typeclass-lattice

stdlib/typeclass-lattice.tur

Semigroup, Monoid, and the selection newtypes.

The vocabulary for "these two values combine, associatively". Turmeric had none of it: no Semigroup, no Monoid, no name for the operation that effects-chain in tur-signal and Category's comp in stdlib/arrow.tur each hand-roll for one shape.

NOT auto-loaded -- (load "stdlib/typeclass-lattice.tur") to use it. The auto-load list is paid by every TU in every build, and nothing in stdlib depends on these classes.

Since: lattice-vocabulary-plan L2 (lattice four: L3)

defclass

Semigroup

(defclass Semigroup [a])

an associative binary operation.

athe type being combined
(combine [x :a y :a] :a)

the associative operation on x and y

associativity -- (combine (combine x y) z) = (combine x (combine y z))
(combine (:: 3 Sum) (:: 7 Sum))   ; => Sum 10

Sum, Product, MinI, MaxI, Any, All

Since: lattice-vocabulary-plan L2

defclass

Monoid

(defclass Monoid [a] [(Semigroup a)])

a Semigroup with an identity element.

athe type being combined
(mempty [] :a)

the identity element (nullary; dispatched by the expected type)

identity -- (combine (mempty) x) = x = (combine x (mempty))
(let [unit : Sum (mempty)] (combine unit (:: 3 Sum)))   ; => Sum 3

Note the binding: (combine (mempty) x) does not elaborate, because argument inference runs left-to-right and in first position mempty has nothing to fix its type yet. In second position, or bound to a typed local, it is fine.

Sum, Product, MinI, MaxI, Any, All

Since: lattice-vocabulary-plan L2

defopaque

Sum

(defopaque Sum :int)

int under addition. Identity 0.

defopaque

Product

(defopaque Product :int)

int under multiplication. Identity 1.

defopaque

MinI

(defopaque MinI :int)

int under minimum. Identity is the largest int64.

defopaque

MaxI

(defopaque MaxI :int)

int under maximum. Identity is the smallest int64.

defopaque

Any

(defopaque Any :bool)

bool under disjunction. Identity false.

defopaque

All

(defopaque All :bool)

bool under conjunction. Identity true.

definstance

Eq[Sum]

(definstance Eq [Sum])

structural equality on the carrier.

definstance

Eq[Product]

(definstance Eq [Product])

structural equality on the carrier.

definstance

Eq[MinI]

(definstance Eq [MinI])

structural equality on the carrier.

definstance

Eq[MaxI]

(definstance Eq [MaxI])

structural equality on the carrier.

definstance

Eq[Any]

(definstance Eq [Any])

structural equality on the carrier.

definstance

Eq[All]

(definstance Eq [All])

structural equality on the carrier.

definstance

Semigroup[Sum]

(definstance Semigroup [Sum])

addition.

definstance

Semigroup[Product]

(definstance Semigroup [Product])

multiplication.

definstance

Semigroup[MinI]

(definstance Semigroup [MinI])

minimum.

definstance

Semigroup[MaxI]

(definstance Semigroup [MaxI])

maximum.

definstance

Semigroup[Any]

(definstance Semigroup [Any])

disjunction.

definstance

Semigroup[All]

(definstance Semigroup [All])

conjunction.

definstance

Monoid[Sum]

(definstance Monoid [Sum])

identity 0.

definstance

Monoid[Product]

(definstance Monoid [Product])

identity 1.

definstance

Monoid[MinI]

(definstance Monoid [MinI])

identity is the largest int64.

definstance

Monoid[MaxI]

(definstance Monoid [MaxI])

identity is the smallest int64.

definstance

Monoid[Any]

(definstance Monoid [Any])

identity false.

definstance

Monoid[All]

(definstance Monoid [All])

identity true.

defn

law-associative?

(law-associative? [^Semigroup A ^Eq A])

does combine associate for these three values?

x y z -- values to check

true when (combine (combine x y) z) equals (combine x (combine y z)).

(law-associative? (:: 1 Sum) (:: 2 Sum) (:: 3 Sum))   ; => true

Since: lattice-vocabulary-plan L2

defn

law-identity?

(law-identity? [^Monoid A ^Eq A])

is mempty a two-sided identity for this value?

xvalue to check

true when (combine (mempty) x) and (combine x (mempty)) both equal x.

(law-identity? (:: 5 Product))   ; => true

Since: lattice-vocabulary-plan L2

defn

mconcat-from

(mconcat-from [^Monoid A])

fold a Vec into an accumulator with combine.

vthe Vec to fold
iindex to start at
nindex to stop before
accaccumulator

acc combined with v[i..n).

Since: lattice-vocabulary-plan L4

defn

mconcat

(mconcat [^Monoid A])

combine every element of a Vec, left to right, from mempty.

vthe Vec to fold

mempty when v is empty, else v[0] combined with the rest in order.

(mconcat (vec-of (:: 1 Sum) (:: 2 Sum) (:: 3 Sum)))   ; => Sum 6

Since: lattice-vocabulary-plan L4

defclass

JoinSemilattice

(defclass JoinSemilattice [a])

associative, commutative, IDEMPOTENT combine (least upper bound).

athe type being joined
(join [x :a y :a] :a)

the least upper bound of x and y

associativity -- (join (join x y) z) = (join x (join y z))
commutativity -- (join x y) = (join y x)
idempotence   -- (join x x) = x
(join (:: 3 MaxI) (:: 7 MaxI))   ; => MaxI 7

MaxI, Any

Since: lattice-vocabulary-plan L3

defclass

MeetSemilattice

(defclass MeetSemilattice [a])

the dual of JoinSemilattice (greatest lower bound).

athe type being met
(meet [x :a y :a] :a)

the greatest lower bound of x and y

associativity, commutativity, idempotence -- as for join
(meet (:: 3 MinI) (:: 7 MinI))   ; => MinI 3

MinI, All

Since: lattice-vocabulary-plan L3

defclass

BoundedJoin

(defclass BoundedJoin [a] [(JoinSemilattice a)])

a JoinSemilattice with a least element.

(bottom [] :a)

the least element (nullary; dispatched by the expected type)

identity -- (join (bottom) x) = x
(let [z : MaxI (bottom)] (join z (:: 3 MaxI)))   ; => MaxI 3

MaxI, Any

Since: lattice-vocabulary-plan L3

defclass

BoundedMeet

(defclass BoundedMeet [a] [(MeetSemilattice a)])

a MeetSemilattice with a greatest element.

(top [] :a)

the greatest element (nullary; dispatched by the expected type)

identity -- (meet (top) x) = x
(let [t : MinI (top)] (meet t (:: 3 MinI)))   ; => MinI 3

MinI, All

Since: lattice-vocabulary-plan L3

definstance

JoinSemilattice[MaxI]

(definstance JoinSemilattice [MaxI])

least upper bound is the larger int.

definstance

BoundedJoin[MaxI]

(definstance BoundedJoin [MaxI])

least element is the smallest int64.

definstance

MeetSemilattice[MinI]

(definstance MeetSemilattice [MinI])

greatest lower bound is the smaller int.

definstance

BoundedMeet[MinI]

(definstance BoundedMeet [MinI])

greatest element is the largest int64.

definstance

JoinSemilattice[Any]

(definstance JoinSemilattice [Any])

disjunction.

definstance

BoundedJoin[Any]

(definstance BoundedJoin [Any])

least element is false.

definstance

MeetSemilattice[All]

(definstance MeetSemilattice [All])

conjunction.

definstance

BoundedMeet[All]

(definstance BoundedMeet [All])

greatest element is true.

defn

law-join-associative?

(law-join-associative? [^JoinSemilattice A ^Eq A])

does join associate?

x y z -- values to check

true when (join (join x y) z) equals (join x (join y z)).

(law-join-associative? (:: 1 MaxI) (:: 2 MaxI) (:: 3 MaxI))   ; => true

Since: lattice-vocabulary-plan L3

defn

law-join-commutative?

(law-join-commutative? [^JoinSemilattice A ^Eq A])

does join commute?

x y -- values to check

true when (join x y) equals (join y x).

(law-join-commutative? (:: 1 MaxI) (:: 2 MaxI))   ; => true

Since: lattice-vocabulary-plan L3

defn

law-join-idempotent?

(law-join-idempotent? [^JoinSemilattice A ^Eq A])

is join idempotent?

xvalue to check

true when (join x x) equals x.

(law-join-idempotent? (:: 1 MaxI))   ; => true

Since: lattice-vocabulary-plan L3

defn

law-bottom-identity?

(law-bottom-identity? [^BoundedJoin A ^Eq A])

is bottom a left identity for join?

xvalue to check

true when (join (bottom) x) equals x.

(law-bottom-identity? (:: 3 MaxI))   ; => true

Since: lattice-vocabulary-plan L3

defn

lattice-leq?

(lattice-leq? [^JoinSemilattice A ^Eq A])

the partial order induced by join.

xcandidate lower value
ycandidate upper value

true when (join x y) equals y, i.e. x <= y in the lattice order.

(lattice-leq? (:: 3 MaxI) (:: 7 MaxI))   ; => true

Since: lattice-vocabulary-plan L3

defn

law-meet-idempotent?

(law-meet-idempotent? [^MeetSemilattice A ^Eq A])

is meet idempotent?

xvalue to check

true when (meet x x) equals x.

(law-meet-idempotent? (:: 1 MinI))   ; => true

Since: lattice-vocabulary-plan L3

defn

law-top-identity?

(law-top-identity? [^BoundedMeet A ^Eq A])

is top a left identity for meet?

xvalue to check

true when (meet (top) x) equals x.

(law-top-identity? (:: 3 MinI))   ; => true

Since: lattice-vocabulary-plan L3