type inferencing "unifying" operation is now bi-directional + begin writing generic version of "unifying" operation (that should work on all types)
This commit is contained in:
parent
c5ac55b0ff
commit
33c156fc5c
5 changed files with 148 additions and 9 deletions
|
|
@ -0,0 +1,38 @@
|
|||
import { makeGeneric } from "../generics/generics";
|
||||
import { Type, typedFnType } from "../metacircular";
|
||||
import { Bool, Byte, Char, Double, Int } from "../primitives/symbols";
|
||||
import { deepEqual } from "../util";
|
||||
import { eqDictType } from "./eq_type";
|
||||
|
||||
export const getEq = numDict => numDict.eq;
|
||||
|
||||
export const ModuleEq = {l:[
|
||||
...typedFnType(eqDictType, fnType => fnType({in: Type, out: Type})),
|
||||
|
||||
...typedFnType(getEq, fnType => makeGeneric(a =>
|
||||
fnType({
|
||||
in: eqDictType(a),
|
||||
out: fnType({
|
||||
in: a,
|
||||
out: fnType({
|
||||
in: a,
|
||||
out: Bool,
|
||||
}),
|
||||
}),
|
||||
}))),
|
||||
]};
|
||||
|
||||
// all our data (and types) are encoded such that we can test equality the same way:
|
||||
|
||||
const eq = x => y => deepEqual(x,y);
|
||||
|
||||
const EqDict = {eq};
|
||||
|
||||
export const EqInstances = new Map([
|
||||
[Int, EqDict],
|
||||
[Bool, EqDict],
|
||||
[Double, EqDict],
|
||||
[Byte, EqDict],
|
||||
[Char, EqDict],
|
||||
[Type, EqDict],
|
||||
]);
|
||||
4
typeclasses/eq_type.js
Normal file
4
typeclasses/eq_type.js
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import { DefaultMap } from "../util.js";
|
||||
|
||||
const eqDictTypeRegistry = new DefaultMap(a => ({ eqDict: a }));
|
||||
export const eqDictType = a => eqDictTypeRegistry.getdefault(a, true);
|
||||
|
|
@ -2,6 +2,7 @@ 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 { getMul, NumInstances } from "./num.js";
|
||||
import { numDictType } from "./num_type.js";
|
||||
|
||||
const square = numDict => x => getMul(numDict)(x)(x);
|
||||
|
|
@ -21,3 +22,7 @@ console.log(assign(squareFnType, makeGeneric(() => numDictType(Int))));
|
|||
// should be: Double -> Double
|
||||
console.log(assign(squareFnType, makeGeneric(() => numDictType(Double))));
|
||||
|
||||
// to call 'square' we need:
|
||||
// - access to a mapping from types to their typeclass instantiation
|
||||
// - to know that our argument is 'Int'
|
||||
console.log(square(NumInstances.get(Int))(42n)); // 1764n
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue