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();
54 if ((extract_ = def->isa<
Extract>())) {
55 pair_ = extract_->tuple();
56 cond_ = extract_->index();
58 if (
auto a =
Lit::isa(pair_->arity()); a && a == 2) {
59 ff_ = pair_->proj(2, 0);
60 tt_ = pair_->proj(2, 1);
69 if ((app_ = def->isa<
App>())) {
70 callee_ = app_->callee();
78 if (def ==
nullptr)
return {};
80 auto& w = def->
world();
81 auto res = std::string();
83 for (
size_t i = 0; i != *n; ++i) {
84 auto elem = def->
proj(*n, i);
85 if (elem->type() == w.type_i8()) {
98 if (
auto a =
Lit::isa(def->
arity()); a && *a != 1 && should_flatten(def) && flatten_muts == mut_val_or_typ(def)) {
100 for (
size_t i = 0; i != *a; ++i)
104 ops.emplace_back(def);
110 if (!should_flatten(def))
return def;
118 auto def =
unflatten(defs, type, j, flatten_muts);
119 assert(j == defs.size());
126 return DefVec(defs.size() + 1, [&](
size_t i) { return i == 0 ? def : defs[i - 1]; });
130 DefVec result(a.size() + b.size());
131 auto [_, o] = std::ranges::copy(a, result.begin());
132 std::ranges::copy(b, o);
142 auto& w = def->
world();
144 auto a = sigma->num_ops();
145 auto tuple =
DefVec(a, [&](
auto i) {
return w.extract(def, a, i); });
146 return w.tuple(
merge(tuple, defs));
153 auto& world = t->world();
154 if (
auto sigma = t->isa<
Sigma>())
return world.tuple(sigma->ops());
155 if (
auto arr = t->isa<
Arr>())
return world.pack(arr->arity(), arr->body());
A (possibly paramterized) Array.
const Def * callee() const
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)