0

我从 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 抛出六个错误,包括突出显示pin let h = p !* g in; 但是,当我将该行更改为let g = p !* g in它时。不幸的是,这样做然后取消注释下一行 ( let x = x - b*g in) 会导致抛出相同的错误(包括p在同一位置突出显示 )。

p并且p0应该是使用 Linear 包的 (n×n) 方阵,而gx应该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.
4

1 回答 1

5

有几个错误:

  • 如果整数d用作浮点数,则需要使用fromIntegral,例如: b = (1 + fromIntegral d * a)/(fromIntegral d + 1)
  • 向量和矩阵的标量乘法/除法不能使用*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)

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

于 2020-08-03T19:28:49.247 回答