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_concat(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 && m) {
14 auto defs = DefVec();
15 for (size_t i = 0, e = *n; i != e; ++i) defs.emplace_back(a->proj(e, i));
16 for (size_t i = 0, e = *m; i != e; ++i) defs.emplace_back(b->proj(e, i));
17 return world.tuple(defs);
18 }
19
20 return nullptr;
21}
22
23const Def* normalize_zip(const Def* type, const Def* c, const Def* arg) {
24 auto& w = type->world();
25 auto callee = c->as<App>();
26 auto is_os = callee->arg();
27 auto [n_i, Is, n_o, Os, f] = is_os->projs<5>();
28 auto [r, s] = callee->decurry()->args<2>();
29 auto lr = Lit::isa(r);
30 auto ls = Lit::isa(s);
31
32 // TODO commute
33 // TODO reassociate
34 // TODO more than one Os
35 // TODO select which Is/Os to zip
36
37 if (lr && ls && *lr == 1 && *ls == 1) return w.app(f, arg);
38
39 if (auto l_in = Lit::isa(n_i)) {
40 auto args = arg->projs(*l_in);
41
42 if (lr && std::ranges::all_of(args, [](const Def* arg) { return arg->isa<Tuple, Pack>(); })) {
43 auto shapes = s->projs(*lr);
44 auto s_n = Lit::isa(shapes.front());
45
46 if (s_n) {
47 auto elems = DefVec(*s_n, [&, f = f](size_t s_i) {
48 auto inner_args = DefVec(args.size(), [&](size_t i) { return args[i]->proj(*s_n, s_i); });
49 if (*lr == 1) {
50 return w.app(f, inner_args);
51 } else {
52 auto app_zip = w.app(w.annex<zip>(), {w.lit_nat(*lr - 1), w.tuple(shapes.view().subspan(1))});
53 return w.app(w.app(app_zip, is_os), inner_args);
54 }
55 });
56 return w.tuple(elems);
57 }
58 }
59 }
60
61 return {};
62}
63
65
66} // namespace mim::plug::tuple
const Def * arg() const
Definition lam.h:230
Base class for all Defs.
Definition def.h:198
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:345
static std::optional< T > isa(const Def *def)
Definition def.h:713
A (possibly paramterized) Tuple.
Definition tuple.h:122
Data constructor for a Sigma.
Definition tuple.h:56
The tuple Plugin
const Def * normalize_zip(const Def *type, const Def *c, const Def *arg)
const Def * normalize_concat(const Def *type, const Def *callee, const Def *arg)
Vector< const Def * > DefVec
Definition def.h:50
std::deque< const App * > decurry(const Def *)
Yields curried Apps in a flat std::deque<const App*>.
Definition lam.cpp:40
#define MIM_tuple_NORMALIZER_IMPL
Definition autogen.h:44