如何使用静态解析类型参数解决递归映射中的奇怪类型错误?

How to resolve the strange type error in a recursive map with statically resolved type parameters?

type CudaInnerExpr<'t> = CudaInnerExpr of expr: string with
    member t.Expr = t |> fun (CudaInnerExpr expr) -> expr

type CudaScalar<'t> = CudaScalar of name: string with
    member t.Name = t |> fun (CudaScalar name) -> name

type CudaAr1D<'t> = CudaAr1D of CudaScalar<int> * name: string with
    member t.Name = t |> fun (CudaAr1D (_, name)) -> name

type CudaAr2D<'t> = CudaAr2D of CudaScalar<int> * CudaScalar<int> * name: string with
    member t.Name = t |> fun (CudaAr2D (_, _, name)) -> name

type ArgsPrinter = ArgsPrinter with
    static member inline PrintArg(_: ArgsPrinter, t: CudaScalar<float32>) = sprintf "float %s" t.Name
    static member inline PrintArg(_: ArgsPrinter, t: CudaScalar<int>) = sprintf "int %s" t.Name
    static member inline PrintArg(_: ArgsPrinter, t: CudaAr1D<float32>) = sprintf "float *%s" t.Name
    static member inline PrintArg(_: ArgsPrinter, t: CudaAr1D<int>) = sprintf "int *%s" t.Name
    static member inline PrintArg(_: ArgsPrinter, t: CudaAr2D<float32>) = sprintf "float *%s" t.Name
    static member inline PrintArg(_: ArgsPrinter, t: CudaAr2D<int>) = sprintf "int *%s" t.Name

    static member inline PrintArg(_: ArgsPrinter, (x1, x2)) = 
        let inline print_arg x = 
            let inline call (tok : ^T) = ((^T or ^in_) : (static member PrintArg: ArgsPrinter * ^in_ -> string) tok, x)
            call ArgsPrinter
        [|print_arg x1;print_arg x2|] |> String.concat ", "
    static member inline PrintArg(_: ArgsPrinter, (x1, x2, x3)) = 
        let inline print_arg x = 
            let inline call (tok : ^T) = ((^T or ^in_) : (static member PrintArg: ArgsPrinter * ^in_ -> string) tok, x)
            call ArgsPrinter
        [|print_arg x1;print_arg x2;print_arg x3|] |> String.concat ", "

在行 static member inline PrintArg(_: ArgsPrinter, (x1, x2, x3)) = 中,表达式 (x1, x2, x3) 给出了以下错误:

Script1.fsx(26,52): error FS0001: This expression was expected to have type
    'in_    
but here has type
    'a * 'b * 'c

知道要怎么做才能使这项工作成功吗?

在我看来你想做这样的事情:

    ...

    static member inline PrintArg(_: ArgsPrinter, t: CudaAr2D<float32>) = sprintf "float *%s" t.Name
    static member inline PrintArg(_: ArgsPrinter, t: CudaAr2D<int>) = sprintf "int *%s" t.Name

let inline print_arg x =
    let inline call (tok : ^T) = ((^T or ^in_) : (static member PrintArg: ArgsPrinter * ^in_ -> string) tok, x)
    call ArgsPrinter       

type ArgsPrinter with
    static member inline PrintArg(_: ArgsPrinter, (x1, x2)) = [|print_arg x1;print_arg x2|] |> String.concat ", "
    static member inline PrintArg(_: ArgsPrinter, (x1, x2, x3)) = [|print_arg x1;print_arg x2;print_arg x3|] |> String.concat ", "

您在类型的中间定义泛型函数,因为您将在最后两个重载中使用它,这将变成 'recursive overloads'.

请注意,这是 FSharpPlus 当前使用的技术,实际上是对该技术的简化。

最后请注意,您的解决方案对我来说似乎也是正确的(虽然更冗长)但出于某种原因,F# 编译器会感到困惑,我无法向您解释原因,但遇到过很多这样的情况,我只能做找到一个最小的重现,一个解决方法并将它报告给 F# 人员。约束求解器中还有很多问题需要解决。