dope2/examples/int_or_bool.js
Joeri Exelmans 8eec5b9239 recursive types (and operations on them, like pretty-printing, comparison and unification) seem to be working.
big part of the code base still needs to be 'ported' to the updated type constructors.
2025-05-05 17:17:45 +02:00

60 lines
1.4 KiB
JavaScript

import { assign, makeGeneric, unify } from "../generics/generics.js";
import { Bool, Int } from "../primitives/types.js";
import { newLeft, newRight, match } from "../structures/sum.js";
import { fnType, sumType } from "../structures/types.js";
import { pretty } from '../util/pretty.js';
const IntOrBool = sumType(() => Int)(() => Bool);
// console.log(int5);
console.log(pretty(unify(
makeGeneric(() => IntOrBool),
makeGeneric(a => sumType(() => Int)(() => a)),
)));
const cipFunction = (x) => {
return match(x)({
left: x_as_int => (x_as_int === 5),
right: x_as_bool => false,
});
}
const cipFunctionType = fnType
(IntOrBool) // in
(Bool);
// console.log(cipFunctionType);
// console.log(IntOrBool);
console.log(assign(
makeGeneric(() => cipFunctionType),
makeGeneric(() => IntOrBool),
));
console.log("calling newLeft with Int:");
const typeAtCallSite = assign(
makeGeneric((a, b) =>
fnType
(a)
(sumType(() => a)(() => b))
),
makeGeneric(() => Int));
console.log(pretty(typeAtCallSite));
console.log("calling cipFunction:");
console.log(pretty(assign(
makeGeneric(() => cipFunctionType),
typeAtCallSite,
)));
console.log("valid function calls:");
console.log(cipFunction(newLeft(5)));
console.log(cipFunction(newLeft(7)));
console.log(cipFunction(newRight(true)));
console.log("invalid function calls:");
console.log(cipFunction(5));
console.log(cipFunction(newLeft("abc")));