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
9
11 analyzed_.clear();
12 lam2lattice_.clear();
13}
14
16 for (auto def : old_world().externals()) {
17 if (auto lam = def->isa<Lam>()) join(lam, Lattice::Known);
18 analyze(def);
19 }
20
21 return false; // no fixed-point neccessary
22}
23
24void EtaExpPhase::analyze(const Def* def) {
25 if (auto [_, ins] = analyzed_.emplace(def); !ins) return;
26
27 if (auto var = def->isa<Var>()) return analyze(var->type()); // ignore Var's mut
28
29 if (auto app = def->isa<App>()) {
30 // clang-format off
31 if (auto lam = app-> type()->isa_mut<Lam>()) join(lam, Lattice::Unknown);
32 if (auto lam = app->callee()->isa_mut<Lam>()) join(lam, Lattice:: Known);
33 if (auto lam = app-> arg()->isa_mut<Lam>()) join(lam, Lattice::Unknown);
34 // clang-format on
35 analyze(app->type());
36 analyze(app->callee());
37 analyze(app->arg());
38 } else {
39 for (auto d : def->deps()) {
40 if (auto lam = d->isa_mut<Lam>()) {
41 if (def->isa<App>()) join(lam, Lattice::Unknown);
42 }
43 analyze(d);
44 }
45 }
46}
47
48void EtaExpPhase::rewrite_external(Def* mut) { mut->transfer_external(rewrite_no_eta(mut)->as_mut()); }
49
50const Def* EtaExpPhase::rewrite(const Def* old_def) {
51 if (auto lam = old_def->isa<Lam>(); lam && lattice(lam) == Both) {
52 auto [i, ins] = lam2eta_.emplace(lam, nullptr);
53 if (ins) i->second = Lam::eta_expand(rewrite_no_eta(lam));
54 return i->second;
55 }
56
57 return Rewriter::rewrite(old_def);
58}
59
60const Def* EtaExpPhase::rewrite_no_eta(const Def* old_def) { return Rewriter::rewrite(old_def); }
61
63 auto callee = rewrite_no_eta(app->callee());
64 return new_world().app(callee, rewrite(app->arg()));
65}
66
68 return new_world().var(rewrite(var->type()), rewrite_no_eta(var->mut())->as_mut());
69}
70
71} // namespace mim
const Def * callee() const
Definition lam.h:273
const Def * arg() const
Definition lam.h:282
Base class for all Defs.
Definition def.h:251
Defs deps() const noexcept
Definition def.cpp:464
void transfer_external(Def *to)
Definition def.cpp:566
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:295
const Def * rewrite_imm_Var(const Var *) final
const Def * rewrite_imm_App(const App *) final
void reset() final
void rewrite_external(Def *) final
bool analyze() final
FPPhase(World &world, std::string name)
Definition phase.h:121
A function.
Definition lam.h:109
static const Def * eta_expand(Filter, const Def *f)
Definition lam.cpp:47
World & new_world()
Create new Defs into this.
Definition phase.h:99
World & world()=delete
Hides both and forbids direct access.
World & old_world()
Get old Defs from here.
Definition phase.h:98
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:14
Def * mut() const
Definition def.h:698
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
const Def * app(const Def *callee, const Def *arg)
Definition world.cpp:196
const Def * var(const Def *type, Def *mut)
Definition world.cpp:177
Definition ast.h:14
@ Lam
Definition def.h:114
@ App
Definition def.h:114