typocomath/src/Complex/type.ts

31 lines
1.0 KiB
TypeScript
Raw Normal View History

/// <reference path="../numbers/type.ts">
import {joinTypes, typeOfDependency, Dependency} from '../core/Dispatcher.js'
export type Complex<T> = {re: T; im: T;}
export const Complex_type = {
test: <T>(dep: {testT: (z: unknown) => z is T}) =>
(z: unknown): z is Complex<T> =>
typeof z === 'object' && 're' in z && 'im' in z
&& dep.testT(z.re) && dep.testT(z.im),
infer: (dep: typeOfDependency) =>
(z: Complex<unknown>) => joinTypes(dep.typeOf(z.re), dep.typeOf(z.im)),
from: {
T: <T>(dep: Dependency<'zero', [T]>) => (t: T) =>
({re: t, im: dep.zero(t)}),
Complex: <U,T>(dep: {convert: (from: U) => T}) =>
(z: Complex<U>) => ({re: dep.convert(z.re), im: dep.convert(z.im)})
}
}
export const complex_1 = <T>(dep: Dependency<'zero', [T]>) =>
(t: T) => ({re: t, im: dep.zero(t)})
export const complex_2 = <T>(t: T, u: T) => ({re: t, im: u})
declare module "../core/Dispatcher" {
interface ImplementationTypes {
complex_1_Complex: typeof complex_1
complex_2_Complex: typeof complex_2
}
}