type inferencing "unifying" operation is now bi-directional + begin writing generic version of "unifying" operation (that should work on all types)
This commit is contained in:
parent
c5ac55b0ff
commit
33c156fc5c
5 changed files with 148 additions and 9 deletions
|
|
@ -1,20 +1,21 @@
|
|||
import { Bool, Int } from "../primitives/symbols.js";
|
||||
import { lsType } from "../structures/list_common.js";
|
||||
import { fnType } from "../metacircular.js";
|
||||
import { assign, makeGeneric, unify } from "./generics.js";
|
||||
import { assign, makeGeneric, mergeSubstitutions, unify } from "./generics.js";
|
||||
import { select } from "@inquirer/prompts";
|
||||
|
||||
// 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}));
|
||||
// should be: Bool -> Int
|
||||
console.log("should be: Bool -> Int")
|
||||
console.log(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}));
|
||||
// should be: (Bool -> Bool) -> a
|
||||
console.log("should be: (Bool -> Bool) -> a");
|
||||
console.log(unify(fnType2, fnType3));
|
||||
|
||||
// (a -> b) -> [a] -> [b]
|
||||
|
|
@ -26,5 +27,14 @@ const mapFnType = makeGeneric((a,b) =>
|
|||
// a -> a
|
||||
const idFnType = makeGeneric(a =>
|
||||
fnType({in: a, out: a}));
|
||||
// should be: [a] -> [a]
|
||||
console.log("should be: [a] -> [a]");
|
||||
console.log(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}),
|
||||
}));
|
||||
console.log("should be: [Int] -> Int");
|
||||
console.log(assign(weirdFnType, idFnType));
|
||||
Loading…
Add table
Add a link
Reference in a new issue