Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ danglingParentheses.preset = true
rewrite.rules = [AvoidInfix, SortImports, RedundantParens, SortModifiers]
docstrings = JavaDoc
newlines.afterCurlyLambda = preserve
docstrings.style = keep
docstrings.style = Asterisk
docstrings.oneline = unfold

runner.dialect = scala213source3
Expand Down
10 changes: 4 additions & 6 deletions algebra-core/src/main/scala/algebra/Priority.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ import scala.annotation.nowarn
/**
* Priority is a type class for prioritized implicit search.
*
* This type class will attempt to provide an implicit instance of `P`
* (the preferred type). If that type is not available it will
* fallback to `F` (the fallback type). If neither type is available
* then a `Priority[P, F]` instance will not be available.
* This type class will attempt to provide an implicit instance of `P` (the preferred type). If that type is not
* available it will fallback to `F` (the fallback type). If neither type is available then a `Priority[P, F]` instance
* will not be available.
*
* This type can be useful for problems where multiple algorithms can
* be used, depending on the type classes available.
* This type can be useful for problems where multiple algorithms can be used, depending on the type classes available.
*/
sealed trait Priority[+P, +F] {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ object StaticMethods {
/**
* Exponentiation function, e.g. x^y
*
* If base^ex doesn't fit in a Long, the result will overflow (unlike
* Math.pow which will return +/- Infinity).
* If base^ex doesn't fit in a Long, the result will overflow (unlike Math.pow which will return +/- Infinity).
*/
final def pow(base: Long, exponent: Long): Long = {
@tailrec def loop(t: Long, b: Long, e: Long): Long =
Expand Down
9 changes: 4 additions & 5 deletions algebra-core/src/main/scala/algebra/instances/double.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ trait DoubleInstances extends cats.kernel.instances.DoubleInstances {
}

/**
* Due to the way floating-point equality works, this instance is not
* lawful under equality, but is correct when taken as an
* approximation of an exact value.
* Due to the way floating-point equality works, this instance is not lawful under equality, but is correct when taken
* as an approximation of an exact value.
*
* If you would prefer an absolutely lawful fractional value, you'll
* need to investigate rational numbers or more exotic types.
* If you would prefer an absolutely lawful fractional value, you'll need to investigate rational numbers or more exotic
* types.
*/
class DoubleAlgebra extends Field[Double] with Serializable {

Expand Down
9 changes: 4 additions & 5 deletions algebra-core/src/main/scala/algebra/instances/float.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ trait FloatInstances extends cats.kernel.instances.FloatInstances {
}

/**
* Due to the way floating-point equality works, this instance is not
* lawful under equality, but is correct when taken as an
* approximation of an exact value.
* Due to the way floating-point equality works, this instance is not lawful under equality, but is correct when taken
* as an approximation of an exact value.
*
* If you would prefer an absolutely lawful fractional value, you'll
* need to investigate rational numbers or more exotic types.
* If you would prefer an absolutely lawful fractional value, you'll need to investigate rational numbers or more exotic
* types.
*/
class FloatAlgebra extends Field[Float] with Serializable {

Expand Down
40 changes: 17 additions & 23 deletions algebra-core/src/main/scala/algebra/lattice/Bool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,18 @@ import ring.BoolRing
import scala.{specialized => sp}

/**
* Boolean algebras are Heyting algebras with the additional
* constraint that the law of the excluded middle is true
* Boolean algebras are Heyting algebras with the additional constraint that the law of the excluded middle is true
* (equivalently, double-negation is true).
*
* This means that in addition to the laws Heyting algebras obey,
* boolean algebras also obey the following:
* This means that in addition to the laws Heyting algebras obey, boolean algebras also obey the following:
*
* - (a ∨ ¬a) = 1
* - ¬¬a = a
* - (a ∨ ¬a) = 1
* - ¬¬a = a
*
* Boolean algebras generalize classical logic: one is equivalent to
* "true" and zero is equivalent to "false". Boolean algebras provide
* additional logical operators such as `xor`, `nand`, `nor`, and
* `nxor` which are commonly used.
* Boolean algebras generalize classical logic: one is equivalent to "true" and zero is equivalent to "false". Boolean
* algebras provide additional logical operators such as `xor`, `nand`, `nor`, and `nxor` which are commonly used.
*
* Every boolean algebras has a dual algebra, which involves reversing
* true/false as well as and/or.
* Every boolean algebras has a dual algebra, which involves reversing true/false as well as and/or.
*/
trait Bool[@sp(Int, Long) A] extends Any with Heyting[A] with GenBool[A] { self =>
def imp(a: A, b: A): A = or(complement(a), b)
Expand All @@ -56,13 +51,12 @@ trait Bool[@sp(Int, Long) A] extends Any with Heyting[A] with GenBool[A] { self
override def dual: Bool[A] = new DualBool(this)

/**
* Every Boolean algebra is a BoolRing, with multiplication defined as
* `and` and addition defined as `xor`. Bool does not extend BoolRing
* because, e.g. we might want a Bool[Int] and CommutativeRing[Int] to
* refer to different structures, by default.
* Every Boolean algebra is a BoolRing, with multiplication defined as `and` and addition defined as `xor`. Bool does
* not extend BoolRing because, e.g. we might want a Bool[Int] and CommutativeRing[Int] to refer to different
* structures, by default.
*
* Note that the ring returned by this method is not an extension of
* the `Rig` returned from `BoundedDistributiveLattice.asCommutativeRig`.
* Note that the ring returned by this method is not an extension of the `Rig` returned from
* `BoundedDistributiveLattice.asCommutativeRig`.
*/
override def asBoolRing: BoolRing[A] = new BoolRingFromBool(self)
}
Expand All @@ -89,11 +83,11 @@ class BoolRingFromBool[A](orig: Bool[A]) extends BoolRngFromGenBool(orig) with B

/**
* Every Boolean ring gives rise to a Boolean algebra:
* - 0 and 1 are preserved;
* - ring multiplication (`times`) corresponds to `and`;
* - ring addition (`plus`) corresponds to `xor`;
* - `a or b` is then defined as `a xor b xor (a and b)`;
* - complement (`¬a`) is defined as `a xor 1`.
* - 0 and 1 are preserved;
* - ring multiplication (`times`) corresponds to `and`;
* - ring addition (`plus`) corresponds to `xor`;
* - `a or b` is then defined as `a xor b xor (a and b)`;
* - complement (`¬a`) is defined as `a xor 1`.
*/
class BoolFromBoolRing[A](orig: BoolRing[A]) extends GenBoolFromBoolRng(orig) with Bool[A] {
def one: A = orig.one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ trait BoundedDistributiveLattice[@sp(Int, Long, Float, Double) A]
with DistributiveLattice[A] { self =>

/**
* Return a CommutativeRig using join and meet. Note this must obey the commutative rig laws since
* meet(a, one) = a, and meet and join are associative, commutative and distributive.
* Return a CommutativeRig using join and meet. Note this must obey the commutative rig laws since meet(a, one) = a,
* and meet and join are associative, commutative and distributive.
*/
private[algebra] def asCommutativeRig: CommutativeRig[A] =
new CommutativeRig[A] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ package lattice
import scala.{specialized => sp}

/**
* A bounded lattice is a lattice that additionally has one element
* that is the bottom (zero, also written as ⊥), and one element that
* is the top (one, also written as ⊤).
* A bounded lattice is a lattice that additionally has one element that is the bottom (zero, also written as ⊥), and
* one element that is the top (one, also written as ⊤).
*
* This means that for any a in A:
*
* join(zero, a) = a = meet(one, a)
* join(zero, a) = a = meet(one, a)
*
* Or written using traditional notation:
*
* (0 ∨ a) = a = (1 ∧ a)
* (0 ∨ a) = a = (1 ∧ a)
*/
trait BoundedLattice[@sp(Int, Long, Float, Double) A]
extends Any
Expand Down
23 changes: 9 additions & 14 deletions algebra-core/src/main/scala/algebra/lattice/DeMorgan.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,19 @@ package lattice
import scala.{specialized => sp}

/**
* De Morgan algebras are bounded lattices that are also equipped with
* a De Morgan involution.
* De Morgan algebras are bounded lattices that are also equipped with a De Morgan involution.
*
* De Morgan involution obeys the following laws:
*
* - ¬¬a = a
* - ¬(x∧y) = ¬x∨¬y
* - ¬¬a = a
* - ¬(x∧y) = ¬x∨¬y
*
* However, in De Morgan algebras this involution does not necessarily
* provide the law of the excluded middle. This means that there is no
* guarantee that (a ∨ ¬a) = 1. De Morgan algebra do not not necessarily
* provide the law of non contradiction either. This means that there is
* no guarantee that (a ∧ ¬a) = 0.
* However, in De Morgan algebras this involution does not necessarily provide the law of the excluded middle. This
* means that there is no guarantee that (a ∨ ¬a) = 1. De Morgan algebra do not not necessarily provide the law of non
* contradiction either. This means that there is no guarantee that (a ∧ ¬a) = 0.
*
* De Morgan algebras are useful to model fuzzy logic. For a model of
* classical logic, see the boolean algebra type class implemented as
* [[Bool]].
* De Morgan algebras are useful to model fuzzy logic. For a model of classical logic, see the boolean algebra type
* class implemented as [[Bool]].
*/
trait DeMorgan[@sp(Int, Long) A] extends Any with Logic[A] { self =>
def meet(a: A, b: A): A = and(a, b)
Expand All @@ -64,8 +60,7 @@ object DeMorgan extends DeMorganFunctions[DeMorgan] {
@inline final def apply[@sp(Int, Long) A](implicit ev: DeMorgan[A]): DeMorgan[A] = ev

/**
* Turn a [[Bool]] into a `DeMorgan`
* Used for binary compatibility.
* Turn a [[Bool]] into a `DeMorgan` Used for binary compatibility.
*/
final def fromBool[@sp(Int, Long) A](bool: Bool[A]): DeMorgan[A] =
new DeMorgan[A] {
Expand Down
31 changes: 14 additions & 17 deletions algebra-core/src/main/scala/algebra/lattice/GenBool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import ring.BoolRng
import scala.{specialized => sp}

/**
* Generalized Boolean algebra, that is, a Boolean algebra without
* the top element. Generalized Boolean algebras do not (in general)
* have (absolute) complements, but they have ''relative complements''
* (see [[GenBool.without]]).
* Generalized Boolean algebra, that is, a Boolean algebra without the top element. Generalized Boolean algebras do not
* (in general) have (absolute) complements, but they have ''relative complements'' (see [[GenBool.without]]).
*/
trait GenBool[@sp(Int, Long) A] extends Any with DistributiveLattice[A] with BoundedJoinSemilattice[A] { self =>
def and(a: A, b: A): A
Expand All @@ -39,37 +37,36 @@ trait GenBool[@sp(Int, Long) A] extends Any with DistributiveLattice[A] with Bou
override def join(a: A, b: A): A = or(a, b)

/**
* The operation of ''relative complement'', symbolically often denoted
* `a\b` (the symbol for set-theoretic difference, which is the
* meaning of relative complement in the lattice of sets).
* The operation of ''relative complement'', symbolically often denoted `a\b` (the symbol for set-theoretic
* difference, which is the meaning of relative complement in the lattice of sets).
*/
def without(a: A, b: A): A

/**
* Logical exclusive or, set-theoretic symmetric difference.
* Defined as `a\b ∨ b\a`.
* Logical exclusive or, set-theoretic symmetric difference. Defined as `a\b ∨ b\a`.
*/
def xor(a: A, b: A): A = or(without(a, b), without(b, a))

/**
* Every generalized Boolean algebra is also a `BoolRng`, with
* multiplication defined as `and` and addition defined as `xor`.
* Every generalized Boolean algebra is also a `BoolRng`, with multiplication defined as `and` and addition defined as
* `xor`.
*/
@deprecated("See typelevel/algebra#108 for discussion", since = "2.7.0")
def asBoolRing: BoolRng[A] = new BoolRngFromGenBool(self)
}

/**
* Every Boolean rng gives rise to a Boolean algebra without top:
* - 0 is preserved;
* - ring multiplication (`times`) corresponds to `and`;
* - ring addition (`plus`) corresponds to `xor`;
* - `a or b` is then defined as `a xor b xor (a and b)`;
* - relative complement `a\b` is defined as `a xor (a and b)`.
* - 0 is preserved;
* - ring multiplication (`times`) corresponds to `and`;
* - ring addition (`plus`) corresponds to `xor`;
* - `a or b` is then defined as `a xor b xor (a and b)`;
* - relative complement `a\b` is defined as `a xor (a and b)`.
*
* `BoolRng.asBool.asBoolRing` gives back the original `BoolRng`.
*
* @see [[algebra.lattice.GenBool.asBoolRing]]
* @see
* [[algebra.lattice.GenBool.asBoolRing]]
*/
class GenBoolFromBoolRng[A](orig: BoolRng[A]) extends GenBool[A] {
def zero: A = orig.zero
Expand Down
34 changes: 14 additions & 20 deletions algebra-core/src/main/scala/algebra/lattice/Heyting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,28 @@ package lattice
import scala.{specialized => sp}

/**
* Heyting algebras are bounded lattices that are also equipped with
* an additional binary operation `imp` (for implication, also
* written as →).
* Heyting algebras are bounded lattices that are also equipped with an additional binary operation `imp` (for
* implication, also written as →).
*
* Implication obeys the following laws:
*
* - a → a = 1
* - a ∧ (a → b) = a ∧ b
* - b ∧ (a → b) = b
* - a → (b ∧ c) = (a → b) ∧ (a → c)
* - a → a = 1
* - a ∧ (a → b) = a ∧ b
* - b ∧ (a → b) = b
* - a → (b ∧ c) = (a → b) ∧ (a → c)
*
* In heyting algebras, `and` is equivalent to `meet` and `or` is
* equivalent to `join`; both methods are available.
* In heyting algebras, `and` is equivalent to `meet` and `or` is equivalent to `join`; both methods are available.
*
* Heyting algebra also define `complement` operation (sometimes
* written as ¬a). The complement of `a` is equivalent to `(a → 0)`,
* and the following laws hold:
* Heyting algebra also define `complement` operation (sometimes written as ¬a). The complement of `a` is equivalent to
* `(a → 0)`, and the following laws hold:
*
* - a ∧ ¬a = 0
* - a ∧ ¬a = 0
*
* However, in Heyting algebras this operation is only a
* pseudo-complement, since Heyting algebras do not necessarily
* provide the law of the excluded middle. This means that there is no
* guarantee that (a ∨ ¬a) = 1.
* However, in Heyting algebras this operation is only a pseudo-complement, since Heyting algebras do not necessarily
* provide the law of the excluded middle. This means that there is no guarantee that (a ∨ ¬a) = 1.
*
* Heyting algebras model intuitionistic logic. For a model of
* classical logic, see the boolean algebra type class implemented as
* `Bool`.
* Heyting algebras model intuitionistic logic. For a model of classical logic, see the boolean algebra type class
* implemented as `Bool`.
*/
trait Heyting[@sp(Int, Long) A] extends Any with BoundedDistributiveLattice[A] { self =>
def and(a: A, b: A): A
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ package lattice
import scala.{specialized => sp}

/**
* A join-semilattice (or upper semilattice) is a semilattice whose
* operation is called "join", and which can be thought of as a least
* upper bound.
* A join-semilattice (or upper semilattice) is a semilattice whose operation is called "join", and which can be thought
* of as a least upper bound.
*/
trait JoinSemilattice[@sp(Int, Long, Float, Double) A] extends Any with Serializable {
def join(lhs: A, rhs: A): A
Expand Down
13 changes: 5 additions & 8 deletions algebra-core/src/main/scala/algebra/lattice/Lattice.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@ package lattice
import scala.{specialized => sp}

/**
* A lattice is a set `A` together with two operations (meet and
* join). Both operations individually constitute semilattices (join-
* and meet-semilattices respectively): each operation is commutative,
* associative, and idempotent.
* A lattice is a set `A` together with two operations (meet and join). Both operations individually constitute
* semilattices (join- and meet-semilattices respectively): each operation is commutative, associative, and idempotent.
*
* Join can be thought of as finding a least upper bound (supremum),
* and meet can be thought of as finding a greatest lower bound
* (infimum).
* Join can be thought of as finding a least upper bound (supremum), and meet can be thought of as finding a greatest
* lower bound (infimum).
*
* The join and meet operations are also linked by absorption laws:
*
* meet(a, join(a, b)) = join(a, meet(a, b)) = a
* meet(a, join(a, b)) = join(a, meet(a, b)) = a
*/
trait Lattice[@sp(Int, Long, Float, Double) A] extends Any with JoinSemilattice[A] with MeetSemilattice[A] { self =>

Expand Down
13 changes: 5 additions & 8 deletions algebra-core/src/main/scala/algebra/lattice/Logic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ package lattice
import scala.{specialized => sp}

/**
* Logic models a logic generally. It is a bounded distributive
* lattice with an extra negation operator.
* Logic models a logic generally. It is a bounded distributive lattice with an extra negation operator.
*
* The negation operator obeys the weak De Morgan laws:
* - ¬(x∨y) = ¬x∧¬y
* - ¬(x∧y) = ¬¬(¬x∨¬y)
* - ¬(x∨y) = ¬x∧¬y
* - ¬(x∧y) = ¬¬(¬x∨¬y)
*
* For intuitionistic logic see [[Heyting]]
* For fuzzy logic see [[DeMorgan]]
* For intuitionistic logic see [[Heyting]] For fuzzy logic see [[DeMorgan]]
*/
trait Logic[@sp(Int, Long) A] extends Any with BoundedDistributiveLattice[A] { self =>
def and(a: A, b: A): A
Expand Down Expand Up @@ -68,8 +66,7 @@ object Logic extends LogicFunctions[Logic] {
@inline final def apply[@sp(Int, Long) A](implicit ev: Logic[A]): Logic[A] = ev

/**
* Turn a [[Heyting]] into a `Logic`.
* Used for binary compatibility.
* Turn a [[Heyting]] into a `Logic`. Used for binary compatibility.
*/
final def fromHeyting[@sp(Int, Long) A](h: Heyting[A]): Logic[A] =
new Logic[A] {
Expand Down
Loading
Loading