MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
union.cpp
Go to the documentation of this file.
1#include "mim/union.h"
2
3#include <algorithm>
4
5#include "mim/check.h"
6#include "mim/lam.h"
7
8namespace mim {
9
11 auto type = scrutinee->unfold_type();
12 if (!type) return {};
13 if (auto join = type->isa<Join>()) return DefVec(join->ops().begin(), join->ops().end());
14 if (auto variant = type->isa<Variant>()) return DefVec(variant->ops().begin(), variant->ops().end());
15 return DefVec{type};
16}
17
18bool Match::accepts(const Def* arm, const Def* c, bool infer) {
19 auto alpha = [infer](const Def* a, const Def* b) { return infer ? Check::alpha(a, b) : Check::test_alpha(a, b); };
20
21 auto pi = arm->isa_type<Pi>();
22 if (!pi) return false;
23 if (alpha(pi->dom(), c)) return true;
24 if (auto join = pi->dom()->isa_imm<Join>())
25 return std::ranges::any_of(join->ops(), [&](const Def* op) { return alpha(op, c); });
26 return false;
27}
28
29} // namespace mim
static bool test_alpha(const Def *d1, const Def *d2)
Definition check.h:101
static bool alpha(const Def *d1, const Def *d2)
Definition check.h:93
const Def * op(size_t i) const noexcept
Definition def.h:377
const Def * unfold_type() const
Yields the type of this Def and builds a new Type (LSucc n) if necessary.
Definition def.cpp:469
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1119
const T * isa_type() const
Is Def::unfold_type a T? Yields nullptr for Level, which has no type at all.
Definition def.h:363
A union type whose ops are sorted and deduplicated: T ∪ T is T.
Definition union.h:44
static bool accepts(const Def *arm, const Def *c, bool infer=false)
Does arm handle c?
Definition union.cpp:18
static DefVec cases(const Def *scrutinee)
Definition union.cpp:10
const Def * scrutinee() const
Definition union.h:135
const Def * arm(size_t i) const
Definition union.h:140
A dependent function type.
Definition lam.h:14
A sum type whose cases are positional: unlike a Join, they are neither sorted nor deduplicated.
Definition union.h:86
Definition ast.h:16
fe::Vector< const Def * > DefVec
Definition def.h:98