This commit is contained in:
Joeri Exelmans 2025-03-24 17:28:07 +01:00
parent 6af72e525c
commit 145835ad5d
22 changed files with 153 additions and 90 deletions

View file

@ -1,41 +1,40 @@
import { Bool, Int } from "../primitives/types.js";
import { lsType } from "../structures/types.js";
import { fnType } from "../structures/function.js";
import { fnType, lsType } from "../structures/types.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}));
const a_to_Int = makeGeneric(a => fnType(a)(Int));
// Bool -> Int
const Bool_to_Int = makeGeneric(() => fnType({in: lsType(Bool), out: Int}));
const Bool_to_Int = makeGeneric(() => fnType(lsType(Bool))(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}));
const fnType2 = makeGeneric((a,b) => fnType(fnType(a)(a))(b));
// (Bool -> Bool) -> a
const fnType3 = makeGeneric(a => fnType({in: fnType({in: Bool, out: Bool}), out: a}));
const fnType3 = makeGeneric(a => fnType(fnType(Bool)(Bool))(a));
console.log("should be: (Bool -> Bool) -> a");
console.log(pretty(unify(fnType2, fnType3)));
// (a -> b) -> [a] -> [b]
const mapFnType = makeGeneric((a,b) =>
fnType({
in: fnType({in: a, out: b}),
out: fnType({in: lsType(a), out: lsType(b)}),
}));
fnType
(fnType(a)(b))
(fnType(lsType(a))(lsType(b))))
// a -> a
const idFnType = makeGeneric(a =>
fnType({in: a, out: a}));
fnType(a)(a));
console.log("should be: [a] -> [a]");
console.log(pretty(assign(mapFnType, idFnType)));
// (a -> Int) -> [a] -> a
const weirdFnType = makeGeneric(a =>
fnType({
in: fnType({in: a, out: Int}),
out: fnType({in: lsType(a), out: a}),
}));
fnType
(fnType(a)(Int))
(fnType
(lsType(a))
(a)))
// we call this function with parameter of type (b -> b) ...
// giving these substitutions:
// a := b