lotta progress

This commit is contained in:
Joeri Exelmans 2025-03-23 13:25:47 +01:00
parent 29d20b2273
commit bc91d9bf39
27 changed files with 317 additions and 475 deletions

View file

@ -1,39 +0,0 @@
// import { fnType } from "../metacircular.js";
// import {Function} from "../metacircular.js";
// // import {Typed} from "../typed.js";
// // import {Bool} from "../primitives/symbols.js";
// // import {deepEqual} from "../util.js";
// // import {Conformable, conformanceCheck} from "../typeclasses/conformable.js";
// // // Identity function
// // const idBoundToType = Symbol('idBoundToType');
// // const id = type => x => x;
// // const idFn = {name: "id", inType: Type, outType: {inType: Type, outType: Type}, fn: id};
// // const idIsConform = {
// // name: "isConform",
// // inType: idBoundToType,
// // outType: Bool,
// // fn: fn => deepEqual(fnIn(fn), fnOut(fn))
// // }
// // export const ModuleId = [
// // {i: idBoundToType, t: Type},
// // {i: idFn, t: Function},
// // ... makeTypedFnInOut({f: idBoundToType, inType: Type, outType: Type}),
// // {i: idBoundToType, t: Conformable},
// // {i: idIsConform, t: conformanceCheck},
// // ];
// // generates explicitly typed id-function
// export const makeIdFn = typ => {
// const Typ_to_Typ = fnType({in: typ, out: typ});
// const id = x => x;
// return {l:[
// {i: id , t: Typ_to_Typ},
// {i: Typ_to_Typ, t: Function},
// ]};
// };

View file

@ -1,65 +1,72 @@
import { Double } from "../primitives/symbols.js";
import { nominalType, NominalType } from "../structures/nominal_type.js";
import { makeProductType } from "../structures/product.js";
import { typedFnType } from "../metacircular.js";
import { prodType } from "../structures/product.js";
import { Type } from "../type.js";
import { typedFnType } from "../structures/types.js"
import { Double } from "../primitives/types.js";
const PointCartesian2D = nominalType(
"PointCartesian2D")
// just a unique number, to make sure that our type is only equal to itself
// BigInt("0xBBAAD62B10EE21993BA690A732DA2A6875CE4B6F5E7139D5AEC9FD887F9D24A8"))
(prodType(Double, Double));
// Create nominal types
export const PointCartesian2D = { symbol: Symbol('PointCartesian2D'), params: [] };
export const PointPolar2D = { symbol: Symbol('PointPolar2D') , params: [] };
const PointPolar2D = nominalType(
"PointPolar2D")
// just a unique number, to make sure that our type is only equal to itself
// BigInt("0x31CDAB4B3D84C4EB27D3C111FD7580E533268B72E05BD694F8B262913E018B72"))
(prodType(Double, Double));
export const cart2polar = ({left: x, right: y}) => {
export const cart2polar = ({x, y}) => {
const r = Math.sqrt(x*x + y*y);
const θ = Math.atan(y/x);
return {left: r, right: θ};
return {r, θ};
};
export const polar2cart = ({left: r, right: θ}) => {
export const polar2cart = ({r, θ}) => {
const x = r * Math.cos(θ);
const y = r * Math.sin(θ);
return {left: x, right: y};
return {x, y};
}
export const translate = dx => dy => ({left: x, right: y}) => {
export const translate = dx => dy => ({x, y}) => {
return {left: x+dx, right: y+dy};
}
export const rotate = => ({left: r, right: θ}) => {
return {left: r, right: θ+};
export const rotate = => ({r, θ}) => {
return {r, θ: θ+};
}
export const scale = dr => ({left: r, right: θ}) => {
return {left: r+dr, right: θ};
export const scale = dr => ({r, θ}) => {
return {r: r+dr, θ};
}
const examplePoint = {left: 1, right: 2};
const examplePoint = {x: 1, y: 2};
export const ModulePoint = {l:[
{i: -1 , t: Double},
{i: 2 , t: Double},
{i: examplePoint , t: PointCartesian2D},
{i: examplePoint , t: prodType(Double, Double)},
...makeProductType(Double, Double).l,
{i: PointCartesian2D , t: Type},
{i: PointPolar2D , t: Type},
{i: PointCartesian2D , t: NominalType},
{i: PointPolar2D , t: NominalType},
...typedFnType(cart2polar, fnType => fnType(PointCartesian2D)(PointPolar2D)),
...typedFnType(cart2polar, fnType => fnType({in: PointCartesian2D, out: PointPolar2D})),
...typedFnType(polar2cart, fnType => fnType(PointPolar2D)(PointCartesian2D)),
...typedFnType(polar2cart, fnType => fnType({in: PointPolar2D, out: PointCartesian2D})),
// Double -> Double -> PointCartesian2D -> PointCartesian2D
...typedFnType(translate, fnType =>
fnType
(Double)
(fnType
(Double)
(fnType
(PointCartesian2D)
(PointCartesian2D)))),
...typedFnType(translate , fnType => fnType({in: Double, out: fnType({in: Double, out: fnType({in: PointCartesian2D, out: PointCartesian2D})})})),
// Double -> PointPolar2D -> PointPolar2D
...typedFnType(rotate, fnType =>
fnType
(Double)
(fnType
(PointPolar2D)
(PointPolar2D))),
...typedFnType(rotate, fnType => fnType({in: Double, out: fnType({in: PointPolar2D, out: PointPolar2D})})),
...typedFnType(scale, fnType => fnType({in: Double, out: fnType({in: PointPolar2D, out: PointPolar2D})})),
// Double -> PointPolar2D -> PointPolar2D
...typedFnType(scale, fnType =>
fnType
(Double)
(fnType
(PointPolar2D)
(PointPolar2D))),
]};

View file

@ -1,50 +0,0 @@
// import { getIn, getOut } from "../metacircular.js";
// import {Module} from "../structures/list_types/module.js";
// import { deepEqual } from "../util.js";
// import { Typed } from "../typed.js";
// import { fnType } from "../metacircular.js";
// import {Num, NumDict, getType, getMul} from "../typeclasses/num.js";
// const squareBoundToType = Symbol('squareBoundToType'); // the type of e.g., squareOfInt
// // returning a function + its signature is the only way to make the outType of square depend on its input
// const square = numDict => {
// const typ = getType(numDict);
// const mul = getMul(numDict);
// return {
// name: "squareOf:" + typ.toString(),
// inType: typ,
// outType: typ,
// fn: x => mul(x)(x),
// };
// };
// export const ModuleSquare = [
// {i: squareBoundToType, t: Type},
// {i: {name: "square", inType: NumDict, outType: squareBoundToType, fn: square}, t: Function},
// ...makeTypedFnInOut({f: squareBoundToType, inType: Num, outType: Num}),
// ];
// export const makeSquare = ({i: mul, t: mulFunction}) => {
// const numType = getIn(mulFunction);
// const boundMulFunction = getOut(mulFunction);
// if (!deepEqual(getOut(boundMulFunction), numType) || !deepEqual(getIn(boundMulFunction), numType)) {
// console.log(getOut(boundMulFunction), getIn(boundMulFunction), numType);
// throw new Error("invalid signature");
// }
// const square = x => mul(x)(x);
// const squareFunction = fnType({in: numType, out: numType});
// return {l:[
// {i: square , t: squareFunction},
// {i: squareFunction, t: Function},
// ]};
// };
// const makeSquareType = fnType({in: Typed, out: Module});
// export const ModuleSquare = {l:[
// {i: makeSquare , t: makeSquareType},
// {i: makeSquareType, t: Function},
// ]};