dope2/structures/list.js
2025-03-24 08:25:53 +01:00

58 lines
1.3 KiB
JavaScript

import { fnType, typedFnType } from "./types.js";
import { Type } from "../type.js";
import { Int } from "../primitives/types.js";
import { makeGeneric } from "../generics/generics.js";
import { lsType } from "./types.js";
// 'normal' implementation
const emptyList = {l:[]};
const get = ls => i => ls.l[i];
const put = ls => i => elem => ({l: ls.l.with(Number(i), elem)});
const push = ls => elem => ({l:ls.l.concat([elem])});
export const ModuleList = {l:[
// Type -> Type
...typedFnType(lsType, fnType =>
fnType
/* in */ (Type)
/* out */ (Type)
),
// [a]
{i: emptyList, t: makeGeneric(a => lsType(a))},
// [a] -> Int -> a
...typedFnType(get, fnType =>
makeGeneric(a =>
fnType
/* in */ (lsType(a))
/* out */ (fnType
/* in */ (Int)
/* out */ (a)
))),
// [a] -> Int -> a -> [a]
...typedFnType(put, fnType =>
makeGeneric(a =>
fnType
/* in */ (lsType(a))
/* out */ (fnType
/* in */ (Int)
/* out */ (fnType
/* in */ (a)
/* out */ (lsType(a))
)
))),
// [a] -> a -> [a]
...typedFnType(push, fnType =>
makeGeneric(a =>
fnType
(lsType(a))
(fnType
(a)
(lsType(a))
)
)
),
]};