Skip to content

Instantly share code, notes, and snippets.

@harukaeru
Created November 10, 2025 18:08
Show Gist options
  • Select an option

  • Save harukaeru/7fffb795bdad539d69ccf25cd530bd49 to your computer and use it in GitHub Desktop.

Select an option

Save harukaeru/7fffb795bdad539d69ccf25cd530bd49 to your computer and use it in GitHub Desktop.
数学的構造とTypeScript
// ========================================
// 基本的な型定義
// ========================================
// 二項演算の型
type BinaryOp<T> = (a: T, b: T) => T;
// 単項演算の型
type UnaryOp<T> = (a: T) => T;
// 関係(順序など)の型
type Relation<T> = (a: T, b: T) => boolean;
// ========================================
// 代数的構造
// ========================================
// マグマ(Magma): 閉じた二項演算を持つ集合
interface Magma<T> {
carrier: Set<T>;
op: BinaryOp<T>;
}
// 半群(Semigroup): 結合律を満たすマグマ
interface Semigroup<T> extends Magma<T> {
// 結合律: (a ⊕ b) ⊕ c = a ⊕ (b ⊕ c)
associative: true;
}
// モノイド(Monoid): 単位元を持つ半群
interface Monoid<T> extends Semigroup<T> {
identity: T; // 単位元 e: e ⊕ a = a ⊕ e = a
}
// 群(Group): 逆元を持つモノイド
interface Group<T> extends Monoid<T> {
inverse: UnaryOp<T>; // 逆元: a ⊕ inverse(a) = identity
}
// アーベル群(Abelian Group): 可換な群
interface AbelianGroup<T> extends Group<T> {
commutative: true; // 可換律: a ⊕ b = b ⊕ a
}
// 環(Ring): 二つの演算を持つ構造
interface Ring<T> {
carrier: Set<T>;
add: BinaryOp<T>; // 加法(アーベル群を成す)
addIdentity: T; // 加法単位元(0)
addInverse: UnaryOp<T>; // 加法逆元
mul: BinaryOp<T>; // 乗法(モノイドを成す)
mulIdentity: T; // 乗法単位元(1)
// 分配律: a ⊗ (b ⊕ c) = (a ⊗ b) ⊕ (a ⊗ c)
}
// 体(Field): 乗法にも逆元を持つ環
interface Field<T> extends Ring<T> {
mulInverse: (a: T) => T; // 乗法逆元(0以外)
}
// ベクトル空間(Vector Space)
interface VectorSpace<V, F> {
vectors: Set<V>;
field: Field<F>;
add: BinaryOp<V>; // ベクトル加法
scalarMul: (s: F, v: V) => V; // スカラー倍
zero: V; // 零ベクトル
}
// ========================================
// 位相的構造
// ========================================
// 位相空間(Topological Space)
interface TopologicalSpace<T> {
carrier: Set<T>;
topology: Set<Set<T>>; // 開集合系
// 位相の公理:
// 1. 空集合と全体集合が開集合
// 2. 任意個の開集合の和が開集合
// 3. 有限個の開集合の共通部分が開集合
}
// 距離空間(Metric Space)
interface MetricSpace<T> {
carrier: Set<T>;
metric: (a: T, b: T) => number; // 距離関数
// 距離の公理:
// 1. d(x,y) ≥ 0 かつ d(x,y) = 0 ⟺ x = y
// 2. d(x,y) = d(y,x) (対称性)
// 3. d(x,z) ≤ d(x,y) + d(y,z) (三角不等式)
}
// ノルム空間(Normed Space)
interface NormedSpace<V, F> extends VectorSpace<V, F> {
norm: (v: V) => number; // ノルム関数
// ノルムの公理:
// 1. ||v|| ≥ 0 かつ ||v|| = 0 ⟺ v = 0
// 2. ||αv|| = |α| ||v||
// 3. ||u + v|| ≤ ||u|| + ||v|| (三角不等式)
}
// 内積空間(Inner Product Space)
interface InnerProductSpace<V, F> extends VectorSpace<V, F> {
innerProduct: (u: V, v: V) => F; // 内積
// 内積の公理:
// 1. ⟨v,v⟩ ≥ 0 かつ ⟨v,v⟩ = 0 ⟺ v = 0
// 2. ⟨u,v⟩ = conj(⟨v,u⟩) (共役対称性)
// 3. ⟨αu + βv, w⟩ = α⟨u,w⟩ + β⟨v,w⟩ (線形性)
}
// ========================================
// 順序構造
// ========================================
// 前順序集合(Preordered Set)
interface PreorderedSet<T> {
carrier: Set<T>;
leq: Relation<T>; // ≤ 関係
// 前順序の公理:
// 1. 反射律: a ≤ a
// 2. 推移律: a ≤ b かつ b ≤ c ⟹ a ≤ c
}
// 半順序集合(Partially Ordered Set / Poset)
interface Poset<T> extends PreorderedSet<T> {
// 反対称律: a ≤ b かつ b ≤ a ⟹ a = b
antisymmetric: true;
}
// 全順序集合(Totally Ordered Set)
interface TotallyOrderedSet<T> extends Poset<T> {
// 全順序性: 任意の a, b について a ≤ b または b ≤ a
total: true;
}
// 束(Lattice)
interface Lattice<T> extends Poset<T> {
join: BinaryOp<T>; // 上限(∨)
meet: BinaryOp<T>; // 下限(∧)
}
// 完備束(Complete Lattice)
interface CompleteLattice<T> extends Lattice<T> {
top: T; // 最大元(⊤)
bottom: T; // 最小元(⊥)
// 任意の部分集合に対して上限と下限が存在
}
// ========================================
// 具体例の構築
// ========================================
// 実数の加法群
const RealAdditionGroup: Group<number> = {
carrier: new Set<number>(), // 実際は全実数
op: (a, b) => a + b,
associative: true,
identity: 0,
inverse: (a) => -a
};
// 整数の環
const IntegerRing: Ring<number> = {
carrier: new Set<number>(), // 全整数
add: (a, b) => a + b,
addIdentity: 0,
addInverse: (a) => -a,
mul: (a, b) => a * b,
mulIdentity: 1
};
// 実数の体
const RealField: Field<number> = {
...IntegerRing,
mulInverse: (a) => 1 / a
};
// ユークリッド空間 R^n の距離
const EuclideanMetric = (x: number[], y: number[]): number => {
return Math.sqrt(
x.reduce((sum, xi, i) => sum + Math.pow(xi - y[i], 2), 0)
);
};
// 実数の通常の順序
const RealOrder: TotallyOrderedSet<number> = {
carrier: new Set<number>(),
leq: (a, b) => a <= b,
antisymmetric: true,
total: true
};
// ========================================
// 型レベルでの表現(ファントム型を使用)
// ========================================
// 構造を型パラメータとして持つラッパー型
class Structure<T, S> {
constructor(
public value: T,
public structure: S
) {}
}
// 使用例
type GroupR = Structure<number, typeof RealAdditionGroup>;
type FieldR = Structure<number, typeof RealField>;
// ========================================
// より型安全な表現(Branded Types)
// ========================================
// ブランド型の定義
type Brand<K, T> = T & { __brand: K };
// 群の要素としてブランド化
type GroupElement<G> = Brand<G, any>;
// 使用例の型定義
type RealAdditive = GroupElement<'RealAdditive'>;
type RealMultiplicative = GroupElement<'RealMultiplicative'>;
// これにより、異なる構造の要素を混同することを防げる
function groupOp<G>(
g: Group<GroupElement<G>>,
a: GroupElement<G>,
b: GroupElement<G>
): GroupElement<G> {
return g.op(a, b);
}
export {
Group, Ring, Field, VectorSpace,
TopologicalSpace, MetricSpace, NormedSpace, InnerProductSpace,
Poset, Lattice, CompleteLattice,
RealAdditionGroup, IntegerRing, RealField,
Structure, GroupElement
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment