2022-08-31 13:29:56 +02:00
|
|
|
import {
|
2023-02-03 17:51:27 +01:00
|
|
|
ClassDeclaration,
|
2022-09-15 13:49:45 +02:00
|
|
|
EnumDeclaration,
|
2022-08-31 13:29:56 +02:00
|
|
|
Expression,
|
2022-09-18 14:33:22 +02:00
|
|
|
ExprOperator,
|
|
|
|
Identifier,
|
|
|
|
IdentifierAlt,
|
2023-03-11 14:24:02 +01:00
|
|
|
InstanceDeclaration,
|
2022-09-05 19:38:55 +02:00
|
|
|
LetDeclaration,
|
2022-09-18 14:33:22 +02:00
|
|
|
ModuleDeclaration,
|
2022-08-31 13:29:56 +02:00
|
|
|
Pattern,
|
2022-09-18 14:33:22 +02:00
|
|
|
ReferenceExpression,
|
|
|
|
ReferenceTypeExpression,
|
2022-09-01 20:06:43 +02:00
|
|
|
SourceFile,
|
2022-09-15 16:56:02 +02:00
|
|
|
StructDeclaration,
|
2023-03-11 14:24:02 +01:00
|
|
|
StructPattern,
|
2022-08-31 13:29:56 +02:00
|
|
|
Syntax,
|
|
|
|
SyntaxKind,
|
2022-09-18 14:33:22 +02:00
|
|
|
TypeExpression,
|
2022-08-31 13:29:56 +02:00
|
|
|
} from "./cst";
|
2023-02-03 17:51:27 +01:00
|
|
|
import { Symkind } from "./scope"
|
2022-09-11 11:20:21 +02:00
|
|
|
import {
|
|
|
|
describeType,
|
2023-02-03 17:51:27 +01:00
|
|
|
BindingNotFoundDiagnostic,
|
2022-09-11 11:20:21 +02:00
|
|
|
Diagnostics,
|
|
|
|
FieldMissingDiagnostic,
|
|
|
|
UnificationFailedDiagnostic,
|
2022-09-15 13:49:45 +02:00
|
|
|
KindMismatchDiagnostic,
|
2022-09-18 14:33:22 +02:00
|
|
|
ModuleNotFoundDiagnostic,
|
2023-02-03 17:51:27 +01:00
|
|
|
TypeclassNotFoundDiagnostic,
|
2023-03-11 14:24:02 +01:00
|
|
|
FieldTypeMismatchDiagnostic,
|
2022-09-11 11:20:21 +02:00
|
|
|
} from "./diagnostics";
|
2023-03-11 14:24:02 +01:00
|
|
|
import { assert, assertNever, first, isEmpty, last, MultiMap } from "./util";
|
2022-09-15 16:04:49 +02:00
|
|
|
import { Analyser } from "./analysis";
|
2022-09-06 15:13:07 +02:00
|
|
|
|
|
|
|
const MAX_TYPE_ERROR_COUNT = 5;
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
export enum TypeKind {
|
|
|
|
Arrow,
|
|
|
|
Var,
|
|
|
|
Con,
|
|
|
|
Tuple,
|
2022-09-11 11:20:21 +02:00
|
|
|
App,
|
2022-09-15 20:33:34 +02:00
|
|
|
Nominal,
|
2023-03-11 14:24:02 +01:00
|
|
|
Field,
|
|
|
|
Nil,
|
|
|
|
Absent,
|
|
|
|
Present,
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract class TypeBase {
|
|
|
|
|
|
|
|
public abstract readonly kind: TypeKind;
|
|
|
|
|
2022-09-09 20:02:35 +02:00
|
|
|
public next: Type = this as any;
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
public node: Syntax | null = null
|
|
|
|
) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static join(a: Type, b: Type): void {
|
|
|
|
const keep = a.next;
|
|
|
|
a.next = b;
|
|
|
|
b.next = keep;
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
public abstract getTypeVars(): Iterable<TVar>;
|
|
|
|
|
2022-09-09 20:02:35 +02:00
|
|
|
public abstract shallowClone(): Type;
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
public abstract substitute(sub: TVSub): Type;
|
|
|
|
|
|
|
|
public hasTypeVar(tv: TVar): boolean {
|
|
|
|
for (const other of this.getTypeVars()) {
|
|
|
|
if (tv.id === other.id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class TVar extends TypeBase {
|
|
|
|
|
|
|
|
public readonly kind = TypeKind.Var;
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
public context = new Set<ClassDeclaration>();
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
public constructor(
|
|
|
|
public id: number,
|
2022-09-09 20:02:35 +02:00
|
|
|
public node: Syntax | null = null,
|
2022-08-31 13:29:56 +02:00
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public *getTypeVars(): Iterable<TVar> {
|
|
|
|
yield this;
|
|
|
|
}
|
|
|
|
|
2022-09-09 20:02:35 +02:00
|
|
|
public shallowClone(): TVar {
|
|
|
|
return new TVar(this.id, this.node);
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
public substitute(sub: TVSub): Type {
|
2022-09-05 17:25:55 +02:00
|
|
|
const other = sub.get(this);
|
|
|
|
return other === undefined
|
|
|
|
? this : other.substitute(sub);
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
export class TNil extends TypeBase {
|
|
|
|
|
|
|
|
public readonly kind = TypeKind.Nil;
|
|
|
|
|
|
|
|
public substitute(_sub: TVSub): Type {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public shallowClone(): Type {
|
|
|
|
return new TNil(this.node);
|
|
|
|
}
|
|
|
|
|
|
|
|
public *getTypeVars(): Iterable<TVar> {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TAbsent extends TypeBase {
|
|
|
|
|
|
|
|
public readonly kind = TypeKind.Absent;
|
|
|
|
|
|
|
|
public substitute(_sub: TVSub): Type {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public shallowClone(): Type {
|
|
|
|
return new TAbsent(this.node);
|
|
|
|
}
|
|
|
|
|
|
|
|
public *getTypeVars(): Iterable<TVar> {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TPresent extends TypeBase {
|
|
|
|
|
|
|
|
public readonly kind = TypeKind.Present;
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
public type: Type,
|
|
|
|
node: Syntax | null = null,
|
|
|
|
) {
|
|
|
|
super(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
public substitute(sub: TVSub): Type {
|
|
|
|
return new TPresent(this.type.substitute(sub), this.node);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getTypeVars(): Iterable<TVar> {
|
|
|
|
return this.type.getTypeVars();
|
|
|
|
}
|
|
|
|
|
|
|
|
public shallowClone(): Type {
|
|
|
|
return new TPresent(this.type, this.node);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-01 20:06:43 +02:00
|
|
|
export class TArrow extends TypeBase {
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
public readonly kind = TypeKind.Arrow;
|
|
|
|
|
|
|
|
public constructor(
|
2022-09-16 12:00:00 +02:00
|
|
|
public paramType: Type,
|
2022-08-31 13:29:56 +02:00
|
|
|
public returnType: Type,
|
2023-03-11 14:24:02 +01:00
|
|
|
node: Syntax | null = null,
|
2022-08-31 13:29:56 +02:00
|
|
|
) {
|
2023-03-11 14:24:02 +01:00
|
|
|
super(node);
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2022-09-16 12:00:00 +02:00
|
|
|
public static build(paramTypes: Type[], returnType: Type, node: Syntax | null = null): Type {
|
|
|
|
let result = returnType;
|
|
|
|
for (let i = paramTypes.length-1; i >= 0; i--) {
|
|
|
|
result = new TArrow(paramTypes[i], result, node);
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
2022-09-16 12:00:00 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public *getTypeVars(): Iterable<TVar> {
|
|
|
|
yield* this.paramType.getTypeVars();
|
2022-08-31 13:29:56 +02:00
|
|
|
yield* this.returnType.getTypeVars();
|
|
|
|
}
|
|
|
|
|
2022-09-09 20:02:35 +02:00
|
|
|
public shallowClone(): TArrow {
|
|
|
|
return new TArrow(
|
2022-09-16 12:00:00 +02:00
|
|
|
this.paramType,
|
2022-09-09 20:02:35 +02:00
|
|
|
this.returnType,
|
|
|
|
this.node,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
public substitute(sub: TVSub): Type {
|
|
|
|
let changed = false;
|
2022-09-16 12:00:00 +02:00
|
|
|
const newParamType = this.paramType.substitute(sub);
|
|
|
|
if (newParamType !== this.paramType) {
|
|
|
|
changed = true;
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
const newReturnType = this.returnType.substitute(sub);
|
|
|
|
if (newReturnType !== this.returnType) {
|
|
|
|
changed = true;
|
|
|
|
}
|
2022-09-16 12:00:00 +02:00
|
|
|
return changed ? new TArrow(newParamType, newReturnType, this.node) : this;
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-09 20:02:35 +02:00
|
|
|
export class TCon extends TypeBase {
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
public readonly kind = TypeKind.Con;
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
public id: number,
|
|
|
|
public argTypes: Type[],
|
|
|
|
public displayName: string,
|
2022-09-09 20:02:35 +02:00
|
|
|
public node: Syntax | null = null,
|
2022-08-31 13:29:56 +02:00
|
|
|
) {
|
2022-09-09 20:02:35 +02:00
|
|
|
super(node);
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public *getTypeVars(): Iterable<TVar> {
|
|
|
|
for (const argType of this.argTypes) {
|
|
|
|
yield* argType.getTypeVars();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 20:02:35 +02:00
|
|
|
public shallowClone(): TCon {
|
|
|
|
return new TCon(
|
|
|
|
this.id,
|
|
|
|
this.argTypes,
|
|
|
|
this.displayName,
|
|
|
|
this.node,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
public substitute(sub: TVSub): Type {
|
|
|
|
let changed = false;
|
|
|
|
const newArgTypes = [];
|
|
|
|
for (const argType of this.argTypes) {
|
|
|
|
const newArgType = argType.substitute(sub);
|
|
|
|
if (newArgType !== argType) {
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
newArgTypes.push(newArgType);
|
|
|
|
}
|
2022-09-09 20:02:35 +02:00
|
|
|
return changed ? new TCon(this.id, newArgTypes, this.displayName, this.node) : this;
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class TTuple extends TypeBase {
|
|
|
|
|
|
|
|
public readonly kind = TypeKind.Tuple;
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
public elementTypes: Type[],
|
2022-09-09 20:02:35 +02:00
|
|
|
public node: Syntax | null = null,
|
2022-08-31 13:29:56 +02:00
|
|
|
) {
|
2022-09-09 20:02:35 +02:00
|
|
|
super(node);
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public *getTypeVars(): Iterable<TVar> {
|
|
|
|
for (const elementType of this.elementTypes) {
|
|
|
|
yield* elementType.getTypeVars();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 20:02:35 +02:00
|
|
|
public shallowClone(): TTuple {
|
|
|
|
return new TTuple(
|
|
|
|
this.elementTypes,
|
|
|
|
this.node,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
public substitute(sub: TVSub): Type {
|
|
|
|
let changed = false;
|
|
|
|
const newElementTypes = [];
|
|
|
|
for (const elementType of this.elementTypes) {
|
|
|
|
const newElementType = elementType.substitute(sub);
|
|
|
|
if (newElementType !== elementType) {
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
newElementTypes.push(newElementType);
|
|
|
|
}
|
2022-09-09 20:02:35 +02:00
|
|
|
return changed ? new TTuple(newElementTypes, this.node) : this;
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
export class TField extends TypeBase {
|
2022-09-07 12:45:38 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
public readonly kind = TypeKind.Field;
|
2022-09-07 12:45:38 +02:00
|
|
|
|
|
|
|
public constructor(
|
|
|
|
public name: string,
|
|
|
|
public type: Type,
|
2023-03-11 14:24:02 +01:00
|
|
|
public restType: Type,
|
2022-09-09 20:02:35 +02:00
|
|
|
public node: Syntax | null = null,
|
2022-09-07 12:45:38 +02:00
|
|
|
) {
|
2022-09-09 20:02:35 +02:00
|
|
|
super(node);
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getTypeVars(): Iterable<TVar> {
|
|
|
|
return this.type.getTypeVars();
|
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
public shallowClone(): TField {
|
|
|
|
return new TField(
|
2022-09-09 20:02:35 +02:00
|
|
|
this.name,
|
|
|
|
this.type,
|
2023-03-11 14:24:02 +01:00
|
|
|
this.restType,
|
2022-09-09 20:02:35 +02:00
|
|
|
this.node,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
public static sort(type: Type): Type {
|
|
|
|
const fields = new Map<string, TField>();
|
|
|
|
while (type.kind === TypeKind.Field) {
|
|
|
|
fields.set(type.name, type);
|
|
|
|
type = type.restType;
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
const keys = [...fields.keys()].sort().reverse();
|
|
|
|
let out: Type = type;
|
|
|
|
for (const key of keys) {
|
|
|
|
const field = fields.get(key)!;
|
|
|
|
out = new TField(key, field.type, out, field.node);
|
|
|
|
}
|
|
|
|
return out
|
2022-09-09 20:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 12:45:38 +02:00
|
|
|
public substitute(sub: TVSub): Type {
|
2023-03-11 14:24:02 +01:00
|
|
|
const newType = this.type.substitute(sub);
|
|
|
|
const newRestType = this.restType.substitute(sub);
|
|
|
|
return newType !== this.type || newRestType !== this.restType
|
|
|
|
? new TField(this.name, newType, newRestType, this.node) : this;
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-11 11:20:21 +02:00
|
|
|
export class TApp extends TypeBase {
|
|
|
|
|
|
|
|
public readonly kind = TypeKind.App;
|
|
|
|
|
|
|
|
public constructor(
|
2022-09-14 22:34:53 +02:00
|
|
|
public left: Type,
|
|
|
|
public right: Type,
|
2022-09-11 11:20:21 +02:00
|
|
|
public node: Syntax | null = null
|
|
|
|
) {
|
|
|
|
super(node);
|
|
|
|
}
|
|
|
|
|
2022-09-15 20:33:34 +02:00
|
|
|
public static build(resultType: Type, types: Type[], node: Syntax | null = null): Type {
|
|
|
|
for (let i = 0; i < types.length; i++) {
|
|
|
|
resultType = new TApp(types[i], resultType, node);
|
2022-09-11 11:20:21 +02:00
|
|
|
}
|
2022-09-15 20:33:34 +02:00
|
|
|
return resultType;
|
2022-09-11 11:20:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public *getTypeVars(): Iterable<TVar> {
|
2022-09-14 22:34:53 +02:00
|
|
|
yield* this.left.getTypeVars();
|
|
|
|
yield* this.right.getTypeVars();
|
2022-09-11 11:20:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public shallowClone() {
|
|
|
|
return new TApp(
|
2022-09-14 22:34:53 +02:00
|
|
|
this.left,
|
|
|
|
this.right,
|
2022-09-11 11:20:21 +02:00
|
|
|
this.node
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public substitute(sub: TVSub): Type {
|
|
|
|
let changed = false;
|
2022-09-14 22:34:53 +02:00
|
|
|
const newOperatorType = this.left.substitute(sub);
|
|
|
|
if (newOperatorType !== this.left) {
|
2022-09-11 11:20:21 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
2022-09-14 22:34:53 +02:00
|
|
|
const newArgType = this.right.substitute(sub);
|
|
|
|
if (newArgType !== this.right) {
|
2022-09-11 11:20:21 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
return changed ? new TApp(newOperatorType, newArgType, this.node) : this;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-15 20:33:34 +02:00
|
|
|
export class TNominal extends TypeBase {
|
2022-09-11 15:23:22 +02:00
|
|
|
|
2022-09-15 20:33:34 +02:00
|
|
|
public readonly kind = TypeKind.Nominal;
|
2022-09-11 15:23:22 +02:00
|
|
|
|
|
|
|
public constructor(
|
2022-09-15 20:48:36 +02:00
|
|
|
public decl: StructDeclaration | EnumDeclaration,
|
2022-09-11 15:23:22 +02:00
|
|
|
public node: Syntax | null = null,
|
|
|
|
) {
|
|
|
|
super(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
public *getTypeVars(): Iterable<TVar> {
|
2022-09-15 20:33:34 +02:00
|
|
|
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public shallowClone(): Type {
|
2022-09-15 20:33:34 +02:00
|
|
|
return new TNominal(
|
2022-09-14 22:34:53 +02:00
|
|
|
this.decl,
|
2022-09-11 15:23:22 +02:00
|
|
|
this.node,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-15 20:48:36 +02:00
|
|
|
public substitute(_sub: TVSub): Type {
|
2022-09-15 20:33:34 +02:00
|
|
|
return this;
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
export type Type
|
|
|
|
= TCon
|
|
|
|
| TArrow
|
|
|
|
| TVar
|
|
|
|
| TTuple
|
2022-09-11 11:20:21 +02:00
|
|
|
| TApp
|
2022-09-15 20:33:34 +02:00
|
|
|
| TNominal
|
2023-03-11 14:24:02 +01:00
|
|
|
| TField
|
|
|
|
| TNil
|
|
|
|
| TPresent
|
|
|
|
| TAbsent
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
export const enum KindType {
|
|
|
|
Star,
|
|
|
|
Arrow,
|
|
|
|
Var,
|
2023-03-11 14:24:02 +01:00
|
|
|
Row,
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class KVSub {
|
|
|
|
|
|
|
|
private mapping = new Map<number, Kind>();
|
|
|
|
|
|
|
|
public set(kv: KVar, kind: Kind): void {
|
|
|
|
this.mapping.set(kv.id, kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
public get(kv: KVar): Kind | undefined {
|
|
|
|
return this.mapping.get(kv.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public has(kv: KVar): boolean {
|
|
|
|
return this.mapping.has(kv.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public values(): Iterable<Kind> {
|
|
|
|
return this.mapping.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class KindBase {
|
2022-09-15 22:39:20 +02:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
public abstract readonly type: KindType;
|
|
|
|
|
|
|
|
public abstract substitute(sub: KVSub): Kind;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class KVar extends KindBase {
|
|
|
|
|
|
|
|
public readonly type = KindType.Var;
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
public id: number,
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public substitute(sub: KVSub): Kind {
|
|
|
|
const other = sub.get(this);
|
|
|
|
return other === undefined
|
|
|
|
? this : other.substitute(sub);
|
|
|
|
}
|
|
|
|
|
2022-09-15 22:39:20 +02:00
|
|
|
public hasFailed(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
class KType extends KindBase {
|
2022-09-14 16:46:30 +02:00
|
|
|
|
|
|
|
public readonly type = KindType.Star;
|
|
|
|
|
|
|
|
public substitute(_sub: KVSub): Kind {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class KRow extends KindBase {
|
|
|
|
|
|
|
|
public readonly type = KindType.Row;
|
|
|
|
|
|
|
|
public substitute(_sub: KVSub): Kind {
|
|
|
|
return this;
|
2022-09-15 22:39:20 +02:00
|
|
|
}
|
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class KArrow extends KindBase {
|
|
|
|
|
|
|
|
public readonly type = KindType.Arrow;
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
public left: Kind,
|
|
|
|
public right: Kind,
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public substitute(sub: KVSub): Kind {
|
|
|
|
return new KArrow(
|
|
|
|
this.left.substitute(sub),
|
|
|
|
this.right.substitute(sub),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
const kindOfTypes = new KType();
|
|
|
|
const kindOfRows = new KRow();
|
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
export type Kind
|
2023-03-11 14:24:02 +01:00
|
|
|
= KType
|
2022-09-14 16:46:30 +02:00
|
|
|
| KArrow
|
|
|
|
| KVar
|
2023-03-11 14:24:02 +01:00
|
|
|
| KRow
|
2022-09-14 16:46:30 +02:00
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
class TVSet {
|
|
|
|
|
|
|
|
private mapping = new Map<number, TVar>();
|
|
|
|
|
|
|
|
public add(tv: TVar): void {
|
|
|
|
this.mapping.set(tv.id, tv);
|
|
|
|
}
|
2022-09-01 20:06:43 +02:00
|
|
|
|
|
|
|
public has(tv: TVar): boolean {
|
|
|
|
return this.mapping.has(tv.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public intersectsType(type: Type): boolean {
|
|
|
|
for (const tv of type.getTypeVars()) {
|
|
|
|
if (this.has(tv)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
public delete(tv: TVar): void {
|
|
|
|
this.mapping.delete(tv.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public [Symbol.iterator](): Iterator<TVar> {
|
|
|
|
return this.mapping.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class TVSub {
|
|
|
|
|
|
|
|
private mapping = new Map<number, Type>();
|
|
|
|
|
|
|
|
public set(tv: TVar, type: Type): void {
|
|
|
|
this.mapping.set(tv.id, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public get(tv: TVar): Type | undefined {
|
|
|
|
return this.mapping.get(tv.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public has(tv: TVar): boolean {
|
|
|
|
return this.mapping.has(tv.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public delete(tv: TVar): void {
|
|
|
|
this.mapping.delete(tv.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public values(): Iterable<Type> {
|
|
|
|
return this.mapping.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const enum ConstraintKind {
|
|
|
|
Equal,
|
|
|
|
Many,
|
2022-09-07 12:45:38 +02:00
|
|
|
Shaped,
|
2023-03-11 14:24:02 +01:00
|
|
|
Class,
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract class ConstraintBase {
|
|
|
|
|
|
|
|
public abstract substitute(sub: TVSub): Constraint;
|
|
|
|
|
2022-09-06 11:56:17 +02:00
|
|
|
public constructor(
|
|
|
|
public node: Syntax | null = null
|
|
|
|
) {
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-06 11:56:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public prevInstantiation: Constraint | null = null;
|
|
|
|
|
|
|
|
public *getNodes(): Iterable<Syntax> {
|
|
|
|
let curr: Constraint | null = this as any;
|
|
|
|
while (curr !== null) {
|
|
|
|
if (curr.node !== null) {
|
|
|
|
yield curr.node;
|
|
|
|
}
|
|
|
|
curr = curr.prevInstantiation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
public get lastNode(): Syntax | null {
|
|
|
|
return last(this.getNodes()[Symbol.iterator]()) ?? null;
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
public get firstNode(): Syntax | null {
|
|
|
|
return first(this.getNodes()[Symbol.iterator]()) ?? null;
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
class CEqual extends ConstraintBase {
|
|
|
|
|
|
|
|
public readonly kind = ConstraintKind.Equal;
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
public left: Type,
|
|
|
|
public right: Type,
|
|
|
|
public node: Syntax,
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public substitute(sub: TVSub): Constraint {
|
|
|
|
return new CEqual(
|
|
|
|
this.left.substitute(sub),
|
|
|
|
this.right.substitute(sub),
|
|
|
|
this.node,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-05 17:25:55 +02:00
|
|
|
public dump(): void {
|
|
|
|
console.error(`${describeType(this.left)} ~ ${describeType(this.right)}`);
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class CMany extends ConstraintBase {
|
|
|
|
|
|
|
|
public readonly kind = ConstraintKind.Many;
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
public elements: Constraint[]
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public substitute(sub: TVSub): Constraint {
|
|
|
|
const newElements = [];
|
|
|
|
for (const element of this.elements) {
|
|
|
|
newElements.push(element.substitute(sub));
|
|
|
|
}
|
|
|
|
return new CMany(newElements);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
interface InstanceRef {
|
|
|
|
node: InstanceDeclaration | null;
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
type Constraint
|
|
|
|
= CEqual
|
|
|
|
| CMany
|
|
|
|
|
|
|
|
class ConstraintSet extends Array<Constraint> {
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class SchemeBase {
|
|
|
|
}
|
|
|
|
|
|
|
|
class Forall extends SchemeBase {
|
|
|
|
|
|
|
|
public constructor(
|
2022-09-14 23:09:22 +02:00
|
|
|
public typeVars: Iterable<TVar>,
|
|
|
|
public constraints: Iterable<Constraint>,
|
2022-08-31 13:29:56 +02:00
|
|
|
public type: Type,
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-15 16:56:02 +02:00
|
|
|
export type Scheme
|
2022-08-31 13:29:56 +02:00
|
|
|
= Forall
|
|
|
|
|
2022-09-18 14:33:22 +02:00
|
|
|
type NodeWithReference
|
|
|
|
= Identifier
|
|
|
|
| IdentifierAlt
|
|
|
|
| ExprOperator
|
|
|
|
| ReferenceExpression
|
|
|
|
| ReferenceTypeExpression
|
|
|
|
|
2023-02-03 17:51:27 +01:00
|
|
|
class TypeEnv {
|
2022-09-05 19:33:08 +02:00
|
|
|
|
2022-09-15 11:49:53 +02:00
|
|
|
private mapping = new MultiMap<string, [Symkind, Scheme]>();
|
2022-09-05 19:33:08 +02:00
|
|
|
|
|
|
|
public constructor(public parent: TypeEnv | null = null) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-15 11:49:53 +02:00
|
|
|
public add(name: string, scheme: Scheme, kind: Symkind): void {
|
|
|
|
this.mapping.add(name, [kind, scheme]);
|
2022-09-05 19:33:08 +02:00
|
|
|
}
|
|
|
|
|
2022-09-18 14:33:22 +02:00
|
|
|
public get(name: string, expectedKind: Symkind): Scheme | null {
|
|
|
|
for (const [kind, scheme] of this.mapping.get(name)) {
|
|
|
|
if (kind & expectedKind) {
|
|
|
|
return scheme;
|
2022-09-05 19:33:08 +02:00
|
|
|
}
|
2022-09-18 14:33:22 +02:00
|
|
|
}
|
2022-09-05 19:33:08 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
class KindEnv {
|
|
|
|
|
2022-09-15 13:56:58 +02:00
|
|
|
private mapping = new Map<string, Kind>();
|
2022-09-14 16:46:30 +02:00
|
|
|
|
|
|
|
public constructor(public parent: KindEnv | null = null) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-18 19:33:34 +02:00
|
|
|
public get(name: string): Kind | null {
|
|
|
|
return this.mapping.get(name) ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public set(name: string, kind: Kind): void {
|
2022-09-15 13:56:58 +02:00
|
|
|
assert(!this.mapping.has(name));
|
|
|
|
this.mapping.set(name, kind);
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
|
2022-09-15 13:56:58 +02:00
|
|
|
public lookup(name: string): Kind | null {
|
2022-09-14 16:46:30 +02:00
|
|
|
let curr: KindEnv | null = this;
|
|
|
|
do {
|
2022-09-15 13:56:58 +02:00
|
|
|
const kind = curr.mapping.get(name);
|
2022-09-14 16:46:30 +02:00
|
|
|
if (kind !== undefined) {
|
|
|
|
return kind;
|
|
|
|
}
|
|
|
|
curr = curr.parent;
|
|
|
|
} while (curr !== null);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-02-03 17:51:27 +01:00
|
|
|
export type { KindEnv, TypeEnv };
|
|
|
|
|
2022-09-18 19:33:34 +02:00
|
|
|
function splitReferences(node: NodeWithReference): [IdentifierAlt[], Identifier | IdentifierAlt | ExprOperator] {
|
|
|
|
let modulePath: IdentifierAlt[];
|
|
|
|
let name: Identifier | IdentifierAlt | ExprOperator;
|
|
|
|
if (node.kind === SyntaxKind.ReferenceExpression || node.kind === SyntaxKind.ReferenceTypeExpression) {
|
|
|
|
modulePath = node.modulePath.map(([name, _dot]) => name);
|
|
|
|
name = node.name;
|
|
|
|
} else {
|
|
|
|
modulePath = [];
|
|
|
|
name = node;
|
|
|
|
}
|
|
|
|
return [modulePath, name]
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
export interface InferContext {
|
|
|
|
typeVars: TVSet;
|
|
|
|
env: TypeEnv;
|
|
|
|
constraints: ConstraintSet;
|
2022-09-05 17:25:55 +02:00
|
|
|
returnType: Type | null;
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2022-09-09 22:37:14 +02:00
|
|
|
function isFunctionDeclarationLike(node: LetDeclaration): boolean {
|
2023-03-11 14:24:02 +01:00
|
|
|
return node.pattern.kind === SyntaxKind.NamedPattern
|
2022-09-09 22:37:14 +02:00
|
|
|
&& (node.params.length > 0 || (node.body !== null && node.body.kind === SyntaxKind.BlockBody));
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
export class Checker {
|
|
|
|
|
|
|
|
private nextTypeVarId = 0;
|
2022-09-14 16:46:30 +02:00
|
|
|
private nextKindVarId = 0;
|
2022-08-31 13:29:56 +02:00
|
|
|
private nextConTypeId = 0;
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
private stringType = this.createTCon([], 'String');
|
|
|
|
private intType = this.createTCon([], 'Int');
|
|
|
|
private boolType = this.createTCon([], 'Bool');
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2022-09-05 17:25:55 +02:00
|
|
|
private contexts: InferContext[] = [];
|
|
|
|
|
|
|
|
private solution = new TVSub();
|
2022-09-14 16:46:30 +02:00
|
|
|
private kindSolution = new KVSub();
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
public constructor(
|
2022-09-15 16:04:49 +02:00
|
|
|
private analyser: Analyser,
|
2022-08-31 13:29:56 +02:00
|
|
|
private diagnostics: Diagnostics
|
|
|
|
) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public getIntType(): Type {
|
|
|
|
return this.intType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getStringType(): Type {
|
|
|
|
return this.stringType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getBoolType(): Type {
|
|
|
|
return this.boolType;
|
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
private createTCon(types: Type[], name: string): TCon {
|
|
|
|
return new TCon(this.nextConTypeId++, types, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private createTypeVar(node: Syntax | null = null): TVar {
|
|
|
|
const typeVar = new TVar(this.nextTypeVarId++, node);
|
2022-09-16 11:31:34 +02:00
|
|
|
this.getContext().typeVars.add(typeVar);
|
2022-08-31 13:29:56 +02:00
|
|
|
return typeVar;
|
|
|
|
}
|
|
|
|
|
2022-09-16 11:31:34 +02:00
|
|
|
public getContext(): InferContext {
|
|
|
|
return this.contexts[this.contexts.length-1];
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
private addConstraint(constraint: Constraint): void {
|
2022-09-16 11:31:34 +02:00
|
|
|
this.getContext().constraints.push(constraint);
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private pushContext(context: InferContext) {
|
2022-09-05 17:25:55 +02:00
|
|
|
this.contexts.push(context);
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private popContext(context: InferContext) {
|
2022-09-05 17:25:55 +02:00
|
|
|
assert(this.contexts[this.contexts.length-1] === context);
|
|
|
|
this.contexts.pop();
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2023-02-03 17:51:27 +01:00
|
|
|
private lookupKind(env: KindEnv, node: NodeWithReference, emitDiagnostic = true): Kind | null {
|
2022-09-18 19:33:34 +02:00
|
|
|
const [modulePath, name] = splitReferences(node);
|
|
|
|
if (modulePath.length > 0) {
|
|
|
|
let maxIndex = 0;
|
|
|
|
let currUp = node.getEnclosingModule();
|
|
|
|
outer: for (;;) {
|
|
|
|
let currDown: SourceFile | ModuleDeclaration = currUp;
|
|
|
|
for (let i = 0; i < modulePath.length; i++) {
|
|
|
|
const moduleName = modulePath[i];
|
|
|
|
const nextDown = currDown.resolveModule(moduleName.text);
|
|
|
|
if (nextDown === null) {
|
|
|
|
if (currUp.kind === SyntaxKind.SourceFile) {
|
2023-02-03 17:51:27 +01:00
|
|
|
if (emitDiagnostic) {
|
|
|
|
this.diagnostics.add(
|
|
|
|
new ModuleNotFoundDiagnostic(
|
|
|
|
modulePath.slice(maxIndex).map(id => id.text),
|
|
|
|
modulePath[maxIndex],
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-09-18 19:33:34 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
currUp = currUp.getEnclosingModule();
|
|
|
|
continue outer;
|
|
|
|
}
|
|
|
|
maxIndex = Math.max(maxIndex, i+1);
|
|
|
|
currDown = nextDown;
|
|
|
|
}
|
|
|
|
const found = currDown.kindEnv!.get(name.text);
|
|
|
|
if (found !== null) {
|
|
|
|
return found;
|
|
|
|
}
|
2023-02-03 17:51:27 +01:00
|
|
|
if (emitDiagnostic) {
|
|
|
|
this.diagnostics.add(
|
|
|
|
new BindingNotFoundDiagnostic(
|
|
|
|
modulePath.map(id => id.text),
|
|
|
|
name.text,
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-09-18 19:33:34 +02:00
|
|
|
return null;
|
|
|
|
}
|
2022-09-18 14:33:22 +02:00
|
|
|
} else {
|
2022-09-18 19:33:34 +02:00
|
|
|
let curr: KindEnv | null = env;
|
|
|
|
do {
|
|
|
|
const found = curr.get(name.text);
|
|
|
|
if (found !== null) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
curr = curr.parent;
|
|
|
|
} while(curr !== null);
|
2023-02-03 17:51:27 +01:00
|
|
|
if (emitDiagnostic) {
|
|
|
|
this.diagnostics.add(
|
|
|
|
new BindingNotFoundDiagnostic(
|
|
|
|
[],
|
|
|
|
name.text,
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-09-18 19:33:34 +02:00
|
|
|
return null;
|
2022-09-18 14:33:22 +02:00
|
|
|
}
|
2022-09-18 19:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private lookup(node: NodeWithReference, expectedKind: Symkind): Scheme | null {
|
|
|
|
const [modulePath, name] = splitReferences(node);
|
2022-09-18 14:33:22 +02:00
|
|
|
if (modulePath.length > 0) {
|
|
|
|
let maxIndex = 0;
|
|
|
|
let currUp = node.getEnclosingModule();
|
|
|
|
outer: for (;;) {
|
|
|
|
let currDown: SourceFile | ModuleDeclaration = currUp;
|
|
|
|
for (let i = 0; i < modulePath.length; i++) {
|
|
|
|
const moduleName = modulePath[i];
|
|
|
|
const nextDown = currDown.resolveModule(moduleName.text);
|
|
|
|
if (nextDown === null) {
|
|
|
|
if (currUp.kind === SyntaxKind.SourceFile) {
|
|
|
|
this.diagnostics.add(
|
|
|
|
new ModuleNotFoundDiagnostic(
|
|
|
|
modulePath.slice(maxIndex).map(id => id.text),
|
|
|
|
modulePath[maxIndex],
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
currUp = currUp.getEnclosingModule();
|
|
|
|
continue outer;
|
|
|
|
}
|
|
|
|
maxIndex = Math.max(maxIndex, i+1);
|
|
|
|
currDown = nextDown;
|
|
|
|
}
|
|
|
|
const found = currDown.typeEnv!.get(name.text, expectedKind);
|
|
|
|
if (found !== null) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
this.diagnostics.add(
|
2023-02-03 17:51:27 +01:00
|
|
|
new BindingNotFoundDiagnostic(
|
2022-09-18 14:33:22 +02:00
|
|
|
modulePath.map(id => id.text),
|
|
|
|
name.text,
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let curr: TypeEnv | null = this.getContext().env;
|
|
|
|
do {
|
|
|
|
const found = curr.get(name.text, expectedKind);
|
|
|
|
if (found !== null) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
curr = curr.parent;
|
|
|
|
} while(curr !== null);
|
2022-09-18 19:33:34 +02:00
|
|
|
this.diagnostics.add(
|
2023-02-03 17:51:27 +01:00
|
|
|
new BindingNotFoundDiagnostic(
|
2022-09-18 19:33:34 +02:00
|
|
|
[],
|
|
|
|
name.text,
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return null;
|
2022-09-18 14:33:22 +02:00
|
|
|
}
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private getReturnType(): Type {
|
2022-09-16 11:31:34 +02:00
|
|
|
const context = this.getContext();
|
|
|
|
assert(context.returnType !== null);
|
2022-09-05 17:25:55 +02:00
|
|
|
return context.returnType;
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 11:20:21 +02:00
|
|
|
private createSubstitution(scheme: Scheme): TVSub {
|
2022-08-31 13:29:56 +02:00
|
|
|
const sub = new TVSub();
|
2022-09-10 16:52:14 +02:00
|
|
|
for (const tv of scheme.typeVars) {
|
2022-08-31 13:29:56 +02:00
|
|
|
sub.set(tv, this.createTypeVar());
|
|
|
|
}
|
2022-09-11 11:20:21 +02:00
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
private instantiate(scheme: Scheme, node: Syntax | null, sub = this.createSubstitution(scheme)): Type {
|
2022-08-31 13:29:56 +02:00
|
|
|
for (const constraint of scheme.constraints) {
|
2022-09-06 11:56:17 +02:00
|
|
|
const substituted = constraint.substitute(sub);
|
|
|
|
substituted.node = node;
|
|
|
|
substituted.prevInstantiation = constraint;
|
|
|
|
this.addConstraint(substituted);
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
return scheme.type.substitute(sub);
|
|
|
|
}
|
|
|
|
|
2022-09-15 11:49:53 +02:00
|
|
|
private addBinding(name: string, scheme: Scheme, kind: Symkind): void {
|
2022-09-18 14:33:22 +02:00
|
|
|
this.getContext().env.add(name, scheme, kind);
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
private unifyKindMany(first: Kind, rest: Kind[], node: TypeExpression): boolean {
|
|
|
|
return rest.every(kind => this.unifyKind(kind, first, node));
|
|
|
|
}
|
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
private inferKindFromTypeExpression(node: TypeExpression, env: KindEnv): Kind {
|
2023-03-11 14:24:02 +01:00
|
|
|
|
|
|
|
// Store the resluting kind in this variable whenever we didn't encounter
|
|
|
|
// any errors and wish to proceed with type inference on this node.
|
|
|
|
let kind: Kind | undefined;
|
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
switch (node.kind) {
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-16 12:43:06 +02:00
|
|
|
case SyntaxKind.TupleTypeExpression:
|
|
|
|
{
|
2023-03-11 14:24:02 +01:00
|
|
|
if (this.unifyKindMany(kindOfTypes, node.elements.map(el => this.inferKindFromTypeExpression(el, env)), node)) {
|
|
|
|
kind = kindOfTypes;
|
2022-09-16 12:43:06 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-15 16:04:02 +02:00
|
|
|
case SyntaxKind.ArrowTypeExpression:
|
|
|
|
{
|
2023-03-11 14:24:02 +01:00
|
|
|
if (node.paramTypeExprs.every(param => this.unifyKind(kindOfTypes, this.inferKindFromTypeExpression(param, env), node))
|
|
|
|
&& this.unifyKind(kindOfTypes, this.inferKindFromTypeExpression(node.returnTypeExpr, env), node)) {
|
|
|
|
kind = kindOfTypes;
|
2022-09-15 16:04:02 +02:00
|
|
|
}
|
2022-09-15 22:39:20 +02:00
|
|
|
break;
|
2022-09-15 16:04:02 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
case SyntaxKind.ReferenceTypeExpression:
|
|
|
|
{
|
2022-09-18 19:33:34 +02:00
|
|
|
const matchedKind = this.lookupKind(env, node);
|
2023-03-11 14:24:02 +01:00
|
|
|
if (matchedKind !== null) {
|
2022-09-18 14:33:22 +02:00
|
|
|
kind = matchedKind;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-18 14:33:22 +02:00
|
|
|
case SyntaxKind.VarTypeExpression:
|
|
|
|
{
|
2023-02-03 17:51:27 +01:00
|
|
|
const matchedKind = this.lookupKind(env, node.name, false);
|
2023-03-11 14:24:02 +01:00
|
|
|
// If no kind is associated to the type variable with the given name,
|
|
|
|
// we can assign a fresh kind variable to the type variable. Next time,
|
|
|
|
// the type variable will remember whatever unified with it in-between.
|
2022-09-18 14:33:22 +02:00
|
|
|
if (matchedKind === null) {
|
|
|
|
kind = this.createKindVar();
|
2023-02-03 17:51:27 +01:00
|
|
|
env.set(node.name.text, kind);
|
2022-09-15 22:39:20 +02:00
|
|
|
} else {
|
|
|
|
kind = matchedKind;
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
2022-09-15 22:39:20 +02:00
|
|
|
break;
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
case SyntaxKind.AppTypeExpression:
|
|
|
|
{
|
2022-09-15 22:39:20 +02:00
|
|
|
kind = this.inferKindFromTypeExpression(node.operator, env);
|
2022-09-14 22:34:53 +02:00
|
|
|
for (const arg of node.args) {
|
2022-09-15 22:39:20 +02:00
|
|
|
kind = this.applyKind(kind, this.inferKindFromTypeExpression(arg, env), node);
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
2022-09-15 22:39:20 +02:00
|
|
|
break;
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
case SyntaxKind.NestedTypeExpression:
|
|
|
|
{
|
2022-09-15 22:39:20 +02:00
|
|
|
kind = this.inferKindFromTypeExpression(node.typeExpr, env);
|
|
|
|
break;
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
default:
|
|
|
|
throw new Error(`Unexpected ${node}`);
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
|
|
|
// We store the kind on the node so there is a one-to-one correspondence
|
|
|
|
// and this way the kind can be refrieved very efficiently.
|
|
|
|
// Note that at this point `kind` may be undefined. This signals further
|
|
|
|
// inference logic that this node should be skipped because it already contains errors.
|
2022-09-15 22:39:20 +02:00
|
|
|
node.inferredKind = kind;
|
2023-03-11 14:24:02 +01:00
|
|
|
|
|
|
|
// Set a filler default for the node in a way that allows other unification
|
|
|
|
// errors to be caught.
|
|
|
|
if (kind === undefined) {
|
|
|
|
kind = this.createKindVar();
|
|
|
|
}
|
|
|
|
|
2022-09-15 22:39:20 +02:00
|
|
|
return kind;
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private createKindVar(): KVar {
|
|
|
|
return new KVar(this.nextKindVarId++);
|
|
|
|
}
|
|
|
|
|
|
|
|
private applyKind(operator: Kind, arg: Kind, node: Syntax): Kind {
|
|
|
|
switch (operator.type) {
|
|
|
|
case KindType.Var:
|
|
|
|
{
|
|
|
|
const a1 = this.createKindVar();
|
|
|
|
const a2 = this.createKindVar();
|
|
|
|
const arrow = new KArrow(a1, a2);
|
|
|
|
this.unifyKind(arrow, operator, node);
|
2022-09-15 20:46:07 +02:00
|
|
|
this.unifyKind(a1, arg, node);
|
|
|
|
return a2;
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
case KindType.Arrow:
|
|
|
|
{
|
|
|
|
// Unify the argument to the operator's argument kind and return
|
|
|
|
// whatever the operator returns.
|
|
|
|
this.unifyKind(operator.left, arg, node);
|
|
|
|
return operator.right;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
default:
|
2022-09-14 16:46:30 +02:00
|
|
|
{
|
|
|
|
this.diagnostics.add(
|
|
|
|
new KindMismatchDiagnostic(
|
|
|
|
operator,
|
|
|
|
new KArrow(
|
|
|
|
this.createKindVar(),
|
|
|
|
this.createKindVar()
|
|
|
|
),
|
|
|
|
node
|
|
|
|
)
|
|
|
|
);
|
|
|
|
// Create a filler kind variable that still will be able to catch other errors.
|
2023-03-11 14:24:02 +01:00
|
|
|
return this.createKindVar();
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private forwardDeclareKind(node: Syntax, env: KindEnv): void {
|
|
|
|
switch (node.kind) {
|
2022-09-18 14:33:22 +02:00
|
|
|
case SyntaxKind.ModuleDeclaration:
|
|
|
|
{
|
|
|
|
const innerEnv = node.kindEnv = new KindEnv(env);
|
|
|
|
for (const element of node.elements) {
|
|
|
|
this.forwardDeclareKind(element, innerEnv);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-09-14 16:46:30 +02:00
|
|
|
case SyntaxKind.SourceFile:
|
|
|
|
{
|
|
|
|
for (const element of node.elements) {
|
|
|
|
this.forwardDeclareKind(element, env);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-09-15 22:39:20 +02:00
|
|
|
case SyntaxKind.TypeDeclaration:
|
|
|
|
{
|
|
|
|
const innerEnv = new KindEnv(env);
|
2023-03-11 14:24:02 +01:00
|
|
|
let kind: Kind = new KType();
|
2022-09-15 22:39:20 +02:00
|
|
|
for (let i = node.varExps.length-1; i >= 0; i--) {
|
|
|
|
const varExpr = node.varExps[i];
|
|
|
|
const paramKind = this.createKindVar();
|
2022-09-18 19:33:34 +02:00
|
|
|
innerEnv.set(varExpr.text, paramKind);
|
2022-09-15 22:39:20 +02:00
|
|
|
kind = new KArrow(paramKind, kind);
|
|
|
|
}
|
2022-09-18 19:33:34 +02:00
|
|
|
env.set(node.name.text, this.inferKindFromTypeExpression(node.typeExpression, innerEnv));
|
2022-09-15 22:39:20 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-09-14 16:46:30 +02:00
|
|
|
case SyntaxKind.StructDeclaration:
|
2022-09-15 11:49:53 +02:00
|
|
|
{
|
2022-09-18 19:33:34 +02:00
|
|
|
env.set(node.name.text, this.createKindVar());
|
2022-09-15 11:49:53 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-09-14 16:46:30 +02:00
|
|
|
case SyntaxKind.EnumDeclaration:
|
|
|
|
{
|
2022-09-18 19:33:34 +02:00
|
|
|
env.set(node.name.text, this.createKindVar());
|
2022-09-14 16:46:30 +02:00
|
|
|
if (node.members !== null) {
|
|
|
|
for (const member of node.members) {
|
2022-09-18 19:33:34 +02:00
|
|
|
env.set(member.name.text, this.createKindVar());
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
private inferKind(node: Syntax, env: KindEnv): void {
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
switch (node.kind) {
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-18 14:33:22 +02:00
|
|
|
case SyntaxKind.ModuleDeclaration:
|
|
|
|
{
|
|
|
|
const innerEnv = node.kindEnv!;
|
|
|
|
for (const element of node.elements) {
|
|
|
|
this.inferKind(element, innerEnv);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2023-02-03 17:51:27 +01:00
|
|
|
case SyntaxKind.ClassDeclaration:
|
|
|
|
case SyntaxKind.InstanceDeclaration:
|
|
|
|
{
|
|
|
|
if (node.constraints !== null) {
|
|
|
|
for (const constraint of node.constraints.constraints) {
|
|
|
|
for (const typeExpr of constraint.types) {
|
2023-03-11 14:24:02 +01:00
|
|
|
this.unifyKind(this.inferKindFromTypeExpression(typeExpr, env), new KType(), typeExpr);
|
2023-02-03 17:51:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const typeExpr of node.constraint.types) {
|
2023-03-11 14:24:02 +01:00
|
|
|
this.unifyKind(this.inferKindFromTypeExpression(typeExpr, env), new KType(), typeExpr);
|
2023-02-03 17:51:27 +01:00
|
|
|
}
|
|
|
|
for (const element of node.elements) {
|
|
|
|
this.inferKind(element, env);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
case SyntaxKind.SourceFile:
|
|
|
|
{
|
|
|
|
for (const element of node.elements) {
|
|
|
|
this.inferKind(element, env);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
case SyntaxKind.StructDeclaration:
|
|
|
|
{
|
2022-09-15 20:33:34 +02:00
|
|
|
const declKind = env.lookup(node.name.text)!;
|
|
|
|
const innerEnv = new KindEnv(env);
|
2023-03-11 14:24:02 +01:00
|
|
|
let kind: Kind = new KType();
|
2022-09-15 20:33:34 +02:00
|
|
|
for (let i = node.varExps.length-1; i >= 0; i--) {
|
|
|
|
const varExpr = node.varExps[i];
|
|
|
|
const paramKind = this.createKindVar();
|
2022-09-18 19:33:34 +02:00
|
|
|
innerEnv.set(varExpr.text, paramKind);
|
2022-09-15 20:33:34 +02:00
|
|
|
kind = new KArrow(paramKind, kind);
|
|
|
|
}
|
|
|
|
this.unifyKind(declKind, kind, node);
|
|
|
|
if (node.fields !== null) {
|
|
|
|
for (const field of node.fields) {
|
2023-03-11 14:24:02 +01:00
|
|
|
this.unifyKind(this.inferKindFromTypeExpression(field.typeExpr, innerEnv), new KType(), field.typeExpr);
|
2022-09-15 20:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-14 16:46:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
case SyntaxKind.EnumDeclaration:
|
|
|
|
{
|
2022-09-15 13:56:58 +02:00
|
|
|
const declKind = env.lookup(node.name.text)!;
|
2022-09-14 16:46:30 +02:00
|
|
|
const innerEnv = new KindEnv(env);
|
2023-03-11 14:24:02 +01:00
|
|
|
let kind: Kind = new KType();
|
2022-09-14 16:46:30 +02:00
|
|
|
// FIXME should I go from right to left or left to right?
|
|
|
|
for (let i = node.varExps.length-1; i >= 0; i--) {
|
|
|
|
const varExpr = node.varExps[i];
|
|
|
|
const paramKind = this.createKindVar();
|
2022-09-18 19:33:34 +02:00
|
|
|
innerEnv.set(varExpr.text, paramKind);
|
2022-09-14 16:46:30 +02:00
|
|
|
kind = new KArrow(paramKind, kind);
|
|
|
|
}
|
|
|
|
this.unifyKind(declKind, kind, node);
|
|
|
|
if (node.members !== null) {
|
|
|
|
for (const member of node.members) {
|
|
|
|
switch (member.kind) {
|
|
|
|
case SyntaxKind.EnumDeclarationTupleElement:
|
|
|
|
{
|
|
|
|
for (const element of member.elements) {
|
2023-03-11 14:24:02 +01:00
|
|
|
this.unifyKind(this.inferKindFromTypeExpression(element, innerEnv), new KType(), element);
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-09-15 22:39:20 +02:00
|
|
|
case SyntaxKind.EnumDeclarationStructElement:
|
|
|
|
{
|
|
|
|
for (const field of member.fields) {
|
2023-03-11 14:24:02 +01:00
|
|
|
this.unifyKind(this.inferKindFromTypeExpression(field.typeExpr, innerEnv), new KType(), field.typeExpr);
|
2022-09-15 22:39:20 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new Error(`Unexpected ${member}`);
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 22:34:53 +02:00
|
|
|
case SyntaxKind.LetDeclaration:
|
|
|
|
{
|
|
|
|
if (node.typeAssert !== null) {
|
2023-03-11 14:24:02 +01:00
|
|
|
this.unifyKind(this.inferKindFromTypeExpression(node.typeAssert.typeExpression, env), new KType(), node.typeAssert.typeExpression);
|
2022-09-14 22:34:53 +02:00
|
|
|
}
|
|
|
|
if (node.body !== null && node.body.kind === SyntaxKind.BlockBody) {
|
2022-09-15 13:56:58 +02:00
|
|
|
const innerEnv = new KindEnv(env);
|
2022-09-14 22:34:53 +02:00
|
|
|
for (const element of node.body.elements) {
|
2022-09-15 13:56:58 +02:00
|
|
|
this.inferKind(element, innerEnv);
|
2022-09-14 22:34:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private unifyKind(a: Kind, b: Kind, node: Syntax): boolean {
|
|
|
|
|
|
|
|
const find = (kind: Kind): Kind => {
|
|
|
|
let curr = kind;
|
|
|
|
while (curr.type === KindType.Var && this.kindSolution.has(curr)) {
|
|
|
|
curr = this.kindSolution.get(curr)!;
|
|
|
|
}
|
|
|
|
// if (kind.type === KindType.Var && ) {
|
|
|
|
// this.kindSolution.set(kind.id, curr);
|
|
|
|
// }
|
|
|
|
return curr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const solve = (kind: Kind) => kind.substitute(this.kindSolution);
|
|
|
|
|
|
|
|
a = find(a);
|
|
|
|
b = find(b);
|
|
|
|
|
|
|
|
if (a.type === KindType.Var) {
|
|
|
|
this.kindSolution.set(a, b);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b.type === KindType.Var) {
|
|
|
|
return this.unifyKind(b, a, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.type === KindType.Star && b.type === KindType.Star) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.type === KindType.Arrow && b.type === KindType.Arrow) {
|
|
|
|
return this.unifyKind(a.left, b.left, node)
|
2022-09-15 20:46:07 +02:00
|
|
|
&& this.unifyKind(a.right, b.right, node);
|
2022-09-14 16:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.diagnostics.add(new KindMismatchDiagnostic(solve(a), solve(b), node));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private infer(node: Syntax): void {
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
switch (node.kind) {
|
|
|
|
|
|
|
|
case SyntaxKind.SourceFile:
|
2022-09-17 13:20:49 +02:00
|
|
|
case SyntaxKind.ModuleDeclaration:
|
2022-08-31 13:29:56 +02:00
|
|
|
{
|
|
|
|
for (const element of node.elements) {
|
|
|
|
this.infer(element);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-03 17:51:27 +01:00
|
|
|
case SyntaxKind.ClassDeclaration:
|
|
|
|
{
|
|
|
|
for (const element of node.elements) {
|
|
|
|
this.infer(element);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SyntaxKind.InstanceDeclaration:
|
|
|
|
{
|
|
|
|
const cls = node.getScope().lookup(node.constraint.name.text, Symkind.Typeclass) as ClassDeclaration | null;
|
|
|
|
if (cls === null) {
|
|
|
|
this.diagnostics.add(new TypeclassNotFoundDiagnostic(node.constraint.name.text, node.constraint.name));
|
|
|
|
}
|
|
|
|
for (const element of node.elements) {
|
|
|
|
this.infer(element);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
case SyntaxKind.ExpressionStatement:
|
|
|
|
{
|
|
|
|
this.inferExpression(node.expression);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-01 20:06:43 +02:00
|
|
|
case SyntaxKind.IfStatement:
|
|
|
|
{
|
|
|
|
for (const cs of node.cases) {
|
|
|
|
if (cs.test !== null) {
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
this.inferExpression(cs.test),
|
|
|
|
this.getBoolType(),
|
|
|
|
cs.test
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for (const element of cs.elements) {
|
|
|
|
this.infer(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
case SyntaxKind.ReturnStatement:
|
|
|
|
{
|
|
|
|
let type;
|
|
|
|
if (node.expression === null) {
|
|
|
|
type = new TTuple([]);
|
|
|
|
} else {
|
|
|
|
type = this.inferExpression(node.expression);
|
|
|
|
}
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
this.getReturnType(),
|
|
|
|
type,
|
|
|
|
node
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SyntaxKind.LetDeclaration:
|
2022-09-07 12:45:38 +02:00
|
|
|
{
|
2022-09-09 22:37:14 +02:00
|
|
|
if (isFunctionDeclarationLike(node)) {
|
2022-09-07 12:45:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-09-09 22:37:14 +02:00
|
|
|
let type;
|
|
|
|
if (node.pattern.kind === SyntaxKind.WrappedOperator) {
|
|
|
|
type = this.createTypeVar();
|
2022-09-15 11:49:53 +02:00
|
|
|
this.addBinding(node.pattern.operator.text, new Forall([], [], type), Symkind.Var);
|
2022-09-09 22:37:14 +02:00
|
|
|
} else {
|
|
|
|
type = this.inferBindings(node.pattern, [], []);
|
|
|
|
}
|
2022-09-07 12:45:38 +02:00
|
|
|
if (node.typeAssert !== null) {
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
this.inferTypeExpression(node.typeAssert.typeExpression),
|
|
|
|
type,
|
|
|
|
node
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (node.body !== null) {
|
|
|
|
switch (node.body.kind) {
|
|
|
|
case SyntaxKind.ExprBody:
|
|
|
|
{
|
|
|
|
const type2 = this.inferExpression(node.body.expression);
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
type,
|
|
|
|
type2,
|
|
|
|
node
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SyntaxKind.BlockBody:
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-11 11:20:21 +02:00
|
|
|
case SyntaxKind.TypeDeclaration:
|
|
|
|
case SyntaxKind.EnumDeclaration:
|
2022-09-07 12:45:38 +02:00
|
|
|
case SyntaxKind.StructDeclaration:
|
2022-08-31 13:29:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-09-07 12:45:38 +02:00
|
|
|
throw new Error(`Unexpected ${node.constructor.name}`);
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public inferExpression(node: Expression): Type {
|
|
|
|
|
|
|
|
switch (node.kind) {
|
|
|
|
|
2022-09-01 20:06:43 +02:00
|
|
|
case SyntaxKind.NestedExpression:
|
|
|
|
return this.inferExpression(node.expression);
|
|
|
|
|
2022-09-16 11:31:34 +02:00
|
|
|
case SyntaxKind.MatchExpression:
|
|
|
|
{
|
|
|
|
let exprType;
|
|
|
|
if (node.expression !== null) {
|
|
|
|
exprType = this.inferExpression(node.expression);
|
|
|
|
} else {
|
|
|
|
exprType = this.createTypeVar();
|
|
|
|
}
|
|
|
|
let resultType: Type = this.createTypeVar();
|
|
|
|
for (const arm of node.arms) {
|
|
|
|
const context = this.getContext();
|
|
|
|
const newEnv = new TypeEnv(context.env);
|
|
|
|
const newContext: InferContext = {
|
|
|
|
constraints: context.constraints,
|
|
|
|
typeVars: context.typeVars,
|
|
|
|
env: newEnv,
|
|
|
|
returnType: context.returnType,
|
|
|
|
};
|
|
|
|
this.pushContext(newContext);
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
this.inferBindings(arm.pattern, [], []),
|
|
|
|
exprType,
|
|
|
|
arm.pattern,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
resultType,
|
|
|
|
this.inferExpression(arm.expression),
|
|
|
|
arm.expression
|
|
|
|
)
|
|
|
|
);
|
|
|
|
this.popContext(newContext);
|
|
|
|
}
|
2022-09-16 11:57:45 +02:00
|
|
|
if (node.expression === null) {
|
2022-09-16 12:00:00 +02:00
|
|
|
resultType = new TArrow(exprType, resultType);
|
2022-09-16 11:31:34 +02:00
|
|
|
}
|
|
|
|
return resultType;
|
|
|
|
}
|
|
|
|
|
2022-09-16 12:43:06 +02:00
|
|
|
case SyntaxKind.TupleExpression:
|
|
|
|
return new TTuple(node.elements.map(el => this.inferExpression(el)), node);
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
case SyntaxKind.ReferenceExpression:
|
|
|
|
{
|
2022-09-05 17:25:55 +02:00
|
|
|
const scope = node.getScope();
|
2022-09-15 20:33:34 +02:00
|
|
|
const target = scope.lookup(node.name.text);
|
2022-09-18 14:33:22 +02:00
|
|
|
if (target !== null && target.kind === SyntaxKind.LetDeclaration && target.activeCycle) {
|
2022-09-15 22:39:20 +02:00
|
|
|
return target.inferredType!;
|
2022-09-01 20:06:43 +02:00
|
|
|
}
|
2022-09-18 14:33:22 +02:00
|
|
|
const scheme = this.lookup(node, Symkind.Var);
|
2022-08-31 13:29:56 +02:00
|
|
|
if (scheme === null) {
|
2022-09-18 14:33:22 +02:00
|
|
|
// this.diagnostics.add(new BindingNotFoudDiagnostic(node.name.text, node.name));
|
2022-09-09 22:37:14 +02:00
|
|
|
return this.createTypeVar();
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
2022-09-09 20:02:35 +02:00
|
|
|
const type = this.instantiate(scheme, node);
|
|
|
|
type.node = node;
|
|
|
|
return type;
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2022-09-09 00:00:28 +02:00
|
|
|
case SyntaxKind.MemberExpression:
|
|
|
|
{
|
|
|
|
let type = this.inferExpression(node.expression);
|
|
|
|
for (const [_dot, name] of node.path) {
|
2023-03-11 14:24:02 +01:00
|
|
|
const newFieldType = this.createTypeVar(name);
|
|
|
|
const newRestType = this.createTypeVar();
|
2022-09-09 00:00:28 +02:00
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
type,
|
2023-03-11 14:24:02 +01:00
|
|
|
new TField(name.text, new TPresent(newFieldType), newRestType, name),
|
2022-09-09 00:00:28 +02:00
|
|
|
node,
|
|
|
|
)
|
|
|
|
);
|
2023-03-11 14:24:02 +01:00
|
|
|
type = newFieldType;
|
2022-09-09 00:00:28 +02:00
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
case SyntaxKind.CallExpression:
|
|
|
|
{
|
|
|
|
const opType = this.inferExpression(node.func);
|
2023-03-11 14:24:02 +01:00
|
|
|
const retType = this.createTypeVar(node);
|
2022-08-31 13:29:56 +02:00
|
|
|
const paramTypes = [];
|
|
|
|
for (const arg of node.args) {
|
|
|
|
paramTypes.push(this.inferExpression(arg));
|
|
|
|
}
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
opType,
|
2022-09-16 12:00:00 +02:00
|
|
|
TArrow.build(paramTypes, retType),
|
2022-08-31 13:29:56 +02:00
|
|
|
node
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return retType;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SyntaxKind.ConstantExpression:
|
|
|
|
{
|
|
|
|
let ty;
|
|
|
|
switch (node.token.kind) {
|
|
|
|
case SyntaxKind.StringLiteral:
|
|
|
|
ty = this.getStringType();
|
|
|
|
break;
|
|
|
|
case SyntaxKind.Integer:
|
|
|
|
ty = this.getIntType();
|
|
|
|
break;
|
|
|
|
}
|
2022-09-09 20:02:35 +02:00
|
|
|
ty = ty.shallowClone();
|
|
|
|
ty.node = node;
|
2022-08-31 13:29:56 +02:00
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2022-09-07 12:45:38 +02:00
|
|
|
case SyntaxKind.StructExpression:
|
|
|
|
{
|
2023-03-11 14:24:02 +01:00
|
|
|
let type: Type = new TNil(node);
|
2022-09-07 12:45:38 +02:00
|
|
|
for (const member of node.members) {
|
|
|
|
switch (member.kind) {
|
|
|
|
case SyntaxKind.StructExpressionField:
|
|
|
|
{
|
2023-03-11 14:24:02 +01:00
|
|
|
type = new TField(member.name.text, new TPresent(this.inferExpression(member.expression)), type, node);
|
2022-09-07 12:45:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SyntaxKind.PunnedStructExpressionField:
|
|
|
|
{
|
2022-09-18 14:33:22 +02:00
|
|
|
const scheme = this.lookup(member.name, Symkind.Var);
|
2022-09-07 12:45:38 +02:00
|
|
|
let fieldType;
|
|
|
|
if (scheme === null) {
|
2022-09-18 14:33:22 +02:00
|
|
|
// this.diagnostics.add(new BindingNotFoudDiagnostic(member.name.text, member.name));
|
2022-09-09 22:37:14 +02:00
|
|
|
fieldType = this.createTypeVar();
|
2022-09-07 12:45:38 +02:00
|
|
|
} else {
|
|
|
|
fieldType = this.instantiate(scheme, member);
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
type = new TField(member.name.text, new TPresent(fieldType), type, node);
|
2022-09-07 12:45:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new Error(`Unexpected ${member}`);
|
|
|
|
}
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
return TField.sort(type);
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
case SyntaxKind.InfixExpression:
|
|
|
|
{
|
2022-09-18 14:33:22 +02:00
|
|
|
const scheme = this.lookup(node.operator, Symkind.Var);
|
2022-08-31 13:29:56 +02:00
|
|
|
if (scheme === null) {
|
2022-09-18 14:33:22 +02:00
|
|
|
// this.diagnostics.add(new BindingNotFoudDiagnostic(node.operator.text, node.operator));
|
2022-09-09 22:37:14 +02:00
|
|
|
return this.createTypeVar();
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
2022-09-06 11:56:17 +02:00
|
|
|
const opType = this.instantiate(scheme, node.operator);
|
2022-08-31 13:29:56 +02:00
|
|
|
const retType = this.createTypeVar();
|
|
|
|
const leftType = this.inferExpression(node.left);
|
|
|
|
const rightType = this.inferExpression(node.right);
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
2022-09-16 12:00:00 +02:00
|
|
|
new TArrow(leftType, new TArrow(rightType, retType)),
|
2022-08-31 13:29:56 +02:00
|
|
|
opType,
|
|
|
|
node,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return retType;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2022-09-01 20:06:43 +02:00
|
|
|
throw new Error(`Unexpected ${node.constructor.name}`);
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-10 16:52:14 +02:00
|
|
|
public inferTypeExpression(node: TypeExpression, introduceTypeVars = false): Type {
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2022-09-15 22:39:20 +02:00
|
|
|
let type;
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
if (!node.inferredKind) {
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2022-09-15 22:39:20 +02:00
|
|
|
type = this.createTypeVar();
|
2022-09-14 22:34:53 +02:00
|
|
|
|
2022-09-15 22:39:20 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
switch (node.kind) {
|
|
|
|
|
|
|
|
case SyntaxKind.ReferenceTypeExpression:
|
|
|
|
{
|
2022-09-18 14:33:22 +02:00
|
|
|
const scheme = this.lookup(node, Symkind.Type);
|
2022-09-15 22:39:20 +02:00
|
|
|
if (scheme === null) {
|
2022-09-18 14:33:22 +02:00
|
|
|
// this.diagnostics.add(new BindingNotFoudDiagnostic(node.name.text, node.name));
|
2022-09-19 14:06:12 +02:00
|
|
|
type = this.createTypeVar();
|
|
|
|
} else {
|
|
|
|
type = this.instantiate(scheme, node.name);
|
|
|
|
// It is not guaranteed that `type` is copied during instantiation,
|
|
|
|
// so the following check ensures that we really are holding a copy
|
|
|
|
// that we can mutate.
|
|
|
|
if (type === scheme.type) {
|
|
|
|
type = type.shallowClone();
|
|
|
|
}
|
|
|
|
type.node = node;
|
2022-09-15 23:05:34 +02:00
|
|
|
}
|
2022-09-15 22:39:20 +02:00
|
|
|
break;
|
2022-09-10 16:52:14 +02:00
|
|
|
}
|
|
|
|
|
2022-09-16 12:43:06 +02:00
|
|
|
case SyntaxKind.TupleTypeExpression:
|
|
|
|
{
|
|
|
|
type = new TTuple(node.elements.map(el => this.inferTypeExpression(el)), node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-15 22:39:20 +02:00
|
|
|
case SyntaxKind.NestedTypeExpression:
|
2022-09-19 14:06:12 +02:00
|
|
|
type = this.inferTypeExpression(node.typeExpr, introduceTypeVars);
|
|
|
|
break;
|
2022-09-11 11:20:21 +02:00
|
|
|
|
2022-09-15 22:39:20 +02:00
|
|
|
case SyntaxKind.VarTypeExpression:
|
|
|
|
{
|
2022-09-18 14:33:22 +02:00
|
|
|
const scheme = this.lookup(node.name, Symkind.Type);
|
2022-09-15 22:39:20 +02:00
|
|
|
if (scheme === null) {
|
|
|
|
if (!introduceTypeVars) {
|
2022-09-18 14:33:22 +02:00
|
|
|
// this.diagnostics.add(new BindingNotFoudDiagnostic(node.name.text, node.name));
|
2022-09-15 22:39:20 +02:00
|
|
|
}
|
|
|
|
type = this.createTypeVar();
|
|
|
|
this.addBinding(node.name.text, new Forall([], [], type), Symkind.Type);
|
|
|
|
} else {
|
|
|
|
assert(isEmpty(scheme.typeVars));
|
|
|
|
assert(isEmpty(scheme.constraints));
|
|
|
|
type = scheme.type;
|
|
|
|
}
|
|
|
|
break;
|
2022-09-09 22:37:14 +02:00
|
|
|
}
|
|
|
|
|
2022-09-15 22:39:20 +02:00
|
|
|
case SyntaxKind.AppTypeExpression:
|
|
|
|
{
|
|
|
|
type = TApp.build(
|
|
|
|
this.inferTypeExpression(node.operator, introduceTypeVars),
|
|
|
|
node.args.map(arg => this.inferTypeExpression(arg, introduceTypeVars)),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SyntaxKind.ArrowTypeExpression:
|
|
|
|
{
|
|
|
|
const paramTypes = [];
|
|
|
|
for (const paramTypeExpr of node.paramTypeExprs) {
|
|
|
|
paramTypes.push(this.inferTypeExpression(paramTypeExpr, introduceTypeVars));
|
|
|
|
}
|
|
|
|
const returnType = this.inferTypeExpression(node.returnTypeExpr, introduceTypeVars);
|
2022-09-16 12:00:00 +02:00
|
|
|
type = TArrow.build(paramTypes, returnType, node);
|
2022-09-15 22:39:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error(`Unrecognised ${node}`);
|
|
|
|
|
|
|
|
}
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-15 22:39:20 +02:00
|
|
|
node.inferredType = type;
|
|
|
|
|
|
|
|
return type;
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2022-09-15 16:56:02 +02:00
|
|
|
public inferBindings(pattern: Pattern, typeVars: Iterable<TVar>, constraints: Iterable<Constraint>): Type {
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
switch (pattern.kind) {
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
case SyntaxKind.NamedPattern:
|
2022-08-31 13:29:56 +02:00
|
|
|
{
|
2022-09-07 12:45:38 +02:00
|
|
|
const type = this.createTypeVar();
|
2022-09-15 11:49:53 +02:00
|
|
|
this.addBinding(pattern.name.text, new Forall(typeVars, constraints, type), Symkind.Var);
|
2022-09-07 12:45:38 +02:00
|
|
|
return type;
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
case SyntaxKind.NestedPattern:
|
|
|
|
return this.inferBindings(pattern.pattern, typeVars, constraints);
|
|
|
|
|
2023-02-03 17:51:27 +01:00
|
|
|
case SyntaxKind.NamedTuplePattern:
|
|
|
|
{
|
|
|
|
const scheme = this.lookup(pattern.name, Symkind.Type);
|
|
|
|
if (scheme === null) {
|
|
|
|
return this.createTypeVar();
|
|
|
|
}
|
|
|
|
let tupleType = pattern.elements.map(p => this.inferBindings(p, typeVars, constraints));
|
|
|
|
// FIXME not tested
|
|
|
|
return TApp.build(
|
|
|
|
new TNominal(scheme.type.node as StructDeclaration | EnumDeclaration, pattern),
|
|
|
|
tupleType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-16 11:31:34 +02:00
|
|
|
case SyntaxKind.LiteralPattern:
|
|
|
|
{
|
|
|
|
let type;
|
|
|
|
switch (pattern.token.kind) {
|
|
|
|
case SyntaxKind.Integer:
|
|
|
|
type = this.getIntType();
|
|
|
|
break;
|
|
|
|
case SyntaxKind.StringLiteral:
|
|
|
|
type = this.getStringType();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
type = type.shallowClone();
|
|
|
|
type.node = pattern;
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SyntaxKind.DisjunctivePattern:
|
|
|
|
{
|
|
|
|
const type = this.createTypeVar();
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
this.inferBindings(pattern.left, typeVars, constraints),
|
|
|
|
type,
|
|
|
|
pattern.left
|
|
|
|
)
|
|
|
|
);
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
this.inferBindings(pattern.right, typeVars, constraints),
|
|
|
|
type,
|
|
|
|
pattern.left
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2022-09-07 12:45:38 +02:00
|
|
|
case SyntaxKind.StructPattern:
|
|
|
|
{
|
2023-03-11 14:24:02 +01:00
|
|
|
const variadicMember = getVariadicMember(pattern);
|
|
|
|
let type: Type;
|
|
|
|
if (variadicMember === null) {
|
|
|
|
type = new TNil(pattern);
|
|
|
|
} else if (variadicMember.pattern === null) {
|
|
|
|
type = this.createTypeVar();
|
2022-09-07 12:45:38 +02:00
|
|
|
} else {
|
2023-03-11 14:24:02 +01:00
|
|
|
type = this.inferBindings(variadicMember.pattern, typeVars, constraints);
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
|
|
|
for (const member of pattern.members) {
|
|
|
|
switch (member.kind) {
|
|
|
|
case SyntaxKind.StructPatternField:
|
|
|
|
{
|
|
|
|
const fieldType = this.inferBindings(member.pattern, typeVars, constraints);
|
2023-03-11 14:24:02 +01:00
|
|
|
type = new TField(member.name.text, new TPresent(fieldType), type, pattern);
|
2022-09-07 12:45:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SyntaxKind.PunnedStructPatternField:
|
|
|
|
{
|
|
|
|
const fieldType = this.createTypeVar();
|
2022-09-15 11:49:53 +02:00
|
|
|
this.addBinding(member.name.text, new Forall([], [], fieldType), Symkind.Var);
|
2023-03-11 14:24:02 +01:00
|
|
|
type = new TField(member.name.text, new TPresent(fieldType), type, pattern);
|
2022-09-07 12:45:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
case SyntaxKind.VariadicStructPatternElement:
|
|
|
|
break;
|
2022-09-07 12:45:38 +02:00
|
|
|
default:
|
2023-03-11 14:24:02 +01:00
|
|
|
assertNever(member);
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
return TField.sort(type);
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error(`Unexpected ${pattern.constructor.name}`);
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-07 12:45:38 +02:00
|
|
|
private initialize(node: Syntax, parentEnv: TypeEnv): void {
|
2022-09-05 19:33:08 +02:00
|
|
|
|
|
|
|
switch (node.kind) {
|
|
|
|
|
|
|
|
case SyntaxKind.SourceFile:
|
2022-09-17 13:20:49 +02:00
|
|
|
case SyntaxKind.ModuleDeclaration:
|
2022-09-05 19:33:08 +02:00
|
|
|
{
|
2022-09-07 12:45:38 +02:00
|
|
|
const env = node.typeEnv = new TypeEnv(parentEnv);
|
2022-09-05 19:33:08 +02:00
|
|
|
for (const element of node.elements) {
|
2022-09-07 12:45:38 +02:00
|
|
|
this.initialize(element, env);
|
2022-09-05 19:33:08 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-03 17:51:27 +01:00
|
|
|
case SyntaxKind.InstanceDeclaration:
|
|
|
|
case SyntaxKind.ClassDeclaration:
|
|
|
|
{
|
|
|
|
for (const element of node.elements) {
|
|
|
|
this.initialize(element, parentEnv);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-05 19:33:08 +02:00
|
|
|
case SyntaxKind.LetDeclaration:
|
|
|
|
{
|
|
|
|
const env = node.typeEnv = new TypeEnv(parentEnv);
|
|
|
|
if (node.body !== null && node.body.kind === SyntaxKind.BlockBody) {
|
|
|
|
for (const element of node.body.elements) {
|
|
|
|
this.initialize(element, env);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:17:27 +02:00
|
|
|
case SyntaxKind.IfStatement:
|
2022-09-05 19:33:08 +02:00
|
|
|
case SyntaxKind.ExpressionStatement:
|
|
|
|
case SyntaxKind.ReturnStatement:
|
2022-09-07 12:45:38 +02:00
|
|
|
break;
|
|
|
|
|
2022-09-10 16:52:14 +02:00
|
|
|
case SyntaxKind.EnumDeclaration:
|
|
|
|
{
|
2022-09-14 22:34:53 +02:00
|
|
|
const env = node.typeEnv = new TypeEnv(parentEnv);
|
|
|
|
const constraints = new ConstraintSet();
|
|
|
|
const typeVars = new TVSet();
|
|
|
|
const context: InferContext = {
|
|
|
|
typeVars,
|
|
|
|
env,
|
|
|
|
constraints,
|
|
|
|
returnType: null,
|
|
|
|
}
|
|
|
|
this.pushContext(context);
|
|
|
|
const kindArgs = [];
|
|
|
|
for (const varExpr of node.varExps) {
|
|
|
|
const kindArg = this.createTypeVar();
|
2022-09-15 11:49:53 +02:00
|
|
|
env.add(varExpr.text, new Forall([], [], kindArg), Symkind.Type);
|
2022-09-14 22:34:53 +02:00
|
|
|
kindArgs.push(kindArg);
|
|
|
|
}
|
|
|
|
let elementTypes: Type[] = [];
|
2022-09-15 20:33:34 +02:00
|
|
|
const type = new TNominal(node, node);
|
2022-09-14 22:34:53 +02:00
|
|
|
if (node.members !== null) {
|
|
|
|
for (const member of node.members) {
|
2023-03-11 14:24:02 +01:00
|
|
|
let ctorType;
|
2022-09-14 22:34:53 +02:00
|
|
|
switch (member.kind) {
|
|
|
|
case SyntaxKind.EnumDeclarationTupleElement:
|
|
|
|
{
|
|
|
|
const argTypes = member.elements.map(el => this.inferTypeExpression(el));
|
2023-03-11 14:24:02 +01:00
|
|
|
ctorType = TArrow.build(argTypes, TApp.build(type, kindArgs), member);
|
2022-09-15 20:33:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SyntaxKind.EnumDeclarationStructElement:
|
|
|
|
{
|
2023-03-11 14:24:02 +01:00
|
|
|
ctorType = new TNil(member);
|
2022-09-15 20:33:34 +02:00
|
|
|
for (const field of member.fields) {
|
2023-03-11 14:24:02 +01:00
|
|
|
ctorType = new TField(field.name.text, new TPresent(this.inferTypeExpression(field.typeExpr)), ctorType, member);
|
2022-09-15 20:33:34 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
ctorType = new TArrow(TField.sort(ctorType), TApp.build(type, kindArgs));
|
2022-09-14 22:34:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new Error(`Unexpected ${member}`);
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
parentEnv.add(member.name.text, new Forall(typeVars, constraints, ctorType), Symkind.Var);
|
|
|
|
elementTypes.push(ctorType);
|
2022-09-14 22:34:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.popContext(context);
|
2022-09-15 11:49:53 +02:00
|
|
|
parentEnv.add(node.name.text, new Forall(typeVars, constraints, type), Symkind.Type);
|
2022-09-10 16:52:14 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-11 11:20:21 +02:00
|
|
|
case SyntaxKind.TypeDeclaration:
|
|
|
|
{
|
|
|
|
const env = node.typeEnv = new TypeEnv(parentEnv);
|
|
|
|
const constraints = new ConstraintSet();
|
|
|
|
const typeVars = new TVSet();
|
|
|
|
const context: InferContext = {
|
|
|
|
constraints,
|
|
|
|
typeVars,
|
|
|
|
env,
|
|
|
|
returnType: null,
|
|
|
|
};
|
|
|
|
this.pushContext(context);
|
2022-09-15 20:33:34 +02:00
|
|
|
const kindArgs = [];
|
2022-09-15 11:49:53 +02:00
|
|
|
for (const varExpr of node.varExps) {
|
2022-09-15 20:33:34 +02:00
|
|
|
const typeVar = this.createTypeVar();
|
|
|
|
kindArgs.push(typeVar);
|
|
|
|
env.add(varExpr.text, new Forall([], [], typeVar), Symkind.Type);
|
2022-09-11 11:20:21 +02:00
|
|
|
}
|
|
|
|
const type = this.inferTypeExpression(node.typeExpression);
|
|
|
|
this.popContext(context);
|
2022-09-15 20:33:34 +02:00
|
|
|
const scheme = new Forall(typeVars, constraints, TApp.build(type, kindArgs));
|
2022-09-15 11:49:53 +02:00
|
|
|
parentEnv.add(node.name.text, scheme, Symkind.Type);
|
2022-09-11 11:20:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-05 19:33:08 +02:00
|
|
|
case SyntaxKind.StructDeclaration:
|
2022-09-07 12:45:38 +02:00
|
|
|
{
|
2022-09-10 16:52:14 +02:00
|
|
|
const env = node.typeEnv = new TypeEnv(parentEnv);
|
|
|
|
const typeVars = new TVSet();
|
|
|
|
const constraints = new ConstraintSet();
|
|
|
|
const context: InferContext = {
|
|
|
|
constraints,
|
|
|
|
typeVars,
|
|
|
|
env,
|
|
|
|
returnType: null,
|
|
|
|
};
|
|
|
|
this.pushContext(context);
|
2022-09-15 11:49:53 +02:00
|
|
|
const kindArgs = [];
|
|
|
|
for (const varExpr of node.varExps) {
|
|
|
|
const kindArg = this.createTypeVar();
|
|
|
|
env.add(varExpr.text, new Forall([], [], kindArg), Symkind.Type);
|
|
|
|
kindArgs.push(kindArg);
|
2022-09-10 16:52:14 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
let type: Type = new TNil(node);
|
2022-09-15 20:33:34 +02:00
|
|
|
if (node.fields !== null) {
|
2022-09-15 22:39:20 +02:00
|
|
|
for (const field of node.fields) {
|
2023-03-11 14:24:02 +01:00
|
|
|
type = new TField(field.name.text, new TPresent(this.inferTypeExpression(field.typeExpr)), type, node);
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-10 16:52:14 +02:00
|
|
|
this.popContext(context);
|
2023-03-11 14:24:02 +01:00
|
|
|
parentEnv.add(node.name.text, new Forall(typeVars, constraints, TField.sort(type)), Symkind.Type);
|
|
|
|
//parentEnv.add(node.name.text, new Forall(typeVars, constraints, new TArrow(type, TApp.build(type, kindArgs))), Symkind.Var);
|
2022-09-05 19:33:08 +02:00
|
|
|
break;
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
2022-09-05 19:33:08 +02:00
|
|
|
|
|
|
|
default:
|
2022-09-15 20:33:34 +02:00
|
|
|
throw new Error(`Unexpected ${node.constructor.name}`);
|
2022-09-05 19:33:08 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-01 20:06:43 +02:00
|
|
|
public check(node: SourceFile): void {
|
|
|
|
|
2022-09-14 16:46:30 +02:00
|
|
|
const kenv = new KindEnv();
|
2023-03-11 14:24:02 +01:00
|
|
|
kenv.set('Int', new KType());
|
|
|
|
kenv.set('String', new KType());
|
|
|
|
kenv.set('Bool', new KType());
|
2022-09-18 14:33:22 +02:00
|
|
|
const skenv = new KindEnv(kenv);
|
|
|
|
this.forwardDeclareKind(node, skenv);
|
|
|
|
this.inferKind(node, skenv);
|
2022-09-14 16:46:30 +02:00
|
|
|
|
2022-09-01 20:06:43 +02:00
|
|
|
const typeVars = new TVSet();
|
2022-08-31 13:29:56 +02:00
|
|
|
const constraints = new ConstraintSet();
|
|
|
|
const env = new TypeEnv();
|
2022-09-05 17:25:55 +02:00
|
|
|
const context: InferContext = { typeVars, constraints, env, returnType: null };
|
2022-09-01 20:06:43 +02:00
|
|
|
|
2022-09-05 17:25:55 +02:00
|
|
|
this.pushContext(context);
|
2022-09-01 20:06:43 +02:00
|
|
|
|
|
|
|
const a = this.createTypeVar();
|
2022-09-14 16:46:30 +02:00
|
|
|
const b = this.createTypeVar();
|
2022-09-01 20:06:43 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
env.add('$', new Forall([ a, b ], [], new TArrow(new TArrow(new TArrow(a, b), a), b)), Symkind.Var);
|
2022-09-15 11:49:53 +02:00
|
|
|
env.add('String', new Forall([], [], this.stringType), Symkind.Type);
|
|
|
|
env.add('Int', new Forall([], [], this.intType), Symkind.Type);
|
2022-09-15 13:48:08 +02:00
|
|
|
env.add('Bool', new Forall([], [], this.boolType), Symkind.Type);
|
2022-09-15 11:49:53 +02:00
|
|
|
env.add('True', new Forall([], [], this.boolType), Symkind.Var);
|
|
|
|
env.add('False', new Forall([], [], this.boolType), Symkind.Var);
|
2022-09-16 12:00:00 +02:00
|
|
|
env.add('+', new Forall([], [], TArrow.build([ this.intType, this.intType ], this.intType)), Symkind.Var);
|
|
|
|
env.add('-', new Forall([], [], TArrow.build([ this.intType, this.intType ], this.intType)), Symkind.Var);
|
|
|
|
env.add('*', new Forall([], [], TArrow.build([ this.intType, this.intType ], this.intType)), Symkind.Var);
|
|
|
|
env.add('/', new Forall([], [], TArrow.build([ this.intType, this.intType ], this.intType)), Symkind.Var);
|
|
|
|
env.add('==', new Forall([ a ], [], TArrow.build([ a, a ], this.boolType)), Symkind.Var);
|
|
|
|
env.add('not', new Forall([], [], new TArrow(this.boolType, this.boolType)), Symkind.Var);
|
2022-09-01 20:06:43 +02:00
|
|
|
|
2022-09-05 19:33:08 +02:00
|
|
|
this.initialize(node, env);
|
|
|
|
|
2022-09-07 12:45:38 +02:00
|
|
|
this.pushContext({
|
|
|
|
typeVars,
|
|
|
|
constraints,
|
|
|
|
env: node.typeEnv!,
|
|
|
|
returnType: null
|
|
|
|
});
|
|
|
|
|
2022-09-15 16:04:49 +02:00
|
|
|
const sccs = [...this.analyser.getSortedDeclarations()];
|
2022-09-05 19:33:08 +02:00
|
|
|
|
|
|
|
for (const nodes of sccs) {
|
2022-09-05 17:25:55 +02:00
|
|
|
|
2022-09-06 15:13:07 +02:00
|
|
|
if (nodes.some(n => n.kind === SyntaxKind.SourceFile)) {
|
|
|
|
assert(nodes.length === 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-05 17:25:55 +02:00
|
|
|
const typeVars = new TVSet();
|
|
|
|
const constraints = new ConstraintSet();
|
|
|
|
|
2022-09-01 20:06:43 +02:00
|
|
|
for (const node of nodes) {
|
2022-09-05 17:25:55 +02:00
|
|
|
|
2022-09-06 15:13:07 +02:00
|
|
|
assert(node.kind === SyntaxKind.LetDeclaration);
|
|
|
|
|
2022-09-09 22:37:14 +02:00
|
|
|
if (!isFunctionDeclarationLike(node)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-05 19:33:08 +02:00
|
|
|
const env = node.typeEnv!;
|
2023-03-11 14:24:02 +01:00
|
|
|
const inner: InferContext = {
|
2022-09-05 17:25:55 +02:00
|
|
|
typeVars,
|
|
|
|
constraints,
|
|
|
|
env,
|
|
|
|
returnType: null,
|
|
|
|
};
|
2023-03-11 14:24:02 +01:00
|
|
|
node.context = inner;
|
2022-09-05 17:25:55 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
this.contexts.push(inner);
|
2022-09-05 17:25:55 +02:00
|
|
|
|
|
|
|
const returnType = this.createTypeVar();
|
2023-03-11 14:24:02 +01:00
|
|
|
inner.returnType = returnType;
|
2022-09-05 17:25:55 +02:00
|
|
|
|
2023-02-03 17:51:27 +01:00
|
|
|
const paramTypes = node.params.map(
|
|
|
|
param => this.inferBindings(param.pattern, [], [])
|
|
|
|
);
|
2022-09-05 17:25:55 +02:00
|
|
|
|
2022-09-16 12:00:00 +02:00
|
|
|
let type = TArrow.build(paramTypes, returnType, node);
|
2022-09-05 17:25:55 +02:00
|
|
|
if (node.typeAssert !== null) {
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
2023-02-03 17:51:27 +01:00
|
|
|
this.inferTypeExpression(node.typeAssert.typeExpression, true),
|
2022-09-05 17:25:55 +02:00
|
|
|
type,
|
2022-09-06 13:40:20 +02:00
|
|
|
node
|
2022-09-05 17:25:55 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-09-15 22:39:20 +02:00
|
|
|
node.inferredType = type;
|
2022-09-05 17:25:55 +02:00
|
|
|
|
2023-02-03 17:51:27 +01:00
|
|
|
// if (node.parent!.kind === SyntaxKind.InstanceDeclaration) {
|
|
|
|
// const inst = node.parent!;
|
|
|
|
// const cls = inst.getScope().lookup(node.parent!.constraint.name.text, Symkind.Typeclass) as ClassDeclaration;
|
2023-03-11 14:24:02 +01:00
|
|
|
// const other = cls.lookup(node)! as LetDeclaration;
|
|
|
|
// assert(other.pattern.kind === SyntaxKind.BindPattern);
|
2023-02-03 17:51:27 +01:00
|
|
|
// console.log(describeType(type));
|
2023-03-11 14:24:02 +01:00
|
|
|
// const otherScheme = this.lookup(other.pattern.name, Symkind.Var)!;
|
|
|
|
// addAll(otherScheme.typeVars, typeVars);
|
|
|
|
// constraints.push(...otherScheme.constraints);
|
2023-02-03 17:51:27 +01:00
|
|
|
// this.addConstraint(new CEqual(type, other.inferredType!, node));
|
|
|
|
// }
|
|
|
|
|
2022-09-05 17:25:55 +02:00
|
|
|
this.contexts.pop();
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
if (node.parent!.kind !== SyntaxKind.InstanceDeclaration) {
|
|
|
|
const scopeDecl = node.parent!.getScope().node;
|
|
|
|
const outer = {
|
|
|
|
typeVars: inner.typeVars,
|
|
|
|
constraints: inner.constraints,
|
|
|
|
env: scopeDecl.typeEnv!,
|
|
|
|
returnType: null,
|
|
|
|
};
|
|
|
|
this.contexts.push(outer)
|
|
|
|
let ty2;
|
|
|
|
if (node.pattern.kind === SyntaxKind.WrappedOperator) {
|
|
|
|
ty2 = this.createTypeVar();
|
|
|
|
this.addBinding(node.pattern.operator.text, new Forall([], [], ty2), Symkind.Var);
|
|
|
|
} else {
|
|
|
|
ty2 = this.inferBindings(node.pattern, typeVars, constraints);
|
|
|
|
}
|
|
|
|
this.addConstraint(new CEqual(ty2, type, node));
|
|
|
|
this.contexts.pop();
|
2022-09-09 22:37:14 +02:00
|
|
|
}
|
2022-09-01 20:06:43 +02:00
|
|
|
}
|
2022-09-05 17:25:55 +02:00
|
|
|
|
2022-09-05 19:33:08 +02:00
|
|
|
}
|
|
|
|
|
2022-09-09 22:37:14 +02:00
|
|
|
const visitElements = (elements: Syntax[]) => {
|
2023-02-03 17:51:27 +01:00
|
|
|
|
2022-09-09 22:37:14 +02:00
|
|
|
for (const element of elements) {
|
|
|
|
if (element.kind === SyntaxKind.LetDeclaration
|
2022-09-15 16:04:49 +02:00
|
|
|
&& isFunctionDeclarationLike(element)) {
|
|
|
|
if (!this.analyser.isReferencedInParentScope(element)) {
|
2023-03-11 14:24:02 +01:00
|
|
|
assert(element.pattern.kind === SyntaxKind.NamedPattern);
|
2022-09-18 14:33:22 +02:00
|
|
|
const scheme = this.lookup(element.pattern.name, Symkind.Var);
|
2022-09-15 16:04:49 +02:00
|
|
|
assert(scheme !== null);
|
|
|
|
this.instantiate(scheme, null);
|
|
|
|
}
|
2022-09-09 22:37:14 +02:00
|
|
|
} else {
|
|
|
|
this.infer(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 19:33:08 +02:00
|
|
|
for (const nodes of sccs) {
|
|
|
|
|
2022-09-06 15:13:07 +02:00
|
|
|
if (nodes.some(n => n.kind === SyntaxKind.SourceFile)) {
|
|
|
|
assert(nodes.length === 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-05 19:33:08 +02:00
|
|
|
for (const node of nodes) {
|
2022-09-06 15:13:07 +02:00
|
|
|
assert(node.kind === SyntaxKind.LetDeclaration);
|
2022-09-18 14:33:22 +02:00
|
|
|
node.activeCycle = true;
|
2022-09-05 19:33:08 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 17:25:55 +02:00
|
|
|
for (const node of nodes) {
|
|
|
|
|
2022-09-06 15:13:07 +02:00
|
|
|
assert(node.kind === SyntaxKind.LetDeclaration);
|
|
|
|
|
2022-09-09 22:37:14 +02:00
|
|
|
if (!isFunctionDeclarationLike(node)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-05 17:25:55 +02:00
|
|
|
const context = node.context!;
|
|
|
|
const returnType = context.returnType!;
|
|
|
|
this.contexts.push(context);
|
|
|
|
|
|
|
|
if (node.body !== null) {
|
|
|
|
switch (node.body.kind) {
|
|
|
|
case SyntaxKind.ExprBody:
|
|
|
|
{
|
|
|
|
this.addConstraint(
|
|
|
|
new CEqual(
|
|
|
|
this.inferExpression(node.body.expression),
|
|
|
|
returnType,
|
|
|
|
node.body.expression
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SyntaxKind.BlockBody:
|
|
|
|
{
|
2022-09-09 22:37:14 +02:00
|
|
|
visitElements(node.body.elements);
|
2022-09-05 17:25:55 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.contexts.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const node of nodes) {
|
2022-09-06 15:13:07 +02:00
|
|
|
assert(node.kind === SyntaxKind.LetDeclaration);
|
2022-09-18 14:33:22 +02:00
|
|
|
node.activeCycle = false;
|
2022-09-05 17:25:55 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 20:06:43 +02:00
|
|
|
}
|
2022-09-09 22:37:14 +02:00
|
|
|
|
|
|
|
visitElements(node.elements);
|
2022-09-01 20:06:43 +02:00
|
|
|
|
2022-09-07 12:45:38 +02:00
|
|
|
this.contexts.pop();
|
2022-09-05 17:25:55 +02:00
|
|
|
this.popContext(context);
|
2022-09-01 20:06:43 +02:00
|
|
|
|
2022-09-06 15:13:07 +02:00
|
|
|
this.solve(new CMany(constraints), this.solution);
|
2023-03-11 14:24:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private *findInstanceContext(type: TCon, clazz: ClassDeclaration): Iterable<ClassDeclaration[]> {
|
|
|
|
for (const instance of clazz.getInstances()) {
|
|
|
|
assert(instance.constraint.types.length === 1);
|
|
|
|
const instTy0 = instance.constraint.types[0];
|
|
|
|
if (instTy0.kind === SyntaxKind.AppTypeExpression
|
|
|
|
&& instTy0.operator.kind === SyntaxKind.ReferenceTypeExpression
|
|
|
|
&& instTy0.operator.name.text === type.displayName) {
|
|
|
|
if (instance.constraints === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const argType of type.argTypes) {
|
|
|
|
const classes = [];
|
|
|
|
for (const constraint of instance.constraints.constraints) {
|
|
|
|
assert(constraint.types.length === 1);
|
|
|
|
const classDecl = this.lookupClass(constraint.name);
|
|
|
|
if (classDecl === null) {
|
|
|
|
this.diagnostics.add(new TypeclassNotFoundDiagnostic(constraint.name.text, constraint.name));
|
|
|
|
} else {
|
|
|
|
classes.push(classDecl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yield classes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 17:25:55 +02:00
|
|
|
private solve(constraint: Constraint, solution: TVSub): void {
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
const queue = [ constraint ];
|
|
|
|
|
2022-09-06 15:13:07 +02:00
|
|
|
let errorCount = 0;
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
const find = (type: Type): Type => {
|
|
|
|
while (type.kind === TypeKind.Var && solution.has(type)) {
|
|
|
|
type = solution.get(type)!;
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
while (queue.length > 0) {
|
|
|
|
|
2022-09-09 20:02:35 +02:00
|
|
|
const constraint = queue.shift()!;
|
2022-08-31 13:29:56 +02:00
|
|
|
|
|
|
|
switch (constraint.kind) {
|
|
|
|
|
|
|
|
case ConstraintKind.Many:
|
|
|
|
{
|
|
|
|
for (const element of constraint.elements) {
|
|
|
|
queue.push(element);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ConstraintKind.Equal:
|
|
|
|
{
|
2023-03-11 14:24:02 +01:00
|
|
|
let path: string[] = [];
|
2022-09-11 15:23:22 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
const unifyField = (left: Type, right: Type): boolean => {
|
|
|
|
|
|
|
|
const swap = () => { [right, left] = [left, right]; }
|
|
|
|
|
|
|
|
if (left.kind === TypeKind.Absent && right.kind === TypeKind.Absent) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (right.kind === TypeKind.Absent) {
|
|
|
|
swap();
|
2022-09-06 15:13:07 +02:00
|
|
|
}
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
if (left.kind === TypeKind.Absent) {
|
|
|
|
assert(right.kind === TypeKind.Present);
|
|
|
|
const fieldName = path[path.length-1];
|
|
|
|
this.diagnostics.add(
|
|
|
|
new FieldMissingDiagnostic(fieldName, left.node, right.type.node, constraint.firstNode)
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(left.kind === TypeKind.Present && right.kind === TypeKind.Present);
|
|
|
|
return unify(left.type, right.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
const unify = (left: Type, right: Type): boolean => {
|
|
|
|
|
2022-09-14 22:34:53 +02:00
|
|
|
left = find(left);
|
|
|
|
right = find(right);
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
//console.log(`unify ${describeType(left)} ~ ${describeType(right)}`);
|
|
|
|
|
|
|
|
const swap = () => { [right, left] = [left, right]; }
|
|
|
|
|
|
|
|
if (left.kind !== TypeKind.Var && right.kind === TypeKind.Var) {
|
|
|
|
swap();
|
|
|
|
}
|
|
|
|
|
2022-09-11 15:23:22 +02:00
|
|
|
if (left.kind === TypeKind.Var) {
|
2023-03-11 14:24:02 +01:00
|
|
|
|
|
|
|
// Perform an occurs check, verifying whether left occurs
|
|
|
|
// somewhere inside the structure of right. If so, unification
|
|
|
|
// makes no sense.
|
2022-09-11 15:23:22 +02:00
|
|
|
if (right.hasTypeVar(left)) {
|
2023-03-11 14:24:02 +01:00
|
|
|
// TODO print a diagnostic
|
2022-09-11 15:23:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
|
|
|
|
// We are ready to join the types, so the first thing we do is
|
|
|
|
// propagating the type classes that 'left' requires to 'right'.
|
|
|
|
// If 'right' is another type variable, we're lucky. We just copy
|
|
|
|
// the missing type classes from 'left' to 'right'. Otherwise,
|
|
|
|
const propagateClasses = (classes: Iterable<ClassDeclaration>, type: Type) => {
|
|
|
|
if (type.kind === TypeKind.Var) {
|
|
|
|
for (const constraint of classes) {
|
|
|
|
type.context.add(constraint);
|
|
|
|
}
|
|
|
|
} else if (type.kind === TypeKind.Con) {
|
|
|
|
for (const constraint of classes) {
|
|
|
|
propagateClassTCon(constraint, type);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//this.diagnostics.add(new );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const propagateClassTCon = (clazz: ClassDeclaration, type: TCon) => {
|
|
|
|
const s = this.findInstanceContext(type, clazz);
|
|
|
|
let i = 0;
|
|
|
|
for (const classes of s) {
|
|
|
|
propagateClasses(classes, type.argTypes[i++]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
propagateClasses(left.context, right);
|
|
|
|
|
|
|
|
// We are all clear; set the actual type of left to right.
|
2022-09-11 15:23:22 +02:00
|
|
|
solution.set(left, right);
|
2023-03-11 14:24:02 +01:00
|
|
|
|
|
|
|
// These types will be join, and we'd like to track that
|
|
|
|
// into a special chain.
|
2022-09-11 15:23:22 +02:00
|
|
|
TypeBase.join(left, right);
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
if (left.node !== undefined) {
|
|
|
|
right.node = left.node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2022-09-11 15:23:22 +02:00
|
|
|
if (left.kind === TypeKind.Arrow && right.kind === TypeKind.Arrow) {
|
|
|
|
let success = true;
|
2022-09-16 12:00:00 +02:00
|
|
|
if (!unify(left.paramType, right.paramType)) {
|
|
|
|
success = false;
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
|
|
|
if (!unify(left.returnType, right.returnType)) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
if (success) {
|
|
|
|
TypeBase.join(left, right);
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2022-09-16 12:43:06 +02:00
|
|
|
if (left.kind === TypeKind.Tuple && right.kind === TypeKind.Tuple) {
|
|
|
|
if (left.elementTypes.length === right.elementTypes.length) {
|
|
|
|
let success = false;
|
|
|
|
const count = left.elementTypes.length;
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
if (!unify(left.elementTypes[i], right.elementTypes[i])) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (success) {
|
|
|
|
TypeBase.join(left, right);
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2022-09-11 15:23:22 +02:00
|
|
|
if (left.kind === TypeKind.Con && right.kind === TypeKind.Con) {
|
|
|
|
if (left.id === right.id) {
|
|
|
|
assert(left.argTypes.length === right.argTypes.length);
|
|
|
|
const count = left.argTypes.length;
|
|
|
|
let success = true;
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
if (!unify(left.argTypes[i], right.argTypes[i])) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (success) {
|
|
|
|
TypeBase.join(left, right);
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 20:18:47 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
if (left.kind === TypeKind.Nil && right.kind === TypeKind.Nil) {
|
|
|
|
return true;
|
2022-09-14 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
if (left.kind === TypeKind.Field && right.kind === TypeKind.Field) {
|
|
|
|
if (left.name === right.name) {
|
|
|
|
let success = true;
|
|
|
|
path.push(left.name);
|
|
|
|
if (!unifyField(left.type, right.type)) {
|
2022-09-11 15:23:22 +02:00
|
|
|
success = false;
|
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
path.pop();
|
|
|
|
if (!unify(left.restType, right.restType)) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
return success;
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
let success = true;
|
|
|
|
const newRestType = new TVar(this.nextTypeVarId++);
|
|
|
|
if (!unify(left.restType, new TField(right.name, right.type, newRestType))) {
|
|
|
|
success = false;
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
if (!unify(right.restType, new TField(left.name, left.type, newRestType))) {
|
|
|
|
success = false;
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
2022-08-31 13:29:56 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
if (left.kind === TypeKind.Nil && right.kind === TypeKind.Field) {
|
|
|
|
swap();
|
2022-09-15 11:49:53 +02:00
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
if (left.kind === TypeKind.Field && right.kind === TypeKind.Nil) {
|
2022-09-11 15:23:22 +02:00
|
|
|
let success = true;
|
2023-03-11 14:24:02 +01:00
|
|
|
path.push(left.name);
|
|
|
|
if (!unifyField(left.type, new TAbsent(right.node))) {
|
|
|
|
success = false;
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
path.pop();
|
|
|
|
if (!unify(left.restType, right)) {
|
|
|
|
success = false;
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
return success
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left.kind === TypeKind.Nominal && right.kind === TypeKind.Nominal) {
|
|
|
|
if (left.decl === right.decl) {
|
|
|
|
return true;
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
2023-03-11 14:24:02 +01:00
|
|
|
// fall through to error reporting
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
2022-09-11 11:20:21 +02:00
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
if (left.kind === TypeKind.App && right.kind === TypeKind.App) {
|
|
|
|
return unify(left.left, right.left)
|
|
|
|
&& unify(left.right, right.right);
|
2022-09-11 15:23:22 +02:00
|
|
|
}
|
2022-09-07 12:45:38 +02:00
|
|
|
|
2022-09-11 15:23:22 +02:00
|
|
|
this.diagnostics.add(
|
|
|
|
new UnificationFailedDiagnostic(
|
|
|
|
left.substitute(solution),
|
|
|
|
right.substitute(solution),
|
|
|
|
[...constraint.getNodes()],
|
2023-03-11 14:24:02 +01:00
|
|
|
path,
|
2022-09-11 15:23:22 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
return false;
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
2022-09-08 23:32:25 +02:00
|
|
|
|
2022-09-11 15:23:22 +02:00
|
|
|
if (!unify(constraint.left, constraint.right)) {
|
|
|
|
errorCount++;
|
|
|
|
if (errorCount === MAX_TYPE_ERROR_COUNT) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
2022-09-11 15:23:22 +02:00
|
|
|
|
|
|
|
break;
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
2022-09-11 15:23:22 +02:00
|
|
|
|
2022-09-07 12:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-11 14:24:02 +01:00
|
|
|
function getVariadicMember(node: StructPattern) {
|
|
|
|
for (const member of node.members) {
|
|
|
|
if (member.kind === SyntaxKind.VariadicStructPatternElement) {
|
|
|
|
return member;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|