This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Add BoolRng and GenBool. Resolves #107. #109
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package algebra | ||
package lattice | ||
|
||
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]]). | ||
*/ | ||
trait GenBool[@sp(Int, Long) A] extends Any with DistributiveLattice[A] with BoundedJoinSemilattice[A] { self => | ||
def and(a: A, b: A): A | ||
override def meet(a: A, b: A): A = and(a, b) | ||
|
||
def or(a: A, b: A): A | ||
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). | ||
*/ | ||
def without(a: A, b: A): 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`. | ||
*/ | ||
def asBoolRing: BoolRng[A] = new BoolRngFromGenBool(self) | ||
} | ||
|
||
private[lattice] class BoolRngFromGenBool[@sp(Int, Long) A](orig: GenBool[A]) extends BoolRng[A] { | ||
def zero: A = orig.zero | ||
def plus(x: A, y: A): A = orig.xor(x, y) | ||
def times(x: A, y: A): A = orig.and(x, y) | ||
override def asBool: GenBool[A] = orig | ||
} | ||
|
||
trait GenBoolFunctions { | ||
def zero[@sp(Int, Long) A](implicit ev: GenBool[A]): A = ev.zero | ||
def and[@sp(Int, Long) A](x: A, y: A)(implicit ev: GenBool[A]): A = ev.and(x, y) | ||
def or[@sp(Int, Long) A](x: A, y: A)(implicit ev: GenBool[A]): A = ev.or(x, y) | ||
def without[@sp(Int, Long) A](x: A, y: A)(implicit ev: GenBool[A]): A = ev.without(x, y) | ||
def xor[@sp(Int, Long) A](x: A, y: A)(implicit ev: GenBool[A]): A = ev.xor(x, y) | ||
} | ||
|
||
object GenBool extends BoolFunctions { | ||
@inline final def apply[@sp(Int, Long) A](implicit ev: GenBool[A]): GenBool[A] = ev | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package algebra | ||
package ring | ||
|
||
import lattice.GenBool | ||
|
||
/** | ||
* A Boolean rng is a rng whose multiplication is idempotent, that is | ||
* `a⋅a = a` for all elements ''a''. This property also implies `a+a = 0` | ||
* for all ''a'', and `a⋅b = b⋅a` (commutativity of multiplication). | ||
* | ||
* Every `BoolRng` is equivalent to [[algebra.lattice.GenBool]]. | ||
* See [[BoolRng.asBool]] for details. | ||
*/ | ||
trait BoolRng[A] extends Any with Rng[A] { self => | ||
override final def negate(x: A): A = x | ||
|
||
/** | ||
* 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)`. | ||
* | ||
* `BoolRng.asBool.asBoolRing` gives back the original `BoolRng`. | ||
* | ||
* @see [[algebra.lattice.GenBool.asBoolRing]] | ||
*/ | ||
def asBool: GenBool[A] = new GenBoolFromBoolRng(self) | ||
} | ||
|
||
private[ring] class GenBoolFromBoolRng[A](orig: BoolRng[A]) extends GenBool[A] { | ||
def zero: A = orig.zero | ||
def and(a: A, b: A): A = orig.times(a, b) | ||
def or(a: A, b: A): A = orig.plus(orig.plus(a, b), orig.times(a, b)) | ||
def without(a: A, b: A): A = orig.plus(a, orig.times(a, b)) | ||
override def asBoolRing: BoolRng[A] = orig | ||
} | ||
|
||
object BoolRng extends AdditiveGroupFunctions with MultiplicativeSemigroupFunctions { | ||
@inline final def apply[A](implicit r: BoolRng[A]): BoolRng[A] = r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,8 +68,10 @@ trait LawTestsBase extends FunSuite with Discipline { | |
laws[OrderLaws, List[String]].check(_.order) | ||
laws[GroupLaws, List[String]].check(_.monoid) | ||
|
||
laws[LatticeLaws, Set[Int]].check(_.distributiveLattice) | ||
laws[LatticeLaws, Set[Int]].check(_.boundedJoinLattice) | ||
laws[LogicLaws, Set[Byte]].check(_.generalizedBool) | ||
laws[RingLaws, Set[Byte]].check(_.boolRng(setBoolRng[Byte])) | ||
laws[LogicLaws, Set[Byte]]("bool-from-rng").check(_.generalizedBool(setBoolRng.asBool)) | ||
laws[RingLaws, Set[Byte]]("rng-from-bool").check(_.boolRng(GenBool[Set[Byte]].asBoolRing)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switched from |
||
laws[OrderLaws, Set[Int]].check(_.partialOrder) | ||
laws[RingLaws, Set[Int]].check(_.semiring) | ||
laws[RingLaws, Set[String]].check(_.semiring) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asBoolRng
, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was intentional, because I didn't want to introduce a new method name.
It is kind of analogous to dual, where the subtype overrides the return
type.
On Oct 30, 2015 9:25 PM, "P. Oscar Boykin" [email protected] wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we change both method names to
BoolRng
? It seems a bit strange to name it for the more derived case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would make sense. Or maybe to asRingLike? Similarly asBool to
asGenBool/asBoolLike?
On Oct 30, 2015 9:45 PM, "P. Oscar Boykin" [email protected] wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we settle #108 first? Because, my first preference is actually to remove these methods and make the classes you wrote public.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, maybe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this PR is basically done. Let's merge this and not block it on #108. If we remove constructions, this is a marginal amount compared with what we have already.