gpt4 book ai didi

haskell - 类型族 - 不能派生 Base Int?

转载 作者:行者123 更新时间:2023-12-01 09:28:55 24 4
gpt4 key购买 nike

这个想法是实现“惰性”长度函数来将列表长度与 Int 进行比较而不计算整个长度。

{-# LANGUAGE DeriveFunctor
, TypeFamilies
, FlexibleInstances #-}
import Data.Functor.Foldable

type instance Base Int = Maybe

现在我们可以拥有可折叠/不可折叠

instance Foldable Int where
project 0 = Nothing
project x = Just (x-1)

instance Unfoldable Int where
embed Nothing = 0
embed (Just x) = x+1

我想将 [a] 转换为 Base Int Int:

leng :: [a] -> Base Int Int
leng = ana phi where
phi :: [a] -> Base Int [a]
phi [] = Nothing
phi (_:t) = Just t

但这不起作用。它提示 [a] -> Base (Maybe Int) [a] 应该是 phi 的类型。我不明白为什么。

如果可行,那么我可以比较:

gt = curry $ hylo psi phi where
phi (Just _, Nothing) = Left True
phi (Nothing, _) = Left False
phi (Just t, Just n) = Right (t, n)

psi (Left t) = t
psi (Right t) = t

main = print $ (leng [1..]) `gt` (ana project 4)

len 有什么问题?

最佳答案

ana 的类型是 (a -> Base t a) -> a -> t .请注意,它返回普通的 t而不是 Base t t .所以 leng 的正确类型是

leng :: [a] -> Int

关于haskell - 类型族 - 不能派生 Base Int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18489022/

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