use fnType everywhere to create function types

This commit is contained in:
Joeri Exelmans 2025-03-14 17:05:04 +01:00
parent a8260f2afb
commit 6023efc295
10 changed files with 33 additions and 27 deletions

View file

@ -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);

View file

@ -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},

View file

@ -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},

View file

@ -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})},
];

View file

@ -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 },

View file

@ -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 = [

View file

@ -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 = [

View file

@ -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},

View file

@ -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},

View file

@ -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},