branching and very basic merging of slots

This commit is contained in:
Joeri Exelmans 2025-04-17 09:19:41 +02:00
parent 614e6c0fdb
commit 3978f7f835
32 changed files with 684 additions and 420 deletions

View file

@ -3,7 +3,7 @@ import { GenericType, SymbolT, Type, Unit } from "../primitives/types";
import { typedFnType } from "../structures/types";
import { Bool, Byte, Char, Double, Int } from "../primitives/types";
import { deepEqual } from "../util/util";
import { eqDictType } from "./eq_type";
import { eqDictType } from "./eq_dict";
export const getEq = numDict => numDict.eq;

4
typeclasses/eq_dict.js Normal file
View file

@ -0,0 +1,4 @@
import { makeTypeConstructor } from "../type_constructor.js";
const eqDictSymbol = Symbol('EqDict');
export const eqDictType = makeTypeConstructor(eqDictSymbol)(1);

View file

@ -1,8 +0,0 @@
import { DefaultMap } from "../util/defaultmap.js";
const eqDictSymbol = Symbol('EqDict');
const eqDictTypeRegistry = new DefaultMap(a => ({
symbol: eqDictSymbol,
params: [a],
}));
export const eqDictType = a => eqDictTypeRegistry.getdefault(a, true);

View file

@ -1,28 +0,0 @@
import { assign } from "../generics/generics.js";
import { makeGeneric } from "../generics/generics.js";
import { Double, Int } from "../primitives/types.js";
import { fnType } from "../structures/types.js";
import { pretty } from '../util/pretty.js';
import { getMul, NumInstances } from "./num.js";
import { numDictType } from "./num_type.js";
const square = numDict => x => getMul(numDict)(x)(x);
// NumDict a -> a -> a
const squareFnType = makeGeneric(a =>
fnType
(numDictType(a))
(fnType(a)(a))
);
console.log("should be: Int -> Int");
console.log(pretty(assign(squareFnType, makeGeneric(() => numDictType(Int)))));
console.log("should be: Double -> Double");
console.log(pretty(assign(squareFnType, makeGeneric(() => numDictType(Double)))));
// to call 'square' we need:
// - the type of our argument (=Int)
// - access to a mapping from types to their typeclass instantiation
console.log("");
console.log(square(NumInstances.get(Int))(42n)); // 1764n

4
typeclasses/num_dict.js Normal file
View file

@ -0,0 +1,4 @@
import { makeTypeConstructor } from "../type_constructor.js";
const numDictSymbol = Symbol("NumDict");
export const numDictType = makeTypeConstructor(numDictSymbol)(1);

View file

@ -1,8 +0,0 @@
import { DefaultMap } from "../util/defaultmap.js";
const numDictSymbol = Symbol("NumDict");
const numDictTypeRegistry = new DefaultMap(a => ({
symbol: numDictSymbol,
params: [a],
}));
export const numDictType = a => numDictTypeRegistry.getdefault(a, true);

9
typeclasses/show.js Normal file
View file

@ -0,0 +1,9 @@
import { cart2Str, NPoint2DCartesian, NPoint2DPolar, polar2Str } from "../lib/point.js";
import { Type } from "../primitives/types.js";
import { prettyT } from "../structures/types.js";
export const ShowInstances = new Map([
[Type , {show: prettyT}],
[NPoint2DCartesian, {show: cart2Str}],
[NPoint2DPolar , {show: polar2Str}],
]);

5
typeclasses/show_dict.js Normal file
View file

@ -0,0 +1,5 @@
import { makeTypeConstructor } from "../type_constructor.js";
const showDictSymbol = Symbol('ShowDict');
export const showDictType = makeTypeConstructor(showDictSymbol)(1);