20 X(Lit, Judge::Intro) \
21 X(Axm, Judge::Intro) \
22 X(Var, Judge::Intro) \
23 X(Global, Judge::Intro) \
24 X(Proxy, Judge::Intro) \
25 X(Hole, Judge::Hole ) \
26 X(Type, Judge::Meta ) X(Univ, Judge::Meta ) X(UMax, Judge::Meta) X(UInc, (Judge::Meta )) \
27 X(Pi, Judge::Form ) X(Lam, Judge::Intro) X(App, Judge::Elim) \
28 X(Sigma, Judge::Form ) X(Tuple, Judge::Intro) X(Extract, Judge::Elim) X(Insert, (Judge::Intro | Judge::Elim)) \
29 X(Arr, Judge::Form ) X(Pack, Judge::Intro) \
30 X(Join, Judge::Form ) X(Inj, Judge::Intro) X(Match, Judge::Elim) X(Top, (Judge::Intro )) \
31 X(Meet, Judge::Form ) X(Merge, Judge::Intro) X(Split, Judge::Elim) X(Bot, (Judge::Intro )) \
32 X(Reform, Judge::Form ) X(Rule, Judge::Intro) \
33 X(Uniq, Judge::Form ) \
34 X(Nat, Judge::Form ) \
37#define MIM_IMM_NODE(X) \
42 X(Type) X(Univ) X(UMax) X(UInc) \
44 X(Sigma) X(Tuple) X(Extract) X(Insert) \
46 X(Join) X(Inj) X(Match) X(Top) \
47 X(Meet) X(Merge) X(Split) X(Bot) \
53#define MIM_MUT_NODE(X) \
106using fe::operator<=>;
114#define CODE(node, _) node,
119#define CODE(node, _) +size_t(1)
124enum class Dep :
unsigned {
156template<>
struct fe::is_bit_enum<
mim::
Dep> : std::true_type {};
157template<>
struct fe::is_bit_enum<
mim::
Judge> : std::true_type {};
158template<>
struct fe::is_bit_enum<
mim::
Mut> : std::true_type {};
165#define MIM_PROJ(NAME, CONST) \
166 nat_t num_##NAME##s() CONST noexcept { return ((const Def*)NAME())->num_projs(); } \
167 nat_t num_t##NAME##s() CONST noexcept { return ((const Def*)NAME())->num_tprojs(); } \
168 const Def* NAME(nat_t a, nat_t i) CONST noexcept { return ((const Def*)NAME())->proj(a, i); } \
169 const Def* NAME(nat_t i) CONST noexcept { return ((const Def*)NAME())->proj(i); } \
170 const Def* t##NAME(nat_t i) CONST noexcept { return ((const Def*)NAME())->tproj(i); } \
171 template<nat_t A = std::dynamic_extent, class F> \
172 auto NAME##s(F f) CONST noexcept { \
173 return ((const Def*)NAME())->projs<A, F>(f); \
176 auto t##NAME##s(F f) CONST noexcept { \
177 return ((const Def*)NAME())->tprojs<F>(f); \
179 template<nat_t A = std::dynamic_extent> \
180 auto NAME##s() CONST noexcept { \
181 return ((const Def*)NAME())->projs<A>(); \
183 auto t##NAME##s() CONST noexcept { return ((const Def*)NAME())->tprojs(); } \
185 auto NAME##s(nat_t a, F f) CONST noexcept { \
186 return ((const Def*)NAME())->projs<F>(a, f); \
188 auto NAME##s(nat_t a) CONST noexcept { return ((const Def*)NAME())->projs(a); }
191template<
class P,
class D = Def>
194 __declspec(empty_bases)
198 P* super() {
return static_cast<P*
>(
this); }
199 const P* super()
const {
return static_cast<const P*
>(
this); }
203 template<
bool Ow = false>
const P*
set(Loc l )
const { super()->D::template
set<Ow>(l);
return super(); }
204 template<
bool Ow = false> P*
set(Loc l ) { super()->D::template
set<Ow>(l);
return super(); }
205 template<
bool Ow = false>
const P*
set( Sym s )
const { super()->D::template
set<Ow>(s);
return super(); }
206 template<
bool Ow = false> P*
set( Sym s ) { super()->D::template
set<Ow>(s);
return super(); }
207 template<
bool Ow = false>
const P*
set( std::string s)
const { super()->D::template
set<Ow>(std::move(s));
return super(); }
208 template<
bool Ow = false> P*
set( std::string s) { super()->D::template
set<Ow>(std::move(s));
return super(); }
209 template<
bool Ow = false>
const P*
set(Loc l, Sym s )
const { super()->D::template
set<Ow>(l, s);
return super(); }
210 template<
bool Ow = false> P*
set(Loc l, Sym s ) { super()->D::template
set<Ow>(l, s);
return super(); }
211 template<
bool Ow = false>
const P*
set(Loc l, std::string s)
const { super()->D::template
set<Ow>(l, std::move(s));
return super(); }
212 template<
bool Ow = false> P*
set(Loc l, std::string s) { super()->D::template
set<Ow>(l, std::move(s));
return super(); }
213 template<
bool Ow = false>
const P*
set(
Dbg d )
const { super()->D::template
set<Ow>(d);
return super(); }
214 template<
bool Ow = false> P*
set(
Dbg d ) { super()->D::template
set<Ow>(d);
return super(); }
252class Def :
public fe::RuntimeCast<Def> {
254 Def& operator=(
const Def&) =
delete;
255 Def(
const Def&) =
delete;
271 constexpr u32 gid() const noexcept {
return gid_; }
272 constexpr u32 tid() const noexcept {
return tid_; }
273 constexpr u32 mark() const noexcept {
return mark_; }
274 constexpr size_t hash() const noexcept {
return hash_; }
275 constexpr Node node() const noexcept {
return node_; }
296 const Def*
type() const noexcept;
305 template<
size_t N =
std::dynamic_extent>
306 constexpr auto
ops() const noexcept {
309 const Def*
op(
size_t i)
const noexcept {
return ops()[i]; }
310 constexpr size_t num_ops() const noexcept {
return num_ops_; }
344 const Def*
dep(
size_t i) const noexcept {
return deps()[i]; }
360 bool has_dep(
unsigned u)
const {
return dep_ & u; }
390 template<nat_t A = std::dynamic_extent,
class F>
392 using R = std::decay_t<
decltype(f(
this))>;
393 if constexpr (A == std::dynamic_extent) {
396 std::array<R, A> array;
397 for (
nat_t i = 0; i != A; ++i)
398 array[i] = f(
proj(A, i));
410 using R = std::decay_t<
decltype(f(
this))>;
413 template<nat_t A = std::dynamic_extent>
415 return projs<A>([](
const Def* def) {
return def; });
418 return tprojs([](
const Def* def) {
return def; });
421 return projs(a, [](
const Def* def) {
return def; });
443 template<
class D = Def>
446 if (
auto var = mut->has_var())
return {mut,
var};
448 return {
nullptr,
nullptr};
497 template<
class T = Def,
bool invert = false>
499 if constexpr (std::is_same<T, Def>::value)
500 return mut_ ^ invert ?
const_cast<Def*
>(
this) :
nullptr;
502 return mut_ ^ invert ?
const_cast<Def*
>(
this)->
template isa<T>() :
nullptr;
506 template<
class T = Def,
bool invert = false>
508 assert(mut_ ^ invert);
509 if constexpr (std::is_same<T, Def>::value)
510 return const_cast<Def*
>(
this);
512 return const_cast<Def*
>(
this)->
template as<T>();
528 template<
bool Ow = false>
const Def*
set(Loc l)
const {
if (Ow || !
dbg_.loc())
dbg_.set(l);
return this; }
529 template<
bool Ow = false> Def*
set(Loc l) {
if (Ow || !
dbg_.loc())
dbg_.set(l);
return this; }
530 template<
bool Ow = false>
const Def*
set(Sym s)
const {
if (Ow || !
dbg_.sym())
dbg_.set(s);
return this; }
531 template<
bool Ow = false> Def*
set(Sym s) {
if (Ow || !
dbg_.sym())
dbg_.set(s);
return this; }
532 template<
bool Ow = false>
const Def*
set( std::string s)
const {
set<Ow>(
sym(std::move(s)));
return this; }
533 template<
bool Ow = false> Def*
set( std::string s) {
set<Ow>(
sym(std::move(s)));
return this; }
534 template<
bool Ow = false>
const Def*
set(Loc l, Sym s )
const {
set<Ow>(l);
set<Ow>(s);
return this; }
536 template<
bool Ow = false>
const Def*
set(Loc l, std::string s)
const {
set<Ow>(l);
set<Ow>(
sym(std::move(s)));
return this; }
537 template<
bool Ow = false> Def*
set(Loc l, std::string s) {
set<Ow>(l);
set<Ow>(
sym(std::move(s)));
return this; }
538 template<
bool Ow = false>
const Def*
set(
Dbg d)
const {
set<Ow>(d.loc(), d.sym());
return this; }
539 template<
bool Ow = false> Def*
set(
Dbg d) {
set<Ow>(d.loc(), d.sym());
return this; }
551 const Def*
debug_suffix(std::string)
const {
return this; }
575 template<
size_t N = std::dynamic_extent>
576 constexpr auto reduce(
const Def* arg)
const {
577 return reduce_(arg).span<N>();
591 virtual const Def*
check([[maybe_unused]]
size_t i,
const Def* def) {
return def; }
619 void dump(
int max)
const;
620 void write(
int max)
const;
621 void write(
int max,
const char* file)
const;
622 std::ostream&
stream(std::ostream&,
int max)
const;
635 [[nodiscard]]
static Cmp cmp(
const Def* a,
const Def* b);
636 [[nodiscard]]
static bool less(
const Def* a,
const Def* b);
637 [[nodiscard]]
static bool greater(
const Def* a,
const Def* b);
644 void dot(std::ostream& os,
int max = std::numeric_limits<int>::max(),
bool types =
false)
const;
646 void dot(
const char* file =
nullptr,
int max = std::numeric_limits<int>::max(),
bool types =
false)
const;
647 void dot(
const std::string& file,
int max = std::numeric_limits<int>::max(),
bool types =
false)
const {
648 return dot(file.c_str(), max, types);
656 Sym
sym(
const char*)
const;
657 Sym
sym(std::string_view)
const;
658 Sym
sym(std::string)
const;
662 Defs reduce_(
const Def* arg)
const;
663 virtual Def*
stub_(
World&,
const Def*) { fe::unreachable(); }
669 const Def** ops_ptr()
const {
670 return reinterpret_cast<const Def**
>(
reinterpret_cast<char*
>(
const_cast<Def*
>(
this + 1)));
672 bool equal(
const Def* other)
const;
675 [[nodiscard]]
static bool cmp_(
const Def* a,
const Def* b);
683 mutable World* world_;
693 mutable bool annex_ : 1;
704 mutable u32 tid_ = 0;
705 mutable const Def* type_;
707 template<
class D,
size_t N>
711 friend std::ostream&
operator<<(std::ostream&,
const Def*);
743class Univ :
public Def,
public Setters<Univ> {
758class UMax :
public Def,
public Setters<UMax> {
762 static constexpr size_t Num_Ops = std::dynamic_extent;
774class UInc :
public Def,
public Setters<UInc> {
797class Type :
public Def,
public Setters<Type> {
799 Type(
const Def*
level)
829 template<
class T = flags_t>
831 static_assert(
sizeof(T) <= 8);
842 template<
class T = nat_t>
843 static std::optional<T>
isa(
const Def* def) {
845 if (
auto lit = def->isa<Lit>())
return lit->get<T>();
848 template<
class T = nat_t>
849 static T
as(
const Def* def) {
850 return def->as<Lit>()->
get<T>();
892 static const Def*
isa(
const Def* def);
893 static const Def*
as(
const Def* def) {
898 static std::optional<nat_t>
isa_lit(
const Def* def);
901 assert(res.has_value());
924class Proxy :
public Def,
public Setters<Proxy> {
939 static constexpr size_t Num_Ops = std::dynamic_extent;
950class Global :
public Def,
public Setters<Global> {
960 const Def*
init()
const {
return op(0); }
bool is_set() const
Yields true if empty or the last op is set.
size_t num_deps() const noexcept
const Def * zonk_mut() const
If mutable, zonk()s all ops and tries to immutabilize it; otherwise just zonk.
const Def * set(Dbg d) const
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
constexpr Node node() const noexcept
Def * set(size_t i, const Def *)
Successively set from left to right.
virtual const Def * check(size_t i, const Def *def)
Checks whether the ith operand can be set to def.
void dot(std::ostream &os, int max=std::numeric_limits< int >::max(), bool types=false) const
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
const Var * has_var() const
As above if this is a mutable.
Defs deps() const noexcept
bool has_dep(Dep d) const
bool is_elim() const noexcept
nat_t num_tprojs() const
As above but yields 1, if Flags::scalarize_threshold is exceeded.
const Def * zonk() const
If Holes have been filled, reconstruct the program without them.
bool has_dep(unsigned u) const
World & world() const noexcept
virtual const Def * check()
After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
const Def * refine(size_t i, const Def *new_op) const
Def * set_type(const Def *)
Update type.
std::string_view node_name() const
auto projs(nat_t a, F f) const
bool is_intro() const noexcept
constexpr auto ops() const noexcept
Vars local_vars() const
Vars reachable by following immutable deps().
constexpr flags_t flags() const noexcept
virtual Def * stub_(World &, const Def *)
const Def * set(std::string s) const
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
constexpr u32 mark() const noexcept
Used internally by free_vars().
constexpr u32 tid() const noexcept
Trie id - only used in Trie.
auto projs(nat_t a) const
friend std::ostream & operator<<(std::ostream &, const Def *)
This will stream def as an operand.
const Def * debug_prefix(std::string) const
const Def * op(size_t i) const noexcept
bool is_immutabilizable()
virtual const Def * rebuild_(World &w, const Def *type, Defs ops) const =0
std::pair< D *, const Var * > isa_binder() const
Is this a mutable that introduces a Var?
const Def * var(nat_t a, nat_t i) noexcept
void transfer_external(Def *to)
const Def * unfold_type() const
Yields the type of this Def and builds a new Type (UInc n) if necessary.
const Def * proj(nat_t i) const
As above but takes Def::num_projs as arity.
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
bool is_open() const
Has free_vars()?
constexpr size_t hash() const noexcept
virtual const Def * immutabilize()
Tries to make an immutable from a mutable.
bool is_form() const noexcept
Muts local_muts() const
Mutables reachable by following immutable deps(); mut->local_muts() is by definition the set { mut }...
const Def * debug_suffix(std::string) const
const Def * set(Sym s) const
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Def * outermost_binder() const
Transitively walks up free_vars() till the outermoust binder has been found.
const Def * dep(size_t i) const noexcept
bool is_mutable() const noexcept
bool is_external() const noexcept
const Def * rebuild(World &w, const Def *type, Defs ops) const
Def::rebuilds this Def while using new_op as substitute for its i'th Def::op.
const Def * set(Loc l, Sym s) const
static bool less(const Def *a, const Def *b)
bool is_meta() const noexcept
static bool greater(const Def *a, const Def *b)
const Def * set(Loc l) const
void write(int max) const
Def * set(Loc l, std::string s)
Def * stub(const Def *type)
static Cmp cmp(const Def *a, const Def *b)
virtual constexpr size_t reduction_offset() const noexcept
First Def::op that needs to be dealt with during reduction; e.g.
nat_t num_projs() const
Yields Def::arity(), if it is a Lit, or 1 otherwise.
std::ostream & stream(std::ostream &, int max) const
friend void swap(World &, World &) noexcept
const Def * var_type()
If this is a binder, compute the type of its Variable.
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
virtual const Def * arity() const
Def * unset()
Unsets all Def::ops; works even, if not set at all or only partially set.
std::string unique_name() const
name + "_" + Def::gid
constexpr auto reduce(const Def *arg) const
const Def * set(Loc l, std::string s) const
const T * isa_imm() const
bool needs_zonk() const
Yields true, if Def::local_muts() contain a Hole that is set.
Muts users()
Set of mutables where this mutable is locally referenced.
bool is_closed() const
Has no free_vars()?
void dot(const std::string &file, int max=std::numeric_limits< int >::max(), bool types=false) const
const Def * rebuild(const Def *type, Defs ops) const
Vars free_vars() const
Compute a global solution by transitively following mutables as well.
Def * stub(World &w, const Def *type)
u32 judge() const noexcept
const Def * tproj(nat_t i) const
As above but takes Def::num_tprojs.
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
bool is_annex() const noexcept
constexpr size_t num_ops() const noexcept
void set(const Def *init)
Global * stub(const Def *type)
const Def * alloced_type() const
const Def * rebuild_(World &, const Def *, Defs) const final
static constexpr size_t Num_Ops
Global * stub_(World &, const Def *) final
static constexpr auto Node
This node is a hole in the IR that is inferred by its context later on.
const Def * rebuild_(World &, const Def *, Defs) const final
static constexpr auto Node
static nat_t as_lit(const Def *def)
static constexpr nat_t size2bitwidth(nat_t n)
static constexpr nat_t bitwidth2size(nat_t n)
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
static std::optional< nat_t > isa_lit(const Def *def)
static constexpr size_t Num_Ops
static const Def * as(const Def *def)
static constexpr auto Node
static std::optional< T > isa(const Def *def)
static T as(const Def *def)
static constexpr size_t Num_Ops
const Def * rebuild_(World &, const Def *, Defs) const final
static constexpr auto Node
const Def * rebuild_(World &, const Def *, Defs) const final
static constexpr size_t Num_Ops
static constexpr size_t Num_Ops
const Def * rebuild_(World &, const Def *, Defs) const final
static constexpr auto Node
u32 pass() const
IPass::index within PassMan.
CRTP-based mixin to declare setters for Def::loc & Def::name using a covariant return type.
const P * set(Sym s) const
const P * set(Loc l, Sym s) const
const P * set(Loc l) const
const P * set(Dbg d) const
const P * set(Loc l, std::string s) const
P * set(Loc l, std::string s)
const P * set(std::string s) const
static constexpr size_t Num_Ops
static constexpr auto Node
const Def * level() const
const Def * rebuild_(World &, const Def *, Defs) const final
static constexpr auto Node
static constexpr size_t Num_Ops
const Def * rebuild_(World &, const Def *, Defs) const final
static constexpr auto Node
const Def * rebuild_(World &, const Def *, Defs) const final
static constexpr size_t Num_Ops
static constexpr size_t Num_Ops
static constexpr auto Node
const Def * rebuild_(World &, const Def *, Defs) const final
A variable introduced by a binder (mutable).
const Def * rebuild_(World &, const Def *, Defs) const final
static constexpr auto Node
static constexpr size_t Num_Ops
This is a thin wrapper for absl::InlinedVector<T, N, A> which is a drop-in replacement for std::vecto...
The World represents the whole program and manages creation of MimIR nodes (Defs).
#define MIM_PROJ(NAME, CONST)
Use as mixin to wrap all kind of Def::proj and Def::projs variants.
DefMap< const Def * > Def2Def
Vector< const Def * > DefVec
D bitcast(const S &src)
A bitcast from src of type S to D.
Dep
Tracks a dependency to certain Defs transitively through the Def::deps() up to but excliding mutables...
absl::flat_hash_map< K, V, GIDHash< K > > GIDMap
GIDSet< const Var * > VarSet
GIDMap< const Var *, To > VarMap
GIDMap< const Def *, To > DefMap
GIDSet< const Def * > DefSet
static constexpr size_t Num_Nodes
const Def *(*)(const Def *, const Def *, const Def *) NormalizeFn
Sets< const Var >::Set Vars
@ Imm
Node may be immmutable.
@ Intro
Term Introduction like λ(x: Nat): Nat = x.
@ Meta
Meta rules for Universe and Type levels.
@ Form
Type Formation like T -> T.
@ Elim
Term Elimination like f a.
absl::flat_hash_set< K, GIDHash< K > > GIDSet
Vector(I, I, A=A()) -> Vector< typename std::iterator_traits< I >::value_type, Default_Inlined_Size< typename std::iterator_traits< I >::value_type >, A >
GIDMap< Def *, To > MutMap
VarMap< const Var * > Var2Var