Skip to content

Commit

Permalink
Enable client to report facts
Browse files Browse the repository at this point in the history
  • Loading branch information
robsimmons committed Nov 18, 2023
1 parent 33747c2 commit 7a0a738
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0",
"type": "module",
"main": "lib/client.js",
"types": "lib/client.d.ts",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
Expand Down
44 changes: 39 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Data } from './datastructures/data';
import { Data, expose } from './datastructures/data';
import {
ChoiceTree,
ChoiceTreeNode,
Stats,
pathToString,
stepTreeRandomDFS,
} from './engine/choiceengine';
import { makeInitialDb } from './engine/forwardengine';
import { Database, makeInitialDb } from './engine/forwardengine';
import { compile } from './langauge/compile';
import { parse } from './langauge/dusa-parser';
import { IndexedProgram } from './langauge/indexize';
Expand All @@ -16,6 +16,13 @@ import { Issue } from './parsing/parser';
export type { Issue, Stats };
export type { SourcePosition, SourceLocation } from './parsing/source-location';

export type Term = null | bigint | string | { name: string; args?: [Term, ...Term[]] };
export interface Fact {
name: string;
args: Term[];
value: Term;
}

export class DusaError extends Error {
issues: Issue[];
constructor(issues: Issue[]) {
Expand All @@ -24,9 +31,20 @@ export class DusaError extends Error {
}
}

function dataToTerm(d: Data): Term {
const view = expose(d);
if (view.type === 'triv') return null;
if (view.type === 'int') return view.value;
if (view.type === 'string') return view.value;
if (view.args.length === 0) return { name: view.name };
const args = view.args.map(dataToTerm) as [Term, ...Term[]];
return { name: view.name, args };
}

export class Dusa {
private program: IndexedProgram;
private debug: boolean;
private db: Database;

constructor(source: string, debug = false) {
const parsed = parse(source);
Expand All @@ -41,12 +59,15 @@ export class Dusa {

this.debug = debug;
this.program = compile(checked.decls, debug);
this.db = makeInitialDb(this.program);
}

run() {
let tree: null | ChoiceTree = { type: 'leaf', db: makeInitialDb(this.program) };
run(): null | Fact[] {
let tree: null | ChoiceTree = { type: 'leaf', db: this.db };
let path: [ChoiceTreeNode, Data | 'defer'][] = [];
const stats: Stats = { cycles: 0, deadEnds: 0 };
let branched = false;

for (;;) {
if (tree === null) return null;
if (this.debug) {
Expand All @@ -57,8 +78,21 @@ export class Dusa {
tree = result.tree;
path = result.tree === null ? path : result.path;

// If we haven't yet made any branching choices, go ahead and update
// the underlying database.
branched ||= path.length > 0 || tree === null || tree.type !== 'leaf';
if (!branched && tree?.type === 'leaf') {
this.db = tree.db;
}

if (result.solution) {
return stats;
return result.solution.factValues
.entries()
.map(([name, args, value]): null | Fact => {
if (value.type === 'is not') return null;
return { name, args: args.map(dataToTerm), value: dataToTerm(value.value) };
})
.filter((x): x is Fact => x !== null);
}
}
}
Expand Down

0 comments on commit 7a0a738

Please sign in to comment.