首页 文章

区分联盟约束通用参数类型的作用

提问于
浏览
3

我试图将一些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约束为type(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#中复制吗?

2 回答

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

    我怀疑你想要使用泛型方法模拟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"
        }
    

相关问题