typelevel / algebra
 1 ```package algebra ``` 2 ```package lattice ``` 3 4 ```import scala.{specialized => sp} ``` 5 6 ```/** ``` 7 ``` * De Morgan algebras are bounded lattices that are also equipped with ``` 8 ``` * a De Morgan involution. ``` 9 ``` * ``` 10 ``` * De Morgan involution obeys the following laws: ``` 11 ``` * ``` 12 ``` * - ¬¬a = a ``` 13 ``` * - ¬(x∧y) = ¬x∨¬y ``` 14 ``` * ``` 15 ``` * However, in De Morgan algebras this involution does not necessarily ``` 16 ``` * provide the law of the excluded middle. This means that there is no ``` 17 ``` * guarantee that (a ∨ ¬a) = 1. De Morgan algebra do not not necessarily ``` 18 ``` * provide the law of non contradiction either. This means that there is ``` 19 ``` * no guarantee that (a ∧ ¬a) = 0. ``` 20 ``` * ``` 21 ``` * De Morgan algebras are useful to model fuzzy logic. For a model of ``` 22 ``` * classical logic, see the boolean algebra type class implemented as ``` 23 ``` * [[Bool]]. ``` 24 ``` */ ``` 25 ```trait DeMorgan[@sp(Int, Long) A] extends Any with Logic[A] { self => ``` 26 2 ``` def meet(a: A, b: A): A = and(a, b) ``` 27 28 2 ``` def join(a: A, b: A): A = or(a, b) ``` 29 30 0 ``` def imp(a: A, b: A): A = or(not(a), b) ``` 31 ```} ``` 32 33 ```trait DeMorganFunctions[H[A] <: DeMorgan[A]] extends ``` 34 ``` BoundedMeetSemilatticeFunctions[H] with ``` 35 ``` BoundedJoinSemilatticeFunctions[H] with ``` 36 ``` LogicFunctions[H] ``` 37 38 39 ```object DeMorgan extends DeMorganFunctions[DeMorgan] { ``` 40 41 ``` /** ``` 42 ``` * Access an implicit `DeMorgan[A]`. ``` 43 ``` */ ``` 44 ``` @inline final def apply[@sp(Int, Long) A](implicit ev: DeMorgan[A]): DeMorgan[A] = ev ``` 45 46 47 ``` /** ``` 48 ``` * Turn a [[Bool]] into a `DeMorgan` ``` 49 ``` * Used for binary compatibility. ``` 50 ``` */ ``` 51 ``` final def fromBool[@sp(Int, Long) A](bool: Bool[A]): DeMorgan[A] = ``` 52 2 ``` new DeMorgan[A] { ``` 53 2 ``` def and(a: A, b: A): A = bool.and(a, b) ``` 54 55 2 ``` def or(a: A, b: A): A = bool.or(a, b) ``` 56 57 2 ``` def not(a: A): A = bool.complement(a) ``` 58 59 2 ``` def one: A = bool.one ``` 60 61 2 ``` def zero: A = bool.zero ``` 62 ``` } ``` 63 ```} ```

Read our documentation on viewing source code .