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

67
tests/generics.js Normal file
View file

@ -0,0 +1,67 @@
import assert from "node:assert";
import { assignFn, makeGeneric, unify, UnifyError } from "../lib/generics/generics.js";
import { getDefaultTypeParser } from "../lib/parser/type_parser.js";
import { prettyT } from "../lib/util/pretty.js";
const mkType = getDefaultTypeParser();
// It would be better to compare the types directly with 'compareTypes', but the assert-module does not allow passing a custom comparison function.
assert.equal(
// actual
prettyT(
unify(
mkType("(a -> Int)"),
makeGeneric(() => mkType("[Bool] -> Int")),
)
),
// expected
"([Bool] -> Int)",
);
assert.equal(
// actual
prettyT(
unify(
mkType("(a -> a) -> b"),
mkType("(Bool -> Bool) -> a"),
)
),
// expected
"((Bool -> Bool) -> a)",
);
assert.equal(
// actual
prettyT(
assignFn(
mkType("(a -> b) -> [a] -> [b]"),
mkType("a -> a")
)
),
// expected
"([a] -> [a])",
);
assert.equal(
// actual
prettyT(
assignFn(
mkType("(a -> Int) -> [a] -> a"),
mkType("a -> a")
)
),
// expected
"([Int] -> Int)",
);
assert.throws(
() => {
unify(
mkType("Int"),
mkType("Bool")
)
},
// expected error
UnifyError,
);

47
tests/parser.js Normal file
View file

@ -0,0 +1,47 @@
import assert from "node:assert";
import { getDefaultTypeParser }from "../lib/parser/type_parser.js";
import { prettyT } from "../lib/util/pretty.js";
const parse = getDefaultTypeParser();
assert.equal(
// actual
prettyT(parse("Int")),
// expected
"Int",
);
assert.equal(
// actual
prettyT(parse("Int * Bool")),
// expected
"(Int Bool)",
);
assert.equal(
// actual
prettyT(parse("(((((((Int)))) => ((Bool)))))")),
// expected
"(Int => Bool)",
);
assert.equal(
// actual
prettyT(parse("#0((Int * #0) + Unit)")),
// expected
"#0((Int #0) + Unit)",
);
assert.equal(
// actual
prettyT(parse("#0((a * #0) + Unit")),
// expected
"#0((a #0) + Unit)",
);
assert.equal(
// actual
prettyT(parse("(a*b) + (c*d)")),
// expected
"((a b) + (c d))",
);