recursive types (and operations on them, like pretty-printing, comparison and unification) seem to be working.

big part of the code base still needs to be 'ported' to the updated type constructors.
This commit is contained in:
Joeri Exelmans 2025-05-05 17:17:45 +02:00
parent 55c5d7cffa
commit 8eec5b9239
34 changed files with 523 additions and 295 deletions

View file

@ -1,38 +1,41 @@
import { Bool, Int } from "../primitives/types.js";
import { fnType, lsType, prettyT } from "../structures/types.js";
import { fnType, lsType } from "../structures/types.js";
import { assign, makeGeneric, unify } from "../generics/generics.js";
import { prettyGenT, prettyT } from "../util/pretty.js";
// a -> Int
const a_to_Int = makeGeneric(a => fnType(a)(Int));
const a_to_Int = makeGeneric(a => fnType(() => a)(() => Int));
console.log((prettyGenT(a_to_Int))); // ∀a: (a -> Int)
// Bool -> Int
const Bool_to_Int = makeGeneric(() => fnType(lsType(Bool))(Int));
const Bool_to_Int = makeGeneric(() => fnType(() => lsType(() =>Bool))(() => Int));
console.log((prettyGenT(Bool_to_Int))); // ∀: ([Bool] -> Int)
console.log("should be: [Bool] -> Int")
console.log(prettyT(unify(a_to_Int, Bool_to_Int)));
console.log(prettyGenT(unify(a_to_Int, Bool_to_Int)));
// (a -> a) -> b
const fnType2 = makeGeneric((a,b) => fnType(fnType(a)(a))(b));
const fnType2 = makeGeneric((a,b) => fnType(() => fnType(a)(a))(() => b));
// (Bool -> Bool) -> a
const fnType3 = makeGeneric(a => fnType(fnType(Bool)(Bool))(a));
const fnType3 = makeGeneric(a => fnType(() => fnType(Bool)(Bool))(() => a));
console.log("should be: (Bool -> Bool) -> a");
console.log(prettyT(unify(fnType2, fnType3)));
// (a -> b) -> [a] -> [b]
const mapFnType = makeGeneric((a,b) =>
fnType
(fnType(a)(b))
(fnType(lsType(a))(lsType(b))))
(fnType(() => a)(() => b))
(fnType(() => lsType(() =>a))(() => lsType(() =>b))))
// a -> a
const idFnType = makeGeneric((_,__,c) =>
fnType(c)(c));
fnType(() => c)(() => c));
console.log("should be: [c] -> [c]");
console.log(prettyT(assign(mapFnType, idFnType)));
// (a -> Int) -> [a] -> a
const weirdFnType = makeGeneric(a =>
fnType
(fnType(a)(Int))
(fnType(() => a)(() => Int))
(fnType
(lsType(a))
(lsType(() =>a))
(a)))
// we call this function with parameter of type (b -> b) ...
// giving these substitutions: