MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
clos_conv_prep.h
Go to the documentation of this file.
1#pragma once
2
3#include <mim/pass/pass.h>
4
6
7namespace mim {
8
9class EtaExp;
10
11namespace plug::clos {
12
13class ClosConvPrep : public RWPass<ClosConvPrep, Lam> {
14public:
16 : RWPass(man, "clos_conv_prep")
17 , eta_exp_(eta_exp)
18 , old2wrapper_()
19 , lam2fscope_() {}
20
21 void enter() override;
22 const Def* rewrite(const Def*) override;
23 const App* rewrite_arg(const App* app);
24 const App* rewrite_callee(const App* app);
25
26 Lam* scope(Lam* lam);
27
28 bool from_outer_scope(Lam* lam) {
29 // return curr_mut()->free_vars().intersects(lam->free_vars()); }
30 // return scope_.free_defs().contains(lam);
31 return scope(lam) && scope(lam) != scope(curr_mut());
32 }
33
34 bool from_outer_scope(const Def* lam) { return curr_mut()->free_vars().has_intersection(lam->free_vars()); }
35
36 const Def* eta_wrap(const Def* def, attr a) {
37 auto& w = world();
38 auto [entry, inserted] = old2wrapper_.emplace(def, nullptr);
39 auto& wrapper = entry->second;
40 if (inserted) {
41 wrapper = w.mut_lam(def->type()->as<Pi>());
42 wrapper->app(false, def, wrapper->var());
43 lam2fscope_[wrapper] = scope(curr_mut());
44 wrapper_.emplace(wrapper);
45 }
46 return world().call(a, wrapper);
47 }
48
49private:
50 EtaExp* eta_exp_;
51 DefMap<Lam*> old2wrapper_;
52 DefSet wrapper_;
53 Lam2Lam lam2fscope_;
54 bool ignore_ = false;
55};
56
57} // namespace plug::clos
58} // namespace mim
Base class for all Defs.
Definition def.h:198
const Def * var(nat_t a, nat_t i) noexcept
Definition def.h:379
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:242
Vars free_vars() const
Compute a global solution by transitively following mutables as well.
Definition def.cpp:321
Performs η-expansion: f -> λx.f x, if f is a Lam with more than one user and does not appear in calle...
Definition eta_exp.h:13
A function.
Definition lam.h:105
World & world()
Definition pass.h:296
PassMan & man()
Definition pass.h:30
friend class PassMan
Definition pass.h:101
A dependent function type.
Definition lam.h:11
RWPass(PassMan &man, std::string_view name)
Definition pass.h:222
bool has_intersection(Set other) const noexcept
Is ?.
Definition sets.h:262
const Def * call(Id id, Args &&... args)
Complete curried call of annexes obeying implicits.
Definition world.h:506
const Def * rewrite(const Def *) override
const App * rewrite_callee(const App *app)
void enter() override
Invoked just before Pass::rewriteing PassMan::curr_mut's body.
const App * rewrite_arg(const App *app)
bool from_outer_scope(const Def *lam)
ClosConvPrep(PassMan &man, EtaExp *eta_exp)
const Def * eta_wrap(const Def *def, attr a)
The clos Plugin
Definition clos.h:7
Definition ast.h:14
GIDMap< const Def *, To > DefMap
Definition def.h:46
LamMap< Lam * > Lam2Lam
Definition lam.h:199
GIDSet< const Def * > DefSet
Definition def.h:47