21 X(Lit, Judge::Intro) \
22 X(Axm, Judge::Intro) \
23 X(Var, Judge::Intro) \
24 X(Global, Judge::Intro) \
25 X(Proxy, Judge::Intro) \
26 X(Hole, Judge::Hole ) \
27 X(Type, Judge::Meta ) X(Univ, Judge::Meta ) X(UMax, Judge::Meta) X(UInc, (Judge::Meta )) \
28 X(Pi, Judge::Form ) X(Lam, Judge::Intro) X(App, Judge::Elim) \
29 X(Sigma, Judge::Form ) X(Tuple, Judge::Intro) X(Extract, Judge::Elim) X(Insert, (Judge::Intro | Judge::Elim)) \
30 X(Arr, Judge::Form ) X(Pack, Judge::Intro) \
31 X(Join, Judge::Form ) X(Inj, Judge::Intro) X(Match, Judge::Elim) X(Top, (Judge::Intro )) \
32 X(Meet, Judge::Form ) X(Merge, Judge::Intro) X(Split, Judge::Elim) X(Bot, (Judge::Intro )) \
33 X(Reform, Judge::Form ) X(Rule, Judge::Intro) \
34 X(Uniq, Judge::Form ) \
35 X(Nat, Judge::Form ) \
38#define MIM_IMM_NODE(X) \
43 X(Type) X(Univ) X(UMax) X(UInc) \
45 X(Sigma) X(Tuple) X(Extract) X(Insert) \
47 X(Join) X(Inj) X(Match) X(Top) \
48 X(Meet) X(Merge) X(Split) X(Bot) \
54#define MIM_MUT_NODE(X) \
108#define CODE(node, _) node,
113#define CODE(node, _) +size_t(1)
118enum class Dep :
unsigned {
150template<>
struct fe::is_bit_enum<
mim::
Dep> : std::true_type {};
151template<>
struct fe::is_bit_enum<
mim::
Judge> : std::true_type {};
152template<>
struct fe::is_bit_enum<
mim::
Mut> : std::true_type {};
159#define MIM_PROJ(NAME, CONST) \
160 nat_t num_##NAME##s() CONST noexcept { return ((const Def*)NAME())->num_projs(); } \
161 nat_t num_t##NAME##s() CONST noexcept { return ((const Def*)NAME())->num_tprojs(); } \
162 const Def* NAME(nat_t a, nat_t i) CONST noexcept { return ((const Def*)NAME())->proj(a, i); } \
163 const Def* NAME(nat_t i) CONST noexcept { return ((const Def*)NAME())->proj(i); } \
164 const Def* t##NAME(nat_t i) CONST noexcept { return ((const Def*)NAME())->tproj(i); } \
165 template<nat_t A = std::dynamic_extent, class F> \
166 auto NAME##s(F f) CONST noexcept { \
167 return ((const Def*)NAME())->projs<A, F>(f); \
170 auto t##NAME##s(F f) CONST noexcept { \
171 return ((const Def*)NAME())->tprojs<F>(f); \
173 template<nat_t A = std::dynamic_extent> \
174 auto NAME##s() CONST noexcept { \
175 return ((const Def*)NAME())->projs<A>(); \
177 auto t##NAME##s() CONST noexcept { return ((const Def*)NAME())->tprojs(); } \
179 auto NAME##s(nat_t a, F f) CONST noexcept { \
180 return ((const Def*)NAME())->projs<F>(a, f); \
182 auto NAME##s(nat_t a) CONST noexcept { return ((const Def*)NAME())->projs(a); }
185template<
class P,
class D = Def>
188 __declspec(empty_bases)
192 P* super() {
return static_cast<P*
>(
this); }
193 const P* super()
const {
return static_cast<const P*
>(
this); }
197 template<
bool Ow = false>
const P*
set(Loc l )
const { super()->D::template
set<Ow>(l);
return super(); }
198 template<
bool Ow = false> P*
set(Loc l ) { super()->D::template
set<Ow>(l);
return super(); }
199 template<
bool Ow = false>
const P*
set( Sym s )
const { super()->D::template
set<Ow>(s);
return super(); }
200 template<
bool Ow = false> P*
set( Sym s ) { super()->D::template
set<Ow>(s);
return super(); }
201 template<
bool Ow = false>
const P*
set( std::string s)
const { super()->D::template
set<Ow>(std::move(s));
return super(); }
202 template<
bool Ow = false> P*
set( std::string s) { super()->D::template
set<Ow>(std::move(s));
return super(); }
203 template<
bool Ow = false>
const P*
set(Loc l, Sym s )
const { super()->D::template
set<Ow>(l, s);
return super(); }
204 template<
bool Ow = false> P*
set(Loc l, Sym s ) { super()->D::template
set<Ow>(l, s);
return super(); }
205 template<
bool Ow = false>
const P*
set(Loc l, std::string s)
const { super()->D::template
set<Ow>(l, std::move(s));
return super(); }
206 template<
bool Ow = false> P*
set(Loc l, std::string s) { super()->D::template
set<Ow>(l, std::move(s));
return super(); }
207 template<
bool Ow = false>
const P*
set(
Dbg d )
const { super()->D::template
set<Ow>(d);
return super(); }
208 template<
bool Ow = false> P*
set(
Dbg d ) { super()->D::template
set<Ow>(d);
return super(); }
246class Def :
public fe::RuntimeCast<Def> {
248 Def& operator=(
const Def&) =
delete;
249 Def(
const Def&) =
delete;
266 constexpr u32 gid() const noexcept {
return gid_; }
267 constexpr u32 tid() const noexcept {
return tid_; }
268 constexpr u32 mark() const noexcept {
return mark_; }
269 constexpr size_t hash() const noexcept {
return hash_; }
270 constexpr Node node() const noexcept {
return node_; }
291 const Def*
type() const noexcept;
300 template<
size_t N =
std::dynamic_extent>
301 constexpr auto
ops() const noexcept {
304 const Def*
op(
size_t i)
const noexcept {
return ops()[i]; }
305 constexpr size_t num_ops() const noexcept {
return num_ops_; }
339 const Def*
dep(
size_t i) const noexcept {
return deps()[i]; }
354 bool has_dep() const noexcept {
return dep_ != 0; }
385 template<nat_t A = std::dynamic_extent,
class F>
387 using R = std::decay_t<
decltype(f(
this))>;
388 if constexpr (A == std::dynamic_extent) {
391 std::array<R, A> array;
392 for (
nat_t i = 0; i != A; ++i)
393 array[i] = f(
proj(A, i));
405 using R = std::decay_t<
decltype(f(
this))>;
408 template<nat_t A = std::dynamic_extent>
410 return projs<A>([](
const Def* def) {
return def; });
413 return tprojs([](
const Def* def) {
return def; });
416 return projs(a, [](
const Def* def) {
return def; });
438 template<
class D = Def>
441 if (
auto var = mut->has_var())
return {mut,
var};
443 return {
nullptr,
nullptr};
492 template<
class T = Def,
bool invert = false>
494 if constexpr (std::is_same<T, Def>::value)
495 return mut_ ^ invert ?
const_cast<Def*
>(
this) :
nullptr;
497 return mut_ ^ invert ?
const_cast<Def*
>(
this)->
template isa<T>() :
nullptr;
501 template<
class T = Def,
bool invert = false>
503 assert(mut_ ^ invert);
504 if constexpr (std::is_same<T, Def>::value)
505 return const_cast<Def*
>(
this);
507 return const_cast<Def*
>(
this)->
template as<T>();
523 template<
bool Ow = false>
const Def*
set(Loc l)
const {
if (Ow || !
dbg_.loc())
dbg_.set(l);
return this; }
524 template<
bool Ow = false> Def*
set(Loc l) {
if (Ow || !
dbg_.loc())
dbg_.set(l);
return this; }
525 template<
bool Ow = false>
const Def*
set(Sym s)
const {
if (Ow || !
dbg_.sym())
dbg_.set(s);
return this; }
526 template<
bool Ow = false> Def*
set(Sym s) {
if (Ow || !
dbg_.sym())
dbg_.set(s);
return this; }
527 template<
bool Ow = false>
const Def*
set( std::string s)
const {
set<Ow>(
sym(std::move(s)));
return this; }
528 template<
bool Ow = false> Def*
set( std::string s) {
set<Ow>(
sym(std::move(s)));
return this; }
529 template<
bool Ow = false>
const Def*
set(Loc l, Sym s )
const {
set<Ow>(l);
set<Ow>(s);
return this; }
531 template<
bool Ow = false>
const Def*
set(Loc l, std::string s)
const {
set<Ow>(l);
set<Ow>(
sym(std::move(s)));
return this; }
532 template<
bool Ow = false> Def*
set(Loc l, std::string s) {
set<Ow>(l);
set<Ow>(
sym(std::move(s)));
return this; }
533 template<
bool Ow = false>
const Def*
set(
Dbg d)
const {
set<Ow>(d.loc(), d.sym());
return this; }
534 template<
bool Ow = false> Def*
set(
Dbg d) {
set<Ow>(d.loc(), d.sym());
return this; }
546 const Def*
debug_suffix(std::string)
const {
return this; }
570 template<
size_t N = std::dynamic_extent>
571 constexpr auto reduce(
const Def* arg)
const {
572 return reduce_(arg).span<N>();
586 virtual const Def*
check([[maybe_unused]]
size_t i,
const Def* def) {
return def; }
614 void dump(
int max)
const;
615 void write(
int max)
const;
616 void write(
int max,
const char* file)
const;
617 std::ostream&
stream(std::ostream&,
int max)
const;
630 [[nodiscard]]
static Cmp cmp(
const Def* a,
const Def* b);
631 [[nodiscard]]
static bool less(
const Def* a,
const Def* b);
632 [[nodiscard]]
static bool greater(
const Def* a,
const Def* b);
639 void dot(std::ostream& os,
int max = std::numeric_limits<int>::max(),
bool types =
false)
const;
641 void dot(
const char* file =
nullptr,
int max = std::numeric_limits<int>::max(),
bool types =
false)
const;
642 void dot(
const std::string& file,
int max = std::numeric_limits<int>::max(),
bool types =
false)
const {
643 return dot(file.c_str(), max, types);
651 Sym
sym(
const char*)
const;
652 Sym
sym(std::string_view)
const;
653 Sym
sym(std::string)
const;
657 Defs reduce_(
const Def* arg)
const;
658 virtual Def*
stub_(
World&,
const Def*) { fe::unreachable(); }
664 const Def** ops_ptr()
const {
665 return reinterpret_cast<const Def**
>(
reinterpret_cast<char*
>(
const_cast<Def*
>(
this + 1)));
667 bool equal(
const Def* other)
const;
670 [[nodiscard]]
static bool cmp_(
const Def* a,
const Def* b);
678 mutable World* world_;
688 mutable bool annex_ : 1;
699 mutable u32 tid_ = 0;
700 mutable const Def* type_;
702 template<
class D,
size_t N>
706 friend std::ostream&
operator<<(std::ostream&,
const Def*);
738class Univ :
public Def,
public Setters<Univ> {
753class UMax :
public Def,
public Setters<UMax> {
757 static constexpr size_t Num_Ops = std::dynamic_extent;
769class UInc :
public Def,
public Setters<UInc> {
792class Type :
public Def,
public Setters<Type> {
794 Type(
const Def*
level)
824 template<
class T = flags_t>
826 static_assert(
sizeof(T) <= 8);
837 template<
class T = nat_t>
838 static std::optional<T>
isa(
const Def* def) {
840 if (
auto lit = def->isa<Lit>())
return lit->get<T>();
843 template<
class T = nat_t>
844 static T
as(
const Def* def) {
845 return def->as<Lit>()->
get<T>();
887 static const Def*
isa(
const Def* def);
888 static const Def*
as(
const Def* def) {
893 static std::optional<nat_t>
isa_lit(
const Def* def);
896 assert(res.has_value());
919class Proxy :
public Def,
public Setters<Proxy> {
934 static constexpr size_t Num_Ops = std::dynamic_extent;
945class Global :
public Def,
public Setters<Global> {
955 const Def*
init()
const {
return op(0); }
989template<class T> requires std::derived_from<T, mim::Def> struct std::formatter< T*> : fe::ostream_formatter {};
990template<
class T>
requires std::derived_from<T, mim::Def>
struct std::formatter<const T*> : fe::ostream_formatter {};
991template<>
struct std::formatter<
mim::
Muts> : fe::ostream_formatter {};
992template<>
struct std::formatter<
mim::
Vars> : fe::ostream_formatter {};
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.
bool has_dep() const noexcept
Defs deps() const noexcept
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.
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 *)
bool has_dep(Dep d) const noexcept
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
Judge judge() const noexcept
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)
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
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
constexpr D bitcast_resize(const S &src) noexcept
A bitcast from src of type S to D, supporting different sizes.
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