MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
ds2cps.cpp
Go to the documentation of this file.
2
3#include <iostream>
4
5#include <mim/lam.h>
6
7#include "mim/rewrite.h"
8
10
11namespace mim::plug::direct {
12
13const Def* DS2CPS::rewrite(const Def* def) {
14 if (auto app = def->isa<App>()) {
15 if (auto lam = app->callee()->isa_mut<Lam>()) {
16 world().DLOG("encountered lam app");
17 auto new_lam = rewrite_lam(lam);
18 world().DLOG("new lam: {} : {}", new_lam, new_lam->type());
19 world().DLOG("arg: {} : {}", app->arg(), app->arg()->type());
20 auto new_app = world().app(new_lam, app->arg());
21 world().DLOG("new app: {} : {}", new_app, new_app->type());
22 return new_app;
23 }
24 }
25 return def;
26}
27
28/// This function generates the cps function `f_cps : cn [a:A, cn B]` for a ds function `f: [a : A] -> B`.
29/// The translation is associated in the `rewritten_` map.
30const Def* DS2CPS::rewrite_lam(Lam* lam) {
31 if (auto i = rewritten_.find(lam); i != rewritten_.end()) return i->second;
32
33 // only look at lambdas (ds not cps)
34 if (Lam::isa_cn(lam)) return lam;
35 // ignore ds on type level
36 if (lam->type()->codom()->isa<Type>()) return lam;
37 // ignore higher order function
38 if (lam->type()->codom()->isa<Pi>()) {
39 // We can not set the filter here as this causes segfaults.
40 return lam;
41 }
42
43 world().DLOG("rewrite DS function {} : {}", lam, lam->type());
44
45 auto ty = lam->type();
46 auto var = ty->has_var();
47 auto dom = ty->dom();
48 auto codom = ty->codom();
49 auto sigma = world().mut_sigma(2);
50 // replace ds dom var with cps sigma var (cps dom)
51 auto rw_codom = var ? VarRewriter(var, sigma->var(2, 0)).rewrite(codom) : codom;
52 sigma->set(0, dom);
53 sigma->set(1, world().cn(rw_codom));
54
55 world().DLOG("original codom: {}", codom);
56 world().DLOG("rewritten codom: {}", rw_codom);
57
58 auto cps_lam = world().mut_con(sigma)->set(lam->sym().str() + "_cps");
59 auto [filter, cps_body] = lam->reduce<2>(cps_lam->var(0_n));
60
61 // rewrite vars of new function
62 // calls handled separately
63 world().DLOG("body: {} : {}", lam->body(), lam->body()->type());
64 world().DLOG("cps body: {} : {}", cps_body, cps_body->type());
65
66 cps_lam->app(filter, cps_lam->vars().back(), cps_body);
67
68 rewritten_[lam] = op_cps2ds_dep(cps_lam);
69 world().DLOG("replace {} : {}", lam, lam->type());
70 world().DLOG("with {} : {}", rewritten_[lam], rewritten_[lam]->type());
71
72 return rewritten_[lam];
73}
74
75} // namespace mim::plug::direct
Base class for all Defs.
Definition def.h:197
Def * set(size_t i, const Def *)
Successively set from left to right.
Definition def.cpp:240
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:241
Sym sym() const
Definition def.h:450
constexpr auto reduce(const Def *arg) const noexcept
Definition def.h:505
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
Definition def.h:382
A function.
Definition lam.h:106
static const Lam * isa_cn(const Def *d)
Definition lam.h:137
Lam * set(Filter filter, const Def *body)
Definition lam.h:165
const Pi * type() const
Definition lam.h:126
const Def * body() const
Definition lam.h:119
World & world()
Definition pass.h:296
A dependent function type.
Definition lam.h:11
const Def * codom() const
Definition lam.h:33
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:11
const Def * app(const Def *callee, const Def *arg)
Definition world.cpp:181
Lam * mut_con(const Def *dom)
Definition world.h:292
Sigma * mut_sigma(const Def *type, size_t size)
Definition world.h:318
const Def * rewrite(const Def *) override
Definition ds2cps.cpp:13
The direct style Plugin
Definition direct.h:7
const Def * op_cps2ds_dep(const Def *k)
Definition direct.h:15