-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup structure fix unicode chars in SM
- Loading branch information
Showing
19 changed files
with
239 additions
and
128 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package svarog | ||
|
||
object EquationalLaws { | ||
|
||
// I * a == a and a == a * I | ||
def unitality[X](a: X, binOp: (X,X) => X, unit: X): Boolean = | ||
(binOp(unit, a) == a) && (a == binOp(a, unit)) | ||
|
||
// (a * b) * c == a * (b * c) | ||
def associativity[X](a: X, b: X, c: X, binOp: (X,X) => X): Boolean = | ||
binOp(binOp(a,b), c) == binOp(a, binOp(b,c)) | ||
|
||
// a - a == 0 | ||
def reflexivity[X](a: X, rel: (X,X) => Boolean): Boolean = | ||
rel(a,a) | ||
|
||
// a < b and b < c then a < c | ||
def transitivity[X](a: X, b: X, c: X, rel: (X,X) => Boolean): Boolean = | ||
if(rel(a,b) && rel(b,c)) rel(a,c) | ||
else true | ||
|
||
// a * b == b * a | ||
def symmetry[X,Y](a: X, b: X, binOp: (X,X) => Y): Boolean = | ||
binOp(a,b) == binOp(b,a) | ||
|
||
def monotonicity[X](a1: X, a2: X, b1: X, b2: X, binOp: (X,X) => X, rel: (X,X) => Boolean): Boolean = | ||
if( rel(a1, b1) && rel(a2, b2) ) rel( binOp(a1, a2), binOp(b1, b2) ) | ||
else true | ||
|
||
// if a ~ b then a == b | ||
def skeletality[X](a: X, b: X, rel: (X,X) => Boolean): Boolean = | ||
if( rel(a,b) ) a == b | ||
else true | ||
|
||
// if (a + b) <= c then a <= b - c | ||
// - could be monus(a,b) = max(0,b-a) | ||
// TODO what is the mathematical name of this property | ||
def minusPlusPreorder[X](a: X, b: X, c: X, plus: (X,X) => X, monus: (X, X) => X, lessOrEqual: (X,X) => Boolean): Boolean = { | ||
if( lessOrEqual(plus(a,b), c) ) lessOrEqual(a, monus(b,c) ) | ||
else if( lessOrEqual(a, monus(b,c) ) ) lessOrEqual( plus(a,b), c) | ||
else true | ||
} | ||
|
||
// TODO this is probabl wrong - generalized badly - redo it | ||
def triangleInequality[X,Y](x: X, y: X, z: X, distance: (X,X) => Y, lessOrEqual: (Y,Y) => Boolean, plus: (Y,Y) => Y): Boolean = | ||
lessOrEqual( | ||
distance(x,z), | ||
plus(distance(x,y), distance(y,z)) | ||
) | ||
|
||
// back(there(start)) == start | ||
def isAdjoint[A,B](start: A, there: A => B, back: B => A)(rel: (A,A) => Boolean): Boolean = { | ||
val dest = there(start) | ||
val bc2 = back(dest) | ||
rel(start,bc2) | ||
} | ||
|
||
// f(x) binOp f(y) == f(x binOp y) | ||
def preserveOp[X,Y](x: X, y: X, f: X => X, binOp: (X,X) => X): Boolean = { | ||
val eq: (X,X) => Boolean = _ == _ | ||
preserveOp[X,X](x, y, f, binOp, binOp, eq) | ||
} | ||
|
||
// f(p1) binOpY f(p2) <= f(p1 binOpX p2) | ||
def preserveOp[X,Y](x: X, y: X, f: X => Y, binOpX: (X,X) => X, binOpY: (Y,Y) => Y, rel: (Y,Y) => Boolean): Boolean = | ||
rel( binOpY( f(x), f(y) ), f(binOpX(x,y)) ) | ||
} |
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 |
---|---|---|
@@ -1,21 +1,20 @@ | ||
package svarog.algebra | ||
|
||
import simulacrum.typeclass | ||
import svarog.EquationalLaws | ||
|
||
@typeclass | ||
trait SymmetricMonoid[X] | ||
extends Monoid[X] | ||
|
||
/** | ||
* Laws for SymmetricMonoidalPreorder: | ||
* - symmetry: forall a,b ∈ X, a ⊗ b = b ⊗ a | ||
* - symmetry: forall a,b ∈ X, a ⊗ b = b ⊗ a | ||
*/ | ||
trait SymmetricMonoidLaws | ||
extends MonoidLaws { | ||
|
||
/** forall a,b ∈ X, a ⊗ b = b ⊗ a */ | ||
def symmetry[X](a: X, b: X)(implicit P: SymmetricMonoid[X]): Boolean = { | ||
import P._ | ||
multiply(a,b) == multiply(b,a) | ||
} | ||
/** forall a,b ∈ X, a ⊗ b = b ⊗ a */ | ||
def symmetry[X](a: X, b: X)(implicit P: SymmetricMonoid[X]): Boolean = | ||
EquationalLaws.symmetry(a,b,P.multiply) | ||
} |
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 |
---|---|---|
@@ -1,35 +1,36 @@ | ||
package svarog.enrichment | ||
|
||
import svarog.enrichment.BoolCategory.BoolCategory | ||
import svarog.EquationalLaws | ||
import svarog.enrichment.BoolCategory.BoolCat | ||
import svarog.preorders.{Preorder, SymmetricMonoidalPreorder} | ||
import svarog.preorders.SymmetricMonoidalPreorder.SMPBoolAndLe | ||
import svarog.preorders.SymmetricMonoidalPreorder.`(B, ≤, true, ∧)` | ||
|
||
// There is a one-to-one correspondence between preorders and Bool-categories | ||
object BoolCategory { | ||
|
||
type BoolCategory[X] = EnrichedCategory[Boolean, X] | ||
type BoolCat[X] = EnrichedCategory[Boolean, X] | ||
|
||
def apply[X](pre: Preorder[X]): BoolCategory[X] = new BoolCategory[X] { | ||
override def base: SymmetricMonoidalPreorder[Boolean] = SMPBoolAndLe | ||
def apply[X](pre: Preorder[X]): BoolCat[X] = new BoolCat[X] { | ||
override def base: SymmetricMonoidalPreorder[Boolean] = `(B, ≤, true, ∧)` | ||
override def homObject(x: X, y: X): Boolean = pre.le(x, y) | ||
} | ||
|
||
implicit def asPreorder[X](implicit bc: BoolCategory[X]): Preorder[X] = | ||
implicit def asPreorder[X](bc: BoolCat[X]): Preorder[X] = | ||
(a: X, b: X) => bc.homObject(a,b) | ||
} | ||
|
||
trait BoolCategoryLaws { | ||
import BoolCategory.asPreorder | ||
|
||
def property1[X](bc: BoolCategory[X]): Boolean = { | ||
val p = asPreorder(bc) | ||
val bc2 = BoolCategory(p) | ||
bc == bc2 // TODO we need equivalence for BoolCategory[X] | ||
def toPreorderAndBackToBoolCategoryIsNoOp[X](bc: BoolCat[X])(a: X, b: X): Boolean = { | ||
EquationalLaws.isAdjoint[BoolCat[X],Preorder[X]](bc,asPreorder,BoolCategory.apply) { case (b1, b2) => | ||
b1.homObject(a, b) == b2.homObject(a, b) | ||
} | ||
} | ||
|
||
def property2[X](p: Preorder[X]): Boolean = { | ||
val bc = BoolCategory(p) | ||
val p2 = asPreorder(bc) | ||
p2 == p // TODO we need equivalence for BoolCategory[X] | ||
def toBoolCatAndBackToPreorderIsNoOp[X](p: Preorder[X],a: X, b:X): Boolean = { | ||
EquationalLaws.isAdjoint[Preorder[X],BoolCat[X]](p,BoolCategory.apply,asPreorder) { case (b1, b2) => | ||
b1.le(a, b) == b2.le(a, b) | ||
} | ||
} | ||
} |
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
16 changes: 12 additions & 4 deletions
16
src/main/scala/svarog/enrichment/SkeletalEnrichedCategory.scala
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 |
---|---|---|
@@ -1,13 +1,21 @@ | ||
package svarog.enrichment | ||
|
||
import svarog.{EquationalLaws, Equivalence} | ||
|
||
trait SkeletalEnrichedCategory[V,X] | ||
extends EnrichedCategory[V,X] | ||
with Equivalence[X] { | ||
|
||
def homRelation(a: X, b: X): Boolean = | ||
base.le(base.I, homObject(a,b)) | ||
|
||
override def equivalent(a: X, b: X): Boolean = | ||
homRelation(a,b) && homRelation(b,a) | ||
} | ||
|
||
trait SkeletalEnrichedCategoryLaws | ||
extends EnrichedCategoryLaws { | ||
|
||
def skeletality[V,X](a: X, b: X)(implicit P: EnrichedCategory[V,X]): Boolean = | ||
if( P.base.le(P.base.I, P.homObject(a,b)) && | ||
P.base.le(P.base.I, P.homObject(b,a)) ) a == b | ||
else true | ||
def skeletality[V,X](a: X, b: X)(implicit P: SkeletalEnrichedCategory[V,X]): Boolean = | ||
EquationalLaws.skeletality(a,b, P.homRelation) | ||
} |
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
Oops, something went wrong.