From 9405ba1b4ec885285ca5afc77f990edb207d8372 Mon Sep 17 00:00:00 2001 From: Joeri Exelmans Date: Thu, 20 Mar 2025 14:09:17 +0100 Subject: [PATCH] add String type + use String to make nominal types unique (worse, but more pleasant when debugging) --- lib/point.js | 19 ++++++++++++++----- lib/square.js | 2 +- main.js | 2 +- primitives/char.js | 11 +++++++++++ primitives/symbols.js | 3 ++- structures/list.js | 2 +- structures/{ => list_types}/module.js | 6 +++--- structures/list_types/string.js | 7 +++++++ structures/sum.js | 2 +- 9 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 primitives/char.js rename structures/{ => list_types}/module.js (54%) create mode 100644 structures/list_types/string.js diff --git a/lib/point.js b/lib/point.js index 3154fc9..4b1deaa 100644 --- a/lib/point.js +++ b/lib/point.js @@ -1,17 +1,21 @@ import { Function, Type } from "../metacircular.js"; import { Double } from "../primitives/symbols.js"; +import { String } from "../structures/list_types/string.js"; import { nominalType, NominalType } from "../structures/nominal_type.js"; +import { makeProductType } from "../structures/product.js"; import { fnType, prodType, typedFnType } from "../type_registry.js"; const PointCartesian2D = nominalType( // just a unique number, to make sure that our type is only equal to itself - BigInt("0xBBAAD62B10EE21993BA690A732DA2A6875CE4B6F5E7139D5AEC9FD887F9D24A8")) - (prodType(Double, Double)); + "PointCartesian2D") + // BigInt("0xBBAAD62B10EE21993BA690A732DA2A6875CE4B6F5E7139D5AEC9FD887F9D24A8")) + (prodType(String, Double)); const PointPolar2D = nominalType( // just a unique number, to make sure that our type is only equal to itself - BigInt("0x31CDAB4B3D84C4EB27D3C111FD7580E533268B72E05BD694F8B262913E018B72")) - (prodType(Double, Double)); + "PointPolar2D") + // BigInt("0x31CDAB4B3D84C4EB27D3C111FD7580E533268B72E05BD694F8B262913E018B72")) + (prodType(String, Double)); export const cart2polar = ({left: x, right: y}) => { const r = Math.sqrt(x*x + y*y); @@ -37,10 +41,15 @@ export const scale = dr => ({left: r, right: θ}) => { return {left: r+dr, right: θ}; } +const examplePoint = {left: 1, right: 2}; + export const ModulePoint = {l:[ {i: -1 , t: Double}, {i: 2 , t: Double}, - {i: {left: 1, right: 2}, t: PointCartesian2D}, + {i: examplePoint , t: PointCartesian2D}, + {i: examplePoint , t: prodType(Double, Double)}, + + ...makeProductType(Double, Double).l, {i: PointCartesian2D , t: NominalType}, {i: PointPolar2D , t: NominalType}, diff --git a/lib/square.js b/lib/square.js index 5a598cc..9bb91cb 100644 --- a/lib/square.js +++ b/lib/square.js @@ -1,5 +1,5 @@ import {Function, getIn, getOut} from "../metacircular.js"; -import {Module} from "../structures/module.js"; +import {Module} from "../structures/list_types/module.js"; import { deepEqual } from "../util.js"; import { Typed } from "../typed.js"; import { fnType } from "../type_registry.js"; diff --git a/main.js b/main.js index a95e3c4..8479df9 100644 --- a/main.js +++ b/main.js @@ -7,7 +7,7 @@ import { makeSquare } from "./lib/square.js"; import {makeIdFn} from "./lib/id.js"; import {DefaultMap, pretty} from "./util.js"; -import { ModuleModule } from "./structures/module.js"; +import { ModuleModule } from "./structures/list_types/module.js"; import { ModuleList } from "./structures/list.js"; import {Int, Bool, Double, Byte} from "./primitives/symbols.js"; import { makeListModule } from "./structures/list_common.js"; diff --git a/primitives/char.js b/primitives/char.js new file mode 100644 index 0000000..afc489f --- /dev/null +++ b/primitives/char.js @@ -0,0 +1,11 @@ +import { typedFnType } from "../type_registry.js"; +import {Type} from "../metacircular.js"; +import {Char, Bool} from "./symbols.js"; + +const eq = x => y => x === y; + +export const ModuleChar = {l:[ + {i: Char, t: Type}, + + ...typedFnType(eq, fnType => fnType({in: Char, out: fnType({in: Char, out: Bool})})), +]}; diff --git a/primitives/symbols.js b/primitives/symbols.js index 99e0e2f..d24622e 100644 --- a/primitives/symbols.js +++ b/primitives/symbols.js @@ -3,4 +3,5 @@ export const Bool = Symbol('Bool'); export const Int = Symbol('Int'); export const Double = Symbol('Double'); -export const Byte = Symbol('Byte'); \ No newline at end of file +export const Byte = Symbol('Byte'); +export const Char = Symbol('Char'); \ No newline at end of file diff --git a/structures/list.js b/structures/list.js index 53a7cc8..d4768a4 100644 --- a/structures/list.js +++ b/structures/list.js @@ -1,7 +1,7 @@ import { fnType, lsType } from "../type_registry.js"; import {Type, Function} from "../metacircular.js"; import { makeListModule } from "./list_common.js"; -import { Module } from "./module.js"; +import { Module } from "./list_types/module.js"; const Type_to_Type = fnType({in: Type, out: Type}); const Type_to_Module = fnType({in: Type, out: Module}); diff --git a/structures/module.js b/structures/list_types/module.js similarity index 54% rename from structures/module.js rename to structures/list_types/module.js index 528206f..87cae61 100644 --- a/structures/module.js +++ b/structures/list_types/module.js @@ -1,6 +1,6 @@ -import { makeListModule } from "./list_common.js"; -import { Typed } from "../typed.js"; -import { lsType } from "../type_registry.js"; +import { makeListModule } from "../list_common.js"; +import { Typed } from "../../typed.js"; +import { lsType } from "../../type_registry.js"; export const Module = lsType(Typed); // a Module is a list of Typeds diff --git a/structures/list_types/string.js b/structures/list_types/string.js new file mode 100644 index 0000000..d25aa70 --- /dev/null +++ b/structures/list_types/string.js @@ -0,0 +1,7 @@ +import { Char } from "../../primitives/symbols.js"; +import { lsType } from "../../type_registry.js"; +import { makeListModule } from "../list_common.js"; + +export const String = lsType(Char); + +export const ModuleString = makeListModule(Char); \ No newline at end of file diff --git a/structures/sum.js b/structures/sum.js index 686fe65..709341c 100644 --- a/structures/sum.js +++ b/structures/sum.js @@ -1,6 +1,6 @@ import { fnType, prodType, sumType } from "../type_registry.js"; import { Function, Type } from "../metacircular.js"; -import { Module } from "./module.js"; +import { Module } from "./list_types/module.js"; const constructorLeft = left => ({variant: "L", value: left }); const constructorRight = right => ({variant: "R", value: right});