12bool should_flatten(
const Def* def) {
13 auto type = (def->is_term() ? def->type() : def);
15 if (
auto arr =
type->isa<
Arr>()) {
16 if (
auto a =
Lit::isa(arr->arity()); a && *a > def->world().flags().scalarize_threshold)
return false;
22bool mut_val_or_typ(
const Def* def) {
23 auto typ = def->is_term() ? def->type() : def;
24 return typ->isa_mut();
28 if (!defs.empty() && defs[0]->type() == type)
return defs[j++];
30 flatten_muts == mut_val_or_typ(type) && a && *a != 1 && a <= type->world().flags().scalarize_threshold) {
31 auto& world =
type->world();
32 auto ops =
DefVec(*a, [&](
size_t i) {
return unflatten(defs,
type->proj(*a, i), j, flatten_muts); });
33 return world.tuple(type, ops);
47 if (
auto arr =
type()->isa<Arr>())
return arr->
arity();
55 if (def ==
nullptr)
return {};
57 auto& w = def->
world();
58 auto res = std::string();
60 for (
size_t i = 0; i != *n; ++i) {
61 auto elem = def->
proj(*n, i);
62 if (elem->type() == w.type_i8()) {
75 if (
auto a =
Lit::isa(def->
arity()); a && *a != 1 && should_flatten(def) && flatten_muts == mut_val_or_typ(def)) {
77 for (
size_t i = 0; i != *a; ++i)
81 ops.emplace_back(def);
87 if (!should_flatten(def))
return def;
95 auto def =
unflatten(defs, type, j, flatten_muts);
96 assert(j == defs.size());
103 return DefVec(defs.size() + 1, [&](
size_t i) { return i == 0 ? def : defs[i - 1]; });
107 DefVec result(a.size() + b.size());
108 auto [_, o] = std::ranges::copy(a, result.begin());
109 std::ranges::copy(b, o);
119 auto& w = def->
world();
121 auto a = sigma->num_ops();
122 auto tuple =
DefVec(a, [&](
auto i) {
return w.extract(def, a, i); });
123 return w.tuple(
merge(tuple, defs));
130 auto& world = t->world();
131 if (
auto sigma = t->isa<
Sigma>())
return world.tuple(sigma->ops());
132 if (
auto arr = t->isa<
Arr>())
return world.pack(arr->arity(), arr->body());
A (possibly paramterized) Array.
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
World & world() const noexcept
bool is_intro() const noexcept
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
const Def * op(size_t i) const noexcept
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
virtual const Def * arity() const
const T * isa_imm() const
constexpr size_t num_ops() const noexcept
static std::optional< T > isa(const Def *def)
static T as(const Def *def)
const Def * arity() const final
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
const Def * arity() const final
const Def * sigma(Defs ops)
const Def * tuple(Defs ops)
const Lit * lit_nat(nat_t a)
const Def * flatten(const Def *def)
Flattens a sigma/array/pack/tuple.
Vector< const Def * > DefVec
bool is_unit(const Def *)
const Def * merge_sigma(const Def *def, Defs defs)
std::string tuple2str(const Def *)
const Def * unflatten(const Def *def, const Def *type)
Applies the reverse transformation on a Pack / Tuple, given the original type.
const Def * tuple_of_types(const Def *t)
const Def * merge_tuple(const Def *def, Defs defs)