【问题标题】:Function in Discriminated Union Constraining the Type of a Generic Parameter可识别联合中约束泛型参数类型的函数
【发布时间】:2013-05-13 19:20:08
【问题描述】:

我正在尝试将一些 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)。有什么办法可以防止这种情况发生并保持 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# 中复制吗?

【问题讨论】:

  • k 不会出现在您的任何代码中。此外,您在 forst 代码块中的 MySub1 之前缺少一个 |
  • 对不起,我赶时间。该|丢失,K 应该是 myFun2。我已经更正了。
  • 更多细节:我假设match myType with |Mysub 应该是MySub2。此外,我不明白您为什么认为 myFun2 应该是通用的 - 它必须具有 float -&gt;MyType&lt;'a&gt; 的类型 MyType 的定义。
  • 我应该把它放在原始帖子中,但我希望 myFun2 是通用的原因是因为我想将 MySub3 添加到 MyType 的参数为 DateTime * string * (bool -> MyType2) 并让 myFun2 也满足该语句中的功能。
  • 我可以想到 2 种方法来做到这一点 - myfun2 将有区别的联合作为参数,或者您放弃类型安全并使用 box/unbox。

标签: .net f# f#-3.0


【解决方案1】:

我怀疑你想要可以用泛型方法模拟的 Rank-2 类型

open System

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>)

type F<'a> = abstract member Invoke<'b> : 'b -> MyType<'a>

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

fun1 DateTime.Now (OtherType1 "a") {
    new F<_> with
        member this.Invoke v = failwith "to be implemented"
    }

【讨论】:

  • 所以我尝试了这个,但是当我实现 Invoke 成员时,我收到一个错误,即代码不是通用的。类型变量 'a 无法泛化,因为它会超出其范围。
【解决方案2】:

据我所知,在 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)

【讨论】:

  • 谢谢,这看起来很接近我的需要。一个问题,我将如何提取实际值并将 myFun2 应用于它?
猜你喜欢
  • 1970-01-01
  • 2021-11-01
  • 2016-11-13
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多