Skip to content

Draft: (partial) fix for 'ce.declare' with SymbolDefinition using prop. 'type' #226

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions src/common/type/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ export function collectionElementType(type: Readonly<Type>): Type | undefined {
return undefined;
}

export function isValidType(t: any): t is Readonly<Type> {
if (typeof t === 'string')
return PRIMITIVE_TYPES.includes(t as PrimitiveType);
if (typeof t !== 'object') return false;
export function isValidType(t: unknown): t is Readonly<Type> {
const isObject = typeof t === 'object' && t !== null;
if (typeof t === 'string' || (isObject && 'type' in t))
return PRIMITIVE_TYPES.includes((isObject ? t.type : t) as PrimitiveType);
if (!isObject) return false;
if (!('kind' in t)) return false;
return (
t.kind === 'signature' ||
Expand Down
23 changes: 17 additions & 6 deletions src/compute-engine/boxed-expression/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { DEFINITIONS_INEQUALITIES } from '../latex-syntax/dictionary/definitions

import { MACHINE_PRECISION } from '../numerics/numeric';
import { Type } from '../../common/type/types';
import { isValidType } from '../../common/type/utils';
import { isSubtype } from '../../common/type/subtype';

export function isBoxedExpression(x: unknown): x is BoxedExpression {
return typeof x === 'object' && x !== null && 'engine' in x;
Expand Down Expand Up @@ -182,17 +184,26 @@ export function normalizeFlags(
return result as NumericFlags;
}

export function isSymbolDefinition(def: any): def is SymbolDefinition {
export function isSymbolDefinition(def: unknown): def is SymbolDefinition {
if (def === undefined || def === null || typeof def !== 'object')
return false;

if (isBoxedExpression(def)) return false;

if ('value' in def || 'constant' in def || 'inferred' in def) {
if ('type' in def && typeof def.type === 'function') {
throw new Error(
'The `type` field of a symbol definition should be of type `string`'
);
if (
'type' in def ||
'value' in def ||
'constant' in def ||
'inferred' in def
) {
if ('type' in def) {
if (!isValidType(def.type))
throw new Error(`Invalid type: "${def.type}"`);
if (isSubtype('function', def.type)) {
throw new Error(
'The `type` field of a symbol definition should be of type `string`'
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think that's OK; but some introduced errs. may be surfacing on account of type being given as undefined: so may require minor adjstment.

}

if ('signature' in def) {
Expand Down
6 changes: 4 additions & 2 deletions src/compute-engine/compute-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1371,8 +1371,10 @@ export class ComputeEngine implements IComputeEngine {
if (
typeof def === 'object' &&
'type' in def &&
((typeof def.type === 'string' && isSubtype(def.type, 'function')) ||
(def.type && isValidType(def.type)))
!(
(typeof def.type === 'string' && isSubtype(def.type, 'function')) ||
(def.type && isValidType(def.type))
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this was a bug/typo? I.e. final cond. to be negated.

) {
throw new Error(
`Invalid definition for "${id}": use a FunctionDefinition to define a function or use 'ce.declare("${id}", "function")'`
Expand Down