// ============================================================
// Type-Level Computation Reference (类型级推导参考实现)
//
// 定义 5 种逻辑门类型(Xor / And / Or / Nand / Nor),
// 通过泛型结构体实现"类型即计算":
// 结果 = IBit.Value 属性在运行时沿嵌套类型链递归求值
//
// 被测试 AI 必须在不运行代码的情况下,
// 通过纯心算跟踪 40 层嵌套的泛型参数展开,
// 才能输出正确答案(0 或 1)。
// ============================================================
// ─
interface IBit { int Value { get; } }
struct B0 : IBit { public int Value => 0; }
struct B1 : IBit { public int Value => 1; }
struct And<A, B> : IBit
where A : struct, IBit
where B : struct, IBit
{
public int Value => default(A).Value & default(B).Value;
}
struct Or<A, B> : IBit
where A : struct, IBit
where B : struct, IBit
{
public int Value => default(A).Value | default(B).Value;
}
struct Xor<A, B> : IBit
where A : struct, IBit
where B : struct, IBit
{
public int Value => default(A).Value ^ default(B).Value;
}
struct Nand<A, B> : IBit
where A : struct, IBit
where B : struct, IBit
{
public int Value => (default(A).Value & default(B).Value) ^ 1;
}
struct Nor<A, B> : IBit
where A : struct, IBit
where B : struct, IBit
{
public int Value => (default(A).Value | default(B).Value) ^ 1;
}
public static class Test
{
// ── 主入口 ──
public static void Run()
{
Console.WriteLine(
new Nor<
And<Nand<Xor<Nor<Or<Nand<Xor<B0, B1>,
Nor<And<Or<Xor<Nand<B0, B1>, B1>, B0>, B1>, B0>>, B1>,
Xor<Nand<Or<And<Nor<B0, B1>, B0>, B1>, B0>, B1>>,
B0>,
Nand<Xor<Nor<B0, B1>, And<Or<Xor<Nand<B1, B0>, B1>, B0>, B1>>>, B1>>
>().Value);
}
}
LLM测试1,修复
使用泛型 类型"Nor<A, B>"需要 2 个类型参数
使用泛型 类型"And<A, B>"需要 2 个类型参数
使用泛型 类型"Nand<A, B>"需要 2 个类型参数
使用泛型 类型"Nand<A, B>"需要 2 个类型参数
报错