simplify: no distinction between generic types and 'normal' types.

This commit is contained in:
Joeri Exelmans 2025-05-08 16:58:07 +02:00
parent b4826605af
commit a664ddac8a
27 changed files with 535 additions and 360 deletions

View file

@ -1,4 +1,4 @@
import { makeCompareFn } from "../lib/compare/registry.js";
import { makeCompareFn } from "../lib/compare/dynamic.js";
import { Int, Unit } from "../lib/primitives/primitive_types.js";
import { unit } from "../lib/primitives/unit.js";
import { makeConstructors, makeMatchFn } from "../lib/structures/enum.js";
@ -7,16 +7,18 @@ import { newProduct } from "../lib/structures/product.js";
import { lsType } from "../lib/structures/type_constructors.js";
import { prettyT } from "../lib/util/pretty.js";
Error.stackTraceLimit = Infinity;
const variants = [
newProduct("price")(Int),
newProduct("prices")(lsType(_ => Int)),
newProduct("not_found")(Unit),
newProduct("price")(_ => Int),
newProduct("prices")(_ => lsType(_ => Int)),
newProduct("not_found")(_ => Unit),
];
const myEnumType = enumType(variants);
const compatibleNestedSumType = enumType(variants);
console.log("observe the type that was generated:");
console.log(" ", prettyT(myEnumType));
console.log(" ", prettyT(compatibleNestedSumType));
const [newPrice, newPrices, newNotFound] = makeConstructors(variants);
@ -39,7 +41,7 @@ console.log(" ", myEnumToString(price));
console.log(" ", myEnumToString(prices));
console.log(" ", myEnumToString(notFound));
const compareMyEnum = makeCompareFn(myEnumType);
const compareMyEnum = makeCompareFn(compatibleNestedSumType);
console.log("observe the generated compare function in action:");
console.log(" should be smaller ->", compareMyEnum(price)(prices));

View file

@ -1,37 +0,0 @@
import { assign, makeGeneric, unify } from "../lib/generics/generics.js";
import { prettyGenT } from "../lib/util/pretty.js";
import { getDefaultTypeParser } from "../lib/parser/type_parser.js";
const mkType = getDefaultTypeParser();
console.log("should be: [Bool] -> Int")
console.log(prettyGenT(
unify(
mkType("∀a: (a -> Int)"),
makeGeneric(() => mkType("[Bool] -> Int")),
)
));
console.log("should be: (Bool -> Bool) -> a");
console.log(prettyGenT(
unify(
mkType("∀a,b: (a -> a) -> b"),
mkType("∀a: (Bool -> Bool) -> a"),
)
));
console.log("should be: [a] -> [a]");
console.log(prettyGenT(
assign(
mkType("∀a,b: (a -> b) -> [a] -> [b]"),
mkType("∀a: a -> a")
)
));
console.log("should be: [Int] -> Int");
console.log(prettyGenT(
assign(
mkType("∀a: (a -> Int) -> [a] -> a"),
mkType("∀a: a -> a")
)
));

View file

@ -1,16 +0,0 @@
import { getDefaultTypeParser }from "../lib/parser/type_parser.js";
import { prettyGenT, prettyT } from "../lib/util/pretty.js";
const parse = getDefaultTypeParser();
console.log(prettyT(parse("Int"))); // Int
console.log(prettyT(parse("Int * Bool"))); // (Int Bool)
console.log(prettyT(parse("(((((((Int)))) => ((Bool)))))"))); // (Int => Bool)
console.log(prettyT(parse("#0((Int * #0) + Unit)"))) // #0((Int #0) + Unit)
console.log(prettyGenT(parse("∀a: #0((a * #0) + Unit"))); // ∀a: #0((a #0) + Unit)
console.log(prettyGenT(parse("∀a,b,c,d: (a*b) + (c*d)"))); // ∀a,b,c,d: ((a b) + (c d))

View file

@ -1,8 +1,9 @@
import { compareTypes } from "../lib/compare/type.js";
import { makeGeneric, substitute, unify } from "../lib/generics/generics.js";
import { Double, Int, Unit } from "../lib/primitives/primitive_types.js";
import { TYPE_VARS } from "../lib/primitives/typevars.js";
import { fnType, lsType, prodType, sumType, setType } from "../lib/structures/type_constructors.js";
import { prettyGenT, prettyT } from "../lib/util/pretty.js";
import { prettyT } from "../lib/util/pretty.js";
Error.stackTraceLimit = Infinity;
@ -28,8 +29,8 @@ const genericLinkedList = makeGeneric(a => makeLinkedList(a));
console.log(prettyT(listOfSetOfSelf)); // #0[{#0}]
console.log(prettyT(linkedListOfInt)); // #0((Int #0) + Unit)
console.log(prettyGenT(genericFunction)); // ∀a,b: (a -> b)
console.log(prettyGenT(genericLinkedList)); // ∀a: #0((a #0) + Unit)
console.log(prettyT(genericFunction)); // (a -> b)
console.log(prettyT(genericLinkedList)); // #0((a #0) + Unit)
// comparison
@ -45,16 +46,15 @@ console.log(compareTypes(listOfSetOfSelf)(linkedListOfDouble)) // -1
const genericList = makeGeneric(a => lsType(_ => a));
const intList = lsType(_ => Int);
console.log(prettyGenT(genericList)); // ∀a: [a]
console.log(prettyT(genericList)); // [a]
// substitution of type parameters
const substituted = substitute(
genericList.type,
new Map([[
genericList.typeVars.keys().next().value,
Int,
]])
genericList,
new Map([
[TYPE_VARS[0], Int],
])
);
console.log(prettyT(substituted)); // [Int]
@ -63,16 +63,15 @@ console.log(prettyT(substituted)); // [Int]
console.log("recursive substitution")
console.log(prettyT(substitute(
genericLinkedList.type,
new Map([[
genericLinkedList.typeVars.keys().next().value,
Int,
]])
genericLinkedList,
new Map([
[TYPE_VARS[0], Int],
])
))); // #0((Int #0) + Unit)
// unification (simple case)
const {typeVars, type} = unify(
const type = unify(
genericList,
makeGeneric(() => intList));
@ -86,7 +85,7 @@ const unified = unify(
genericLinkedList,
makeGeneric(() => linkedListOfInt));
console.log(prettyGenT(unified)); // ∀: #0((Int #0) + Unit)
console.log(prettyT(unified)); // #0((Int #0) + Unit)
// unification (strange case)
@ -95,4 +94,4 @@ const unified2 = unify(
genericList,
);
console.log(prettyGenT(unified2)); // ∀: #0[{#0}]
console.log(prettyT(unified2)); // #0[{#0}]

View file

@ -1,8 +1,7 @@
import { pretty } from "../util/pretty.js";
import { newLiteral, transform, read, getReadDependencies, verifyValue } from "../versioning/value.js";
import { merge, merge2, newSlot, overwrite } from "../versioning/slot.js";
import { add, emptySet, RBTreeWrapper } from "../structures/set.js";
import { compareNumbers } from "../compare/primitives.js";
import { pretty } from "../lib/util/pretty.js";
import { newLiteral, transform, read, getReadDependencies, verifyValue } from "../lib/versioning/value.js";
import { merge, merge2, newSlot, overwrite } from "../lib/versioning/slot.js";
import { compareNumbers } from "../lib/compare/primitives.js";
const inc = x => x + 1;