Equality graphs (#1285)
Working towards a full e-graph implementation as described by the [egg
paper](https://arxiv.org/pdf/2004.03082).
The type system has a couple of places where e-graphs would've been
useful and solved some classes of problems trivially. For example:
1. Normalization and simplification cannot handle cyclic types due to
the nature of their implementation.
2. Normalization can't tell when two tables or functions are equivalent,
but simplification theoretically can albeit not implemented.
3. Normalization requires deep normalization for inhabitance check,
whereas simplification would've returned the `never` type itself
indicating uninhabited.
4. Simplification requires constraint ordering to have perfect timing to
simplify.
5. Adding a rewrite rule requires implementing it twice, once in
simplification and once again in normalization with completely different
code design making it hard to verify that their behavior is materially
equivalent.
6. In cases where we must cache for performance, two different types
that are isomorphic have different cache entries resulting in cache
misses.
7. Type family reduction can handle cyclic type families, but only if
the cycle is not obscured by a different type family instance. (`t1
where t1 = union<number, add<t1, number>>` is irreducible)
I think we're getting the point!
---
Currently the implementation is missing a few features that makes
e-graphs actually useful. Those will be coming in a future PR.
1. Pattern matching,
6. Applying rewrites,
7. Rewrite until saturation, and
8. Extracting the best e-node according to some cost function.
2024-07-16 18:35:20 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace Luau::EqSat
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct LanguageHash
|
|
|
|
{
|
|
|
|
size_t operator()(const T& t, decltype(std::hash<T>{}(std::declval<T>()))* = 0) const
|
|
|
|
{
|
|
|
|
return std::hash<T>{}(t);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-08-02 15:30:04 +01:00
|
|
|
template<typename T>
|
Equality graphs (#1285)
Working towards a full e-graph implementation as described by the [egg
paper](https://arxiv.org/pdf/2004.03082).
The type system has a couple of places where e-graphs would've been
useful and solved some classes of problems trivially. For example:
1. Normalization and simplification cannot handle cyclic types due to
the nature of their implementation.
2. Normalization can't tell when two tables or functions are equivalent,
but simplification theoretically can albeit not implemented.
3. Normalization requires deep normalization for inhabitance check,
whereas simplification would've returned the `never` type itself
indicating uninhabited.
4. Simplification requires constraint ordering to have perfect timing to
simplify.
5. Adding a rewrite rule requires implementing it twice, once in
simplification and once again in normalization with completely different
code design making it hard to verify that their behavior is materially
equivalent.
6. In cases where we must cache for performance, two different types
that are isomorphic have different cache entries resulting in cache
misses.
7. Type family reduction can handle cyclic type families, but only if
the cycle is not obscured by a different type family instance. (`t1
where t1 = union<number, add<t1, number>>` is irreducible)
I think we're getting the point!
---
Currently the implementation is missing a few features that makes
e-graphs actually useful. Those will be coming in a future PR.
1. Pattern matching,
6. Applying rewrites,
7. Rewrite until saturation, and
8. Extracting the best e-node according to some cost function.
2024-07-16 18:35:20 +01:00
|
|
|
size_t languageHash(const T& lang)
|
|
|
|
{
|
|
|
|
return LanguageHash<T>{}(lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void hashCombine(size_t& seed, size_t hash)
|
|
|
|
{
|
|
|
|
// Golden Ratio constant used for better hash scattering
|
|
|
|
// See https://softwareengineering.stackexchange.com/a/402543
|
|
|
|
seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t I>
|
|
|
|
struct LanguageHash<std::array<T, I>>
|
|
|
|
{
|
|
|
|
size_t operator()(const std::array<T, I>& array) const
|
|
|
|
{
|
|
|
|
size_t seed = 0;
|
|
|
|
for (const T& t : array)
|
|
|
|
hashCombine(seed, languageHash(t));
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct LanguageHash<std::vector<T>>
|
|
|
|
{
|
|
|
|
size_t operator()(const std::vector<T>& vector) const
|
|
|
|
{
|
|
|
|
size_t seed = 0;
|
|
|
|
for (const T& t : vector)
|
|
|
|
hashCombine(seed, languageHash(t));
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Luau::EqSat
|