Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed Any Operator to be less Wasteful #1582

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
66 changes: 31 additions & 35 deletions quint/src/runtime/impl/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,55 +140,51 @@ export function lazyBuiltinLambda(
}

case 'actionAny': {
// Executes any of the given actions.
// First, we filter actions so that we only consider those that are enabled.
// Then, we use `rand()` to pick one of the enabled actions.
// Executes the first enabled action from a randomized list of actions.
// Returns false if no enabled actions are found.
const app: QuintApp = { id: 0n, kind: 'app', opcode: 'actionAny', args: [] }
return (ctx, args) => {
const nextVarsSnapshot = ctx.varStorage.snapshot()

const evaluationResults = args.map((arg, i) => {
// on `any`, we reset the action taken as the goal is to save the last
// action picked in an `any` call
// Create array of indices and shuffle them
const indices = Array.from(args.keys())
for (let i = indices.length - 1; i > 0; i--) {
const j = Number(ctx.rand(BigInt(i + 1)))
;[indices[i], indices[j]] = [indices[j], indices[i]]
}

// Try actions in shuffled order until we find one that's enabled
for (const i of indices) {
// Reset state for each attempt
ctx.varStorage.actionTaken = undefined
ctx.varStorage.nondetPicks.forEach((_, key) => {
ctx.varStorage.nondetPicks.set(key, undefined)
})

ctx.recorder.onAnyOptionCall(app, i)
const result = arg(ctx).map(result => {
// Save vars
const successor = ctx.varStorage.snapshot()

return result.toBool() ? [{ snapshot: successor, index: i }] : []
})
const result = args[i](ctx)
ctx.recorder.onAnyOptionReturn(app, i)

return result
})
if (result.isLeft()) {
return result
}

const processedResults = mergeInMany(evaluationResults)
.map(suc => suc.flat())
.mapLeft(errors => errors[0])

return processedResults.map(potentialSuccessors => {
switch (potentialSuccessors.length) {
case 0:
ctx.recorder.onAnyReturn(args.length, -1)
ctx.varStorage.recoverSnapshot(nextVarsSnapshot)
return rv.mkBool(false)
case 1:
ctx.recorder.onAnyReturn(args.length, potentialSuccessors[0].index)
ctx.varStorage.recoverSnapshot(potentialSuccessors[0].snapshot)
return rv.mkBool(true)
default: {
const choice = Number(ctx.rand(BigInt(potentialSuccessors.length)))
ctx.recorder.onAnyReturn(args.length, potentialSuccessors[choice].index)
ctx.varStorage.recoverSnapshot(potentialSuccessors[choice].snapshot)
return rv.mkBool(true)
}
if (result.value.toBool()) {
// Found an enabled action - record it and return true
const successor = ctx.varStorage.snapshot()
ctx.recorder.onAnyReturn(args.length, i)
ctx.varStorage.recoverSnapshot(successor)
return right(rv.mkBool(true))
}
})

// Reset state before trying next action
ctx.varStorage.recoverSnapshot(nextVarsSnapshot)
}

// No enabled actions found
ctx.recorder.onAnyReturn(args.length, -1)
ctx.varStorage.recoverSnapshot(nextVarsSnapshot)
return right(rv.mkBool(false))
}
}
case 'actionAll':
Expand Down