12bool should_flatten(
const Def* def) {
13 auto type = (def->is_term() ? def->type() : def);
14 if (type->isa<Sigma>())
return true;
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();
27const Def*
unflatten(
Defs defs,
const Def* type,
size_t& j,
bool flatten_muts) {
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 array = def->
projs(
Lit::as(def->
arity()), [](
auto op) { return Lit::as(op); });
52 return std::string(array.begin(), array.end());
56 if (
auto a = def->
isa_lit_arity(); a && *a != 1 && should_flatten(def) && flatten_muts == mut_val_or_typ(def)) {
58 for (
size_t i = 0; i != *a; ++i) n +=
flatten(ops, def->
proj(*a, i), flatten_muts);
61 ops.emplace_back(def);
67 if (!should_flatten(def))
return def;
75 auto def =
unflatten(defs, type, j, flatten_muts);
76 assert(j == defs.size());
83 return DefVec(defs.size() + 1, [&](
auto i) { return i == 0 ? def : defs[i - 1]; });
87 DefVec result(a.size() + b.size());
88 auto [_, o] = std::ranges::copy(a, result.begin());
89 std::ranges::copy(b, o);
99 auto& w = def->
world();
101 auto a = sigma->num_ops();
102 auto tuple =
DefVec(a, [&](
auto i) {
return w.extract(def, a, i); });
103 return w.tuple(
merge(tuple, defs));
110 auto& world = t->world();
111 if (
auto sigma = t->isa<
Sigma>())
return world.tuple(sigma->ops());
112 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.
bool is_term() const
Yields true if this:T and T:(Type 0).
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == -1_n) or std::array (otherwise).
std::optional< nat_t > isa_lit_arity() const
const T * isa_imm() const
const Def * shape() const
Helper class to retrieve Infer::arg if present.
This is a thin wrapper for std::span<T, N> with the following additional features:
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.
Ref tuple_of_types(Ref t)
const Def * merge_tuple(const Def *def, Defs defs)