Skip to content

Commit

Permalink
Add procedure for checking type equivalence
Browse files Browse the repository at this point in the history
  • Loading branch information
tytus-metrycki committed Jul 27, 2020
1 parent 89ced82 commit 56e9226
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ast/TypeSystem.cpp
Expand Up @@ -367,4 +367,8 @@ std::optional<TypeAttribute> getTypeAttribute(const TypeSet& type) {
return {};
}

bool areEquivalentTypes(const Type& a, const Type& b) {
return isSubtypeOf(a, b) && isSubtypeOf(b, a);
}

} // end of namespace souffle
6 changes: 6 additions & 0 deletions src/ast/TypeSystem.h
Expand Up @@ -511,4 +511,10 @@ TypeSet getGreatestCommonSubtypes(const Types&... types) {
*/
bool haveCommonSupertype(const Type& a, const Type& b);

/**
* Determine if two types are equivalent.
* That is, check if a <: b and b <: a
*/
bool areEquivalentTypes(const Type& a, const Type& b);

} // end namespace souffle
9 changes: 9 additions & 0 deletions src/tests/type_system_test.cpp
Expand Up @@ -255,4 +255,13 @@ TEST(TypeSystem, RecordSubsets) {
EXPECT_EQ("{A}", toString(getGreatestCommonSubtypes(A, R)));
}

TEST(TypeSystem, EquivTypes) {
TypeEnvironment env;

auto& A = env.createType<SubsetType>("A", env.getType("number"));
auto& U = env.createType<UnionType>("U", toVector(dynamic_cast<const Type*>(&A)));

EXPECT_TRUE(areEquivalentTypes(A, U));
}

} // namespace souffle::test

0 comments on commit 56e9226

Please sign in to comment.