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 = arr->isa_lit_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++];
29 if (
auto a =
type->isa_lit_arity();
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);
41 if (
auto arr =
type()->isa<Arr>())
return arr->shape();
49 if (def ==
nullptr)
return {};
51 auto& w = def->
world();
52 auto res = std::string();
54 for (
size_t i = 0; i != *n; ++i) {
55 auto elem = def->
proj(*n, i);
56 if (elem->type() == w.type_i8()) {
69 if (
auto a = def->
isa_lit_arity(); a && *a != 1 && should_flatten(def) && flatten_muts == mut_val_or_typ(def)) {
71 for (
size_t i = 0; i != *a; ++i) n +=
flatten(ops, def->
proj(*a, i), flatten_muts);
74 ops.emplace_back(def);
80 if (!should_flatten(def))
return def;
88 auto def =
unflatten(defs, type, j, flatten_muts);
89 assert(j == defs.size());
96 return DefVec(defs.size() + 1, [&](
size_t i) { return i == 0 ? def : defs[i - 1]; });
100 DefVec result(a.size() + b.size());
101 auto [_, o] = std::ranges::copy(a, result.begin());
102 std::ranges::copy(b, o);
112 auto& w = def->
world();
114 auto a = sigma->num_ops();
115 auto tuple =
DefVec(a, [&](
auto i) {
return w.extract(def, a, i); });
116 return w.tuple(
merge(tuple, defs));
123 auto& world = t->world();
124 if (
auto sigma = t->isa<
Sigma>())
return world.tuple(sigma->ops());
125 if (
auto arr = t->isa<
Arr>())
return world.pack(arr->shape(), 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
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).
std::optional< nat_t > isa_lit_arity() const
const Def * arity() const
const T * isa_imm() const
static std::optional< T > isa(const Def *def)
static T as(const Def *def)
const Def * shape() const
const Def * sigma(Defs ops)
const Def * tuple(Defs ops)
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)