MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
eta_exp_phase.cpp
Go to the documentation of this file.
2
3#include "mim/rewrite.h"
4
5namespace mim {
6
8 for (auto def : old_world().externals())
9 visit(def, Lattice::Known);
10
11 return false; // no fixed-point neccessary
12}
13
14void EtaExpPhase::analyze(const Def* def) {
15 if (auto [_, ins] = analyzed_.emplace(def); !ins) return;
16 if (def->isa<Var>()) return; // ignore Var's mut
17
18 if (auto app = def->isa<App>()) {
19 visit(app->type());
20 visit(app->callee(), Lattice::Known);
21 visit(app->arg());
22 } else {
23 for (auto d : def->deps())
24 visit(d);
25 }
26}
27
28void EtaExpPhase::visit(const Def* def, Lattice l) {
29 if (auto lam = def->isa_mut<Lam>()) join(lam, l);
30 analyze(def);
31}
32
34 auto new_mut = rewrite_no_eta(old_mut)->as_mut();
35 if (old_mut->is_external()) new_mut->make_external();
36}
37
38const Def* EtaExpPhase::rewrite(const Def* old_def) {
39 if (auto lam = old_def->isa<Lam>(); lam && lattice(lam) == Both) {
40 auto [i, ins] = lam2eta_.emplace(lam, nullptr);
41 if (ins) i->second = Lam::eta_expand(rewrite_no_eta(lam));
42 DLOG("eta-expand: `{}` → `{}`", lam, i->second);
43 return i->second;
44 }
45
46 return Rewriter::rewrite(old_def);
47}
48
50 auto callee = rewrite_no_eta(app->callee());
51 return new_world().app(callee, rewrite(app->arg()));
52}
53
55 return new_world().var(rewrite_no_eta(var->mut())->as_mut());
56}
57
58} // namespace mim
const Def * callee() const
Definition lam.h:277
const Def * arg() const
Definition lam.h:286
Base class for all Defs.
Definition def.h:251
Defs deps() const noexcept
Definition def.cpp:470
bool is_external() const noexcept
Definition def.h:467
const Def * rewrite(const Def *) final
const Def * rewrite_imm_Var(const Var *) final
const Def * rewrite_imm_App(const App *) final
void rewrite_external(Def *) final
bool analyze() final
You can do an optional fixed-point loop on the RWPhase::old_world before rewriting.
A function.
Definition lam.h:111
static Lam * eta_expand(Filter, const Def *f)
Definition lam.cpp:51
World & new_world()
Create new Defs into this.
Definition phase.h:100
World & old_world()
Get old Defs from here.
Definition phase.h:99
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:27
A variable introduced by a binder (mutable).
Definition def.h:700
Def * mut() const
Definition def.h:712
const Def * app(const Def *callee, const Def *arg)
Definition world.cpp:195
const Def * var(Def *mut)
Definition world.cpp:178
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Definition log.h:95
Definition ast.h:14
bool visit(IndexSet< Indexer, Key > &set, const Key &key)
Definition indexset.h:100
@ Lam
Definition def.h:114