gpt4 book ai didi

haskell - 纯 Haskell Lambda 微积分中列表的仿函数性

转载 作者:行者123 更新时间:2023-12-04 02:35:42 24 4
gpt4 key购买 nike

我正在尝试使用 Haskell 在纯 lambda 演算中实现各种功能。一切正常

{-# LANGUAGE RankNTypes #-}

type List a = forall b. (a -> b -> b) -> b -> b

empty :: List a
empty = const id

cons :: a -> List a -> List a
cons x xs f y = f x (xs f y)

直到 Listmap 出现。

map :: (a -> b) -> List a -> List b
map f xs = xs (cons . f) empty

导致此错误消息:

• Couldn't match type ‘List b’ with ‘(b -> b1 -> b1) -> b1 -> b1’
Expected type: b
-> ((b -> b1 -> b1) -> b1 -> b1) -> (b -> b1 -> b1) -> b1 -> b1
Actual type: b -> List b -> List b
• In the first argument of ‘(.)’, namely ‘cons’
In the first argument of ‘xs’, namely ‘(cons . f)’
In the expression: xs (cons . f) empty
• Relevant bindings include
f :: a -> b (bound at Basic.hs:12:5)
map :: (a -> b) -> List a -> List b (bound at Basic.hs:12:1)

为什么 cons 有效而 map 无效?难道 List 的每个实例都适用于 b 的每个值,因为它受 forall 约束吗?

最佳答案

Haskell 的类型系统不够强大,无法像您那样编写 map。改为这样写:

map f xs c n = xs (c . f) n

关于haskell - 纯 Haskell Lambda 微积分中列表的仿函数性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61980228/

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