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

87
examples/compare_types.js Normal file
View file

@ -0,0 +1,87 @@
import { compareTypes } from "../compare/type.js";
import { makeGeneric, substitute, unify } from "../generics/generics.js";
import { Double, Int, Unit } from "../primitives/types.js";
import { fnType, lsType, prodType, setType, sumType } from "../structures/types.js";
import { prettyGenT, prettyT } from "../util/pretty.js";
// some recursive types:
const listOfSetOfSelf = lsType(self => setType(_ => self));
const makeLinkedList = elementType => sumType
(self => prodType
(_ => elementType)
(_ => self))
(_ => Unit);
const linkedListOfInt = makeLinkedList(Int);
const linkedListOfDouble = makeLinkedList(Double);
// some generic types
const genericFunction = makeGeneric((a,b) => fnType(_ => a)(_ => b));
const genericLinkedList = makeGeneric(a => makeLinkedList(a));
// pretty-printing of recursive types:
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)
// comparison
console.log(compareTypes(listOfSetOfSelf)(listOfSetOfSelf)) // 0
console.log(compareTypes(linkedListOfInt)(linkedListOfInt)) // 0
console.log(compareTypes(linkedListOfInt)(linkedListOfDouble)) // 1
console.log(compareTypes(linkedListOfDouble)(linkedListOfInt)) // -1
console.log(compareTypes(linkedListOfDouble)(linkedListOfDouble)) // 0
console.log(compareTypes(linkedListOfDouble)(listOfSetOfSelf)) // 1
console.log(compareTypes(listOfSetOfSelf)(linkedListOfDouble)) // -1
const genericList = makeGeneric(a => lsType(_ => a));
const intList = lsType(_ => Int);
console.log(prettyGenT(genericList)); // ∀a: [a]
// substitution of type parameters
const substituted = substitute(
genericList.type,
new Map([[
genericList.typeVars.keys().next().value,
Int,
]])
);
console.log(prettyT(substituted)); // [Int]
// substitution (recursive this time)
console.log("recursive substitution")
console.log(prettyT(substitute(
genericLinkedList.type,
new Map([[
genericLinkedList.typeVars.keys().next().value,
Int,
]])
))); // #0((Int #0) + Unit)
// unification (simple case)
const {typeVars, type} = unify(
genericList,
makeGeneric(() => intList));
console.log(prettyT(type)); // [Int]
// unification (recursive case)
console.log("complex case...")
const unified = unify(
genericLinkedList,
makeGeneric(() => linkedListOfInt));
console.log(prettyGenT(unified)); // ∀: #0((Int #0) + Unit)

View file

@ -7,7 +7,7 @@ import { lsType, prettyT } from "../structures/types.js";
const variants = [
newProduct("price")(Int),
newProduct("prices")(lsType(Int)),
newProduct("prices")(lsType(() =>Int)),
newProduct("not_found")(Unit),
];

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:

View file

@ -4,14 +4,14 @@ import { newLeft, newRight, match } from "../structures/sum.js";
import { fnType, sumType } from "../structures/types.js";
import { pretty } from '../util/pretty.js';
const IntOrBool = sumType(Int)(Bool);
const IntOrBool = sumType(() => Int)(() => Bool);
// console.log(int5);
console.log(pretty(unify(
makeGeneric(() => IntOrBool),
makeGeneric(a => sumType(Int)(a)),
makeGeneric(a => sumType(() => Int)(() => a)),
)));
const cipFunction = (x) => {
@ -38,7 +38,7 @@ const typeAtCallSite = assign(
makeGeneric((a, b) =>
fnType
(a)
(sumType(a)(b))
(sumType(() => a)(() => b))
),
makeGeneric(() => Int));
console.log(pretty(typeAtCallSite));

View file

@ -12,7 +12,7 @@ const square = numDict => x => getMul(numDict)(x)(x);
const squareFnType = makeGeneric(a =>
fnType
(numDictType(a))
(fnType(a)(a))
(fnType(() => a)(() => a))
);
console.log("should be: Int -> Int");

View file

@ -6,7 +6,7 @@ import { isFunction } from '../structures/types.js';
import { ModuleStd } from '../stdlib.js';
import { Double, GenericType, Int, SymbolT, Type } from "../primitives/types.js";
import { eqType } from '../primitives/type.js';
import { Any } from "../primitives/types.js";
import { Top } from "../primitives/types.js";
import { assignFn, makeGeneric, onlyOccurring } from '../generics/generics.js';
import { prettyT } from '../util/pretty.js';
@ -48,7 +48,7 @@ class Context {
// console.log(strI, '::', strT);
this.types.getdefault(i, true).add(t);
this.types.getdefault(i, true).add(Any);
this.types.getdefault(i, true).add(Top);
if (t.typeVars) {
// console.log("generic:", prettyT(t));
this.types.getdefault(t, true).add(GenericType);
@ -59,7 +59,7 @@ class Context {
}
this.instances.getdefault(t, true).add(i);
this.instances.getdefault(Any, true).add(i);
this.instances.getdefault(Top, true).add(i);
}
const addIfFunctionType = (t, originalT, add) => {
if (isFunction(t)) {