diff --git a/generics/generics.js b/generics/generics.js index 58cd914..8a4754f 100644 --- a/generics/generics.js +++ b/generics/generics.js @@ -1,8 +1,9 @@ import { lsType } from "../structures/list_common.js"; import { fnType } from "../metacircular.js"; -import { deepEqual, pretty } from "../util.js"; +import { deepEqual, DefaultMap, pretty } from "../util.js"; +import { numDictType } from "../typeclasses/num_type.js"; -const genericTypeRegistry = new DefaultMap(underlyingType => ({generic: underlyingType})); +const genericTypeRegistry = new DefaultMap(underlyingType => ({ generic: underlyingType })); // type constructor for generic kinds, // for instance: @@ -23,15 +24,15 @@ export const makeGeneric = callback => { const c = Symbol('c'); const d = Symbol('d'); const e = Symbol('e'); - const type = callback(a,b,c,d,e); + const type = callback(a, b, c, d, e); return { - typeVars: occurring(type, new Set([a,b,c,d,e])), + typeVars: occurring(type, new Set([a, b, c, d, e])), type, }; }; // From the given set of type variables, return only those that occur in the given type. -const occurring = (type, typeVars) => { +export const occurring = (type, typeVars) => { if (typeVars.has(type)) { return new Set([type]); } @@ -130,9 +131,24 @@ export const unify = ( } if (formalType.numDict !== undefined) { - + if (actualType.numDict === undefined) { + throw new Error(`cannot assign ${pretty(actualType)} to ${pretty(formalType)}`); + } + else { + // both are NumDict type + const underlyingType = unify( + {typeVars: formalTypeVars, type: formalType.numDict}, + {typeVars: actualTypeVars, type: actualType.numDict}); + return { + substitutions: underlyingType.substitutions, + typeVars: new Set([ + ...actualTypeVars, + ...formalTypeVars].filter(a => !underlyingType.substitutions.has(a))), + type: numDictType(underlyingType.type), + }; + } } - throw new Error("i don't know what to do :(") + throw new Error("i don't know what to do :("); }; // export const matchConcrete = ({typeVars, type: formalType}, actualType) => { @@ -167,4 +183,4 @@ export const assign = (genFnType, paramType) => { typeVars: matchedInType.typeVars, type: substitutedOutType, }; -} +}; diff --git a/generics/types.js b/generics/types.js new file mode 100644 index 0000000..139597f --- /dev/null +++ b/generics/types.js @@ -0,0 +1,2 @@ + + diff --git a/metacircular.js b/metacircular.js index 5070987..5601f13 100644 --- a/metacircular.js +++ b/metacircular.js @@ -53,7 +53,6 @@ export const typedFnType = (instance, callback) => { export const typedFnType2 = callback => { const fnTs = []; const wrappedFnType = ({ in: inType, out: outType }) => { - console.log('wrappedFnType called'); const fnT = fnType({ in: inType, out: outType }); fnTs.push(fnT); return fnT; diff --git a/package.json b/package.json index d76b85f..c0a0cfb 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,5 @@ { + "type": "module", "dependencies": { "@inquirer/prompts": "^7.4.0" } diff --git a/structures/sum.js b/structures/sum.js index eca45d6..0c1dcf1 100644 --- a/structures/sum.js +++ b/structures/sum.js @@ -1,8 +1,8 @@ -import { sumType } from "../type_registry.js"; import { prodType } from "./product.js"; import { fnType } from "../metacircular.js"; import { Function, Type } from "../metacircular.js"; import { Module } from "./list_types/module.js"; +import { DefaultMap } from "../util.js"; const sumTypeRegistry = new DefaultMap(leftType => new DefaultMap(rightType => ({ operator: "sum", leftType, rightType }))); diff --git a/typeclasses/eq.js b/typeclasses/eq.js new file mode 100644 index 0000000..e69de29 diff --git a/typeclasses/num.js b/typeclasses/num.js index 8996933..c68cd60 100644 --- a/typeclasses/num.js +++ b/typeclasses/num.js @@ -1,10 +1,9 @@ -import { makeGeneric } from "../generics/generics"; -import { addDouble, mulDouble } from "../primitives/double"; -import { addInt, mulInt } from "../primitives/int"; -import { typedFnType, typedFnType2 } from "../metacircular"; - -const numDictTypeRegistry = new DefaultMap(a => ({numDict: a})); -export const numDictType = a => numDictTypeRegistry.getdefault(a, true); +import { makeGeneric } from "../generics/generics.js"; +import { addDouble, mulDouble } from "../primitives/double.js"; +import { addInt, mulInt } from "../primitives/int.js"; +import { Type, typedFnType, typedFnType2 } from "../metacircular.js"; +import { Double, Int } from "../primitives/symbols.js"; +import { numDictType } from "./num_type.js"; export const getAdd = numDict => numDict.add; export const getMul = numDict => numDict.mul; @@ -40,8 +39,8 @@ const DoubleNumDict = { } export const ModuleNumInstances = {l:[ - {i: IntNumDict , t: NumDict}, - {i: DoubleNumDict, t: NumDict}, + {i: IntNumDict , t: numDictType(Int)}, + {i: DoubleNumDict, t: numDictType(Double)}, ]}; // mapping from type to type class implementation diff --git a/typeclasses/num.test.js b/typeclasses/num.test.js new file mode 100644 index 0000000..95c3f19 --- /dev/null +++ b/typeclasses/num.test.js @@ -0,0 +1,23 @@ +import { assign } from "../generics/generics.js"; +import { makeGeneric } from "../generics/generics.js"; +import { fnType } from "../metacircular.js"; +import { Double, Int } from "../primitives/symbols.js"; +import { numDictType } from "./num_type.js"; + +const square = numDict => x => getMul(numDict)(x)(x); + +const squareFnType = makeGeneric(a => + fnType({ + in: numDictType(a), + out: fnType({ + in: a, + out: a, + }), + })); + +// should be: Int -> Int +console.log(assign(squareFnType, makeGeneric(() => numDictType(Int)))); + +// should be: Double -> Double +console.log(assign(squareFnType, makeGeneric(() => numDictType(Double)))); + diff --git a/typeclasses/num_type.js b/typeclasses/num_type.js new file mode 100644 index 0000000..54537c9 --- /dev/null +++ b/typeclasses/num_type.js @@ -0,0 +1,4 @@ +import { DefaultMap } from "../util.js"; + +const numDictTypeRegistry = new DefaultMap(a => ({ numDict: a })); +export const numDictType = a => numDictTypeRegistry.getdefault(a, true); diff --git a/typeclasses/resolve.js b/typeclasses/resolve.js new file mode 100644 index 0000000..8aaf7c0 --- /dev/null +++ b/typeclasses/resolve.js @@ -0,0 +1 @@ +// const resolveTypeClass = (mapping, ) \ No newline at end of file