gpt4 book ai didi

haskell - 以这种方式定义构图有什么问题?

转载 作者:行者123 更新时间:2023-12-04 14:04:38 25 4
gpt4 key购买 nike

我正在经历some Arrow tutorial ,玩弄返回自己新版本的函数以试图保持某种状态。

新类型的定义如下:

newtype Circuit a b = Circuit {runCircuit :: a -> (b, Circuit a b)}

因为我希望能够组成电路,所以我将其设为 Category 的一个实例。组成两个电路时,结果也必须是一个电路。 (Circuit b c) . (Circuit a b)给出 Circuit a c .

我写了这个:
import qualified Control.Category as Cat
instance Cat.Category Circuit where
(Circuit g) . (Circuit f) = Circuit $ \a -> let
(b, new_f) = f a
(c, new_g) = g b
new_circ = new_g . new_f
in (c, new_circ)

但它失败了:
Main.hs:70:64:
Couldn't match expected type `b0 -> c0'
with actual type `Circuit b c'
In the first argument of `(.)', namely `new_g'
In the expression: new_g . new_f
In an equation for `new_circ': new_circ = new_g . new_f

我在教程中查找了答案,这个答案引入了一个像这样的中间函数,它编译得很好:
(.) = dot where
(Circuit g) `dot` (Circuit f) = Circuit $ \a -> let
(b, new_f) = f a
(c, new_g) = g b
new_circ = new_g `dot` new_f
in (c, new_circ)

我看不出有什么区别。

最佳答案

.new_g . new_f来自前奏,而不是来自Control.Category .所以你需要使用Cat.. .

但是通常的使用方式是Control.Category是:

import Prelude hiding (id, (.))
import Control.Category

关于haskell - 以这种方式定义构图有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8787338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com