From 6023efc2952c2eb2ca82432b11e008752b423ea1 Mon Sep 17 00:00:00 2001 From: Joeri Exelmans Date: Fri, 14 Mar 2025 17:05:04 +0100 Subject: [PATCH] use fnType everywhere to create function types --- function_registry.js | 2 +- lib/id.js | 3 ++- lib/square.js | 5 +++-- metacircular.js | 8 ++++---- primitives/bool.js | 5 +++-- primitives/double.js | 9 +++++---- primitives/int.js | 9 +++++---- structures/list.js | 5 +++-- structures/list_common.js | 9 +++++---- typed.js | 5 ++--- 10 files changed, 33 insertions(+), 27 deletions(-) diff --git a/function_registry.js b/function_registry.js index 287eb12..b23d786 100644 --- a/function_registry.js +++ b/function_registry.js @@ -2,7 +2,7 @@ import { DefaultMap } from "./util.js"; const mapping = new DefaultMap(() => new Map()); -export const getFnType = (inType, outType) => { +export const fnType = ({in: inType, out: outType}) => { const m2 = mapping.getdefault(inType); if (m2.has(outType)) { return m2.get(outType); diff --git a/lib/id.js b/lib/id.js index 038a100..4fedbe8 100644 --- a/lib/id.js +++ b/lib/id.js @@ -1,3 +1,4 @@ +import { fnType } from "../function_registry.js"; import {Function} from "../metacircular.js"; // import {Typed} from "../typed.js"; @@ -29,7 +30,7 @@ import {Function} from "../metacircular.js"; // generates explicitly typed id-function export const makeIdFn = typ => { - const Typ_to_Typ = {in: typ, out: typ}; + const Typ_to_Typ = fnType({in: typ, out: typ}); const id = x => x; return [ {i: id , t: Typ_to_Typ}, diff --git a/lib/square.js b/lib/square.js index c4c3907..6328aa2 100644 --- a/lib/square.js +++ b/lib/square.js @@ -2,6 +2,7 @@ import {Function, getIn, getOut} from "../metacircular.js"; import {Module} from "../structures/module.js"; import { deepEqual } from "../util.js"; import { Typed } from "../typed.js"; +import { fnType } from "../function_registry.js"; // import {Num, NumDict, getType, getMul} from "../typeclasses/num.js"; @@ -34,14 +35,14 @@ export const makeSquare = ({i: mul, t: mulFunction}) => { throw new Error("invalid signature"); } const square = x => mul(x)(x); - const squareFunction = {in: numType, out: numType}; + const squareFunction = fnType({in: numType, out: numType}); return [ {i: square , t: squareFunction}, {i: squareFunction, t: Function}, ]; }; -const makeSquareType = {in: Typed, out: Module}; +const makeSquareType = fnType({in: Typed, out: Module}); export const ModuleSquare = [ {i: makeSquare , t: makeSquareType}, diff --git a/metacircular.js b/metacircular.js index b8a992d..e17059a 100644 --- a/metacircular.js +++ b/metacircular.js @@ -1,4 +1,4 @@ -import { getFnType } from "./function_registry.js"; +import { fnType } from "./function_registry.js"; export const Type = Symbol('Type'); export const Function = Symbol('Function'); @@ -24,8 +24,8 @@ export const ModuleMetaCircular = [ {i: Function, t: Type}, // (Function -> Type) : Function - {i: getFnType(Function, Type), t: Function}, + {i: fnType({in: Function, out: Type}), t: Function}, - {i: getIn , t: getFnType(Function, Type)}, - {i: getOut, t: getFnType(Function, Type)}, + {i: getIn , t: fnType({in: Function, out: Type})}, + {i: getOut, t: fnType({in: Function, out: Type})}, ]; diff --git a/primitives/bool.js b/primitives/bool.js index 98beaf2..e6190a1 100644 --- a/primitives/bool.js +++ b/primitives/bool.js @@ -1,10 +1,11 @@ +import { fnType } from "../function_registry.js"; import {Type, Function} from "../metacircular.js"; import {Bool} from "./symbols.js"; const eqBool = x => y => x === y; -const Bool_to_Bool = {in: Bool, out: Bool}; -const Bool_to_Bool_to_Bool = {in: Bool, out: Bool_to_Bool}; +const Bool_to_Bool = fnType({in: Bool, out: Bool}); +const Bool_to_Bool_to_Bool = fnType({in: Bool, out: Bool_to_Bool}); export const ModuleBool = [ {i: Bool , t: Type }, diff --git a/primitives/double.js b/primitives/double.js index 3ab3de2..2209dda 100644 --- a/primitives/double.js +++ b/primitives/double.js @@ -1,3 +1,4 @@ +import { fnType } from "../function_registry.js"; import {Type, Function} from "../metacircular.js"; import {Bool, Double} from "./symbols.js"; @@ -6,10 +7,10 @@ export const addDouble = x => y => x + y; export const mulDouble = x => y => x * y; export const eqDouble = x => y => x === y; -const Double_to_Double = {in: Double, out: Double}; -const Double_to_Bool = {in: Double, out: Bool}; -export const Double_to_Double_to_Double = {in: Double, out: Double_to_Double}; -export const Double_to_Double_to_Bool = {in: Double, out: Double_to_Bool}; +const Double_to_Double = fnType({in: Double, out: Double}); +const Double_to_Bool = fnType({in: Double, out: Bool}); +export const Double_to_Double_to_Double = fnType({in: Double, out: Double_to_Double}); +export const Double_to_Double_to_Bool = fnType({in: Double, out: Double_to_Bool}); export const ModuleDouble = [ diff --git a/primitives/int.js b/primitives/int.js index cdf1907..e566384 100644 --- a/primitives/int.js +++ b/primitives/int.js @@ -1,3 +1,4 @@ +import { fnType } from "../function_registry.js"; import {Type, Function} from "../metacircular.js"; import {Bool, Int} from "./symbols.js"; @@ -6,10 +7,10 @@ export const addInt = x => y => x + y; export const mulInt = x => y => x * y; export const eqInt = x => y => x === y; -const Int_to_Int = {in: Int, out: Int }; -const Int_to_Bool = {in: Int, out: Bool}; -export const Int_to_Int_to_Int = {in: Int, out: Int_to_Int}; -export const Int_to_Int_to_Bool = {in: Int, out: Int_to_Bool}; +const Int_to_Int = fnType({in: Int, out: Int }); +const Int_to_Bool = fnType({in: Int, out: Bool}); +export const Int_to_Int_to_Int = fnType({in: Int, out: Int_to_Int}); +export const Int_to_Int_to_Bool = fnType({in: Int, out: Int_to_Bool}); export const ModuleInt = [ diff --git a/structures/list.js b/structures/list.js index 0c385bb..45f8ef7 100644 --- a/structures/list.js +++ b/structures/list.js @@ -1,8 +1,9 @@ +import { fnType } from "../function_registry.js"; import {Type, Function} from "../metacircular.js"; import { makeListModule } from "./list_common.js"; -const Type_to_Type = {in: Type, out: Type}; -const Type_to_Module = {in: Type, out: Module}; +const Type_to_Type = fnType({in: Type, out: Type}); +const Type_to_Module = fnType({in: Type, out: Module}); export const ModuleList = [ {i: getListType , t: Type_to_Type}, diff --git a/structures/list_common.js b/structures/list_common.js index 6062a52..cc713f3 100644 --- a/structures/list_common.js +++ b/structures/list_common.js @@ -1,3 +1,4 @@ +import { fnType } from "../function_registry.js"; import {Type, Function} from "../metacircular.js"; import {Int} from "../primitives/symbols.js"; @@ -24,12 +25,12 @@ export const makeListModule = elementType => { const emptyList = {l:[]}; const get = ls => i => ls.l[i]; - const getBoundType = {in: Int, out: elementType} - const getType = {in: ListOfElement, out: getBoundType}; + const getBoundType = fnType({in: Int, out: elementType}); + const getType = fnType({in: ListOfElement, out: getBoundType}); const push = ls => elem => ({l:ls.l.concat([elem])}); - const pushBoundType = {in: elementType, out: ListOfElement}; - const pushType = {in: ListOfElement, out: pushBoundType}; + const pushBoundType = fnType({in: elementType, out: ListOfElement}); + const pushType = fnType({in: ListOfElement, out: pushBoundType}); return [ {i: ListOfElement, t: Type}, diff --git a/typed.js b/typed.js index e621e3f..6ea87a3 100644 --- a/typed.js +++ b/typed.js @@ -1,4 +1,4 @@ -import { getFnType } from "./function_registry.js"; +import { fnType } from "./function_registry.js"; import {Type, Function} from "./metacircular.js"; export const Typed = Symbol('Typed'); @@ -6,8 +6,7 @@ export const Typed = Symbol('Typed'); const getInst = lnk => lnk.i; const getType = lnk => lnk.t; -// const Typed_to_Type = {in: Typed, out: Type}; -const Typed_to_Type = getFnType(Typed, Type); +const Typed_to_Type = fnType({in: Typed, out: Type}); export const ModuleTyped = [ {i: Typed, t: Type},