mirror of
https://github.com/luau-lang/luau.git
synced 2024-12-12 21:10:37 +00:00
e1bf6289c7
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.
58 lines
1.2 KiB
C++
58 lines
1.2 KiB
C++
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
#include <doctest.h>
|
|
|
|
#include "Luau/Slice.h"
|
|
|
|
#include <vector>
|
|
|
|
using namespace Luau;
|
|
|
|
TEST_SUITE_BEGIN("EqSatSlice");
|
|
|
|
TEST_CASE("slice_is_a_view_over_array")
|
|
{
|
|
std::array<int, 8> a{1, 2, 3, 4, 5, 6, 7, 8};
|
|
|
|
EqSat::Slice<int> slice{a};
|
|
|
|
CHECK(slice.data() == a.data());
|
|
CHECK(slice.size() == a.size());
|
|
|
|
for (size_t i = 0; i < a.size(); ++i)
|
|
{
|
|
CHECK(slice[i] == a[i]);
|
|
CHECK(&slice[i] == &a[i]);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("slice_is_a_view_over_vector")
|
|
{
|
|
std::vector<int> vector{1, 2, 3, 4, 5, 6, 7, 8};
|
|
|
|
EqSat::Slice<int> slice{vector.data(), vector.size()};
|
|
|
|
CHECK(slice.data() == vector.data());
|
|
CHECK(slice.size() == vector.size());
|
|
|
|
for (size_t i = 0; i < vector.size(); ++i)
|
|
{
|
|
CHECK(slice[i] == vector[i]);
|
|
CHECK(&slice[i] == &vector[i]);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("mutate_via_slice")
|
|
{
|
|
std::array<int, 2> a{1, 2};
|
|
CHECK(a[0] == 1);
|
|
CHECK(a[1] == 2);
|
|
|
|
EqSat::Slice<int> slice{a};
|
|
slice[0] = 42;
|
|
slice[1] = 37;
|
|
|
|
CHECK(a[0] == 42);
|
|
CHECK(a[1] == 37);
|
|
}
|
|
|
|
TEST_SUITE_END();
|