rename some things
This commit is contained in:
parent
e04cac4f7d
commit
8a4bd44f04
16 changed files with 92 additions and 60 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { getInst, getType } from "../primitives/dynamic.js";
|
||||||
import { SymbolBool, SymbolChar, SymbolDouble, SymbolInt, SymbolType, SymbolUnit } from "../primitives/types.js";
|
import { SymbolBool, SymbolChar, SymbolDouble, SymbolInt, SymbolType, SymbolUnit } from "../primitives/types.js";
|
||||||
import { symbolList, symbolProduct, symbolSet, symbolSum } from "../structures/types.js";
|
import { symbolList, symbolProduct, symbolSet, symbolSum } from "../structures/types.js";
|
||||||
import { compareBools, compareNumbers, compareStrings, compareUnits } from "./primitives.js";
|
import { compareBools, compareNumbers, compareStrings, compareUnits } from "./primitives.js";
|
||||||
|
|
@ -17,11 +18,15 @@ const typeSymbolToCmp = new Map([
|
||||||
[symbolProduct, compareProducts],
|
[symbolProduct, compareProducts],
|
||||||
[symbolSum , compareSums],
|
[symbolSum , compareSums],
|
||||||
[symbolSet , compareSets],
|
[symbolSet , compareSets],
|
||||||
])
|
]);
|
||||||
|
|
||||||
export const makeCompareFn = type => {
|
export const makeCompareFn = type => {
|
||||||
return type.params.reduce(
|
return type.params.reduce(
|
||||||
(acc, cur) => acc(makeCompareFn(cur)),
|
(acc, cur) => acc(makeCompareFn(cur)),
|
||||||
typeSymbolToCmp.get(type.symbol)
|
typeSymbolToCmp.get(type.symbol)
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export const compareDynamic = x => y =>
|
||||||
|
compareTypes(getType(x))(getType(y))
|
||||||
|
|| makeCompareFn(getType(x))(getInst(x))(getInst(y));
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
import { compareNumbers } from "./primitives.js"
|
import { compareNumbers } from "./primitives.js"
|
||||||
import { get, length as lengthLs } from "../structures/list.js";
|
import { get, length as lengthLs } from "../structures/list.js";
|
||||||
import { read, length as lengthSet } from "../structures/set.js";
|
import { read, length as lengthSet } from "../structures/set.js";
|
||||||
import { constructorProduct, getLeft, getRight } from "../structures/product.js";
|
import { newProduct, getLeft, getRight } from "../structures/product.js";
|
||||||
import { match } from "../structures/sum.js";
|
import { match } from "../structures/sum.js";
|
||||||
|
|
||||||
// (a -> a -> Int) -> [a] -> [a] -> Int
|
// (a -> a -> Int) -> [a] -> [a] -> Int
|
||||||
|
|
@ -28,12 +28,12 @@ export const compareProducts = compareLeft => compareRight => x => y => {
|
||||||
|
|
||||||
// (a -> a -> Int) -> (b -> b -> Int) -> (a | b) -> (a | b) -> Int
|
// (a -> a -> Int) -> (b -> b -> Int) -> (a | b) -> (a | b) -> Int
|
||||||
export const compareSums = compareLeft => compareRight => x => y => {
|
export const compareSums = compareLeft => compareRight => x => y => {
|
||||||
return match(x)(constructorProduct
|
return match(x)(newProduct
|
||||||
(leftValueX => match(y)(constructorProduct
|
(leftValueX => match(y)(newProduct
|
||||||
(leftValueY => compareLeft(leftValueX)(leftValueY))
|
(leftValueY => compareLeft(leftValueX)(leftValueY))
|
||||||
((rightValueY) => -1) // x is 'left' and y is 'right' => x < y
|
((rightValueY) => -1) // x is 'left' and y is 'right' => x < y
|
||||||
))
|
))
|
||||||
(rightValueX => match(y)(constructorProduct
|
(rightValueX => match(y)(newProduct
|
||||||
(leftValueY => 1) // x is 'right' and y is 'left' => x > y
|
(leftValueY => 1) // x is 'right' and y is 'left' => x > y
|
||||||
(rightValueY => compareRight(rightValueX)(rightValueY))
|
(rightValueY => compareRight(rightValueX)(rightValueY))
|
||||||
))
|
))
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@ import { makeCompareFn } from "../compare/registry.js";
|
||||||
import { Int, Unit } from "../primitives/types.js";
|
import { Int, Unit } from "../primitives/types.js";
|
||||||
import { unit } from "../primitives/unit.js";
|
import { unit } from "../primitives/unit.js";
|
||||||
import { enumType, makeConstructors, makeMatchFn } from "../structures/enum.js";
|
import { enumType, makeConstructors, makeMatchFn } from "../structures/enum.js";
|
||||||
import { constructorProduct } from "../structures/product.js";
|
import { newProduct } from "../structures/product.js";
|
||||||
import { lsType, prettyT } from "../structures/types.js";
|
import { lsType, prettyT } from "../structures/types.js";
|
||||||
|
|
||||||
const variants = [
|
const variants = [
|
||||||
constructorProduct("price")(Int),
|
newProduct("price")(Int),
|
||||||
constructorProduct("prices")(lsType(Int)),
|
newProduct("prices")(lsType(Int)),
|
||||||
constructorProduct("not_found")(Unit),
|
newProduct("not_found")(Unit),
|
||||||
];
|
];
|
||||||
|
|
||||||
const myEnumType = enumType(variants);
|
const myEnumType = enumType(variants);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { assign, makeGeneric, unify } from "../generics/generics.js";
|
import { assign, makeGeneric, unify } from "../generics/generics.js";
|
||||||
import { Bool, Int } from "../primitives/types.js";
|
import { Bool, Int } from "../primitives/types.js";
|
||||||
import { constructorLeft, constructorRight, match } from "../structures/sum.js";
|
import { newLeft, newRight, match } from "../structures/sum.js";
|
||||||
import { fnType, sumType } from "../structures/types.js";
|
import { fnType, sumType } from "../structures/types.js";
|
||||||
import { pretty } from '../util/pretty.js';
|
import { pretty } from '../util/pretty.js';
|
||||||
|
|
||||||
|
|
@ -33,7 +33,7 @@ console.log(assign(
|
||||||
makeGeneric(() => IntOrBool),
|
makeGeneric(() => IntOrBool),
|
||||||
));
|
));
|
||||||
|
|
||||||
console.log("calling constructorLeft with Int:");
|
console.log("calling newLeft with Int:");
|
||||||
const typeAtCallSite = assign(
|
const typeAtCallSite = assign(
|
||||||
makeGeneric((a, b) =>
|
makeGeneric((a, b) =>
|
||||||
fnType
|
fnType
|
||||||
|
|
@ -51,10 +51,10 @@ console.log(pretty(assign(
|
||||||
)));
|
)));
|
||||||
|
|
||||||
console.log("valid function calls:");
|
console.log("valid function calls:");
|
||||||
console.log(cipFunction(constructorLeft(5)));
|
console.log(cipFunction(newLeft(5)));
|
||||||
console.log(cipFunction(constructorLeft(7)));
|
console.log(cipFunction(newLeft(7)));
|
||||||
console.log(cipFunction(constructorRight(true)));
|
console.log(cipFunction(newRight(true)));
|
||||||
|
|
||||||
console.log("invalid function calls:");
|
console.log("invalid function calls:");
|
||||||
console.log(cipFunction(5));
|
console.log(cipFunction(5));
|
||||||
console.log(cipFunction(constructorLeft("abc")));
|
console.log(cipFunction(newLeft("abc")));
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
import { prettyT, typedFnType } from "../structures/types.js"
|
import { prettyT, typedFnType } from "../structures/types.js"
|
||||||
import { Double } from "../primitives/types.js";
|
import { Double } from "../primitives/types.js";
|
||||||
import { makeConstructor, makeConstructorType, makeGetters, makeGettersTypes, structType } from "../structures/struct.js";
|
import { makeConstructor, makeConstructorType, makeGetters, makeGettersTypes, structType } from "../structures/struct.js";
|
||||||
import { constructorProduct } from "../structures/product.js";
|
import { newProduct } from "../structures/product.js";
|
||||||
import { makeTypeConstructor } from "../type_constructor.js";
|
import { makeTypeConstructor } from "../type_constructor.js";
|
||||||
|
|
||||||
const cartFields = [
|
const cartFields = [
|
||||||
constructorProduct("x")(Double),
|
newProduct("x")(Double),
|
||||||
constructorProduct("y")(Double),
|
newProduct("y")(Double),
|
||||||
];
|
];
|
||||||
|
|
||||||
const polarFields = [
|
const polarFields = [
|
||||||
constructorProduct("r")(Double),
|
newProduct("r")(Double),
|
||||||
constructorProduct("θ")(Double),
|
newProduct("θ")(Double),
|
||||||
];
|
];
|
||||||
|
|
||||||
// Nominal types:
|
// Nominal types:
|
||||||
|
|
|
||||||
19
primitives/dynamic.js
Normal file
19
primitives/dynamic.js
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { typedFnType } from "../structures/types.js";
|
||||||
|
import { Any, Type } from "./types.js";
|
||||||
|
import { makeTypeConstructor } from "../type_constructor.js";
|
||||||
|
|
||||||
|
// A type-link, connecting a value to its Type.
|
||||||
|
export const symbolDynamic = Symbol('Dynamic');
|
||||||
|
export const Dynamic = makeTypeConstructor(symbolDynamic)(0);
|
||||||
|
|
||||||
|
export const getInst = lnk => lnk.i;
|
||||||
|
export const getType = lnk => lnk.t;
|
||||||
|
|
||||||
|
export const ModuleDynamic = {l:[
|
||||||
|
{i: Dynamic, t: Type},
|
||||||
|
{i: Any , t: Type},
|
||||||
|
|
||||||
|
...typedFnType(getInst, fnType => fnType(Dynamic)(Any)),
|
||||||
|
...typedFnType(getType, fnType => fnType(Dynamic)(Any)),
|
||||||
|
]};
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { constructorLeft, constructorRight } from "../structures/sum.js";
|
import { newLeft, newRight } from "../structures/sum.js";
|
||||||
import { setType, sumType, typedFnType } from "../structures/types.js";
|
import { setType, sumType, typedFnType } from "../structures/types.js";
|
||||||
import { Any, GenericType, SymbolT, Type, Unit } from "./types.js";
|
import { Any, GenericType, SymbolT, Type, Unit } from "./types.js";
|
||||||
import { unit } from "./unit.js";
|
import { unit } from "./unit.js";
|
||||||
|
|
@ -7,8 +7,8 @@ export const getType = genericType => genericType.type;
|
||||||
export const getTypeVars = genericType => genericType.typeVars;
|
export const getTypeVars = genericType => genericType.typeVars;
|
||||||
|
|
||||||
export const toNonGeneric = genericType => (genericType.typeVars.size === 0)
|
export const toNonGeneric = genericType => (genericType.typeVars.size === 0)
|
||||||
? constructorRight(genericType.type)
|
? newRight(genericType.type)
|
||||||
: constructorLeft(unit);
|
: newLeft(unit);
|
||||||
|
|
||||||
export const ModuleGenericType = {l:[
|
export const ModuleGenericType = {l:[
|
||||||
{i: GenericType, t: Any},
|
{i: GenericType, t: Any},
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,14 @@ import { ModuleList } from "./structures/list.js";
|
||||||
import { ModuleProduct } from "./structures/product.js";
|
import { ModuleProduct } from "./structures/product.js";
|
||||||
import { ModuleSum } from "./structures/sum.js";
|
import { ModuleSum } from "./structures/sum.js";
|
||||||
import { ModuleType } from "./primitives/type.js";
|
import { ModuleType } from "./primitives/type.js";
|
||||||
import { ModuleTyped } from "./typed.js";
|
import { ModuleDynamic } from "./primitives/dynamic.js";
|
||||||
import { ModuleSet } from "./structures/set.js";
|
import { ModuleSet } from "./structures/set.js";
|
||||||
import { ModuleGenericType } from "./primitives/generic_type.js";
|
import { ModuleGenericType } from "./primitives/generic_type.js";
|
||||||
|
|
||||||
export const ModuleStd = {l:[
|
export const ModuleStd = {l:[
|
||||||
...ModuleType.l,
|
...ModuleType.l,
|
||||||
...ModuleGenericType.l,
|
...ModuleGenericType.l,
|
||||||
...ModuleTyped.l,
|
...ModuleDynamic.l,
|
||||||
|
|
||||||
...ModuleTypeConstructor.l,
|
...ModuleTypeConstructor.l,
|
||||||
...ModuleSymbols.l,
|
...ModuleSymbols.l,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { Bottom } from "../primitives/types.js";
|
import { Bottom } from "../primitives/types.js";
|
||||||
import { capitalizeFirstLetter } from "../util/util.js";
|
import { capitalizeFirstLetter } from "../util/util.js";
|
||||||
import { constructorProduct, getLeft, getRight } from "./product.js";
|
import { newProduct as newProduct, getLeft, getRight } from "./product.js";
|
||||||
import { constructorLeft, constructorRight, match } from "./sum.js";
|
import { newLeft, newRight, match } from "./sum.js";
|
||||||
import { sumType } from "./types.js";
|
import { sumType } from "./types.js";
|
||||||
|
|
||||||
// 'variants' is an array of (name: string, type: Type) pairs.
|
// 'variants' is an array of (name: string, type: Type) pairs.
|
||||||
|
|
@ -34,7 +34,7 @@ export const makeMatchFn = variants => {
|
||||||
const [_, ...remainingVariants] = variants;
|
const [_, ...remainingVariants] = variants;
|
||||||
return sum => handler => {
|
return sum => handler => {
|
||||||
return (
|
return (
|
||||||
match(sum)(constructorProduct
|
match(sum)(newProduct
|
||||||
(leftValue => eatParameters(remainingVariants.length, handler(leftValue)))
|
(leftValue => eatParameters(remainingVariants.length, handler(leftValue)))
|
||||||
(rightValue => makeMatchFn(remainingVariants)(rightValue))
|
(rightValue => makeMatchFn(remainingVariants)(rightValue))
|
||||||
));
|
));
|
||||||
|
|
@ -48,10 +48,10 @@ export const makeConstructors = variants => {
|
||||||
const [variant, ...remainingVariants] = variants;
|
const [variant, ...remainingVariants] = variants;
|
||||||
const name = getLeft(variant);
|
const name = getLeft(variant);
|
||||||
const ctorName = `new${capitalizeFirstLetter(name)}`;
|
const ctorName = `new${capitalizeFirstLetter(name)}`;
|
||||||
const constructor = { [ctorName]: val => constructorLeft(val) }[ctorName];
|
const constructor = { [ctorName]: val => newLeft(val) }[ctorName];
|
||||||
return [
|
return [
|
||||||
constructor,
|
constructor,
|
||||||
...makeConstructors(remainingVariants).map(ctor =>
|
...makeConstructors(remainingVariants).map(ctor =>
|
||||||
({[ctor.name]: val => constructorRight(ctor(val))}[ctor.name])),
|
({[ctor.name]: val => newRight(ctor(val))}[ctor.name])),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
import { fnType, typedFnType } from "./types.js";
|
import { typedFnType } from "./types.js";
|
||||||
import { Char, GenericType, Type } from "../primitives/types.js";
|
import { Char, GenericType, Type } from "../primitives/types.js";
|
||||||
import { Int } from "../primitives/types.js";
|
import { Int } from "../primitives/types.js";
|
||||||
import { makeGeneric } from "../generics/generics.js";
|
import { makeGeneric } from "../generics/generics.js";
|
||||||
import { lsType } from "./types.js";
|
import { lsType } from "./types.js";
|
||||||
import { Typed } from "../typed.js"
|
import { Dynamic } from "../primitives/dynamic.js"
|
||||||
|
|
||||||
// 'normal' implementation
|
// 'normal' implementation
|
||||||
export const emptyList = {l:[]};
|
export const emptyList = {l:[]};
|
||||||
|
|
@ -22,7 +22,7 @@ export const fold = ls => callback => initial => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const String = lsType(Char); // alias
|
export const String = lsType(Char); // alias
|
||||||
export const Module = lsType(Typed);
|
export const Module = lsType(Dynamic);
|
||||||
|
|
||||||
export const ModuleList = {l:[
|
export const ModuleList = {l:[
|
||||||
// Type -> Type
|
// Type -> Type
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import { typedFnType } from "./types.js";
|
||||||
import { prodType } from "./types.js";
|
import { prodType } from "./types.js";
|
||||||
|
|
||||||
// In JS, all products are encoded in the same way:
|
// In JS, all products are encoded in the same way:
|
||||||
export const constructorProduct = l => r => ({l, r});
|
export const newProduct = l => r => ({l, r});
|
||||||
export const getLeft = product => product.l;
|
export const getLeft = product => product.l;
|
||||||
export const getRight = product => product.r;
|
export const getRight = product => product.r;
|
||||||
|
|
||||||
|
|
@ -26,7 +26,7 @@ export const ModuleProduct = {l: [
|
||||||
),
|
),
|
||||||
|
|
||||||
// a -> b -> (a, b)
|
// a -> b -> (a, b)
|
||||||
...typedFnType(constructorProduct, fnType =>
|
...typedFnType(newProduct, fnType =>
|
||||||
makeGeneric((a, b) =>
|
makeGeneric((a, b) =>
|
||||||
fnType
|
fnType
|
||||||
(a)
|
(a)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { Unit } from "../primitives/types.js";
|
import { Unit } from "../primitives/types.js";
|
||||||
import { unit } from "../primitives/unit.js";
|
import { unit } from "../primitives/unit.js";
|
||||||
import { capitalizeFirstLetter } from "../util/util.js";
|
import { capitalizeFirstLetter } from "../util/util.js";
|
||||||
import { constructorProduct, getLeft, getRight } from "./product.js";
|
import { newProduct, getLeft, getRight } from "./product.js";
|
||||||
import { fnType, prodType } from "./types.js";
|
import { fnType, prodType } from "./types.js";
|
||||||
|
|
||||||
// 'fields' is an array of (name: string, type: Type) pairs.
|
// 'fields' is an array of (name: string, type: Type) pairs.
|
||||||
|
|
@ -26,7 +26,7 @@ export const makeConstructor = fields => {
|
||||||
return nextParam => {
|
return nextParam => {
|
||||||
const wrappedName = 'wrapped_' + ret.name;
|
const wrappedName = 'wrapped_' + ret.name;
|
||||||
const newRet = {
|
const newRet = {
|
||||||
[wrappedName]: inner => constructorProduct(nextParam)(ret(inner)),
|
[wrappedName]: inner => newProduct(nextParam)(ret(inner)),
|
||||||
}[wrappedName];
|
}[wrappedName];
|
||||||
return internal(nParams-1, newRet);
|
return internal(nParams-1, newRet);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ import { typedFnType } from "./types.js";
|
||||||
import { makeGeneric } from "../generics/generics.js";
|
import { makeGeneric } from "../generics/generics.js";
|
||||||
import { sumType } from "./types.js";
|
import { sumType } from "./types.js";
|
||||||
|
|
||||||
export const constructorLeft = left => ({t: "L", v: left }); // 't': tag, 'v': value
|
export const newLeft = left => ({t: "L", v: left }); // 't': tag, 'v': value
|
||||||
export const constructorRight = right => ({t: "R", v: right});
|
export const newRight = right => ({t: "R", v: right});
|
||||||
|
|
||||||
// signature:
|
// signature:
|
||||||
// sum-type -> (leftType -> resultType, rightType -> resultType) -> resultType
|
// sum-type -> (leftType -> resultType, rightType -> resultType) -> resultType
|
||||||
|
|
@ -31,7 +31,7 @@ export const ModuleSum = {l:[
|
||||||
),
|
),
|
||||||
|
|
||||||
// a -> a | b
|
// a -> a | b
|
||||||
...typedFnType(constructorLeft, fnType =>
|
...typedFnType(newLeft, fnType =>
|
||||||
makeGeneric((a, b) =>
|
makeGeneric((a, b) =>
|
||||||
fnType
|
fnType
|
||||||
(a)
|
(a)
|
||||||
|
|
@ -39,7 +39,7 @@ export const ModuleSum = {l:[
|
||||||
), GenericType),
|
), GenericType),
|
||||||
|
|
||||||
// b -> a | b
|
// b -> a | b
|
||||||
...typedFnType(constructorRight, fnType =>
|
...typedFnType(newRight, fnType =>
|
||||||
makeGeneric((a, b) =>
|
makeGeneric((a, b) =>
|
||||||
fnType
|
fnType
|
||||||
(b)
|
(b)
|
||||||
|
|
|
||||||
18
typed.js
18
typed.js
|
|
@ -1,18 +0,0 @@
|
||||||
import { typedFnType } from "./structures/types.js";
|
|
||||||
import { Any, Type } from "./primitives/types.js";
|
|
||||||
import { makeTypeConstructor } from "./type_constructor.js";
|
|
||||||
|
|
||||||
// A type-link, connecting a value to its Type.
|
|
||||||
const symbolTyped = Symbol('Typed');
|
|
||||||
export const Typed = makeTypeConstructor(symbolTyped)(0);
|
|
||||||
|
|
||||||
const getInst = lnk => lnk.i;
|
|
||||||
const getType = lnk => lnk.t;
|
|
||||||
|
|
||||||
export const ModuleTyped = {l:[
|
|
||||||
{i: Typed, t: Type},
|
|
||||||
{i: Any , t: Type},
|
|
||||||
|
|
||||||
...typedFnType(getInst, fnType => fnType(Typed)(Any)),
|
|
||||||
...typedFnType(getType, fnType => fnType(Typed)(Any)),
|
|
||||||
]};
|
|
||||||
26
versioning/types.js
Normal file
26
versioning/types.js
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
import { Dynamic } from "../primitives/dynamic.js";
|
||||||
|
import { Int } from "../primitives/types.js";
|
||||||
|
import { enumType } from "../structures/enum.js";
|
||||||
|
import { newProduct } from "../structures/product.js";
|
||||||
|
import { structType } from "../structures/struct.js";
|
||||||
|
import { prettyT, prodType } from "../structures/types.js";
|
||||||
|
|
||||||
|
const Slot = structType([
|
||||||
|
newProduct("value")(() => Value),
|
||||||
|
newProduct("depth")(() => Int),
|
||||||
|
newProduct("overwrites")(() => Slot),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// A Value is either:
|
||||||
|
// - a literal, without any dependencies.
|
||||||
|
// - read from a slot. the Value then has a read-dependency on that slot.
|
||||||
|
// - a transformation of another Value, by a function. the function is also a Value.
|
||||||
|
const variants = [
|
||||||
|
newProduct("literal", () => Dynamic),
|
||||||
|
newProduct("read", () => Slot),
|
||||||
|
newProduct("transformation", () => prodType(Value, Dynamic)),
|
||||||
|
];
|
||||||
|
const Value = enumType(variants);
|
||||||
|
|
||||||
|
// console.log(prettyT(Slot));
|
||||||
|
// console.log(prettyT(Value));
|
||||||
Loading…
Add table
Add a link
Reference in a new issue