dope2/structures/list.js
Joeri Exelmans 8eec5b9239 recursive types (and operations on them, like pretty-printing, comparison and unification) seem to be working.
big part of the code base still needs to be 'ported' to the updated type constructors.
2025-05-05 17:17:45 +02:00

82 lines
2.4 KiB
JavaScript

import { typedFnType } from "./types.js";
import { Char, GenericType, Type } from "../primitives/types.js";
import { Int } from "../primitives/types.js";
import { makeGeneric } from "../generics/generics.js";
import { lsType } from "./types.js";
import { Dynamic } from "../primitives/dynamic.js"
// 'normal' implementation
export const emptyList = {l:[]};
// const emptyListType = makeGeneric(a => lsType(() => a));
export const get = ls => i => ls.l[i];
export const put = ls => i => elem => ({l: ls.l.with(Number(i), elem)});
export const push = ls => elem => ({l:ls.l.concat([elem])});
export const map = ls => fn => ({ l: ls.l.map(elem => fn(elem)) });
export const length = ls => ls.l.length;
export const fold = ls => callback => initial => {
let acc = initial;
for (let i=0; i<ls.l.length; i++) {
acc = callback(acc)(ls.l[i]);
}
return acc;
}
export const String = lsType(() =>Char); // alias
export const Module = lsType(() =>Dynamic);
export const ModuleList = {l:[
// // Type -> Type
// ...typedFnType(lsType, fnType =>
// fnType
// /* in */ (Type)
// /* out */ (Type)
// ),
// // [a]
// // {i: emptyList, t: emptyListType},
// // {i: emptyListType, t: GenericType},
// // [a] -> Int -> a
// ...typedFnType(get, fnType =>
// makeGeneric(a =>
// fnType
// /* in */ (() => lsType(() => a))
// /* out */ (() => fnType
// /* in */ (() => Int)
// /* out */ (() => a)
// )), GenericType),
// // [a] -> Int -> a -> [a]
// ...typedFnType(put, fnType =>
// makeGeneric(a =>
// fnType
// /* in */ (() => lsType(() => a))
// /* out */ (() => fnType
// /* in */ (() => Int)
// /* out */ (() => fnType
// /* in */ (() => a)
// /* out */ (() => lsType(() => a))
// )
// )), GenericType),
// // [a] -> a -> [a]
// ...typedFnType(push, fnType =>
// makeGeneric(a =>
// fnType
// (() => lsType(() => a))
// (() => fnType
// (() => a)
// (() => lsType(() => a))
// )
// ), GenericType),
// // [a] -> (a -> b) -> [b]
// ...typedFnType(map, fnType =>
// makeGeneric((a, b) =>
// fnType
// (() => lsType(() => a))
// (() => fnType
// (() => fnType(() => a)(() => b))
// (() => lsType(() => b))
// )), GenericType),
]};