Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion bootstrap/s2c-seed.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ struct E_Coll__List__D__Draft;
struct E_D__Draft;
struct E_D__With__Coll__List_1_D__Draft;
struct E_Coll__Option__D__Rel;
struct E_D__Loading;
struct E_D__With__D__RelRef;
struct E_D__Pass;
struct E_D__Stage;
Expand Down Expand Up @@ -2453,6 +2454,8 @@ struct E_D__With__Coll__List_1_D__Draft { union { uint8_t tag; union { struct {
_Static_assert(sizeof(((struct E_D__With__Coll__List_1_D__Draft*)0)->u.C_D__With) == 24, "srs layout");
struct E_Coll__Option__D__Rel { union { uint8_t tag; union { struct { uint8_t t_; struct E_D__Rel* f_v; } C_Coll__Some; uint8_t none_; } u; }; };
_Static_assert(sizeof(((struct E_Coll__Option__D__Rel*)0)->u.C_Coll__Some) == 16, "srs layout");
struct E_D__Loading { union { uint8_t tag; union { struct { uint8_t t_; struct E_Coll__List__D__RelRef* f_tab; struct E_Sym__Sym* f_rel; int64_t f_id; struct E_D__Rel* f_rl; struct E_Coll__IMap__D__Rel* f_rels; } C_D__Loading; uint8_t none_; } u; }; };
_Static_assert(sizeof(((struct E_D__Loading*)0)->u.C_D__Loading) == 48, "srs layout");
struct E_D__With__D__RelRef { union { uint8_t tag; union { struct { uint8_t t_; struct E_Coll__List__D__RelRef* f_tab; struct E_D__RelRef* f_v; } C_D__With; uint8_t none_; } u; }; };
_Static_assert(sizeof(((struct E_D__With__D__RelRef*)0)->u.C_D__With) == 24, "srs layout");
struct E_D__Pass { union { uint8_t tag; union { uint8_t none_; } u; }; };
Expand Down Expand Up @@ -4167,6 +4170,10 @@ static struct E_Coll__IMap__Coll__List_1_I64* f_D__probesPlans(struct E_Coll__Li
static struct E_Coll__IMap__Coll__List_1_I64* f_D__probesAll(struct E_Coll__List__D__CRule* p_rs, struct E_Coll__IMap__Coll__List_1_I64* p_ics);
static struct E_Coll__IMap__D__Rel* f_D__initRels(struct E_Coll__IMap__Coll__List_1_I64* p_ics, int64_t p_id, int64_t p_k, struct E_Coll__IMap__D__Rel* p_acc);
static struct E_D__With__Coll__IMap_1_D__Rel* f_D__load(struct E_Coll__List__D__RelRef* p_tab, struct E_Coll__List__D__Fact* p_fs, struct E_Coll__IMap__D__Rel* p_rels);
static struct E_D__With__Coll__IMap_1_D__Rel* f_D__loadIn(struct E_D__Loading* p_st, struct E_Coll__List__D__Fact* p_fs);
static struct E_D__Loading* f_D__loadStep(struct E_D__Loading* p_st, struct E_D__Fact* p_f);
static struct E_D__Loading* f_D__loadStart(struct E_Coll__List__D__RelRef* p_tab, struct E_D__Fact* p_f, struct E_Coll__IMap__D__Rel* p_rels);
static struct E_D__Rel* f_D__withRow(struct E_D__Rel* p_rl, struct E_Coll__List__D__Val* p_vals);
static struct E_D__Compiled* f_D__compile(struct E_Coll__List__D__Rule* p_rules);
static struct E_D__Result* f_D__run(struct E_D__Compiled* p_c, struct E_Coll__List__D__Fact* p_facts);
static struct E_D__Result* f_D__solve(struct E_Coll__List__D__Rule* p_rules, struct E_Coll__List__D__Fact* p_facts);
Expand Down Expand Up @@ -7092,7 +7099,11 @@ static struct E_Coll__IMap__Coll__List_1_I64* f_D__probesSteps(struct E_Coll__Li
static struct E_Coll__IMap__Coll__List_1_I64* f_D__probesPlans(struct E_Coll__List__D__Plan* p_ps, struct E_Coll__IMap__Coll__List_1_I64* p_ics) { srs_tail: return ({ __auto_type s0 = p_ps; s0->tag == 0 ? ({ p_ics; }) : ({ __auto_type v_p_0 = s0->u.C_Coll__Cons.f_h; __auto_type v_rest_1 = s0->u.C_Coll__Cons.f_t; ({ struct E_Coll__List__D__Plan* t0 = v_rest_1; struct E_Coll__IMap__Coll__List_1_I64* t1 = f_D__probesPlan(v_p_0, p_ics); p_ps = t0; p_ics = t1; goto srs_tail; (struct E_Coll__IMap__Coll__List_1_I64*){0}; }); }); }); }
static struct E_Coll__IMap__Coll__List_1_I64* f_D__probesAll(struct E_Coll__List__D__CRule* p_rs, struct E_Coll__IMap__Coll__List_1_I64* p_ics) { srs_tail: return ({ __auto_type s0 = p_rs; s0->tag == 0 ? ({ p_ics; }) : ({ __auto_type v_rl_0 = s0->u.C_Coll__Cons.f_h; __auto_type v_rest_1 = s0->u.C_Coll__Cons.f_t; ({ __auto_type res_ = ({ __auto_type s2 = v_rl_0; ({ __auto_type v_body_2 = s2->u.C_D__CRule.f_body; __auto_type v_plans_3 = s2->u.C_D__CRule.f_plans; ({ __auto_type res_ = ({ struct E_Coll__List__D__CRule* t0 = v_rest_1; struct E_Coll__IMap__Coll__List_1_I64* t1 = f_D__probesPlans(v_plans_3, f_D__probesOf(v_body_2, P_Coll__Nil__I64, p_ics)); p_rs = t0; p_ics = t1; goto srs_tail; (struct E_Coll__IMap__Coll__List_1_I64*){0}; }); res_; }); }); }); res_; }); }); }); }
static struct E_Coll__IMap__D__Rel* f_D__initRels(struct E_Coll__IMap__Coll__List_1_I64* p_ics, int64_t p_id, int64_t p_k, struct E_Coll__IMap__D__Rel* p_acc) { srs_tail: return ({ __auto_type res_ = ((p_id == p_k) ? p_acc : ({ struct E_Coll__IMap__Coll__List_1_I64* t0 = p_ics; int64_t t1 = (p_id + ((int64_t)1)); int64_t t2 = p_k; struct E_Coll__IMap__D__Rel* t3 = f_Coll__insert__D__Rel(p_acc, p_id, f_D__newRel(f_D__colsOf(p_ics, p_id))); p_ics = t0; p_id = t1; p_k = t2; p_acc = t3; goto srs_tail; (struct E_Coll__IMap__D__Rel*){0}; })); res_; }); }
static struct E_D__With__Coll__IMap_1_D__Rel* f_D__load(struct E_Coll__List__D__RelRef* p_tab, struct E_Coll__List__D__Fact* p_fs, struct E_Coll__IMap__D__Rel* p_rels) { srs_tail: return ({ __auto_type s0 = p_fs; s0->tag == 0 ? ({ ({ struct E_D__With__Coll__IMap_1_D__Rel* o0 = (struct E_D__With__Coll__IMap_1_D__Rel*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__With__Coll__IMap_1_D__Rel, u) + sizeof(((struct E_D__With__Coll__IMap_1_D__Rel*)0)->u.C_D__With))); o0->u.C_D__With.t_ = 0; o0->u.C_D__With.f_tab = p_tab; o0->u.C_D__With.f_v = p_rels; o0; }); }) : ({ __auto_type v_f_0 = s0->u.C_Coll__Cons.f_h; __auto_type v_rest_1 = s0->u.C_Coll__Cons.f_t; ({ __auto_type res_ = ({ __auto_type s2 = v_f_0; ({ __auto_type v_rel_2 = s2->u.C_D__Fact.f_rel; __auto_type v_vals_3 = s2->u.C_D__Fact.f_vals; ({ __auto_type res_ = ({ __auto_type s4 = f_D__internId(p_tab, v_rel_2); ({ __auto_type v_tab2_4 = s4->u.C_D__With.f_tab; __auto_type v_ref_5 = s4->u.C_D__With.f_v; ({ int64_t v_id_6 = f_D__idOfRef(v_ref_5); struct E_Coll__IMap__D__Rel* v_rels2_7 = ({ __auto_type s7 = f_D__addNew(f_D__relOf(p_rels, v_id_6), ({ struct E_D__Row* o7 = (struct E_D__Row*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Row, u) + sizeof(((struct E_D__Row*)0)->u.C_D__Input))); o7->u.C_D__Input.t_ = 0; o7->u.C_D__Input.f_t = v_vals_3; o7; })); s7->tag == 0 ? ({ p_rels; }) : ({ __auto_type v_rl_7 = s7->u.C_Coll__Some.f_v; f_Coll__insert__D__Rel(p_rels, v_id_6, v_rl_7); }); }); __auto_type res_ = ({ struct E_Coll__List__D__RelRef* t0 = v_tab2_4; struct E_Coll__List__D__Fact* t1 = v_rest_1; struct E_Coll__IMap__D__Rel* t2 = v_rels2_7; p_tab = t0; p_fs = t1; p_rels = t2; goto srs_tail; (struct E_D__With__Coll__IMap_1_D__Rel*){0}; }); res_; }); }); }); res_; }); }); }); res_; }); }); }); }
static struct E_D__With__Coll__IMap_1_D__Rel* f_D__load(struct E_Coll__List__D__RelRef* p_tab, struct E_Coll__List__D__Fact* p_fs, struct E_Coll__IMap__D__Rel* p_rels) { return ({ __auto_type s0 = p_fs; s0->tag == 0 ? ({ ({ struct E_D__With__Coll__IMap_1_D__Rel* o0 = (struct E_D__With__Coll__IMap_1_D__Rel*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__With__Coll__IMap_1_D__Rel, u) + sizeof(((struct E_D__With__Coll__IMap_1_D__Rel*)0)->u.C_D__With))); o0->u.C_D__With.t_ = 0; o0->u.C_D__With.f_tab = p_tab; o0->u.C_D__With.f_v = p_rels; o0; }); }) : ({ __auto_type v_f_0 = s0->u.C_Coll__Cons.f_h; __auto_type v_rest_1 = s0->u.C_Coll__Cons.f_t; f_D__loadIn(f_D__loadStart(p_tab, v_f_0, p_rels), v_rest_1); }); }); }
static struct E_D__With__Coll__IMap_1_D__Rel* f_D__loadIn(struct E_D__Loading* p_st, struct E_Coll__List__D__Fact* p_fs) { srs_tail: return ({ __auto_type s0 = p_fs; s0->tag == 0 ? ({ ({ __auto_type res_ = ({ __auto_type s0 = p_st; ({ __auto_type v_tab_0 = s0->u.C_D__Loading.f_tab; __auto_type v_id_1 = s0->u.C_D__Loading.f_id; __auto_type v_rl_2 = s0->u.C_D__Loading.f_rl; __auto_type v_rels_3 = s0->u.C_D__Loading.f_rels; ({ __auto_type res_ = ({ struct E_D__With__Coll__IMap_1_D__Rel* o4 = (struct E_D__With__Coll__IMap_1_D__Rel*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__With__Coll__IMap_1_D__Rel, u) + sizeof(((struct E_D__With__Coll__IMap_1_D__Rel*)0)->u.C_D__With))); o4->u.C_D__With.t_ = 0; o4->u.C_D__With.f_tab = v_tab_0; o4->u.C_D__With.f_v = f_Coll__insert__D__Rel(v_rels_3, v_id_1, v_rl_2); o4; }); res_; }); }); }); res_; }); }) : ({ __auto_type v_f_0 = s0->u.C_Coll__Cons.f_h; __auto_type v_rest_1 = s0->u.C_Coll__Cons.f_t; ({ struct E_D__Loading* t0 = f_D__loadStep(p_st, v_f_0); struct E_Coll__List__D__Fact* t1 = v_rest_1; p_st = t0; p_fs = t1; goto srs_tail; (struct E_D__With__Coll__IMap_1_D__Rel*){0}; }); }); }); }
static struct E_D__Loading* f_D__loadStep(struct E_D__Loading* p_st, struct E_D__Fact* p_f) { return ({ __auto_type res_ = ({ __auto_type s0 = p_st; ({ __auto_type v_tab_0 = s0->u.C_D__Loading.f_tab; __auto_type v_rel_1 = s0->u.C_D__Loading.f_rel; __auto_type v_id_2 = s0->u.C_D__Loading.f_id; __auto_type v_rl_3 = s0->u.C_D__Loading.f_rl; __auto_type v_rels_4 = s0->u.C_D__Loading.f_rels; ({ __auto_type res_ = ({ __auto_type s5 = p_f; ({ __auto_type v_r_5 = s5->u.C_D__Fact.f_rel; __auto_type v_vals_6 = s5->u.C_D__Fact.f_vals; ({ __auto_type res_ = (f_Sym__eq(v_r_5, v_rel_1) ? ({ struct E_D__Loading* o7 = (struct E_D__Loading*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Loading, u) + sizeof(((struct E_D__Loading*)0)->u.C_D__Loading))); o7->u.C_D__Loading.t_ = 0; o7->u.C_D__Loading.f_tab = v_tab_0; o7->u.C_D__Loading.f_rel = v_rel_1; o7->u.C_D__Loading.f_id = v_id_2; o7->u.C_D__Loading.f_rl = f_D__withRow(v_rl_3, v_vals_6); o7->u.C_D__Loading.f_rels = v_rels_4; o7; }) : f_D__loadStart(v_tab_0, p_f, f_Coll__insert__D__Rel(v_rels_4, v_id_2, v_rl_3))); res_; }); }); }); res_; }); }); }); res_; }); }
static struct E_D__Loading* f_D__loadStart(struct E_Coll__List__D__RelRef* p_tab, struct E_D__Fact* p_f, struct E_Coll__IMap__D__Rel* p_rels) { return ({ __auto_type res_ = ({ __auto_type s0 = p_f; ({ __auto_type v_r_0 = s0->u.C_D__Fact.f_rel; __auto_type v_vals_1 = s0->u.C_D__Fact.f_vals; ({ __auto_type res_ = ({ __auto_type s2 = f_D__internId(p_tab, v_r_0); ({ __auto_type v_tab2_2 = s2->u.C_D__With.f_tab; __auto_type v_ref_3 = s2->u.C_D__With.f_v; ({ int64_t v_id_4 = f_D__idOfRef(v_ref_3); __auto_type res_ = ({ struct E_D__Loading* o5 = (struct E_D__Loading*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Loading, u) + sizeof(((struct E_D__Loading*)0)->u.C_D__Loading))); o5->u.C_D__Loading.t_ = 0; o5->u.C_D__Loading.f_tab = v_tab2_2; o5->u.C_D__Loading.f_rel = v_r_0; o5->u.C_D__Loading.f_id = v_id_4; o5->u.C_D__Loading.f_rl = f_D__withRow(f_D__relOf(p_rels, v_id_4), v_vals_1); o5->u.C_D__Loading.f_rels = p_rels; o5; }); res_; }); }); }); res_; }); }); }); res_; }); }
static struct E_D__Rel* f_D__withRow(struct E_D__Rel* p_rl, struct E_Coll__List__D__Val* p_vals) { return ({ __auto_type s0 = f_D__addNew(p_rl, ({ struct E_D__Row* o0 = (struct E_D__Row*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Row, u) + sizeof(((struct E_D__Row*)0)->u.C_D__Input))); o0->u.C_D__Input.t_ = 0; o0->u.C_D__Input.f_t = p_vals; o0; })); s0->tag == 0 ? ({ p_rl; }) : ({ __auto_type v_x_0 = s0->u.C_Coll__Some.f_v; v_x_0; }); }); }
static struct E_D__Compiled* f_D__compile(struct E_Coll__List__D__Rule* p_rules) { return ({ __auto_type res_ = ({ __auto_type s0 = f_D__drafts(P_Coll__Nil__D__RelRef, p_rules, ((int64_t)0), P_Coll__Nil__D__Draft); ({ __auto_type v_tab_0 = s0->u.C_D__With.f_tab; __auto_type v_ds_1 = s0->u.C_D__With.f_v; ({ struct E_Coll__List__D__CRule* v_crs_2 = f_D__crules(v_ds_1, f_D__headIds(v_ds_1, P_Coll__Nil__I64), P_Coll__Nil__D__CRule); __auto_type res_ = ({ __auto_type s3 = f_D__firstUnsafe(v_crs_2); s3->tag == 1 ? ({ __auto_type v_bad_3 = s3->u.C_Coll__Some.f_v; ({ struct E_D__Compiled* o4 = (struct E_D__Compiled*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Compiled, u) + sizeof(((struct E_D__Compiled*)0)->u.C_D__UnsafeRule))); o4->u.C_D__UnsafeRule.t_ = 1; o4->u.C_D__UnsafeRule.f_rel = v_bad_3; o4; }); }) : ({ ({ int64_t v_k_3 = f_Coll__length__D__RelRef(v_tab_0); __auto_type res_ = ({ __auto_type s4 = f_D__strata(v_crs_2, P_Coll__Tip__I64, v_k_3); s4->tag == 1 ? ({ __auto_type v_w_4 = s4->u.C_D__Looped.f_w; ({ struct E_D__Compiled* o5 = (struct E_D__Compiled*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Compiled, u) + sizeof(((struct E_D__Compiled*)0)->u.C_D__NegLoop))); o5->u.C_D__NegLoop.t_ = 2; o5->u.C_D__NegLoop.f_rel = f_D__report(v_crs_2, v_w_4); o5; }); }) : ({ __auto_type v_st_4 = s4->u.C_D__Layered.f_st; ({ struct E_D__Compiled* o5 = (struct E_D__Compiled*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Compiled, u) + sizeof(((struct E_D__Compiled*)0)->u.C_D__Ready))); o5->u.C_D__Ready.t_ = 0; o5->u.C_D__Ready.f_p = ({ struct E_D__Prog* o5 = (struct E_D__Prog*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Prog, u) + sizeof(((struct E_D__Prog*)0)->u.C_D__Prog))); o5->u.C_D__Prog.t_ = 0; o5->u.C_D__Prog.f_tab = v_tab_0; o5->u.C_D__Prog.f_rels = f_D__initRels(f_D__probesAll(v_crs_2, P_Coll__Tip__Coll__List_1_I64), ((int64_t)0), v_k_3, P_Coll__Tip__D__Rel); o5->u.C_D__Prog.f_strata = f_D__byStratum(v_crs_2, v_st_4, ((int64_t)0), f_D__topOf(v_st_4, ((int64_t)0), v_k_3, ((int64_t)0)), P_Coll__Nil__Coll__List_1_D__CRule); o5; }); o5; }); }); }); res_; }); }); }); res_; }); }); }); res_; }); }
static struct E_D__Result* f_D__run(struct E_D__Compiled* p_c, struct E_Coll__List__D__Fact* p_facts) { return ({ __auto_type s0 = p_c; s0->tag == 1 ? ({ __auto_type v_rel_0 = s0->u.C_D__UnsafeRule.f_rel; ({ struct E_D__Result* o1 = (struct E_D__Result*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Result, u) + sizeof(((struct E_D__Result*)0)->u.C_D__Unsafe))); o1->u.C_D__Unsafe.t_ = 1; o1->u.C_D__Unsafe.f_rel = v_rel_0; o1; }); }) : s0->tag == 2 ? ({ __auto_type v_rel_0 = s0->u.C_D__NegLoop.f_rel; ({ struct E_D__Result* o1 = (struct E_D__Result*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Result, u) + sizeof(((struct E_D__Result*)0)->u.C_D__NegCycle))); o1->u.C_D__NegCycle.t_ = 2; o1->u.C_D__NegCycle.f_rel = v_rel_0; o1; }); }) : ({ __auto_type v_p_0 = s0->u.C_D__Ready.f_p; ({ __auto_type res_ = ({ __auto_type s1 = v_p_0; ({ __auto_type v_tab1_1 = s1->u.C_D__Prog.f_tab; __auto_type v_init_2 = s1->u.C_D__Prog.f_rels; __auto_type v_strata_3 = s1->u.C_D__Prog.f_strata; ({ __auto_type res_ = ({ __auto_type s4 = f_D__load(v_tab1_1, p_facts, v_init_2); ({ __auto_type v_tab_4 = s4->u.C_D__With.f_tab; __auto_type v_rels0_5 = s4->u.C_D__With.f_v; ({ int64_t v_k_6 = f_Coll__length__D__RelRef(v_tab_4); __auto_type res_ = ({ __auto_type s7 = f_D__advance(v_rels0_5, ((int64_t)0), v_k_6, v_rels0_5, ((int64_t)0)); ({ __auto_type v_rels_7 = s7->u.C_D__Next.f_rels; ({ __auto_type res_ = ({ struct E_D__Result* o8 = (struct E_D__Result*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Result, u) + sizeof(((struct E_D__Result*)0)->u.C_D__Solved))); o8->u.C_D__Solved.t_ = 0; o8->u.C_D__Solved.f_m = ({ struct E_D__Model* o8 = (struct E_D__Model*)srs_alloc(eff__Region, (int64_t)(offsetof(struct E_D__Model, u) + sizeof(((struct E_D__Model*)0)->u.C_D__Model))); o8->u.C_D__Model.t_ = 0; o8->u.C_D__Model.f_tab = v_tab_4; o8->u.C_D__Model.f_rels = f_D__runStrata(v_strata_3, v_rels_7, f_D__firstRound(), v_k_6); o8; }); o8; }); res_; }); }); }); res_; }); }); }); res_; }); }); }); res_; }); }); }); }
static struct E_D__Result* f_D__solve(struct E_Coll__List__D__Rule* p_rules, struct E_Coll__List__D__Fact* p_facts) { return f_D__run(f_D__compile(p_rules), p_facts); }
Expand Down
37 changes: 28 additions & 9 deletions s2/datalog.srs
Original file line number Diff line number Diff line change
Expand Up @@ -680,17 +680,36 @@ def D.initRels(ics: Coll.IMap[Coll.List[I64]], id: I64, k: I64, acc: Coll.IMap[D
enum D.Model:
case D.Model(tab: Coll.List[D.RelRef], rels: Coll.IMap[D.Rel])

// the input facts as rows of the input round; a relation that only facts name gets its id here
// the input facts as rows of the input round; a relation that only facts name gets its id here. A run of facts of one
// relation takes one id lookup and one insert into rels
def D.load(tab: Coll.List[D.RelRef], fs: Coll.List[D.Fact], rels: Coll.IMap[D.Rel]): D.With[Coll.IMap[D.Rel]] = fs match
case Coll.Nil => D.With(tab, rels)
case Coll.Cons(f, rest) =>
val D.Fact(rel, vals) = f
val D.With(tab2, ref) = D.internId(tab, rel)
val id = D.idOfRef(ref)
val rels2: Coll.IMap[D.Rel] = D.addNew(D.relOf(rels, id), D.Input(vals)) match
case Coll.None => rels
case Coll.Some(rl) => Coll.insert(rels, id, rl)
D.load(tab2, rest, rels2)
case Coll.Cons(f, rest) => D.loadIn(D.loadStart(tab, f, rels), rest)

// a load in a run: rl is relation rel (id id) with the run's rows so far, not yet in rels
enum D.Loading:
case D.Loading(tab: Coll.List[D.RelRef], rel: Sym.Sym, id: I64, rl: D.Rel, rels: Coll.IMap[D.Rel])

def D.loadIn(st: D.Loading, fs: Coll.List[D.Fact]): D.With[Coll.IMap[D.Rel]] = fs match
case Coll.Nil =>
val D.Loading(tab, _, id, rl, rels) = st
D.With(tab, Coll.insert(rels, id, rl))
case Coll.Cons(f, rest) => D.loadIn(D.loadStep(st, f), rest)

def D.loadStep(st: D.Loading, f: D.Fact): D.Loading =
val D.Loading(tab, rel, id, rl, rels) = st
val D.Fact(r, vals) = f
if Sym.eq(r, rel) then D.Loading(tab, rel, id, D.withRow(rl, vals), rels) else D.loadStart(tab, f, Coll.insert(rels, id, rl))

def D.loadStart(tab: Coll.List[D.RelRef], f: D.Fact, rels: Coll.IMap[D.Rel]): D.Loading =
val D.Fact(r, vals) = f
val D.With(tab2, ref) = D.internId(tab, r)
val id = D.idOfRef(ref)
D.Loading(tab2, r, id, D.withRow(D.relOf(rels, id), vals), rels)

def D.withRow(rl: D.Rel, vals: Coll.List[D.Val]): D.Rel = D.addNew(rl, D.Input(vals)) match
case Coll.None => rl
case Coll.Some(x) => x

// a program's result: its model, or why it is not a program (an unsafe rule, a cycle through negation)
enum D.Result:
Expand Down
Loading