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
5,946 changes: 2,973 additions & 2,973 deletions bootstrap/s2c-seed.c

Large diffs are not rendered by default.

92 changes: 46 additions & 46 deletions lib/datalog.srs

Large diffs are not rendered by default.

92 changes: 46 additions & 46 deletions lib/kanren.srs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def occursIn(s: Subst, n: I64, ts: Terms): Bool = ts match
case TCons(h, t) => occurs(s, n, h) || occursIn(s, n, t)

// s extended so a and b are equal, or Fail
def unify {r: Region} (s: Subst, a: Term, b: Term): Unified =
def unify(s: Subst, a: Term, b: Term): Unified =
val x = walk(s, a)
val y = walk(s, b)
x match
Expand All @@ -53,12 +53,12 @@ def unify {r: Region} (s: Subst, a: Term, b: Term): Unified =
case KInt(w) => if v == w then Ok(s) else Fail
case TCon(_, _) => Fail

def bind {r: Region} (s: Subst, n: I64, t: Term): Unified = t match
def bind(s: Subst, n: I64, t: Term): Unified = t match
case TVar(m) => if m == n then Ok(s) else Ok(SBind(n, t, s))
case TCon(_, _) => if occurs(s, n, t) then Fail else Ok(SBind(n, t, s))
case KInt(_) => Ok(SBind(n, t, s))

def unifyAll {r: Region} (s: Subst, xs: Terms, ys: Terms): Unified = xs match
def unifyAll(s: Subst, xs: Terms, ys: Terms): Unified = xs match
case TNil => ys match
case TNil => Ok(s)
case TCons(_, _) => Fail
Expand All @@ -69,12 +69,12 @@ def unifyAll {r: Region} (s: Subst, xs: Terms, ys: Terms): Unified = xs match
case Ok(s2) => unifyAll(s2, xt, yt)

// t with every bound variable replaced, deeply
def walkAll {r: Region} (s: Subst, t: Term): Term = walk(s, t) match
def walkAll(s: Subst, t: Term): Term = walk(s, t) match
case TVar(n) => TVar(n)
case TCon(f, args) => TCon(f, walkEach(s, args))
case KInt(v) => KInt(v)

def walkEach {r: Region} (s: Subst, ts: Terms): Terms = ts match
def walkEach(s: Subst, ts: Terms): Terms = ts match
case TNil => TNil
case TCons(h, t) => TCons(walkAll(s, h), walkEach(s, t))

Expand Down Expand Up @@ -199,16 +199,16 @@ enum Run:
case Run(answers: Answers, status: Status)

// rename apart: every variable moved by k
def shift {r: Region} (t: Term, k: I64): Term = t match
def shift(t: Term, k: I64): Term = t match
case TVar(n) => TVar(n + k)
case TCon(f, args) => TCon(f, shiftAll(args, k))
case KInt(v) => KInt(v)

def shiftAll {r: Region} (ts: Terms, k: I64): Terms = ts match
def shiftAll(ts: Terms, k: I64): Terms = ts match
case TNil => TNil
case TCons(h, t) => TCons(shift(h, k), shiftAll(t, k))

def shiftGoal {r: Region} (g: Goal, k: I64): Goal = g match
def shiftGoal(g: Goal, k: I64): Goal = g match
case GTrue => GTrue
case GEq(a, b) => GEq(shift(a, k), shift(b, k))
case GEqAll(as, bs) => GEqAll(shiftAll(as, k), shiftAll(bs, k))
Expand All @@ -220,7 +220,7 @@ def shiftGoal {r: Region} (g: Goal, k: I64): Goal = g match
case GLabel(xs) => GLabel(shiftAll(xs, k))
case GCall(f, args) => GCall(f, shiftAll(args, k))

def shiftFd {r: Region} (c: Fd, k: I64): Fd = c match
def shiftFd(c: Fd, k: I64): Fd = c match
case FBin(op, x, y) => FBin(op, shift(x, k), shift(y, k))
case FPlus(x, y, z) => FPlus(shift(x, k), shift(y, k), shift(z, k))

Expand All @@ -229,19 +229,19 @@ def substSize(s: Subst): I64 = s match
case SBind(_, _, rest) => 1 + substSize(rest)

// the first n bindings of s: what unify added on top of the substitution it started from
def newest {r: Region} (s: Subst, n: I64): Subst = if n <= 0 then SNil else s match
def newest(s: Subst, n: I64): Subst = if n <= 0 then SNil else s match
case SNil => SNil
case SBind(v, t, rest) => SBind(v, t, newest(rest, n - 1))

// s extended with every binding of p
def unifyBinds {r: Region} (s: Subst, p: Subst): Unified = p match
def unifyBinds(s: Subst, p: Subst): Unified = p match
case SNil => Ok(s)
case SBind(v, t, rest) => unify(s, TVar(v), t) match
case Fail => Fail
case Ok(s2) => unifyBinds(s2, rest)

// a =/= b: drop it when they can never unify, Bad when they already are equal, else store what equality needs
def addNeq {r: Region} (st: State, a: Term, b: Term): Checked = st match
def addNeq(st: State, a: Term, b: Term): Checked = st match
case State(s, ns, ds, fs, nx) => unify(s, a, b) match
case Fail => Good(st)
case Ok(s2) =>
Expand All @@ -253,7 +253,7 @@ enum NeqsR:
case Kept(ns: Neqs)

// the store under substitution s: each disequality checked again (as addNeq)
def recheck {r: Region} (s: Subst, ns: Neqs): NeqsR = ns match
def recheck(s: Subst, ns: Neqs): NeqsR = ns match
case NeqNil => Kept(NeqNil)
case NeqCons(p, t) => recheck(s, t) match
case Violated => Violated
Expand All @@ -264,12 +264,12 @@ def recheck {r: Region} (s: Subst, ns: Neqs): NeqsR = ns match
if n == 0 then Violated else Kept(NeqCons(newest(s2, n), kept))

// the state after unify gave u: dead on Fail, else every constraint settled again
def post {r: Region} (st: State, u: Unified): Checked = u match
def post(st: State, u: Unified): Checked = u match
case Fail => Bad
case Ok(s2) => st match
case State(_, ns, ds, fs, nx) => settle(State(s2, ns, ds, fs, nx))

def settled {r: Region} (c: Checked): Checked = c match
def settled(c: Checked): Checked = c match
case Bad => Bad
case Good(st) => settle(st)

Expand All @@ -281,7 +281,7 @@ def member(v: I64, xs: Ints): Bool = xs match
def inDom(v: I64, d: Dom): Bool = d match
case Dom(h, t) => h == v || member(v, t)

def ints {r: Region} (d: Dom): Ints = d match
def ints(d: Dom): Ints = d match
case Dom(h, t) => ICons(h, t)

def lastOf(h: I64, t: Ints): I64 = t match
Expand All @@ -302,18 +302,18 @@ def domSize(d: Dom): I64 = d match
case Dom(_, t) => 1 + intsSize(t)

// a..b ascending
def range {r: Region} (a: I64, b: I64): Ints = if a > b then INil else ICons(a, range(a + 1, b))
def range(a: I64, b: I64): Ints = if a > b then INil else ICons(a, range(a + 1, b))

// the values of xs in a..b
def between {r: Region} (xs: Ints, a: I64, b: I64): Ints = xs match
def between(xs: Ints, a: I64, b: I64): Ints = xs match
case INil => INil
case ICons(h, t) => if h >= a && h <= b then ICons(h, between(t, a, b)) else between(t, a, b)

def kwithout {r: Region} (xs: Ints, v: I64): Ints = xs match
def kwithout(xs: Ints, v: I64): Ints = xs match
case INil => INil
case ICons(h, t) => if h == v then kwithout(t, v) else ICons(h, kwithout(t, v))

def meetInts {r: Region} (xs: Ints, d: Dom): Ints = xs match
def meetInts(xs: Ints, d: Dom): Ints = xs match
case INil => INil
case ICons(h, t) => if inDom(h, d) then ICons(h, meetInts(t, d)) else meetInts(t, d)

Expand All @@ -322,23 +322,23 @@ enum DomOf:
case Has(d: Dom)
case NotInt

def lookupDom {r: Region} (ds: Doms, n: I64): DomOf = ds match
def lookupDom(ds: Doms, n: I64): DomOf = ds match
case DNil => NoDom
case DCons(v, d, t) => if v == n then Has(d) else lookupDom(t, n)

def dropDom {r: Region} (ds: Doms, n: I64): Doms = ds match
def dropDom(ds: Doms, n: I64): Doms = ds match
case DNil => DNil
case DCons(v, d, t) => if v == n then dropDom(t, n) else DCons(v, d, dropDom(t, n))

// the domain of t: an integer has itself, a variable its entry (or none), a constructor is not an integer
def domOf {r: Region} (st: State, t: Term): DomOf = st match
def domOf(st: State, t: Term): DomOf = st match
case State(s, _, ds, _, _) => walk(s, t) match
case TVar(n) => lookupDom(ds, n)
case KInt(v) => Has(Dom(v, INil))
case TCon(_, _) => NotInt

// t takes a value of xs (ascending): Bad when none is left, bound when one is left
def setDom {r: Region} (st: State, t: Term, xs: Ints): Checked = st match
def setDom(st: State, t: Term, xs: Ints): Checked = st match
case State(s, ns, ds, fs, nx) => xs match
case INil => Bad
case ICons(h, rest) => walk(s, t) match
Expand All @@ -351,46 +351,46 @@ def setDom {r: Region} (st: State, t: Term, xs: Ints): Checked = st match
case ICons(_, _) => Good(State(s, ns, DCons(n, Dom(h, rest), dropDom(ds, n)), fs, nx))

// t takes a value of xs and of the domain it already has
def meet {r: Region} (st: State, t: Term, xs: Ints): Checked = domOf(st, t) match
def meet(st: State, t: Term, xs: Ints): Checked = domOf(st, t) match
case NoDom => setDom(st, t, xs)
case Has(d) => setDom(st, t, meetInts(xs, d))
case NotInt => Bad

// every domain entry added again under the current substitution: bound ones checked, unified ones met
def redom {r: Region} (st: State, ds: Doms): Checked = ds match
def redom(st: State, ds: Doms): Checked = ds match
case DNil => Good(st)
case DCons(n, d, t) => redom(st, t) match
case Bad => Bad
case Good(st2) => meet(st2, TVar(n), ints(d))

// ── propagators (bounds) ──
// x <= y - k: k = 0 for <=, 1 for <
def leAt {r: Region} (st: State, x: Term, y: Term, dx: Dom, dy: Dom, k: I64): Checked =
def leAt(st: State, x: Term, y: Term, dx: Dom, dy: Dom, k: I64): Checked =
setDom(st, x, between(ints(dx), lo(dx), hi(dy) - k)) match
case Bad => Bad
case Good(st2) => setDom(st2, y, between(ints(dy), lo(dx) + k, hi(dy)))

def neqAt {r: Region} (st: State, x: Term, y: Term, dx: Dom, dy: Dom): Checked =
def neqAt(st: State, x: Term, y: Term, dx: Dom, dy: Dom): Checked =
if domSize(dx) == 1 && domSize(dy) == 1 then (if lo(dx) == lo(dy) then Bad else Good(st))
else if domSize(dx) == 1 then setDom(st, y, kwithout(ints(dy), lo(dx)))
else if domSize(dy) == 1 then setDom(st, x, kwithout(ints(dx), lo(dy)))
else Good(st)

def binAt {r: Region} (st: State, op: Bin, x: Term, y: Term, dx: Dom, dy: Dom): Checked = op match
def binAt(st: State, op: Bin, x: Term, y: Term, dx: Dom, dy: Dom): Checked = op match
case BLe => leAt(st, x, y, dx, dy, 0)
case BLt => leAt(st, x, y, dx, dy, 1)
case BNeq => neqAt(st, x, y, dx, dy)

// x + y = z
def plusAt {r: Region} (st: State, x: Term, y: Term, z: Term, dx: Dom, dy: Dom, dz: Dom): Checked =
def plusAt(st: State, x: Term, y: Term, z: Term, dx: Dom, dy: Dom, dz: Dom): Checked =
setDom(st, z, between(ints(dz), lo(dx) + lo(dy), hi(dx) + hi(dy))) match
case Bad => Bad
case Good(st1) => setDom(st1, x, between(ints(dx), lo(dz) - hi(dy), hi(dz) - lo(dy))) match
case Bad => Bad
case Good(st2) => setDom(st2, y, between(ints(dy), lo(dz) - hi(dx), hi(dz) - lo(dx)))

// a propagator runs once every variable it reads has a domain
def fire {r: Region} (st: State, c: Fd): Checked = c match
def fire(st: State, c: Fd): Checked = c match
case FBin(op, x, y) => domOf(st, x) match
case NoDom => Good(st)
case NotInt => Bad
Expand All @@ -409,7 +409,7 @@ def fire {r: Region} (st: State, c: Fd): Checked = c match
case NotInt => Bad
case Has(dz) => plusAt(st, x, y, z, dx, dy, dz)

def fireAll {r: Region} (st: State, fs: Fds): Checked = fs match
def fireAll(st: State, fs: Fds): Checked = fs match
case FNil => Good(st)
case FCons(c, t) => fire(st, c) match
case Bad => Bad
Expand All @@ -420,15 +420,15 @@ def domTotal(ds: Doms): I64 = ds match
case DCons(_, d, t) => 1 + domSize(d) + domTotal(t)

// one round: disequalities, domains, propagators
def round {r: Region} (st: State): Checked = st match
def round(st: State): Checked = st match
case State(s, ns, ds, fs, nx) => recheck(s, ns) match
case Violated => Bad
case Kept(ns2) => redom(State(s, ns2, DNil, fs, nx), ds) match
case Bad => Bad
case Good(st2) => fireAll(st2, fs)

// rounds until nothing changes. Ends: a changing round binds a variable or removes a domain value or entry
def settle {r: Region} (st: State): Checked = round(st) match
def settle(st: State): Checked = round(st) match
case Bad => Bad
case Good(st2) => if same(st, st2) then Good(st2) else settle(st2)

Expand All @@ -442,7 +442,7 @@ enum Pick:
case Pick(t: Term, d: Dom)

// the variable of xs with the smallest domain of 2 or more values (first-fail)
def pickVar {r: Region} (st: State, xs: Terms, best: Pick): Pick = xs match
def pickVar(st: State, xs: Terms, best: Pick): Pick = xs match
case TNil => best
case TCons(h, t) => domOf(st, h) match
case Has(d) => if domSize(d) > 1 && smaller(d, best) then pickVar(st, t, Pick(h, d)) else pickVar(st, t, best)
Expand All @@ -453,28 +453,28 @@ def smaller(d: Dom, best: Pick): Bool = best match
case NoPick => true
case Pick(_, e) => domSize(d) < domSize(e)

def labelBranches {r: Region} (st: State, t: Term, vs: Ints, after: Goals): Queue = vs match
def labelBranches(st: State, t: Term, vs: Ints, after: Goals): Queue = vs match
case INil => QNil
case ICons(v, more) => QCons(Branch(st, GCons(GEq(t, KInt(v)), after)), labelBranches(st, t, more, after))

// every two of xs differ
def distinct {r: Region} (xs: Terms): Goal = xs match
def distinct(xs: Terms): Goal = xs match
case TNil => GTrue
case TCons(h, t) => GConj(neqEach(h, t), distinct(t))

def neqEach {r: Region} (x: Term, ys: Terms): Goal = ys match
def neqEach(x: Term, ys: Terms): Goal = ys match
case TNil => GTrue
case TCons(y, t) => GConj(GFd(FBin(BNeq, x, y)), neqEach(x, t))

def onward {r: Region} (c: Checked, rest: Goals): Step = c match
def onward(c: Checked, rest: Goals): Step = c match
case Bad => Dead
case Good(st) => More(Branch(st, rest))

// one branch per rule of rel, in rule order; each pick costs 1 fuel (vic B1)
enum Picked:
case Picked(bs: Queue, cost: I64)

def picks {r: Region} (rules: Rules, rel: Str, args: Terms, st: State, nx: I64, rest: Goals): Picked = rules match
def picks(rules: Rules, rel: Str, args: Terms, st: State, nx: I64, rest: Goals): Picked = rules match
case RNil => Picked(QNil, 0)
case RCons(h, t) => picks(t, rel, args, st, nx, rest) match
case Picked(bs, cost) => h match
Expand All @@ -484,7 +484,7 @@ def picks {r: Region} (rules: Rules, rel: Str, args: Terms, st: State, nx: I64,
Picked(QCons(b, bs), cost + 1)
else Picked(bs, cost)

def kstep {r: Region} (rules: Rules, b: Branch): Step = b match
def kstep(rules: Rules, b: Branch): Step = b match
case Branch(st, gs) => gs match
case GNil => Done(st)
case GCons(g, rest) => st match
Expand All @@ -503,17 +503,17 @@ def kstep {r: Region} (rules: Rules, b: Branch): Step = b match
case GCall(f, args) => picks(rules, f, args, st, nx, rest) match
case Picked(bs, cost) => Split(bs, cost)

def enqueue {r: Region} (q: Queue, bs: Queue): Queue = q match
def enqueue(q: Queue, bs: Queue): Queue = q match
case QNil => bs
case QCons(h, t) => QCons(h, enqueue(t, bs))

def revAnswers {r: Region} (xs: Answers, acc: Answers): Answers = xs match
def revAnswers(xs: Answers, acc: Answers): Answers = xs match
case ANil => acc
case ACons(h, t) => revAnswers(t, ACons(h, acc))

// prove g (variables 0..nvars-1) over rules. Deterministic: a branch runs until a choice, the choice's branches
// go to the back of the queue (fair, so complete). Every infinite path makes choices, so fuel bounds the run (§17).
def solve {r: Region} (rules: Rules, g: Goal, nvars: I64, fuel: I64, limit: I64): Run =
def solve(rules: Rules, g: Goal, nvars: I64, fuel: I64, limit: I64): Run =
var q = QCons(Branch(State(SNil, NeqNil, DNil, FNil, nvars), GCons(g, GNil)), QNil)
var left = fuel
var found = ANil
Expand Down Expand Up @@ -548,8 +548,8 @@ def solve {r: Region} (rules: Rules, g: Goal, nvars: I64, fuel: I64, limit: I64)
def substOf(st: State): Subst = st match
case State(s, _, _, _, _) => s

def withNext {r: Region} (st: State, nx: I64): State = st match
def withNext(st: State, nx: I64): State = st match
case State(s, ns, ds, fs, _) => State(s, ns, ds, fs, nx)

def withFds {r: Region} (st: State, fs: Fds): State = st match
def withFds(st: State, fs: Fds): State = st match
case State(s, ns, ds, _, nx) => State(s, ns, ds, fs, nx)
Loading
Loading