gdp-wired 0.50.1 → 0.50.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/core/cli.d.ts +12 -1
- package/dist/core/cli.js +64 -26
- package/dist/core/discover.d.ts +6 -1
- package/dist/core/discover.js +54 -7
- package/dist/core/dsl-deadline.d.ts +3 -0
- package/dist/core/dsl-deadline.js +26 -2
- package/dist/core/dsl.d.ts +3 -1
- package/dist/core/dsl.js +97 -32
- package/dist/core/effect.d.ts +12 -0
- package/dist/core/effect.js +26 -9
- package/dist/core/ex/harness.exs +171 -5
- package/dist/core/extract-c.js +64 -7
- package/dist/core/extract-check.d.ts +5 -1
- package/dist/core/extract-check.js +28 -10
- package/dist/core/extract-go-check.js +130 -3
- package/dist/core/extract-go.js +8 -2
- package/dist/core/extract-jvm.js +2 -2
- package/dist/core/extract-rs.js +51 -5
- package/dist/core/extract-step.js +49 -6
- package/dist/core/extract-swift.js +47 -3
- package/dist/core/extract.d.ts +9 -0
- package/dist/core/infer.d.ts +8 -0
- package/dist/core/infer.js +133 -16
- package/dist/core/jvm/Harness.java +135 -3
- package/dist/core/jvm/Rt.java +66 -4
- package/dist/core/laws.d.ts +4 -0
- package/dist/core/laws.js +59 -10
- package/dist/core/native-law.d.ts +3 -1
- package/dist/core/native-law.js +126 -14
- package/dist/core/native-observations.d.ts +20 -0
- package/dist/core/native-observations.js +41 -0
- package/dist/core/native-wire.d.ts +2 -0
- package/dist/core/native-wire.js +99 -0
- package/dist/core/py/harness.py +53 -2
- package/dist/core/rb/harness.rb +178 -9
- package/dist/core/search-termination.d.ts +35 -0
- package/dist/core/search-termination.js +13 -0
- package/dist/core/search-watchdog.d.ts +8 -0
- package/dist/core/search-watchdog.js +34 -0
- package/dist/core/source-check.d.ts +5 -0
- package/dist/core/source-check.js +21 -0
- package/dist/core/swift/Harness.swift +65 -4
- package/dist/core/swift/Main.swift +27 -4
- package/dist/core/wall.d.ts +3 -1
- package/dist/core/wall.js +7 -4
- package/dist/core/work-refusal.d.ts +3 -0
- package/dist/core/work-refusal.js +8 -0
- package/dist/kernel/prove.d.ts +2 -0
- package/dist/kernel/prove.js +13 -5
- package/dist/kernel/reader.js +5 -1
- package/dist/lean-kernel/axiom-audit.js +5 -2
- package/dist/lean-kernel/emit.d.ts +1 -0
- package/dist/lean-kernel/emit.js +22 -4
- package/package.json +1 -1
package/dist/core/dsl.js
CHANGED
|
@@ -21,19 +21,21 @@ import { binder, copyVariable, encodeBinders, generatedVariable, guardReserved,
|
|
|
21
21
|
import { candidates, cone, searchCounted } from "./counts.js";
|
|
22
22
|
import { proveCandidateSeeds } from "./candidate-seeds.js";
|
|
23
23
|
import { proveCountedCandidateSeeds } from "./counted-candidate-seeds.js";
|
|
24
|
-
import { inheritSource, translationVerdict } from "./source-check.js";
|
|
24
|
+
import { bindSourceMachine, inheritSource, translationVerdict } from "./source-check.js";
|
|
25
25
|
import { env as knobs } from "./solver.js";
|
|
26
26
|
import { argsKey, bindDirect, directOf, machineTerms, part, tuple, carriedView, compileFunction, divName, divisor, divisorsIn, evalC, evalE, fnCall, hasFinite, interpret, isArithmetic, loadJs, modName, numbersOf, pairedArgs, seeded, sourceFreeFn, withArithmetic } from "./machine.js";
|
|
27
27
|
import { allowed, keysOf, requestEnv, watch, watchWork } from "./watch.js";
|
|
28
28
|
import { ghostOf, proveSome, skolemOf, someHolds, substVars, substVarsE, witnessSupport } from "./exists.js";
|
|
29
29
|
import { check, mapArities, checkRecur, foldObligations, ghostedMachine, loopObligations, someOfGhostsOk, substC, sumMachine } from "../kernel/kernel.js";
|
|
30
30
|
import { certLoop, clock, counted, factsFor, fullRate, grow, pastWall, proveLaw, proveLaws, proveLiveness, proveSetFast, proveStepLaws, usualProof, withModelRounds, withWall } from "../kernel/prove.js";
|
|
31
|
-
import { WallExpired, checkWall, checkWorkEndpoint, searchWall, withBudget, BudgetExpired } from "./wall.js";
|
|
31
|
+
import { WallExpired, checkWall, checkBudget, checkWorkEndpoint, searchWall, withBudget, BudgetExpired } from "./wall.js";
|
|
32
32
|
import { completeWorkProfile } from "./work-profile.js";
|
|
33
33
|
import { workLedger } from "./work-admission.js";
|
|
34
|
-
import { withStageAllowance, withStageDeadline, withStageWall } from "./dsl-deadline.js";
|
|
34
|
+
import { withStageAllowance, withStageDeadline, withStageWall, withStageWork, remainingStageWork } from "./dsl-deadline.js";
|
|
35
|
+
import { completedSearch, searchContext, stoppedSearch } from "./search-termination.js";
|
|
36
|
+
import { WatchdogExpired, checkWatchdog, withSearchWatchdog } from "./search-watchdog.js";
|
|
35
37
|
import { withPublicSearchPolicy } from "./search-policy.js";
|
|
36
|
-
import { workRefusalWhy } from "./work-refusal.js";
|
|
38
|
+
import { captureWorkContext, workContext, workRefusalWhy } from "./work-refusal.js";
|
|
37
39
|
import { workDivide, workFromNumber, workToNumber } from "./work-amount.js";
|
|
38
40
|
import { certCyclic, certOn, crossCyclic, cyclicOn, foldNodes, proveCyclic, quietly, recurNodes, translatedProof, uncovered } from "./cyclic.js";
|
|
39
41
|
import { discover } from "./discover.js";
|
|
@@ -2550,7 +2552,9 @@ export class Rule {
|
|
|
2550
2552
|
const sums = sumsIn(machineTerms(m), s.sumsRead ?? []);
|
|
2551
2553
|
const out = withArithmetic(sums ? { ...m, state: { ...m.state, ...Object.fromEntries(Object.keys(sums).map(n => [n, { op: 'const', value: 0 }])) }, sums } : m, [], s.arith ?? []);
|
|
2552
2554
|
const encoded = encodeBinders(out, this.names().map(([n]) => n));
|
|
2553
|
-
|
|
2555
|
+
const machine = s.ghost ? s.ghost(encoded) : encoded;
|
|
2556
|
+
bindSourceMachine(this, machine);
|
|
2557
|
+
return machine;
|
|
2554
2558
|
}
|
|
2555
2559
|
// The same rule with ghosts its machine gains (the search's, over a law with "there exists" made plain: skolemOf).
|
|
2556
2560
|
withGhosts(ghost) { return this.with({ ghost }); }
|
|
@@ -3244,7 +3248,7 @@ function refuteByRuns(r, laws, stepLaws = [], budget = refuteBudget) {
|
|
|
3244
3248
|
return at(params, 0);
|
|
3245
3249
|
}
|
|
3246
3250
|
catch (e) {
|
|
3247
|
-
if (e instanceof WallExpired)
|
|
3251
|
+
if (e instanceof WallExpired || e instanceof WatchdogExpired)
|
|
3248
3252
|
throw e;
|
|
3249
3253
|
if (e instanceof BudgetExpired)
|
|
3250
3254
|
checkWorkEndpoint(e.phase);
|
|
@@ -3333,7 +3337,7 @@ function refuteByRuns(r, laws, stepLaws = [], budget = refuteBudget) {
|
|
|
3333
3337
|
}
|
|
3334
3338
|
}
|
|
3335
3339
|
catch (e) {
|
|
3336
|
-
if (e instanceof WallExpired)
|
|
3340
|
+
if (e instanceof WallExpired || e instanceof WatchdogExpired)
|
|
3337
3341
|
throw e;
|
|
3338
3342
|
if (e instanceof BudgetExpired)
|
|
3339
3343
|
checkWorkEndpoint(e.phase);
|
|
@@ -3357,13 +3361,19 @@ function workPlan(opts) {
|
|
|
3357
3361
|
if (work === undefined || opts.search === false)
|
|
3358
3362
|
return { work, opts };
|
|
3359
3363
|
const search = typeof opts.search === 'object' ? { ...opts.search } : {};
|
|
3364
|
+
// An explicit public work grant remains a work grant through every plain
|
|
3365
|
+
// search phase. It must not be re-expressed as a counted-millisecond
|
|
3366
|
+
// deadline: the enclosing ledger endpoint is its sole semantic authority.
|
|
3367
|
+
if (explicit !== undefined) {
|
|
3368
|
+
delete search.ms;
|
|
3369
|
+
return { work, opts: { ...opts, search } };
|
|
3370
|
+
}
|
|
3360
3371
|
delete search.work;
|
|
3361
|
-
//
|
|
3362
|
-
// original amount. This display must never become the root's authority.
|
|
3372
|
+
// Legacy ms compatibility keeps its historical counted-clock scheduling.
|
|
3363
3373
|
search.ms = typeof work === 'number' ? work / fullRate : workToNumber(workDivide(work, workFromNumber(fullRate)));
|
|
3364
3374
|
return { work, opts: { ...opts, search } };
|
|
3365
3375
|
}
|
|
3366
|
-
function workVerdict(opts,
|
|
3376
|
+
function workVerdict(opts, run) {
|
|
3367
3377
|
const plan = workPlan(opts);
|
|
3368
3378
|
// An implicit child inherits the owner's hard allowance, not a fresh default.
|
|
3369
3379
|
// Keep workPlan's validation and scheduling hint; let the owner's refusal
|
|
@@ -3373,37 +3383,56 @@ function workVerdict(opts, supported, run) {
|
|
|
3373
3383
|
return run(plan.opts);
|
|
3374
3384
|
if (plan.work === undefined)
|
|
3375
3385
|
return run(plan.opts);
|
|
3376
|
-
if (!completeWorkProfile.enabled && !supported)
|
|
3377
|
-
return { proven: false, checked: false, stuck: 'unknown', why: 'work mode: plain-path migration pending' };
|
|
3378
3386
|
try {
|
|
3379
3387
|
return withBudget(plan.work, () => run(plan.opts));
|
|
3380
3388
|
}
|
|
3381
3389
|
catch (error) {
|
|
3382
3390
|
if (!(error instanceof BudgetExpired))
|
|
3383
3391
|
throw error;
|
|
3384
|
-
|
|
3392
|
+
// Once this child's scope has unwound, an exhausted ancestor still owns
|
|
3393
|
+
// its refusal. Do not turn the parent's cancellation into a child result.
|
|
3394
|
+
checkBudget(error.phase); // work-only: this call's own watchdog is still installed
|
|
3395
|
+
return { proven: false, checked: false, stuck: 'unknown', why: workRefusalWhy(error), stopped: true, termination: { kind: 'work-exhausted', phase: error.phase, scope: 'search' }, ...(workContext(error) ? { context: workContext(error) } : {}) };
|
|
3396
|
+
}
|
|
3397
|
+
}
|
|
3398
|
+
function watchdogVerdict(ms, run) {
|
|
3399
|
+
let result;
|
|
3400
|
+
try {
|
|
3401
|
+
return withSearchWatchdog(ms, () => result = run());
|
|
3402
|
+
}
|
|
3403
|
+
catch (e) {
|
|
3404
|
+
if (!(e instanceof WatchdogExpired))
|
|
3405
|
+
throw e;
|
|
3406
|
+
checkWatchdog(e.termination.phase); // an expired ancestor still owns cancellation
|
|
3407
|
+
checkBudget(e.termination.phase); // an owned clock must not hide exhausted ancestor work
|
|
3408
|
+
if (completedSearch(result) || result?.stopped && result.termination)
|
|
3409
|
+
return result;
|
|
3410
|
+
return stoppedSearch({ ...result, proven: false, checked: false, stuck: 'unknown' }, e.termination);
|
|
3385
3411
|
}
|
|
3386
3412
|
}
|
|
3387
3413
|
export function proveRule(r, laws, opts = {}) {
|
|
3388
|
-
return withModelRounds(opts.modelRounds, () => withPublicRule(outermost => {
|
|
3414
|
+
return watchdogVerdict(opts.watchdogMs, () => withModelRounds(opts.modelRounds, () => withPublicRule(outermost => {
|
|
3389
3415
|
const captureMachine = r.machine, hasCounts = !!Object.keys(captureMachine.counts ?? {}).length;
|
|
3390
|
-
const result = workVerdict(opts,
|
|
3416
|
+
const result = workVerdict(opts, plan => withPublicSearchPolicy(hasCounts, () => proveRuleWall(r, laws, plan)));
|
|
3391
3417
|
return outermost ? publishProof(result, captureMachine, laws) : result;
|
|
3392
|
-
}));
|
|
3418
|
+
})));
|
|
3393
3419
|
}
|
|
3394
3420
|
function proveRuleWall(r, laws, opts) {
|
|
3395
3421
|
const ms = typeof opts.search === 'object' ? opts.search.ms : undefined;
|
|
3396
3422
|
const run = () => {
|
|
3423
|
+
let result;
|
|
3397
3424
|
try {
|
|
3398
3425
|
checkWall('rule preflight');
|
|
3399
|
-
const out = proveRuleIn(r, laws, opts);
|
|
3400
|
-
|
|
3426
|
+
const out = result = proveRuleIn(r, laws, opts);
|
|
3427
|
+
if (!completedSearch(out) && !(out.stopped && out.termination))
|
|
3428
|
+
checkWall('rule result');
|
|
3401
3429
|
return out;
|
|
3402
3430
|
}
|
|
3403
3431
|
catch (e) {
|
|
3404
|
-
if (!(e instanceof WallExpired))
|
|
3432
|
+
if (!(e instanceof WallExpired) && !(e instanceof WatchdogExpired))
|
|
3405
3433
|
throw e;
|
|
3406
|
-
|
|
3434
|
+
const out = { ...result, proven: false, checked: false, stuck: 'unknown', why: result?.why ?? e.message };
|
|
3435
|
+
return e.termination ? stoppedSearch(out, e.termination) : out;
|
|
3407
3436
|
}
|
|
3408
3437
|
};
|
|
3409
3438
|
// One absolute wall for setup, sampling, generation and proof, not a fresh
|
|
@@ -3460,7 +3489,11 @@ function proveRuleIn(r, laws, opts) {
|
|
|
3460
3489
|
// Quantified witness preflight needs a counted allocation: a slow attempt must
|
|
3461
3490
|
// not seed different memoized decisions. Other laws keep their existing budget;
|
|
3462
3491
|
// inference's query rate is not calibrated for their whole-set preflight.
|
|
3463
|
-
const
|
|
3492
|
+
const explicitWork = typeof opts.search === 'object' ? opts.search.work : undefined;
|
|
3493
|
+
// Whole-set preflight is only one search strategy. In work mode reserve a
|
|
3494
|
+
// deterministic eighth for it, rather than starving inference at the root.
|
|
3495
|
+
const preflight = explicitWork === undefined ? undefined : remainingStageWork(explicitWork, 8);
|
|
3496
|
+
const out = preflight !== undefined ? withStageWork(preflight, given, () => undefined) : ms === undefined ? given() : withStageWall(countedSearchNow() + ms / share, () => share === 1 ? given() : counted(inferRates.work, () => withStageDeadline(clock() + ms / share, given, () => undefined)), () => undefined);
|
|
3464
3497
|
const v = out ? { ...verdictOf(out), ...(out.witnesses && out.proven ? { witnesses: out.witnesses } : {}) }
|
|
3465
3498
|
: { proven: false, checked: false, stuck: 'unknown' };
|
|
3466
3499
|
if (out)
|
|
@@ -3468,7 +3501,7 @@ function proveRuleIn(r, laws, opts) {
|
|
|
3468
3501
|
return searched(r, laws, [], v, opts);
|
|
3469
3502
|
}
|
|
3470
3503
|
export function proveSteps(r, laws, stepLaws, opts = {}) {
|
|
3471
|
-
return
|
|
3504
|
+
return watchdogVerdict(opts.watchdogMs, () => workVerdict(opts, plan => proveStepsWall(r, laws, stepLaws, plan)));
|
|
3472
3505
|
}
|
|
3473
3506
|
function proveStepsWall(r, laws, stepLaws, opts) {
|
|
3474
3507
|
const ms = typeof opts.search === 'object' ? opts.search.ms : undefined;
|
|
@@ -3479,9 +3512,10 @@ function proveStepsWall(r, laws, stepLaws, opts) {
|
|
|
3479
3512
|
return proveStepsIn(r, laws, stepLaws, opts);
|
|
3480
3513
|
}
|
|
3481
3514
|
catch (e) {
|
|
3482
|
-
if (!(e instanceof WallExpired))
|
|
3515
|
+
if (!(e instanceof WallExpired) && !(e instanceof WatchdogExpired))
|
|
3483
3516
|
throw e;
|
|
3484
|
-
|
|
3517
|
+
const out = { proven: false, checked: false, stuck: 'unknown', why: e.message };
|
|
3518
|
+
return e.termination ? stoppedSearch(out, e.termination) : out;
|
|
3485
3519
|
}
|
|
3486
3520
|
};
|
|
3487
3521
|
const verdict = opts.from !== 'any' || ms === undefined ? run()
|
|
@@ -3530,13 +3564,30 @@ function stepGhosts(m, stepLaws) {
|
|
|
3530
3564
|
// recurrences or carried lists (the rule made again with them), then invariants over what it then is. The verdict is the
|
|
3531
3565
|
// prover's on the laws with what was found; when nothing is found, the prover's own (with why).
|
|
3532
3566
|
function searched(r, laws, stepLaws, v, opts) {
|
|
3567
|
+
try {
|
|
3568
|
+
return searchedIn(r, laws, stepLaws, v, opts);
|
|
3569
|
+
}
|
|
3570
|
+
catch (e) {
|
|
3571
|
+
// Keep the initial SAT/goal/model before the outer catch can replace it.
|
|
3572
|
+
// The exception carries provenance stamped before the clock scope unwinds.
|
|
3573
|
+
if (e instanceof BudgetExpired) {
|
|
3574
|
+
captureWorkContext(e, searchContext(v));
|
|
3575
|
+
throw e;
|
|
3576
|
+
}
|
|
3577
|
+
if ((!(e instanceof WallExpired) && !(e instanceof WatchdogExpired)) || !e.termination)
|
|
3578
|
+
throw e;
|
|
3579
|
+
return stoppedSearch(v, e.termination);
|
|
3580
|
+
}
|
|
3581
|
+
}
|
|
3582
|
+
function searchedIn(r, laws, stepLaws, v, opts) {
|
|
3533
3583
|
const any = opts.from === 'any';
|
|
3534
3584
|
if ((v.proven && v.checked) || opts.search === false || v.run || v.stuck === 'sat' && (any || !/@step|@\d+ steps/.test(v.goal ?? '')))
|
|
3535
3585
|
return v;
|
|
3536
3586
|
// The deeper runs; then runs over small values (keys that meet, small params), whose requests meet their
|
|
3537
3587
|
// assumption's equalities and clocks, or near the counterexample the verdict ends at.
|
|
3538
3588
|
// (the three within the search's budget again, on the wall: past it they stop, and the search starts as it would)
|
|
3539
|
-
const
|
|
3589
|
+
const explicitWork = typeof opts.search === 'object' ? opts.search.work : undefined;
|
|
3590
|
+
const ms = explicitWork === undefined ? (typeof opts.search === 'object' && opts.search.ms !== undefined ? opts.search.ms : 10_000) : Infinity, runsUntil = explicitWork === undefined ? countedSearchNow() + ms : Infinity;
|
|
3540
3591
|
for (const b of [deeperBudget, { ...deeperBudget, bound: 1, seed: 31, states: false }, { ...deeperBudget, trials: 1200, seed: 41, states: false, mixed: true, ...(v.witness ? { near: v.witness } : {}) }]) {
|
|
3541
3592
|
const deeper = refuteByRuns(r, laws, stepLaws, { ...b, until: runsUntil });
|
|
3542
3593
|
if (deeper?.run)
|
|
@@ -3551,9 +3602,16 @@ function searched(r, laws, stepLaws, v, opts) {
|
|
|
3551
3602
|
if (deeper?.run)
|
|
3552
3603
|
return refutedVerdict(deeper);
|
|
3553
3604
|
}
|
|
3554
|
-
const until = clock() + (typeof opts.search === 'object' && opts.search.ms !== undefined ? opts.search.ms : 10_000);
|
|
3555
|
-
//
|
|
3556
|
-
|
|
3605
|
+
const until = explicitWork === undefined ? clock() + (typeof opts.search === 'object' && opts.search.ms !== undefined ? opts.search.ms : 10_000) : Infinity;
|
|
3606
|
+
// Work uses only its ledger; legacy stages retain their captured real/count endpoint.
|
|
3607
|
+
if (explicitWork !== undefined)
|
|
3608
|
+
return searchedUntil(r, laws, stepLaws, v, opts, until);
|
|
3609
|
+
return withStageWall(countedSearchNow() + Math.max(0, until - clock()), () => {
|
|
3610
|
+
const out = searchedUntil(r, laws, stepLaws, v, opts, until);
|
|
3611
|
+
if (!(out.proven && out.checked) && !out.run)
|
|
3612
|
+
checkWall('search result');
|
|
3613
|
+
return out;
|
|
3614
|
+
}, () => stoppedSearch(v, { kind: 'work-exhausted', phase: 'search stage', scope: 'stage' }));
|
|
3557
3615
|
}
|
|
3558
3616
|
// Two machines alike up to the names of the keys their quantified flags and writes at every key bind (the rule builds
|
|
3559
3617
|
// fresh ones each time it is read): each bound name by its place of first binding.
|
|
@@ -3566,7 +3624,7 @@ const sameMachine = (a, b) => {
|
|
|
3566
3624
|
return canon(a) === canon(b);
|
|
3567
3625
|
};
|
|
3568
3626
|
function searchedUntil(r, laws, stepLaws, v, opts, until) {
|
|
3569
|
-
const any = opts.from === 'any';
|
|
3627
|
+
const any = opts.from === 'any', explicitWork = typeof opts.search === 'object' ? opts.search.work : undefined;
|
|
3570
3628
|
// Bounded runs (core/bmc.ts): a law broken k steps from the start, a model of the kernel's k-step goal, the run taken
|
|
3571
3629
|
// by the interpreter. The shallow depths first, within a fortieth of the search's budget (at most 250 ms, counted in
|
|
3572
3630
|
// the cost model's work: kernel/prove.ts's `counted` with `full`); the deeper ones only where the invariant search
|
|
@@ -3725,13 +3783,19 @@ function searchedUntil(r, laws, stepLaws, v, opts, until) {
|
|
|
3725
3783
|
keep = keep.filter(c => !drop.includes(c));
|
|
3726
3784
|
}
|
|
3727
3785
|
}
|
|
3728
|
-
|
|
3786
|
+
let searchStop;
|
|
3787
|
+
const stageStopped = () => { searchStop = { kind: 'work-exhausted', phase: 'invariant search', scope: 'stage' }; return undefined; };
|
|
3788
|
+
const inferenceBudget = explicitWork === undefined ? { ms } : { work: explicitWork };
|
|
3789
|
+
const infer = () => inferLaws(target, [...fresh.map(f => f.law), ...sought], stepLaws, { ...inferenceBudget, ...((rule === r && !fresh.length) || sk ? { first: v } : {}), ...(sk ? { join: 0 } : {}) });
|
|
3790
|
+
const inf = explicitWork === undefined ? withStageWall(tailAt(), infer, stageStopped) : infer();
|
|
3729
3791
|
if (!(inf?.proven && inf.checked)) {
|
|
3730
3792
|
// The sum of each map the step writes (withMapSums): the search again over the rule with them, within half of what
|
|
3731
3793
|
// is left (at most 20 s), where a law of cells against a total needs one (no ERC20 balance past the supply).
|
|
3732
3794
|
const summed = target.withMapSums();
|
|
3733
3795
|
if (summed && until - clock() - tail > 1000) {
|
|
3734
|
-
const
|
|
3796
|
+
const infer2 = () => inferLaws(summed, [...fresh.map(f => f.law), ...sought], stepLaws, explicitWork === undefined ? { ms: Math.min(20_000, (until - clock() - tail) / 2) } : { work: explicitWork });
|
|
3797
|
+
const inf2 = explicitWork === undefined ? withStageWall(tailAt(), infer2, stageStopped) : infer2();
|
|
3798
|
+
searchStop = inf2?.termination ?? searchStop;
|
|
3735
3799
|
if (inf2?.proven && inf2.checked) {
|
|
3736
3800
|
const { found, stats: _s2, ...verdict } = inf2;
|
|
3737
3801
|
void _s2;
|
|
@@ -3744,7 +3808,8 @@ function searchedUntil(r, laws, stepLaws, v, opts, until) {
|
|
|
3744
3808
|
const deep = bounded(Math.max(0, Math.min(until - clock(), ghosted && !laws.length ? 10_000 : Infinity)), Math.max(1, reached), true);
|
|
3745
3809
|
if (deep)
|
|
3746
3810
|
return refutedBy(deep);
|
|
3747
|
-
|
|
3811
|
+
const termination = inf?.termination ?? searchStop;
|
|
3812
|
+
return termination ? stoppedSearch(v, termination) : { ...v, why: [v.why, inf ? `the search found nothing that proves it in ${Math.round(inf.stats.ms)} ms` : 'the search reached its stage deadline'].filter(Boolean).join('; ') };
|
|
3748
3813
|
}
|
|
3749
3814
|
const { found, stats: _stats, ...verdict } = inf;
|
|
3750
3815
|
void _stats;
|
package/dist/core/effect.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as dsl from './dsl.js';
|
|
1
2
|
import type { Rule } from './dsl.js';
|
|
2
3
|
import { type Law, type Rand } from './machine.js';
|
|
3
4
|
import { type Source } from './extract-read.js';
|
|
@@ -41,12 +42,23 @@ export type Primitive = Prim & {
|
|
|
41
42
|
laws: Law[];
|
|
42
43
|
lemmas?: Law[];
|
|
43
44
|
};
|
|
45
|
+
export type LemmaAttempt = {
|
|
46
|
+
name: string;
|
|
47
|
+
verdict?: dsl.Verdict;
|
|
48
|
+
failure?: {
|
|
49
|
+
name: string;
|
|
50
|
+
message: string;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
44
53
|
export declare function withLemmas(x: Extraction, opts?: {
|
|
45
54
|
ms?: number;
|
|
55
|
+
work?: number;
|
|
56
|
+
watchdogMs?: number;
|
|
46
57
|
}): {
|
|
47
58
|
x: Extraction;
|
|
48
59
|
proven: string[];
|
|
49
60
|
unproven: string[];
|
|
61
|
+
attempts: LemmaAttempt[];
|
|
50
62
|
};
|
|
51
63
|
export { fiberRun } from './fiber-run.js';
|
|
52
64
|
export declare function fromSchedule(source: string, opts: {
|
package/dist/core/effect.js
CHANGED
|
@@ -18,6 +18,8 @@ import ts from 'typescript';
|
|
|
18
18
|
import * as dsl from "./dsl.js";
|
|
19
19
|
import { interpret, lcg, seeded } from "./machine.js";
|
|
20
20
|
import { allowed } from "./watch.js";
|
|
21
|
+
import { BudgetExpired, checkWorkEndpoint } from "./wall.js";
|
|
22
|
+
import { WatchdogExpired } from "./search-watchdog.js";
|
|
21
23
|
import * as I from "./extract-ir.js";
|
|
22
24
|
import { Refusal, bare as strip, beside, impure, memberName, normalize, placed, refuse, shapeOf, sourceOf, text, where } from "./extract-read.js";
|
|
23
25
|
import { readFactory, setupOf } from "./extract-shapes.js";
|
|
@@ -671,22 +673,37 @@ function modelled(code, file, entry, E, opts) {
|
|
|
671
673
|
const lemmas = prims.flatMap(p => p.lemmas ?? []);
|
|
672
674
|
return { ...x, invariants: [...x.invariants ?? [], ...prims.flatMap(p => p.invariants)], notes: [...w.notes, ...x.notes], primitives: prims, ...(lemmas.length ? { lemmas } : {}) };
|
|
673
675
|
}
|
|
674
|
-
//
|
|
675
|
-
//
|
|
676
|
-
//
|
|
677
|
-
// are left out (a lemma is no claim: the laws stated are).
|
|
676
|
+
// work and watchdogMs are PER LEMMA, like the legacy ms option, not a
|
|
677
|
+
// whole-call grant. Every child is clamped to any enclosing work/watchdog
|
|
678
|
+
// scope; callers needing a shared total can wrap this synchronous helper.
|
|
678
679
|
export function withLemmas(x, opts = {}) {
|
|
680
|
+
if (opts.work !== undefined && opts.ms !== undefined)
|
|
681
|
+
throw new RangeError('choose a work budget or a legacy ms budget, not both');
|
|
679
682
|
const lemmas = x.lemmas ?? [], inv = [...x.invariants ?? []], proven = [], unproven = [];
|
|
683
|
+
const attempts = [];
|
|
680
684
|
if (!x.rule)
|
|
681
|
-
return { x, proven, unproven };
|
|
685
|
+
return { x, proven, unproven, attempts };
|
|
682
686
|
for (const l of lemmas) {
|
|
683
|
-
let v;
|
|
687
|
+
let v, failure;
|
|
688
|
+
const proofOptions = { search: opts.work === undefined ? { ms: opts.ms ?? 30_000 } : { work: opts.work }, ...(opts.watchdogMs === undefined ? {} : { watchdogMs: opts.watchdogMs }) };
|
|
684
689
|
try {
|
|
685
|
-
v = dsl.proveRule(x.rule, [...inv, l],
|
|
690
|
+
v = dsl.proveRule(x.rule, [...inv, l], proofOptions);
|
|
686
691
|
}
|
|
687
|
-
catch {
|
|
692
|
+
catch (error) {
|
|
693
|
+
// An enclosing work owner must observe its own cancellation. A helper
|
|
694
|
+
// cannot renew that authority by treating the error as a dropped lemma.
|
|
695
|
+
if (error instanceof BudgetExpired) {
|
|
696
|
+
checkWorkEndpoint(error.phase);
|
|
697
|
+
throw error;
|
|
698
|
+
}
|
|
699
|
+
if (error instanceof WatchdogExpired)
|
|
700
|
+
throw error;
|
|
701
|
+
failure = { name: error instanceof Error ? error.name : typeof error, message: error instanceof Error ? error.message : String(error) };
|
|
688
702
|
v = undefined;
|
|
689
703
|
}
|
|
704
|
+
// Keep local optional-lemma stops separate from the eventual source-law
|
|
705
|
+
// verdict. An owned refusal is data, never an invented ancestor expiry.
|
|
706
|
+
attempts.push({ name: l.name, ...(v ? { verdict: v } : { failure }) });
|
|
690
707
|
// These are mathematical supports, not source claims. The caller's marked
|
|
691
708
|
// rule still requires native finalization; a native translation error is not admitted.
|
|
692
709
|
if (v && ((v.proven && v.checked) || (v.kernelProven === true && v.translation === 'unchecked'))) {
|
|
@@ -696,7 +713,7 @@ export function withLemmas(x, opts = {}) {
|
|
|
696
713
|
else
|
|
697
714
|
unproven.push(l.name);
|
|
698
715
|
}
|
|
699
|
-
return { x: { ...x, invariants: inv }, proven, unproven };
|
|
716
|
+
return { x: { ...x, invariants: inv }, proven, unproven, attempts };
|
|
700
717
|
}
|
|
701
718
|
// --- many fibers: a method that is several steps ---------------------------------------------------------------------
|
|
702
719
|
// A method's atomic units: the statements of its body that run an effect on the service's state (a transaction, a Ref
|
package/dist/core/ex/harness.exs
CHANGED
|
@@ -8,15 +8,175 @@
|
|
|
8
8
|
Code.require_file("json.exs", __DIR__)
|
|
9
9
|
import Bitwise
|
|
10
10
|
|
|
11
|
+
# Immutable BEAM values have structural equality, not JS identity. Fresh wire IDs
|
|
12
|
+
# never unify equal maps. Container equality must be refused by native-law;
|
|
13
|
+
# this transport supports scalar/property laws only (nativeEquality: scalar).
|
|
14
|
+
defmodule GdpNative do
|
|
15
|
+
@table :gdp_native_observations
|
|
16
|
+
def init, do: :ets.new(@table, [:named_table, :public, :set])
|
|
17
|
+
def begin_call(mod, fun) do
|
|
18
|
+
:ets.delete_all_objects(@table)
|
|
19
|
+
invocation = Process.get(:gdp_native_invocation, 0) + 1
|
|
20
|
+
if invocation > 9007199254740991, do: raise(ArgumentError, "native invocation exceeds exact integer range")
|
|
21
|
+
Process.put(:gdp_native_invocation, invocation)
|
|
22
|
+
:ets.insert(@table, [{:target, {mod, fun, invocation}}, {:invocation, invocation}])
|
|
23
|
+
end
|
|
24
|
+
defp error(message) do
|
|
25
|
+
if active?(), do: :ets.insert_new(@table, {{:error, Process.get(:gdp_native_active)}, message})
|
|
26
|
+
end
|
|
27
|
+
def capture(mod, fun, bindings, names) do
|
|
28
|
+
invocation = case :ets.lookup(@table, :target) do [{:target, {^mod, ^fun, id}}] -> id; _ -> nil end
|
|
29
|
+
if is_integer(invocation) and :ets.insert_new(@table, {{:entered, invocation}, true}) do
|
|
30
|
+
result = try do
|
|
31
|
+
actual = Map.take(Map.new(bindings), names) |> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
|
|
32
|
+
%{"snapshot" => snapshot(actual, :object)}
|
|
33
|
+
rescue e -> %{"error" => Exception.message(e)} end
|
|
34
|
+
:ets.insert(@table, {{:bindings, invocation}, result})
|
|
35
|
+
previous = Process.get(:gdp_native_active)
|
|
36
|
+
Process.put(:gdp_native_active, invocation)
|
|
37
|
+
{invocation, previous}
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
def leave(nil), do: :ok
|
|
41
|
+
def leave({invocation, previous}) do
|
|
42
|
+
if Process.get(:gdp_native_active) == invocation do
|
|
43
|
+
if is_nil(previous), do: Process.delete(:gdp_native_active), else: Process.put(:gdp_native_active, previous)
|
|
44
|
+
end
|
|
45
|
+
:ok
|
|
46
|
+
end
|
|
47
|
+
defp active? do
|
|
48
|
+
invocation = Process.get(:gdp_native_active)
|
|
49
|
+
is_integer(invocation) and :ets.lookup(@table, :invocation) == [{:invocation, invocation}] and :ets.member(@table, :target)
|
|
50
|
+
end
|
|
51
|
+
# Instrument the original function head, not the reader's argument labels.
|
|
52
|
+
# The first instruction runs after pattern matching, guards and defaults.
|
|
53
|
+
def instrument(ast) do
|
|
54
|
+
Macro.prewalk(ast, fn
|
|
55
|
+
{kind, meta, [head, body]} = node when kind in [:def, :defp] and is_list(body) ->
|
|
56
|
+
plain = case head do {:when, _, [h | _]} -> h; h -> h end
|
|
57
|
+
case plain do
|
|
58
|
+
{name, _, args} when is_list(args) ->
|
|
59
|
+
request = if name in [:handle_call, :handle_cast], do: Enum.take(args, 1), else: Enum.drop(args, 1)
|
|
60
|
+
names = Enum.flat_map(request, ¶meter_names/1) |> Enum.uniq()
|
|
61
|
+
body = Keyword.update!(body, :do, fn original ->
|
|
62
|
+
quote do
|
|
63
|
+
native_entry = GdpNative.capture(__MODULE__, unquote(name), binding(), unquote(names))
|
|
64
|
+
try do
|
|
65
|
+
unquote(original)
|
|
66
|
+
after
|
|
67
|
+
GdpNative.leave(native_entry)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end)
|
|
71
|
+
{kind, meta, [head, body]}
|
|
72
|
+
_ -> node
|
|
73
|
+
end
|
|
74
|
+
node -> node
|
|
75
|
+
end)
|
|
76
|
+
end
|
|
77
|
+
defp parameter_names({:\\, _, [pattern, _default]}), do: parameter_names(pattern)
|
|
78
|
+
defp parameter_names(pattern) do
|
|
79
|
+
{_, names} = Macro.prewalk(pattern, [], fn
|
|
80
|
+
{name, _, context} = node, acc when is_atom(name) and (is_atom(context) or is_nil(context)) ->
|
|
81
|
+
{node, if(String.starts_with?(Atom.to_string(name), "_"), do: acc, else: [name | acc])}
|
|
82
|
+
node, acc -> {node, acc}
|
|
83
|
+
end)
|
|
84
|
+
names
|
|
85
|
+
end
|
|
86
|
+
defp scalar(value) when is_nil(value) or is_boolean(value), do: value
|
|
87
|
+
defp scalar(value) when is_atom(value), do: Atom.to_string(value)
|
|
88
|
+
defp scalar(value) when is_binary(value) do
|
|
89
|
+
if not String.valid?(value) or Regex.match?(~r/[\x00-\x08\x0b\x0c\x0e-\x1f]/, value), do: raise(ArgumentError, "unsupported native binary")
|
|
90
|
+
value
|
|
91
|
+
end
|
|
92
|
+
defp scalar(value) when is_integer(value) and abs(value) <= 9007199254740991, do: value
|
|
93
|
+
defp scalar(value) when is_float(value) and abs(value) <= 9007199254740991 and trunc(value) == value, do: trunc(value)
|
|
94
|
+
defp scalar(_), do: raise(ArgumentError, "unsupported native scalar observation")
|
|
95
|
+
def snapshot(value, root_kind \\ :value) do
|
|
96
|
+
{root, _} = encode(value, root_kind, 0)
|
|
97
|
+
%{"version" => 1, "root" => root}
|
|
98
|
+
end
|
|
99
|
+
defp encode(_, _, count) when count >= 10000, do: raise(ArgumentError, "native snapshot exceeds node bound")
|
|
100
|
+
defp encode(value, kind, count) when is_map(value) do
|
|
101
|
+
if Map.get(value, :__struct__) in [Date, Time, DateTime, NaiveDateTime, MapSet, Range, Regex], do: raise(ArgumentError, "unsupported native snapshot struct")
|
|
102
|
+
fields = if is_struct(value), do: Map.from_struct(value), else: value
|
|
103
|
+
object = kind in [:object, :records] or is_struct(value)
|
|
104
|
+
{items, count} = Enum.map_reduce(fields, count + 1, fn {k, v}, n ->
|
|
105
|
+
key = if object and is_atom(k), do: Atom.to_string(k), else: k
|
|
106
|
+
if not (is_binary(key) or (not object and is_integer(key))), do: raise(ArgumentError, "unsupported native map key")
|
|
107
|
+
{key, n} = encode(key, :value, n)
|
|
108
|
+
{v, n} = encode(v, if(kind == :records, do: :records, else: :value), n)
|
|
109
|
+
{[key, v], n}
|
|
110
|
+
end)
|
|
111
|
+
{%{"id" => Integer.to_string(:erlang.unique_integer([:positive, :monotonic])), "kind" => if(object, do: "object", else: "map"), "items" => items}, count}
|
|
112
|
+
end
|
|
113
|
+
defp encode(value, _, count) when is_list(value) do
|
|
114
|
+
{items, count} = Enum.map_reduce(value, count + 1, &encode(&1, :value, &2))
|
|
115
|
+
{%{"id" => Integer.to_string(:erlang.unique_integer([:positive, :monotonic])), "kind" => "array", "items" => items}, count}
|
|
116
|
+
end
|
|
117
|
+
defp encode(value, _, count), do: {scalar(value), count + 1}
|
|
118
|
+
defp record(key, outcome) do
|
|
119
|
+
if active?() do
|
|
120
|
+
slot = {:call, Process.get(:gdp_native_active), key}
|
|
121
|
+
inserted = :ets.insert_new(@table, {slot, outcome})
|
|
122
|
+
if not inserted and :ets.lookup(@table, slot) != [{slot, outcome}], do: error("inconsistent native call #{key}")
|
|
123
|
+
end
|
|
124
|
+
:ok
|
|
125
|
+
end
|
|
126
|
+
def answered(key, call) do
|
|
127
|
+
try do
|
|
128
|
+
value = call.()
|
|
129
|
+
if active?() do
|
|
130
|
+
try do record(key, %{"kind" => "returned", "value" => scalar(value)}) rescue e -> error(Exception.message(e)) end
|
|
131
|
+
end
|
|
132
|
+
value
|
|
133
|
+
rescue e ->
|
|
134
|
+
if active?(), do: record(key, %{"kind" => "raised", "raised" => inspect(e.__struct__)})
|
|
135
|
+
reraise e, __STACKTRACE__
|
|
136
|
+
catch kind, reason ->
|
|
137
|
+
error("unsupported native call outcome #{kind}")
|
|
138
|
+
:erlang.raise(kind, reason, __STACKTRACE__)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
def clock(value) do
|
|
142
|
+
if active?() do
|
|
143
|
+
try do
|
|
144
|
+
actual = scalar(value)
|
|
145
|
+
slot = {:clock, Process.get(:gdp_native_active)}
|
|
146
|
+
if not :ets.insert_new(@table, {slot, actual}) and :ets.lookup(@table, slot) != [{slot, actual}], do: error("inconsistent native clock reads")
|
|
147
|
+
rescue e -> error(Exception.message(e)) end
|
|
148
|
+
end
|
|
149
|
+
:ok
|
|
150
|
+
end
|
|
151
|
+
def finish_call do
|
|
152
|
+
[{:invocation, invocation}] = :ets.lookup(@table, :invocation)
|
|
153
|
+
bindings = case :ets.lookup(@table, {:bindings, invocation}) do [{_, b}] -> b; _ -> %{"error" => "native function entry was not observed"} end
|
|
154
|
+
observed = case :ets.lookup(@table, {:error, invocation}) do
|
|
155
|
+
[{_, message}] -> %{"error" => message}
|
|
156
|
+
[] ->
|
|
157
|
+
calls = Map.new(for {{:call, ^invocation, key}, outcome} <- :ets.tab2list(@table), do: {key, outcome})
|
|
158
|
+
base = %{"invocation" => invocation, "calls" => calls}
|
|
159
|
+
frame = case :ets.lookup(@table, {:clock, invocation}) do [{_, clock}] -> Map.put(base, "clock", clock); [] -> base end
|
|
160
|
+
%{"snapshot" => snapshot(frame, :records)}
|
|
161
|
+
end
|
|
162
|
+
:ets.delete(@table, :target)
|
|
163
|
+
%{"nativeBindings" => bindings, "nativeObservations" => observed}
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
11
167
|
defmodule GdpClock do
|
|
12
|
-
def now
|
|
168
|
+
def now do
|
|
169
|
+
value = :persistent_term.get(:gdp_now, 0)
|
|
170
|
+
GdpNative.clock(value)
|
|
171
|
+
value
|
|
172
|
+
end
|
|
13
173
|
def int(_ \\ nil), do: now()
|
|
14
174
|
def utc_now(_ \\ nil), do: DateTime.from_unix!(now())
|
|
15
175
|
end
|
|
16
176
|
|
|
17
177
|
# An answered call's value: the request's input for it, None (nil) where it says so; every process reads it.
|
|
18
178
|
defmodule GdpAnswer do
|
|
19
|
-
def a(key), do: (case Map.get(:persistent_term.get(:gdp_ans, %{}), key) do nil -> nil; %{"none" => true} -> nil; spec -> GdpHarness.arg(spec) end)
|
|
179
|
+
def a(key), do: GdpNative.answered(key, fn -> case Map.get(:persistent_term.get(:gdp_ans, %{}), key) do nil -> nil; %{"none" => true} -> nil; spec -> GdpHarness.arg(spec) end end)
|
|
20
180
|
end
|
|
21
181
|
|
|
22
182
|
defmodule GdpHarness do
|
|
@@ -37,9 +197,10 @@ defmodule GdpHarness do
|
|
|
37
197
|
end
|
|
38
198
|
x -> x
|
|
39
199
|
end)
|
|
40
|
-
|
|
200
|
+
GdpNative.init()
|
|
201
|
+
Code.compile_quoted(GdpNative.instrument(ast), req["path"])
|
|
41
202
|
Process.put(:s, req |> Map.put("mod", Module.concat([req["module"]])))
|
|
42
|
-
%{"ok" => true}
|
|
203
|
+
%{"ok" => true, "nativeSnapshot" => 1, "nativeBindings" => 1, "nativeObservations" => 1, "nativeEquality" => "scalar"}
|
|
43
204
|
end
|
|
44
205
|
def s(k), do: Process.get(:s)[k]
|
|
45
206
|
def leaf_t(l), do: (case l["t"] do %{"k" => "opt", "of" => o} -> o; t -> t || %{} end)
|
|
@@ -163,12 +324,17 @@ lines |> Enum.each(fn line ->
|
|
|
163
324
|
"at" ->
|
|
164
325
|
(try do o = GdpHarness.make(req["a"]) |> GdpHarness.set(req["state"]); i = :ets.info(objs, :size) + 1; :ets.insert(objs, {i, o}); %{"id" => i} rescue e -> %{"bad" => Exception.message(e)} catch :exit, r -> %{"bad" => inspect(r)} end)
|
|
165
326
|
"get" -> [{_, o}] = :ets.lookup(objs, req["id"]); %{"state" => GdpHarness.state_of(GdpHarness.get(o))}
|
|
327
|
+
"native" -> [{_, o}] = :ets.lookup(objs, req["id"]); %{"snapshot" => GdpNative.snapshot(GdpHarness.get(o), :object)}
|
|
166
328
|
"call" ->
|
|
167
329
|
:persistent_term.put(:gdp_ans, req["answers"] || %{})
|
|
168
330
|
[{_, o}] = :ets.lookup(objs, req["id"])
|
|
331
|
+
msg = GdpHarness.s("msgs")[req["m"]]
|
|
332
|
+
fun = case msg["kind"] do "handle_call" -> :handle_call; "handle_cast" -> :handle_cast; _ -> String.to_atom(msg["fun"]) end
|
|
333
|
+
GdpNative.begin_call(GdpHarness.s("mod"), fun)
|
|
169
334
|
{o2, r} = GdpHarness.call(o, req["m"], req["a"])
|
|
170
335
|
:ets.insert(objs, {req["id"], o2})
|
|
171
|
-
case r do {:err, e} -> %{"out" => nil, "err" => e}; v -> %{"out" => GdpHarness.tag(v)} end
|
|
336
|
+
result = case r do {:err, e} -> %{"out" => nil, "err" => e}; v -> %{"out" => GdpHarness.tag(v)} end
|
|
337
|
+
Map.merge(result, GdpNative.finish_call())
|
|
172
338
|
"coverage" -> %{"hit" => []}
|
|
173
339
|
op -> %{"error" => "no op #{op}"}
|
|
174
340
|
end
|