Haskell 代码不会使用某些变量名称进行编译

zjs*_*zjs 0 haskell haskell-linear

我从 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 
Run Code Online (Sandbox Code Playgroud)

此代码导致 GHCi 抛出六个错误,包括突出显示pin let h = p !* g in; 然而,当我改变那条线时,let g = p !* g in它会通过。不幸的是,这样做然后取消对下一行 ( let x = x - b*g in) 的注释会导致抛出相同的错误(包括p在同一位置突出显示)。

p并且p0应该是(n乘n)的使用线性包方阵,而gxx0应该是(n乘1)载体; d是整数,opt是在n维空间的线性函数,funs是凸函数对n维空间的列表,d_optd_funs是各自的梯度,eps是真实的。

任何帮助编译它的帮助将不胜感激。谢谢!

编辑:这是错误消息之一。类似的还有药粥let g = d_opt xlet f_best = min (opt x) f_bestlet g = (d_funs!!i) xlet 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.
Run Code Online (Sandbox Code Playgroud)

K. *_*uhr 5

有几个错误:

  • 如果整数d用作浮点数,则需要使用fromIntegral,例如: b = (1 + fromIntegral d * a)/(fromIntegral d + 1)
  • 向量和矩阵的标量乘法/除法不能使用*and /。您必须使用*^^*以及^/运营商对向量和!!*!!/的矩阵。
  • 向量和矩阵的逐点和和差不能使用+and -。您必须将^+^and^-^用于向量和!+!and!-!用于矩阵。
  • 对于 vector g'transpose g'不会起作用,所以g' * transpose g'没有祈祷。使用outer g' g'以获得产品的产生的矩阵g'与列向量g'作为行向量,如果这是你想要做什么。
  • 没有定义的g'范围q。也许你想g'从你的if陈述中回来?
  • let g = some expression with g不会起作用,因为您将创建一个永远循环的递归定义。您将需要使用一个新变量——您在某些地方正确地做到了这一点,但在其他地方却没有。

还有一个严重的逻辑错误,至少在您的注释语句未注释的版本中。aux除了对 的尾调用之外aux,该函数从不返回任何内容,因此它必然会永远循环。我什至不知道它应该返回什么类型。您需要一些停止条件(可能返回f_best或什么)。

您会发现添加类型签名optimize及其aux功能有助于控制这些错误。以下类型检查但仍包含几个错误(无限循环等):

import Linear
import Data.Vector (Vector)

apply x f = f x

pos :: (Ord a, Num a) => [a] -> Int
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

type Matrix a = Vector (Vector a)

optimize
  :: Integer
  -> (Vector Double -> Double)
  -> (Vector Double -> Vector Double)
  -> [Vector Double -> Double]
  -> [Vector Double -> Vector Double]
  -> Vector Double
  -> Matrix Double
  -> Double
  -> a
optimize d opt d_opt funs d_funs x0 p0 eps =
    let n = length funs in
    let aux
          :: Vector Double
          -> Matrix Double
          -> Double
          -> a
        aux x p f_best = let feas = map (apply x) funs in
                         let i = pos feas in
                         let g :: Vector Double
                             (g,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  -- **LOOP**
                                    let f_best = min (opt x) f_best in
                                    let a = (opt x - f_best) / (sqrt prod) in
                                    (g,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  -- **LOOP**
                                    let a = ((funs!!i) x) / (sqrt prod) in
                                    (g,g',a,f_best)
                         in
                         let b = (1+fromIntegral d*a)/(fromIntegral d+1) in
                         let b' = 2/(1+a) in
                         let b'' = (1-a^2)*(fromIntegral d^2)/(fromIntegral d^2-1) in
                         let h = p !* g in
                         let y = x ^-^ b*^g in
                         let q = (p !-! outer g' g' !!* (b*b')) !!* b'' in
                         aux y q f_best
    in aux x0 p0 (1/0)
Run Code Online (Sandbox Code Playgroud)

最后,当您运行该程序时,您可能希望将其提交给Code Review Stack Exchange,连同算法说明和一些运行示例。我认为有很多风格上的改进可以使它更惯用。