haskell 中的 RankNTypes。列表理解有效但地图不
RankNTypes in haskell. List comprehension works but map does not
我想具体了解为什么 地图在以下情况下不起作用:
{-# Language RankNTypes #-}
module Demo where
import Numeric.AD
newtype Fun = Fun (forall a. Num a => [a] -> a)
test1 :: Fun
test1 = Fun $ \[u, v] -> (v - (u * u * u))
test2 :: Fun
test2 = Fun $ \ [u, v] -> ((u * u) + (v * v) - 1)
works :: (Ord a, Num a) => [[a] -> [a]]
works = [ grad f | Fun f <- fList ]
fList :: [Fun]
fList = [test1, test2]
将其放入 GHCI 我得到以下信息:
*Demo> w = [ grad f | Fun f <- fList ]
*Demo> map (\f -> f [1, 1]) w
[[-3,1],[2,2]]
效果很好,但是据我所知应该做同样的事情的以下内容不起作用。为什么不?这里有什么问题?
*Demo> map (\f -> grad (Fun f)) fList
<interactive>:31:18: error:
• Couldn't match expected type ‘f (Numeric.AD.Internal.Reverse.Reverse
s a)
-> Numeric.AD.Internal.Reverse.Reverse s a’
with actual type ‘Fun’
• Possible cause: ‘Fun’ is applied to too many arguments
In the first argument of ‘grad’, namely ‘(Fun f)’
In the expression: grad (Fun f)
In the first argument of ‘map’, namely ‘(\ f -> grad (Fun f))’
• Relevant bindings include
it :: [f a -> f a] (bound at <interactive>:31:1)
<interactive>:31:22: error:
• Couldn't match expected type ‘[a1] -> a1’ with actual type ‘Fun’
• In the first argument of ‘Fun’, namely ‘f’
In the first argument of ‘grad’, namely ‘(Fun f)’
In the expression: grad (Fun f)
我正在使用的库是 haskell 广告库:https://hackage.haskell.org/package/ad-4.3.4
干杯!
这些不等价:
*Demo> [ grad f | Fun f <- fList ]
*Demo> map (\f -> grad (Fun f)) fList
第一个,粗略地说,从fList
中提取一个值,比如x
,选择f
使得Fun f = x
,然后调用grad f
.
第二个从 fList
中提取一个值,称为 f
(!),并计算 Fun f
,并将其传递给 grad
。
因此,第一个从列表元素中删除 Fun
包装器,第二个添加包装器。
比较一下:
*Demo> map (\ (Fun f) -> grad f) fList
这将像列表理解一样删除包装器。
我想具体了解为什么 地图在以下情况下不起作用:
{-# Language RankNTypes #-}
module Demo where
import Numeric.AD
newtype Fun = Fun (forall a. Num a => [a] -> a)
test1 :: Fun
test1 = Fun $ \[u, v] -> (v - (u * u * u))
test2 :: Fun
test2 = Fun $ \ [u, v] -> ((u * u) + (v * v) - 1)
works :: (Ord a, Num a) => [[a] -> [a]]
works = [ grad f | Fun f <- fList ]
fList :: [Fun]
fList = [test1, test2]
将其放入 GHCI 我得到以下信息:
*Demo> w = [ grad f | Fun f <- fList ]
*Demo> map (\f -> f [1, 1]) w
[[-3,1],[2,2]]
效果很好,但是据我所知应该做同样的事情的以下内容不起作用。为什么不?这里有什么问题?
*Demo> map (\f -> grad (Fun f)) fList
<interactive>:31:18: error:
• Couldn't match expected type ‘f (Numeric.AD.Internal.Reverse.Reverse
s a)
-> Numeric.AD.Internal.Reverse.Reverse s a’
with actual type ‘Fun’
• Possible cause: ‘Fun’ is applied to too many arguments
In the first argument of ‘grad’, namely ‘(Fun f)’
In the expression: grad (Fun f)
In the first argument of ‘map’, namely ‘(\ f -> grad (Fun f))’
• Relevant bindings include
it :: [f a -> f a] (bound at <interactive>:31:1)
<interactive>:31:22: error:
• Couldn't match expected type ‘[a1] -> a1’ with actual type ‘Fun’
• In the first argument of ‘Fun’, namely ‘f’
In the first argument of ‘grad’, namely ‘(Fun f)’
In the expression: grad (Fun f)
我正在使用的库是 haskell 广告库:https://hackage.haskell.org/package/ad-4.3.4
干杯!
这些不等价:
*Demo> [ grad f | Fun f <- fList ]
*Demo> map (\f -> grad (Fun f)) fList
第一个,粗略地说,从fList
中提取一个值,比如x
,选择f
使得Fun f = x
,然后调用grad f
.
第二个从 fList
中提取一个值,称为 f
(!),并计算 Fun f
,并将其传递给 grad
。
因此,第一个从列表元素中删除 Fun
包装器,第二个添加包装器。
比较一下:
*Demo> map (\ (Fun f) -> grad f) fList
这将像列表理解一样删除包装器。