turn one example into a test + fix bug in type variable substition function
This commit is contained in:
parent
a664ddac8a
commit
35d682429b
4 changed files with 148 additions and 102 deletions
|
|
@ -1,97 +0,0 @@
|
|||
import { compareTypes } from "../lib/compare/type.js";
|
||||
import { makeGeneric, substitute, unify } from "../lib/generics/generics.js";
|
||||
import { Double, Int, Unit } from "../lib/primitives/primitive_types.js";
|
||||
import { TYPE_VARS } from "../lib/primitives/typevars.js";
|
||||
import { fnType, lsType, prodType, sumType, setType } from "../lib/structures/type_constructors.js";
|
||||
import { prettyT } from "../lib/util/pretty.js";
|
||||
|
||||
Error.stackTraceLimit = Infinity;
|
||||
|
||||
// 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(prettyT(genericFunction)); // (a -> b)
|
||||
console.log(prettyT(genericLinkedList)); // #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(prettyT(genericList)); // [a]
|
||||
|
||||
// substitution of type parameters
|
||||
|
||||
const substituted = substitute(
|
||||
genericList,
|
||||
new Map([
|
||||
[TYPE_VARS[0], Int],
|
||||
])
|
||||
);
|
||||
|
||||
console.log(prettyT(substituted)); // [Int]
|
||||
|
||||
// substitution (recursive this time)
|
||||
|
||||
console.log("recursive substitution")
|
||||
console.log(prettyT(substitute(
|
||||
genericLinkedList,
|
||||
new Map([
|
||||
[TYPE_VARS[0], Int],
|
||||
])
|
||||
))); // #0((Int ⨯ #0) + Unit)
|
||||
|
||||
// unification (simple case)
|
||||
|
||||
const 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(prettyT(unified)); // #0((Int ⨯ #0) + Unit)
|
||||
|
||||
// unification (strange case)
|
||||
|
||||
const unified2 = unify(
|
||||
makeGeneric(() => listOfSetOfSelf),
|
||||
genericList,
|
||||
);
|
||||
|
||||
console.log(prettyT(unified2)); // #0[{#0}]
|
||||
Loading…
Add table
Add a link
Reference in a new issue