Skip to content

Commit

Permalink
wip: working on #126 and #117
Browse files Browse the repository at this point in the history
  • Loading branch information
leissa committed Nov 6, 2022
1 parent ea8350a commit 37e8946
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
34 changes: 33 additions & 1 deletion dialects/clos/clos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@

namespace thorin::clos {

template<bool type>
size_t env_idx(const Def* def) {
size_t n = def->num_projs();
if (n == 0) return 0;
auto first = def->proj(n, 0_s);
if ((type && match<mem::M>(first)) || (!type && match<mem::M>(first->type()))) return 1;
return 0;
}

template<bool type>
const Def* env_insert(const Def* def, const Def* env) {
auto& w = env->world();
size_t n = def->num_projs();
size_t x = env_idx<type>(def);

DefArray new_ops(n + 1);
for (size_t i = 0, j = 0; i != n + 1; ++i) new_ops[i] = i == x ? env : def->proj(n, j++);

return type ? w.sigma(new_ops) : w.tuple(new_ops);
}

Sigma* pi2clos(const Pi* pi) {
auto& world = pi->world();
auto sigma = world.nom_sigma(world.type(), 3, world.dbg("Clos"))->set(0, world.type());
auto env_t = sigma->var(0_s);
auto new_dom = env_insert<true>(pi->dom(), env_t);
sigma->set(1, world.pi(new_dom, pi->codom()));
sigma->set(2, env_t);
return sigma;
}

/*
* ClosLit
*/
Expand Down Expand Up @@ -99,7 +130,8 @@ const Sigma* isa_clos_type(const Def* def) {
return (pi && pi->is_cn() && pi->num_ops() > 1_u64 && pi->dom(Clos_Env_Param) == var) ? sig : nullptr;
}

Sigma* clos_type(const Pi* pi) { return ctype(pi->world(), pi->doms(), nullptr)->as_nom<Sigma>(); }
Sigma* clos_type(const Pi* pi) { return pi2clos(pi); }
//return ctype(pi->world(), pi->doms(), nullptr)->as_nom<Sigma>(); }

const Pi* clos_type_to_pi(const Def* ct, const Def* new_env_type) {
assert(isa_clos_type(ct));
Expand Down
7 changes: 5 additions & 2 deletions dialects/clos/clos.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ class ClosLit {
///@}

operator bool() const { return def_ != nullptr; }

operator const Tuple*() { return def_; }

const Tuple* operator->() {
assert(def_);
return def_;
Expand Down Expand Up @@ -163,6 +161,11 @@ inline const Def* clos_remove_env(const Def* tup_or_sig) {
inline const Def* clos_sub_env(const Def* tup_or_sig, const Def* new_env) {
return tup_or_sig->refine(Clos_Env_Param, new_env);
}

size_t env_idx(const Def*);
const Def* env_insert(const Def* def, const Def* env);


///@}

} // namespace thorin::clos
2 changes: 1 addition & 1 deletion dialects/clos/phase/clos_conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FreeDefAna {
FreeDefAna(World& world)
: world_(world)
, cur_pass_id(1)
, lam2nodes_(){};
, lam2nodes_() {}

/// FreeDefAna::run will compute free defs (FD) that appear in @p lam%s body.
/// Nominal Def%s are only considered free if they are annotated with Clos::freeBB or
Expand Down
2 changes: 1 addition & 1 deletion thorin/def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ const Def* Def::proj(nat_t a, nat_t i, const Def* dbg) const {

World& w = world();

if (isa<Tuple>() || isa<Sigma>()) {
if (isa<Tuple, Sigma>()) {
return op(i);
} else if (auto arr = isa<Arr>()) {
if (arr->arity()->isa<Top>()) return arr->body();
Expand Down

0 comments on commit 37e8946

Please sign in to comment.