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_contains(const Def* type, const Def*, const Def* arg) {
24 auto& w = type->world();
25 auto [xs, x] = arg->projs<2>();
26
27 if (auto mut_pack = xs->isa_mut<Pack>()) {
28 if (auto imm = mut_pack->immutabilize())
29 xs = imm;
30 else
31 return nullptr;
32 }
33
34 if (auto tuple = xs->isa<Tuple>()) {
35 for (auto op : tuple->ops())
36 if (op == x) return w.lit_tt();
37
38 return tuple->is_closed() ? w.lit_ff() : nullptr;
39 }
40
41 if (auto pack = xs->isa<Pack>()) {
42 if (pack->body() == x) return w.lit_tt();
43 return pack->is_closed() ? w.lit_ff() : nullptr;
44 }
45
46 return nullptr;
47}
48
49const Def* normalize_zip(const Def* type, const Def* c, const Def* arg) {
50 auto& w = type->world();
51 auto callee = c->as<App>();
52 auto is_os = callee->arg();
53 auto [n_i, Is, n_o, Os, f] = is_os->projs<5>();
54 auto [r, s] = callee->decurry()->args<2>();
55 auto lr = Lit::isa(r);
56 auto ls = Lit::isa(s);
57
58 // TODO commute
59 // TODO reassociate
60 // TODO more than one Os
61 // TODO select which Is/Os to zip
62
63 if (lr && ls && *lr == 1 && *ls == 1) return w.app(f, arg);
64
65 if (auto l_in = Lit::isa(n_i)) {
66 auto args = arg->projs(*l_in);
67
68 if (lr && std::ranges::all_of(args, [](const Def* arg) { return arg->isa<Tuple, Pack>(); })) {
69 auto shapes = s->projs(*lr);
70 auto s_n = Lit::isa(shapes.front());
71
72 if (s_n) {
73 auto elems = DefVec(*s_n, [&, f = f](size_t s_i) {
74 auto inner_args = DefVec(args.size(), [&](size_t i) { return args[i]->proj(*s_n, s_i); });
75 if (*lr == 1) {
76 return w.app(f, inner_args);
77 } else {
78 auto app_zip = w.app(w.annex<zip>(), {w.lit_nat(*lr - 1), w.tuple(shapes.view().subspan(1))});
79 return w.app(w.app(app_zip, is_os), inner_args);
80 }
81 });
82 return w.tuple(elems);
83 }
84 }
85 }
86
87 return {};
88}
89
91
92} // 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:340
static std::optional< T > isa(const Def *def)
Definition def.h:719
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)
const Def * normalize_contains(const Def *type, const Def *, 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:52