interactive prompt can handle polymorphic types

This commit is contained in:
Joeri Exelmans 2025-04-02 15:49:43 +02:00
parent a0e3aa0cb3
commit 4a4983f693
20 changed files with 485 additions and 276 deletions

View file

@ -1,21 +1,20 @@
import { Bool, Int } from "../primitives/types.js";
import { fnType, lsType } from "../structures/types.js";
import { fnType, lsType, prettyT } from "../structures/types.js";
import { assign, makeGeneric, unify } from "./generics.js";
import { pretty } from "../util/pretty.js";
// a -> Int
const a_to_Int = makeGeneric(a => fnType(a)(Int));
// Bool -> 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)));
console.log(prettyT(unify(a_to_Int, Bool_to_Int)));
// (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));
console.log("should be: (Bool -> Bool) -> a");
console.log(pretty(unify(fnType2, fnType3)));
console.log(prettyT(unify(fnType2, fnType3)));
// (a -> b) -> [a] -> [b]
const mapFnType = makeGeneric((a,b) =>
@ -23,10 +22,10 @@ const mapFnType = makeGeneric((a,b) =>
(fnType(a)(b))
(fnType(lsType(a))(lsType(b))))
// a -> a
const idFnType = makeGeneric(a =>
fnType(a)(a));
console.log("should be: [a] -> [a]");
console.log(pretty(assign(mapFnType, idFnType)));
const idFnType = makeGeneric((_,__,c) =>
fnType(c)(c));
console.log("should be: [c] -> [c]");
console.log(prettyT(assign(mapFnType, idFnType)));
// (a -> Int) -> [a] -> a
const weirdFnType = makeGeneric(a =>
@ -40,4 +39,4 @@ const weirdFnType = makeGeneric(a =>
// a := b
// b := Int
console.log("should be: [Int] -> Int");
console.log(pretty(assign(weirdFnType, idFnType)));
console.log(prettyT(assign(weirdFnType, idFnType)));