MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
axm.cpp
Go to the documentation of this file.
1#include "mim/axm.h"
2
3#include "mim/world.h"
4
5namespace mim {
6
7Axm::Axm(NormalizeFn normalizer, u8 curry, u8 trip, const Def* type, plugin_t plugin, tag_t tag, sub_t sub)
8 : Def(Node, type, Defs{}, Annex::flags(plugin, tag, sub)) {
9 normalizer_ = normalizer;
10 curry_ = curry;
11 trip_ = trip;
12}
13
14std::pair<u8, u8> Axm::infer_curry_and_trip(const Def* type) {
15 u8 curry = 0;
16 u8 trip = 0;
17 MutSet done;
18 while (auto pi = type->isa<Pi>()) {
19 if (auto mut = pi->isa_mut()) {
20 if (auto [_, ins] = done.emplace(mut); !ins) {
21 // infer trip
22 auto curr = pi;
23 do {
24 ++trip;
25 curr = curr->codom()->as<Pi>();
26 } while (curr != mut);
27 break;
28 }
29 }
30
31 ++curry;
32 type = pi->codom();
33 }
34
35 return {curry, trip};
36}
37
38std::tuple<const Axm*, u8, u8> Axm::get(const Def* def) {
39 if (auto axm = def->isa<Axm>()) return {axm, axm->curry(), axm->trip()};
40 if (auto app = def->isa<App>()) return {app->axm(), app->curry(), app->trip()};
41 return {nullptr, 0, 0};
42}
43
44std::tuple<const Axm*, u8, u8> Axm::next(const Def* callee) {
45 auto [axm, curry, trip] = get(callee);
46 if (axm) {
47 curry = curry == 0 ? trip : curry; // counter exhausted - start the next trip
48 curry = curry == Trip_End ? curry : curry - 1; // Trip_End sticks
49 }
50 return {axm, curry, trip};
51}
52
53} // namespace mim
u8 trip() const
Definition axm.h:38
static std::tuple< const Axm *, u8, u8 > next(const Def *callee)
Like Axm::get, but advances the counter as one more App is about to be built on top of callee.
Definition axm.cpp:44
static std::pair< u8, u8 > infer_curry_and_trip(const Def *type)
Definition axm.cpp:14
static constexpr u8 Trip_End
Definition axm.h:148
u8 curry() const
Definition axm.h:37
static std::tuple< const Axm *, u8, u8 > get(const Def *def)
Yields currying counter of def.
Definition axm.cpp:38
Base class for all Defs.
Definition def.h:273
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1111
A dependent function type.
Definition lam.h:14
Definition ast.h:16
u8 sub_t
Definition types.h:42
fe::View< const Def * > Defs
Definition def.h:91
GIDSet< Def * > MutSet
Definition def.h:101
const Def *(*)(const Def *, const Def *, const Def *) NormalizeFn
Definition def.h:115
u64 plugin_t
Definition types.h:40
u8 tag_t
Definition types.h:41
uint8_t u8
Definition types.h:27
Node
Definition def.h:120
Holds info about an entity defined within a Plugin (called Annex).
Definition plugin.h:182