This commit is contained in:
Joeri Exelmans 2025-03-23 09:15:37 +01:00
parent afd78c3b3e
commit 29d20b2273
25 changed files with 369 additions and 469 deletions

View file

@ -1,21 +1,22 @@
import { Bool, Int } from "../primitives/symbols.js";
import { lsType } from "../structures/list_common.js";
import { fnType } from "../metacircular.js";
import { assign, makeGeneric, mergeSubstitutions, unify } from "./generics.js";
import { lsType } from "../structures/list.js";
import { fnType } from "../structures/function.js";
import { assign, makeGeneric, unify } from "./generics.js";
import { pretty } from "../util.js";
// a -> Int
const a_to_Int = makeGeneric(a => fnType({in: a, out: Int}));
// Bool -> Int
const Bool_to_Int = makeGeneric(() => fnType({in: lsType(Bool), out: Int}));
console.log("should be: Bool -> Int")
console.log(unify(a_to_Int, Bool_to_Int));
console.log("should be: [Bool] -> Int")
console.log(pretty(unify(a_to_Int, Bool_to_Int)));
// (a -> a) -> b
const fnType2 = makeGeneric((a,b) => fnType({in: fnType({in: a, out: a}), out: b}));
// (Bool -> Bool) -> a
const fnType3 = makeGeneric(a => fnType({in: fnType({in: Bool, out: Bool}), out: a}));
console.log("should be: (Bool -> Bool) -> a");
console.log(unify(fnType2, fnType3));
console.log(pretty(unify(fnType2, fnType3)));
// (a -> b) -> [a] -> [b]
const mapFnType = makeGeneric((a,b) =>
@ -27,7 +28,7 @@ const mapFnType = makeGeneric((a,b) =>
const idFnType = makeGeneric(a =>
fnType({in: a, out: a}));
console.log("should be: [a] -> [a]");
console.log(assign(mapFnType, idFnType));
console.log(pretty(assign(mapFnType, idFnType)));
// (a -> Int) -> [a] -> a
const weirdFnType = makeGeneric(a =>
@ -36,4 +37,4 @@ const weirdFnType = makeGeneric(a =>
out: fnType({in: lsType(a), out: a}),
}));
console.log("should be: [Int] -> Int");
console.log(assign(weirdFnType, idFnType));
console.log(pretty(assign(weirdFnType, idFnType)));