I'm getting an error from GHCi that I can't explain. I'm working with the following code (the vast majority of which is seemingly-irrelevant to the issue, but I can't replicate the issue with less code; commented-out lines are ones that I would like to add to replace the dummy in 0
lines)
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
This code causes GHCi to throw six errors, including highlighting the p
in let h = p !* g in
; however, when I change that line to let g = p !* g in
it goes through. Unfortunately, doing this and then uncommenting the next line (let x = x - b*g in
) causes the same errors to be thrown (including highlighting the p
in the same spot).
p
and p0
are supposed to be (n-by-n) square matrices using the Linear package, while g
, x
and x0
are supposed to be (n-by-1) vectors; d
is an integer, opt
is a linear function on n-space, funs
is a list of convex functions on n-space, d_opt
and d_funs
are the respective gradients, eps
is real.
Any help with getting this to compile would be greatly appreciated. Thanks!
Edit: here is one of the error messages. There are similar ones for 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)
, and 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.