luau/EqSat/include/Luau/Slice.h

79 lines
1.4 KiB
C
Raw Normal View History

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 "Luau/Common.h"
#include <array>
#include <cstddef>
namespace Luau::EqSat
{
template<typename T>
struct Slice final
{
Slice()
: _data(nullptr)
, _size(0)
{
}
/// Use this constructor if you have a dynamically sized vector.
/// The slice is valid for as long as the backing vector has not moved
/// elsewhere in memory.
///
/// In general, a slice should never be used from vectors except for
/// any vectors whose size are statically unknown, but remains fixed
/// upon the construction of such a slice over a vector.
Slice(T* first, size_t last)
: _data(first)
, _size(last)
{
}
template<size_t I>
explicit Slice(std::array<T, I>& array)
: _data(array.data())
, _size(array.size())
{
}
T* data() const
{
return _data;
}
size_t size() const
{
return _size;
}
bool empty() const
{
return _size == 0;
}
T& operator[](size_t i) const
{
LUAU_ASSERT(i < _size);
return _data[i];
}
public:
T* _data;
size_t _size;
public:
T* begin() const
{
return _data;
}
T* end() const
{
return _data + _size;
}
};
} // namespace Luau::EqSat