序列表达式和多态递归如何一起发挥作用?

时间:2016-11-14 12:34:23

标签: f# seq recursive-datastructures parametric-polymorphism computation-expression

This project对我来说真的是source of questions

我已经了解了多态递归,我理解为什么它是一个特例,因此F#需要完整的类型注释。

对于常规功能,我可能需要一些fiddeling但通常是正确的。现在我尝试将(工作)基本toSeq调整为更专业的手指树,但不能。

我的感觉是计算表达式的使用与它有关。这是精简的工作版本:

module ThisWorks =

    module Node =
        type Node<'a> =
            | Node2 of 'a * 'a
            | Node3 of 'a * 'a * 'a

        let toList = function
            | Node2(a, b) -> [a; b]
            | Node3(a, b, c) -> [a; b; c]

    module Digit =
        type Digit<'a> =
            | One of 'a
            | Two of 'a * 'a
            | Three of 'a * 'a * 'a
            | Four of 'a * 'a * 'a * 'a

        let toList = function
            | One a -> [a]
            | Two(a, b) -> [a; b]
            | Three(a, b, c) -> [a; b; c]
            | Four(a, b, c, d) -> [a; b; c; d]

    module FingerTree =
        open Node
        open Digit

        type FingerTree<'a> =
            | Empty
            | Single of 'a
            | Deep of Digit<'a> * Lazy<FingerTree<Node<'a>>> * Digit<'a>

        let rec toSeq<'a> (tree:FingerTree<'a>) : seq<'a> = seq {
            match tree with
            | Single single ->
                yield single
            | Deep(prefix, Lazy deeper, suffix) ->
                yield! prefix |> Digit.toList
                yield! deeper |> toSeq |> Seq.collect Node.toList
                yield! suffix |> Digit.toList
            | Empty -> ()
        }

我没有设法编译的是:

module ThisDoesnt =

    module Monoids =
        type IMonoid<'m> =
            abstract Zero:'m
            abstract Plus:'m -> 'm

        type IMeasured<'m when 'm :> IMonoid<'m>> =
            abstract Measure:'m

        type Size(value) =
            new() = Size 0

            member __.Value = value

            interface IMonoid<Size> with
                member __.Zero = Size()
                member __.Plus rhs = Size(value + rhs.Value)

        type Value<'a> =
            | Value of 'a

            interface IMeasured<Size> with
                member __.Measure = Size 1

    open Monoids

    module Node =
        type Node<'m, 'a when 'm :> IMonoid<'m>> =
            | Node2 of 'm * 'a * 'a
            | Node3 of 'm * 'a * 'a * 'a

        let toList = function
            | Node2(_, a, b) -> [a; b]
            | Node3(_, a, b, c) -> [a; b; c]

    module Digit =
        type Digit<'m, 'a when 'm :> IMonoid<'m>> =
            | One of 'a
            | Two of 'a * 'a
            | Three of 'a * 'a * 'a
            | Four of 'a * 'a * 'a * 'a

        let toList = function
            | One a -> [a]
            | Two(a, b) -> [a; b]
            | Three(a, b, c) -> [a; b; c]
            | Four(a, b, c, d) -> [a; b; c; d]

    module FingerTree =
        open Node
        open Digit

        type FingerTree<'m, 'a when 'm :> IMonoid<'m>> =
            | Empty
            | Single of 'a
            | Deep of 'm * Digit<'m, 'a> * Lazy<FingerTree<'m, Node<'m, 'a>>> * Digit<'m, 'a>

        let unpack (Value v) = v

        let rec toSeq<'a> (tree:FingerTree<Size, Value<'a>>) : seq<'a> = seq {
            match tree with
            | Single(Value single) ->
                yield single
            | Deep(_, prefix, Lazy deeper, suffix) ->
                yield! prefix |> Digit.toList |> List.map unpack

                #if ITERATE
                for (Value deep) in toSeq deeper do
                                    ^^^^^
                    yield deep

                #else

                yield! deeper |> toSeq |> Seq.collect (Node.toList >> List.map unpack)
                                 ^^^^^
                #endif

                yield! suffix |> Digit.toList |> List.map unpack
            | Empty -> ()
        }

我收到的错误消息

  

错误类型不匹配。期待着       FingerTree&LT;尺寸,节点&lt;尺寸,数值&LT;&#39 a取代;&GT;&GT; - &GT; &#39; b
  但是给了一个       FingerTree&LT;尺寸,数值&LT;&#39 c取代;&GT; - &GT; SEQ&LT;&#39 c取代;
  类型&#39;节点&lt;大小,值&lt;&#39;&gt;&gt;&#39;与类型&#39;值&lt;&#b;&gt;&#39;

不匹配

并且曲线强调了toSeq的递归调用。

我知道“更深层”类型封装在Node中,而在工作代码中我​​只是将其解压缩。但是在这里,编译器在我有机会解压之前就已经开始了。尝试for (Value deep) in toSeq deeper do yield deep会遇到同样的问题。

我已经有了出路,即之后使用“基础”toSeqTree的{​​{1}}。不正确,尝试收益率一个非常相似的错误信息。

我很好奇是什么让这段代码破解以及如何修复它。

1 个答案:

答案 0 :(得分:3)

编译器的错误消息对我来说似乎很清楚:toSeq仅适用于某些FingerTree<Size, Value<'a>>类型'a的值,但您尝试调用它取而代之的是FingerTree<Size,Node<Size,Value<'a>>>类型的值,这是不兼容的。没有什么特定的多态递归或序列表达式,这些类型不匹配。

相反,似乎通过采用toSeq类型的输入(不引用FingerTree<Size, 'a>)使Value更通用会更简单,这将使递归成为可能打电话给你想要。然后,您可以通过使用toSeq编写更一般的Seq.map unpack来轻松派生您真正想要的更具体的功能。