branching and very basic merging of slots

This commit is contained in:
Joeri Exelmans 2025-04-17 09:19:41 +02:00
parent 614e6c0fdb
commit 3978f7f835
32 changed files with 684 additions and 420 deletions

140
lib/point_structural.js Normal file
View file

@ -0,0 +1,140 @@
import { prettyT, typedFnType } from "../structures/types.js"
import { Double } from "../primitives/types.js";
import { makeConstructor, makeConstructorType, makeGetters, makeGettersTypes, structType } from "../structures/struct.js";
import { constructorProduct } from "../structures/product.js";
import { makeTypeConstructor } from "../type_constructor.js";
const cartFields = [
constructorProduct("x")(Double),
constructorProduct("y")(Double),
];
const polarFields = [
constructorProduct("r")(Double),
constructorProduct("θ")(Double),
];
// Nominal types:
export const NPoint2DCartesian = makeTypeConstructor(Symbol('Point2DCartesian'))(0);
export const NPoint2DPolar = makeTypeConstructor(Symbol('Point2DPolar'))(0);
// Structural types:
export const SPoint2DCartesian = structType(cartFields); // (Double, (Double, Unit))
export const SPoint2DPolar = structType(polarFields); // (Double, (Double, Unit))
export const constructorPoint2DCartesian = makeConstructor(cartFields);
export const constructorPoint2DPolar = makeConstructor(polarFields);
export const constructorPoint2DCartesianFnType = makeConstructorType(NPoint2DCartesian)(cartFields);
export const constructorPoint2DPolarFnType = makeConstructorType(NPoint2DPolar)(polarFields);
export const [getX, getY] = makeGetters(cartFields);
export const [getR, getΘ] = makeGetters(polarFields);
export const [getXFnType, getYFnType] = makeGettersTypes(NPoint2DCartesian)(cartFields);
export const [getRFnType, getΘFnType] = makeGettersTypes(NPoint2DPolar)(polarFields);
export const cart2polar = cart => {
const x = getX(cart);
const y = getY(cart);
const r = Math.sqrt(x*x + y*y);
const θ = Math.atan(y/x);
return constructorPoint2DPolar(r)(θ);
};
export const polar2cart = polar => {
const r = getR(polar);
const θ = getΘ(polar);
const x = r * Math.cos(θ);
const y = r * Math.sin(θ);
return constructorPoint2DCartesian(x)(y);
};
export const cart2Str = cart => {
const x = getX(cart);
const y = getY(cart);
return {l: `{x: ${x}, y: ${y}}`};
};
export const polar2Str = polar => {
const r = getR(polar);
const θ = getΘ(polar);
return {l: `{r: ${r}, θ: ${θ}}`};
};
// export const translate = dx => dy => ({x, y}) => {
// return {x: x+dx, y: y+dy};
// };
// export const rotate = dθ => ({r, θ}) => {
// return {r, θ: θ+dθ};
// };
// export const scale = dr => ({r, θ}) => {
// return {r: r+dr, θ};
// };
// const examplePoint = {x: 1, y: 2};
const examplePointCart = constructorPoint2DCartesian(1)(2);
const examplePointPolar = constructorPoint2DCartesian(0)(5);
console.log(prettyT(getXFnType));
export const ModulePoint = {l:[
// {i: -1 , t: Double},
// {i: 2 , t: Double},
// {i: examplePoint , t: Point2DCartesian},
{i: examplePointCart, t: SPoint2DCartesian},
{i: examplePointCart, t: NPoint2DCartesian},
{i: examplePointPolar, t: SPoint2DPolar},
{i: examplePointPolar, t: NPoint2DPolar},
// {i: SPoint2DCartesian , t: Type},
// {i: SPoint2DPolar , t: Type},
// {i: NPoint2DCartesian , t: Type},
// {i: NPoint2DPolar , t: Type},
{i: getX, t: getXFnType},
{i: getY, t: getYFnType},
{i: getR, t: getRFnType},
{i: getΘ, t: getΘFnType},
...typedFnType(cart2polar, fnType => fnType(NPoint2DCartesian)(NPoint2DPolar)),
...typedFnType(polar2cart, fnType => fnType(NPoint2DPolar)(NPoint2DCartesian)),
// ...typedFnType(polar2cart, fnType => fnType(Point2DPolar)(Point2DCartesian)),
// // Double -> Double -> PointCartesian2D -> PointCartesian2D
// ...typedFnType(translate, fnType =>
// fnType
// (Double)
// (fnType
// (Double)
// (fnType
// (Point2DCartesian)
// (Point2DCartesian)))),
// ...typedFnType(cart2tuple, fnType => fnType(Point2DCartesian)(prodType(Double)(Double))),
// ...typedFnType(polar2tuple, fnType => fnType(Point2DPolar)(prodType(Double)(Double))),
// // Double -> PointPolar2D -> PointPolar2D
// ...typedFnType(rotate, fnType =>
// fnType
// (Double)
// (fnType
// (Point2DPolar)
// (Point2DPolar))),
// // Double -> PointPolar2D -> PointPolar2D
// ...typedFnType(scale, fnType =>
// fnType
// (Double)
// (fnType
// (Point2DPolar)
// (Point2DPolar))),
]};