.net - 可区分联合中的函数约束泛型参数的类型

标签 .net f# f#-3.0

我正在尝试将一些 Haskell 代码移植到 F#,但我遇到了一个奇怪的错误,我不知道如何解决。我有一个定义如下的函数的可区分联合:

type OtherType =
    OtherType1 of string
    | OtherType2 of string
type MyType<'a> = 
    MySub1 of DateTime * string * (float -> MyType<'a>)
    | MySub2 of 'a
    | MySub3 of DateTime * string * (bool -> MyType<'a>)

后来我有一个函数可以处理这种类型

let fun1 date myType (myFun2: ('b -> MyType<'a>)) = 
    match myType with
    | OtherType1(string1) -> MySub1(date, string1, myFun2)
    | OtherType2(string1) -> MySub3(date, string1, myFun2)

这将约束 myFun2 类型 (float -> MyType<'a>)。有什么办法可以防止这种情况发生并保持 k 的通用性吗?

结果是第二次模式匹配失败。

谢谢。

更新:

查看我试图复制的 Haskell 代码,我认为问题是在 Haskell 版本中,OtherType 是一个 GADT,而 OtherType1 变成了 OtherType Double,而 OtherType2 变成了 OtherType Bool。然后 myFun2 将能够执行这两个功能。如果有人感兴趣,这是代码。

data OtherType a where
    OtherType1 :: String -> OtherType Double
    OtherType2 :: String -> OtherType Bool 

data MyType a = MySub1 UTCTime String (Double -> MyType a)
    | MySub2 a
    | MySub3 UTCTime String (Bool -> MyType a)

myFun1 :: UTCTime -> OtherType a -> MyType a
myFun1 time o = myFun1' o MySub2
    where
        myFun1' :: OtherType b-> (b -> MyType a) -> MyType a
        myFun1' (OtherType1 name) k = MySub1 time name k
        myFun1' (OtherType2 name) k = MySub3 time name k                

所以我想要问的问题是,GADT 能否在 F# 中复制?

最佳答案

据我所知,在 F# 中没有可靠的方法来表示任意 GADT。但是,鉴于您的 GADT 结构和您正在编写的函数,应该可以使用以下(笨拙的)编码:

// Module for witnessing the equality of two types
module Eq =
    // opaque type equating 'a and 'b
    type eq<'a,'b> = private Eq of ('a -> 'b) * ('b -> 'a) with
        member eq.Apply(v) = match eq with | Eq(f,_) -> f v
        member eq.Unapply(v) = match eq with | Eq(_,g) -> g v

    // constructs an eq<'a,'a>
    [<GeneralizableValue>]
    let refl<'a> : eq<'a,'a> = Eq(id,id)

    // Not used, but included for completeness
    let sym (Eq(f,g)) = Eq(g,f)
    let trans (Eq(f,g)) (Eq(h,i)) = Eq(f >> h, i >> g)

    // ideally, we'd also provide a way to lift an eq<'a,'b> to an eq<F<'a>,F<'b>>, but this can't be expressed by F#'s type system

type OtherType<'a> =
| OtherType1 of Eq.eq<'a,double> * string
| OtherType2 of Eq.eq<'a,bool> * string

// "smart" constructors
let otherType1 s = OtherType1(Eq.refl, s)
let otherType2 s = OtherType2(Eq.refl, s)

type MyType<'a> =
| MySub1 of DateTime * string * (float -> MyType<'a>)
| MySub2 of 'a
| MySub3 of DateTime * string * (bool -> MyType<'a>)

let fun1 date myType myFun2 = 
    match myType with
    | OtherType1(eq, string1) -> MySub1(date, string1, eq.Unapply >> myFun2)
    | OtherType2(eq, string1) -> MySub3(date, string1, eq.Unapply >> myFun2)

关于.net - 可区分联合中的函数约束泛型参数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16629098/

相关文章:

c# - 使用 asp.NET 保存 Excel 文件时出现问题

F# Seq.FindIndex 从末尾开始而不是从开头开始?

parallel-processing - F#如何并行做List.map

postgresql - Postgresql 的 FSharp 数据类型提供程序

.net - 最佳实践 : 3-Tier Architecture in LINQ

.net - 在控制台应用程序中托管 WCF 服务时出现 IMetadataExchange MEX 端点错误

c# - 如何将查询字符串添加到 httpwebrequest

.net - 在 F# 中使用 System.Collections Queue 类时出现类型参数错误

F# 查询表达式产量

f# - 查询表达式连接如何取决于键的顺序?