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 Ref rewrite(Ref) 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
35
36 Ref eta_wrap(Ref 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
Ref type() const noexcept
Yields the raw type of this Def, i.e. maybe nullptr.
Definition def.h:241
Ref var(nat_t a, nat_t i) noexcept
Definition def.h:381
Vars free_vars() const
Compute a global solution, i.e., by transitively following mutables as well.
Definition def.cpp:304
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
An optimizer that combines several optimizations in an optimal way.
Definition pass.h:107
World & world()
Definition pass.h:296
PassMan & man()
Definition pass.h:30
A dependent function type.
Definition lam.h:11
bool has_intersection(PooledSet< T > other)
Is ?
Definition pool.h:72
Inherit from this class using CRTP, if your Pass does not need state and a fixed-point iteration.
Definition pass.h:220
Helper class to retrieve Infer::arg if present.
Definition def.h:86
const Def * call(Id id, Args &&... args)
Complete curried call of annexes obeying implicits.
Definition world.h:514
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)
ClosConvPrep(PassMan &man, EtaExp *eta_exp)
Ref eta_wrap(Ref def, attr a)
Definition ast.h:14
GIDMap< const Def *, To > DefMap
Definition def.h:58
LamMap< Lam * > Lam2Lam
Definition lam.h:198
GIDSet< const Def * > DefSet
Definition def.h:59