Skip to content

Commit

Permalink
Implement a few array-document functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wewei committed Sep 9, 2024
1 parent bea2b49 commit 12c5025
Showing 1 changed file with 73 additions and 16 deletions.
89 changes: 73 additions & 16 deletions packages/@ot-doc/document/src/lib/array-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
* data E s = Ins Integer s
* | Del Integer s
* type T s = [E s]
* @8.2 Inv:
* @8.2 Idn:
* idn :: [E s]
* idn = []
* @8.3 Inv:
* inv :: Order s => [E s] -> [E s]
* inv = fmap $ \case
* Ins n s -> Del n s
* Del n s -> Ins n s
* @8.3 Comp:
* inv = inverse . (
* fmap $ \case
* Ins n s -> Del n s
* Del n s -> Ins n s)
* @8.4 Comp:
* comp :: Order s => [E s] -> [E s] -> Maybe [E s]
* comp xs ys = combine (inverse xs) ys where
* combine [] ys = Just ys
Expand All @@ -29,21 +33,74 @@
* else Nothing
* else combine xs (x:y:ys)
* otherwise -> combine xs (x:y:ys)
* @8.4 Tran:
* @8.5 Tran:
* tran :: Order s => [E s] -> [E s] -> Maybe [E s]
* tran = liftTran tranElem where
* tranElem :: E s -> E s -> Maybe [E s]
* tranElem (Ins m x) (Ins n y)
* | m < n = Ins m x
* | m > n = Ins (m + 1) x
* | x < y = Ins m x
* | otherwise = Ins (m + 1) x
* | m < n = Just [Ins m x]
* | m > n = Just [Ins (m + 1) x]
* | x < y = Just [Ins m x)
* | otherwise = Just ]Ins (m + 1) x]
* tranElem (Del m x) (Ins n y)
* | m < n = Del m x
* | otherwise = Del (m + 1) x
* | m < n = Just [Del m x]
* | otherwise = Just [Del (m + 1) x]
* tranElem (Ins m x) (Del n y)
* | m <= n = Ins m x
* | otherwise = Ins (m - 1) x
* | m <= n = Just [Ins m x]
* | otherwise = Just [Ins (m - 1) x]
* tranElem (Del m x) (Del n y)
* |
*/
* | m < n = Just [Del m x]
* | m > n = Just [Del (m - 1) x]
* | x == y = Just []
* | otherwise = Nothing
*/

import { Ordered, PartialBinaryOperator } from "./algebra";
import { DocumentMeta } from "./document-meta";

export type Oplet<T> = {
typ: 'ins' | 'del',
idx: number,
val: T,
};

export const arrayDocument = <T>({ lt, equ }: Ordered<T>): DocumentMeta<Oplet<T>[]> => {
const rep = (a: Oplet<T>[]): Oplet<T>[] | undefined => a; // TODO
const liftTran = (tranElem: (eA: Oplet<T>) => (eB: Oplet<T>) => Oplet<T>[] | undefined): PartialBinaryOperator<Oplet<T>[]> => {
const tran: PartialBinaryOperator<Oplet<T>[]> = (a) => (b) => {
if (a.length === 0) return [];
if (b.length === 0) return a;

};
return tran;
};

return {
idn: [],
inv: (a) =>
a
.map(({ typ, idx, val }): Oplet<T> => ({
typ: typ === 'ins' ? 'del' : 'ins',
idx,
val,
}))
.reverse(),
comp: (a) => (b) => rep([...a, ...b]),
tran: (a) => (b) => a, // TODO
equ: (a) => (b) => {
if (a === b) return true;
const rA = rep(a);
const rB = rep(b);
if (!rA || !rB) return false;
if (rA.length !== rB.length) return false;
for (let i = 0; i < rA.length; i += 1) {
const eA = rA[i];
const eB = rB[i];
if (eA.typ !== eB.typ || eA.idx !== eB.idx || !equ(eA.val)(eB.val)) {
return false;
}
}
return true;
},
};
};

0 comments on commit 12c5025

Please sign in to comment.