Skip to content

Commit

Permalink
Prepare 0.0.9 because I really want INT_TIMES for AoC
Browse files Browse the repository at this point in the history
  • Loading branch information
robsimmons committed Dec 4, 2023
1 parent d93bb66 commit 54119bf
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dusa",
"version": "0.0.8",
"version": "0.0.9",
"type": "module",
"main": "lib/client.js",
"types": "lib/client.d.ts",
Expand Down
10 changes: 10 additions & 0 deletions src/engine/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function* runBuiltinBackward(
case 'BOOLEAN_FALSE':
case 'BOOLEAN_TRUE':
case 'NAT_ZERO':
case 'INT_TIMES':
throw new TypeError(`${pred} should not be run backwards`);
case 'NAT_SUCC': {
if (v.type !== 'int') return;
Expand Down Expand Up @@ -161,6 +162,15 @@ export function runBuiltinForward(pred: BUILT_IN_PRED, args: Data[]): Data | nul
}
return hide({ type: 'int', value: sum });
}
case 'INT_TIMES': {
let product = 1n;
for (const arg of args) {
const view = expose(arg);
if (view.type !== 'int') return null;
product *= view.value;
}
return hide({ type: 'int', value: product });
}
case 'NAT_SUCC': {
const n = expose(args[0]);
if (n.type !== 'int') return null;
Expand Down
11 changes: 11 additions & 0 deletions src/engine/choiceengine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,14 @@ test('Open ended and closed ended possibility', () => {
'a 0 is tt, a 1 is ff, a 2 is ff, a 3 is ff, a 4 is ff, a 5 is ff, a 6 is ff, a 7 is ff, a 8 is ff, a 9 is ff, choice is 0, n 0, n 1, n 2, n 3, n 4, n 5, n 6, n 7, n 8, n 9',
]);
});

test('INT_TIMES', () => {
const { solutions } = testExecution(`
#builtin INT_PLUS plus
#builtin INT_TIMES times
a (plus (times 5 10) 2).
b (times 1 2 3 4 5 6).
c (times 0 1 2 3 4 5).
`);
expect(solutionsToStrings(solutions)).toEqual(['a 52, b 720, c 0']);
});
20 changes: 20 additions & 0 deletions src/language/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,25 @@ function checkFunctionalPredicatesInTerm(
];
}
break;
case 'INT_TIMES': {
for (const arg of pattern.args) {
if (!theseVarsGroundThisPattern(boundVars, arg)) {
return [
{
type: 'Issue',
loc: pattern.loc,
msg: `Built-in ${pattern.name} (${
pattern.symbol
}) needs to have all of its arguments grounded by previous premises, but the argument '${termToString(
arg,
)}' is not ground`,
},
];
}
}
break;
}

case 'EQUAL':
case 'INT_PLUS':
case 'NAT_SUCC':
Expand All @@ -287,6 +306,7 @@ function checkFunctionalPredicatesInTerm(
}
}
}
break;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/language/dusa-builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const BUILT_IN_MAP = {
NAT_SUCC: null as null | string,
INT_PLUS: null as null | string,
INT_MINUS: null as null | string,
INT_TIMES: null as null | string,
STRING_CONCAT: null as null | string,
EQUAL: null as null | string,
} as const;
Expand Down

0 comments on commit 54119bf

Please sign in to comment.