3#include <absl/container/fixed_array.h>
28 if (
auto new_def =
lookup(old_def))
return new_def;
31 return new_def->set(old_def->
dbg());
35#define CODE_MUT(N) case Node::N: new_def = rewrite_mut_##N(old_mut->as<N>()); break;
36#define CODE_IMM(N) case Node::N: new_def = rewrite_imm_##N(old_def->as<N>()); break;
41 switch (old_def->
node()) {
43 default: fe::unreachable();
45 return map(old_def, new_def);
50 switch (old_mut->
node()) {
52 default: fe::unreachable();
61 auto new_ops =
DefVec(ops.size());
62 for (
size_t i = 0, e = ops.size(); i != e; ++i)
70const Def* Rewriter::rewrite_imm_Nat (
const Nat* ) {
return world().
type_nat(); }
71const Def* Rewriter::rewrite_imm_Univ (
const Univ* ) {
return world().
univ(); }
107const Def* Rewriter::rewrite_imm_Axm(
const Axm* a) {
108 if (&
a->world() != &
world()) {
110 return world().
axm(
a->normalizer(),
a->curry(),
a->trip(), type,
a->plugin(),
a->tag(),
a->sub());
115const Def* Rewriter::rewrite_imm_Extract(
const Extract* ex) {
116 auto new_index =
rewrite(ex->index());
117 if (
auto index =
Lit::isa(new_index)) {
118 if (
auto tuple = ex->tuple()->isa<
Tuple>())
return map(ex,
rewrite(tuple->op(*index)));
119 if (
auto pack = ex->tuple()->isa_imm<
Pack>(); pack && pack->arity()->is_closed())
123 auto new_tuple =
rewrite(ex->tuple());
127const Def* Rewriter::rewrite_mut_Hole(
Hole* hole) {
128 auto [
last,
op] = hole->find();
143 return map(seq, new_seq);
150 if (
auto var = seq->
has_var(); var && l && *l <=
world().flags().scalarize_threshold) {
151 auto new_ops = absl::FixedArray<const Def*>(*l);
152 for (
size_t i = 0, e = *l; i != e; ++i) {
166 map(old_mut, new_mut);
169 for (
size_t i = 0, e = old_mut->
num_ops(); i != e; ++i)
171 if (
auto new_imm = new_mut->
immutabilize())
return map(old_mut, new_imm);
182 if (
auto new_def =
lookup(old_def))
return new_def;
184 if (
auto old_mut = old_def->
isa_mut())
185 return has_intersection(old_mut) ?
rewrite_mut(old_mut)->
set(old_mut->dbg()) : old_mut;
189 return has_intersection(old_def) ?
rewrite_imm(old_def)->
set(old_def->
dbg()) : old_def;
193 if (
auto var = mut->
has_var()) {
194 auto& vars = vars_.back();
206 auto repr =
lookup(new_def);
207 if (!repr) repr = new_def;
212 for (
auto& old2new :
old2news_ | std::views::reverse) {
218 if (repr ==
nullptr)
break;
220 path.emplace_back(repr);
221 if (repr == old_def)
break;
226 if (path.empty())
continue;
229 for (
auto def : path)
240 auto [last, op] = hole->find();
241 def = op ? op : last;
250 auto old_type = mut->
type();
251 auto old_ops = absl::FixedArray<const Def*>(mut->
ops().begin(), mut->
ops().end());
255 for (
size_t i = 0, e = mut->
num_ops(); i != e; ++i)
A (possibly paramterized) Array.
bool is_set() const
Yields true if empty or the last op is set.
constexpr Node node() const noexcept
Def * set(size_t i, const Def *)
Successively set from left to right.
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
Def * set_type(const Def *)
Update type.
bool is_intro() const noexcept
constexpr auto ops() const noexcept
Vars local_vars() const
Vars reachable by following immutable deps().
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
virtual const Def * immutabilize()
Tries to make an immutable from a mutable.
Muts local_muts() const
Mutables reachable by following immutable deps(); mut->local_muts() is by definition the set { mut }...
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
virtual const Def * arity() const
Def * unset()
Unsets all Def::ops; works even, if not set at all or only partially set.
bool needs_zonk() const
Yields true, if Def::local_muts() contain a Hole that is set.
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
constexpr size_t num_ops() const noexcept
This node is a hole in the IR that is inferred by its context later on.
A built-in constant of type Nat -> *.
Creates a new Tuple / Pack by inserting Insert::value at position Insert::index into Insert::tuple.
static std::optional< T > isa(const Def *def)
Scrutinize Match::scrutinee() and dispatch to Match::arms.
A (possibly paramterized) Tuple.
A dependent function type.
virtual const Def * rewrite_imm_Seq(const Seq *seq)
virtual const Def * rewrite_mut_Seq(Seq *seq)
virtual const Def * rewrite_mut(Def *)
virtual const Def * rewrite_stub(Def *, Def *)
virtual const Def * map(const Def *old_def, const Def *new_def)
std::deque< Def2Def > old2news_
virtual const Def * rewrite_imm(const Def *)
virtual const Def * rewrite(const Def *)
virtual const Def * lookup(const Def *old_def)
Lookup old_def by searching in reverse through the stack of maps.
Base class for Arr and Pack.
constexpr bool empty() const noexcept
Is empty?
Picks the aspect of a Meet [value](Pick::value) by its [type](Def::type).
Data constructor for a Sigma.
A singleton wraps a type into a higher order type.
const Def * rewrite_mut(Def *) final
const Def * rewrite(const Def *) final
A variable introduced by a binder (mutable).
const Def * insert(const Def *d, const Def *i, const Def *val)
const Def * meet(Defs ops)
const Def * uinc(const Def *op, level_t offset=1)
const Lit * lit(const Def *type, u64 val)
const Type * type(const Def *level)
const Def * sigma(Defs ops)
const Def * app(const Def *callee, const Def *arg)
const Pi * pi(const Def *dom, const Def *codom, bool implicit=false)
const Def * seq(bool term, const Def *arity, const Def *body)
const Def * bot(const Def *type)
const Lam * lam(const Pi *pi, Lam::Filter f, const Def *body)
const Def * tuple(Defs ops)
const Def * inj(const Def *type, const Def *value)
const Axm * axm(NormalizeFn n, u8 curry, u8 trip, const Def *type, plugin_t p, tag_t t, sub_t s)
const Def * extract(const Def *d, const Def *i)
const Def * join(Defs ops)
const Proxy * proxy(const Def *type, Defs ops, u32 index, u32 tag)
const Def * var(Def *mut)
const Def * uniq(const Def *inhabitant)
const Def * prod(bool term, Defs ops)
const Def * merge(const Def *type, Defs ops)
const Def * top(const Def *type)
const Def * split(const Def *type, const Def *value)
const Rule * rule(const Reform *type, const Def *lhs, const Def *rhs, const Def *guard)
const Def * rewire_mut(Def *)
const Def * lookup(const Def *old_def) final
Lookup old_def by searching in reverse through the stack of maps.
const Def * rewrite(const Def *) final
const Def * map(const Def *old_def, const Def *new_def) final
const Def * op(trait o, const Def *type)
Vector< const Def * > DefVec
TBound< true > Join
AKA union.
TBound< false > Meet
AKA intersection.