MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
normalizers.cpp
Go to the documentation of this file.
1#include <mim/tuple.h>
2#include <mim/world.h>
3
5
6namespace mim::plug::tuple {
7
8const Def* normalize_cat(const Def* type, const Def* callee, const Def* arg) {
9 auto& world = type->world();
10 auto [a, b] = arg->projs<2>();
11 auto [n, m] = callee->as<App>()->decurry()->args<2>([](auto def) { return Lit::isa(def); });
12
13 if (n && *n == 0) return b;
14 if (m && *m == 0) return a;
15
16 if (n && m) {
17 auto defs = DefVec();
18 for (size_t i = 0, e = *n; i != e; ++i)
19 defs.emplace_back(a->proj(e, i));
20 for (size_t i = 0, e = *m; i != e; ++i)
21 defs.emplace_back(b->proj(e, i));
22 return world.tuple(defs);
23 }
24
25 return nullptr;
26}
27
28const Def* normalize_contains(const Def* type, const Def*, const Def* arg) {
29 auto& w = type->world();
30 auto [xs, x] = arg->projs<2>();
31
32 if (auto mut_pack = xs->isa_mut<Pack>()) {
33 if (auto imm = mut_pack->immutabilize())
34 xs = imm;
35 else
36 return nullptr;
37 }
38
39 if (auto tuple = xs->isa<Tuple>()) {
40 for (auto op : tuple->ops())
41 if (op == x) return w.lit_tt();
42
43 return tuple->is_closed() ? w.lit_ff() : nullptr;
44 }
45
46 if (auto pack = xs->isa<Pack>()) {
47 if (pack->body() == x) return w.lit_tt();
48 return pack->is_closed() ? w.lit_ff() : nullptr;
49 }
50
51 return nullptr;
52}
53
54const Def* normalize_zip(const Def* type, const Def* c, const Def* arg) {
55 auto& w = type->world();
56 auto callee = c->as<App>();
57 auto is_os = callee->arg();
58 auto [n_i, Is, n_o, Os, f] = is_os->projs<5>();
59 auto [r, s] = callee->decurry()->args<2>();
60 auto lr = Lit::isa(r);
61 auto ls = Lit::isa(s);
62
63 // TODO commute
64 // TODO reassociate
65 // TODO more than one Os
66 // TODO select which Is/Os to zip
67
68 if (lr && ls && *lr == 1 && *ls == 1) return w.app(f, arg);
69
70 if (auto l_in = Lit::isa(n_i)) {
71 auto args = arg->projs(*l_in);
72
73 if (lr && std::ranges::all_of(args, [](const Def* arg) { return arg->isa<Prod>(); })) {
74 auto shapes = s->projs(*lr);
75 auto s_n = Lit::isa(shapes.front());
76
77 if (s_n) {
78 auto elems = DefVec(*s_n, [&, f = f](size_t s_i) {
79 auto inner_args = DefVec(args.size(), [&](size_t i) { return args[i]->proj(*s_n, s_i); });
80 if (*lr == 1) {
81 return w.app(f, inner_args);
82 } else {
83 auto app_zip = w.app(w.annex<zip>(), {w.lit_nat(*lr - 1), w.tuple(shapes.view().subspan(1))});
84 return w.app(w.app(app_zip, is_os), inner_args);
85 }
86 });
87 return w.tuple(elems);
88 }
89 }
90 }
91
92 return {};
93}
94
96
97} // namespace mim::plug::tuple
const Def * arg() const
Definition lam.h:286
Base class for all Defs.
Definition def.h:251
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
Definition def.h:390
static std::optional< T > isa(const Def *def)
Definition def.h:810
A (possibly paramterized) Tuple.
Definition tuple.h:166
Base class for Sigma and Tuple.
Definition tuple.h:10
Data constructor for a Sigma.
Definition tuple.h:68
The tuple Plugin
const Def * normalize_cat(const Def *type, const Def *callee, const Def *arg)
const Def * normalize_zip(const Def *type, const Def *c, const Def *arg)
const Def * normalize_contains(const Def *type, const Def *, const Def *arg)
Vector< const Def * > DefVec
Definition def.h:77
#define MIM_tuple_NORMALIZER_IMPL
Definition autogen.h:52