diff --git a/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/NonEmptySet.java b/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/NonEmptySet.java index b574733f..5b5dcf2a 100644 --- a/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/NonEmptySet.java +++ b/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/NonEmptySet.java @@ -28,6 +28,6 @@ default boolean isEmpty() { // TODO: the cardinality of the union is actually not known // at this point: - return new Union(this, (NonEmptySet) cpl); + return new Union((NonEmptySet) cpl, (NonEmptySet) that); } } diff --git a/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/SingletonSet.java b/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/SingletonSet.java index 5ca379a6..56cd7682 100644 --- a/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/SingletonSet.java +++ b/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/SingletonSet.java @@ -16,7 +16,12 @@ public interface SingletonSet, S extends SingletonSet FiniteSet.B64 { E elem(); - + + @Override + default boolean contains(E that) { + return elem().eq(that); + } + @Override default Set where(Predicate Φ) { return Φ.test(elem()) ? this : EmptySet.empty(); diff --git a/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/Union.java b/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/Union.java index 9226d69b..012a64a7 100644 --- a/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/Union.java +++ b/dedekind-ontology/src/main/java/com/github/vincentk/dedekind/sets/Union.java @@ -13,6 +13,12 @@ record Union< implements NonEmptySet> { + @Override + public boolean + contains(E elem) { + return fst().contains(elem) || snd().contains(elem); + } + @Override public Set diff --git a/dedekind-ontology/src/test/java/com/github/vincentk/dedekind/sets/UnionTest.java b/dedekind-ontology/src/test/java/com/github/vincentk/dedekind/sets/UnionTest.java new file mode 100644 index 00000000..1c5bfa95 --- /dev/null +++ b/dedekind-ontology/src/test/java/com/github/vincentk/dedekind/sets/UnionTest.java @@ -0,0 +1,56 @@ +package com.github.vincentk.dedekind.sets; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +import com.github.vincentk.dedekind.algebra.numbers.N; + +public class UnionTest { + + private static final SingletonSet S1 = new SingletonSet.Ordered.Default<>(N.ONE); + private static final SingletonSet S2 = new SingletonSet.Ordered.Default<>(N.TWO); + private static final EmptySet EMPTY = EmptySet.empty(); + + @Test + public void testConstruction() { + assertThat(EMPTY.union(EMPTY)).isEqualTo(EMPTY); + assertThat(S1.union(EMPTY)).isEqualTo(S1); + assertThat(EMPTY.union(S1)).isEqualTo(S1); + assertThat(S1.union(S1)).isEqualTo(S1); + + final var u2 = S1.union(S2); + assertThat(u2).isInstanceOf(Union.class); + assertThat(u2.contains(N.ONE)).isTrue(); + assertThat(u2.contains(N.TWO)).isTrue(); + } + + @Test + public void testEmptySet() { + + final var SUBJECT = S1.union(S1); + + assertTrue(SUBJECT.eq(SUBJECT)); + + assertThat(SUBJECT.intersection(SUBJECT)).isEqualTo(SUBJECT); + + assertThat(SUBJECT.union(SUBJECT)).isEqualTo(SUBJECT); + + assertThat(SUBJECT.sub(SUBJECT)) + .as(() -> "The empty set is a subset of itself.") + .isTrue(); + + assertThat(SUBJECT.sup(SUBJECT)) + .as(() -> "The empty set is a super-set of itself.") + .isTrue(); + + assertThat(SUBJECT.where(x -> true)) + .as(() -> "A conditioned empty set is still empty.") + .isEqualTo(SUBJECT); + + assertThat(SUBJECT.complement(SUBJECT)) + .as(() -> "The difference between the empty set and itself empty.") + .isEqualTo(EmptySet.empty()); + } +}