我从 GHCi 收到一个我无法解释的错误。我正在使用以下代码(其中绝大多数似乎与问题无关,但我无法用更少的代码复制问题;注释掉的行是我想添加以替换虚拟的行in 0
行)
import Linear
apply x f = f x
pos xs = -- smallest i where xs!!i > 0, else length xs
let aux xs n = case xs of
x:t -> if x > 0 then n
else aux t (n+1)
[] -> n
in aux xs 0
optimize d opt d_opt funs d_funs x0 p0 eps =
let n = length funs in
let aux x p f_best = let feas = map (apply x) funs in
let i = pos feas in
let (g,a,f_best) =
if i == n then
let g = d_opt x in
let g' = p !* g in
let prod = g `dot` g' in
let g = g / (sqrt prod) in
let f_best = min (opt x) f_best in
let a = (opt x - f_best) / (sqrt prod) in
(g,a,f_best)
else
let g = (d_funs!!i) x in
let g' = p !* g in
let prod = g `dot` g' in
let g = g / (sqrt prod) in
let a = ((funs!!i) x) / (sqrt prod) in
(g,a,f_best)
in
let b = (1+d*a)/(d+1) in
let b' = 2/(1+a) in
let b'' = (1-a^2)*(d^2)/(d^2-1) in
let h = p !* g in
let y = x - b*g in
-- let q = (p - g'*(transpose g')*b*b')*b'' in
-- aux y q f_best
0
-- in aux x0 p0 (1/0)
in 0
此代码导致 GHCi 抛出六个错误,包括突出显示p
in let h = p !* g in
; 但是,当我将该行更改为let g = p !* g in
它时。不幸的是,这样做然后取消注释下一行 ( let x = x - b*g in
) 会导致抛出相同的错误(包括p
在同一位置突出显示 )。
p
并且p0
应该是使用 Linear 包的 (n×n) 方阵,而g
和x
应该x0
是 (n×1) 向量;d
是整数,opt
是 n 空间上的线性函数,是 n 空间funs
上的凸函数列表,d_opt
并且d_funs
是各自的梯度,eps
是实数。
任何有关编译此文件的帮助将不胜感激。谢谢!
编辑:这是错误消息之一。let g = d_opt x
, let f_best = min (opt x) f_best
, let g = (d_funs!!i) x
,let a = ((funs!!i) x) / (sqrt prod)
和也有类似的let b = (1+d*a)/(d+1)
。
Lenstra.hs:57:34: error:
• Occurs check: cannot construct the infinite type: a1 ~ m a1
Expected type: m (m a1)
Actual type: m (m (m a1))
• In the first argument of ‘(!*)’, namely ‘p’
In the expression: p !* g
In an equation for ‘h’: h = p !* g
• Relevant bindings include
h :: m a1 (bound at Lenstra.hs:57:30)
b'' :: m a1 (bound at Lenstra.hs:56:30)
b' :: m a1 (bound at Lenstra.hs:55:30)
b :: m a1 (bound at Lenstra.hs:54:30)
g :: m a1 (bound at Lenstra.hs:37:31)
a :: m a1 (bound at Lenstra.hs:37:33)
aux :: m a1 -> m (m (m a1)) -> p8 -> p9 (bound at Lenstra.hs:35:9)
(Some bindings suppressed; use -fmax-relevant-binds=N or -fno-max-relevant-binds)
|
57 | let h = p !* g in
| ^
Failed, no modules loaded.