【发布时间】:2014-11-15 15:23:13
【问题描述】:
我尝试编译来自Graphics-Rendering-Plot-Gtk hackage 上的示例代码。
这是我得到的错误:
src/DynamicPlot.hs:20:6: Not in scope: ‘linspace’
src/DynamicPlot.hs:21:6: Not in scope: ‘randomVector’
src/DynamicPlot.hs:21:21: Not in scope: data constructor ‘Gaussian’
src/DynamicPlot.hs:25:6: Not in scope: ‘constant’
src/DynamicPlot.hs:31:6: Not in scope: ‘buildMatrix’
我正在使用 cabal 沙箱,这是我的 cabal 文件:
name: arduinoPlot
version: 1.0.0.2
cabal-version: >=1.6
build-type: Simple
license: BSD3
license-file: LICENSE
copyright: (c) Simon Marlow
maintainer: Simon Marlow <marlowsd@gmail.com>
stability: stable
homepage: http://www.haskell.org/hello/
bug-reports: mailto:marlowsd@gmail.com
synopsis: Hello World, an example package
description: This is an implementation of the classic "Hello World" program in
Haskell, as an example of how to create a minimal Haskell
application using Cabal and Hackage. Please submit any suggestions and
improvements.
category: Console, Text
author: Simon Marlow
data-dir: ""
source-repository head
type: darcs
location: http://darcs.haskell.org/hello/
flag threaded
Default: False
executable arduinoPlot
build-depends: base >=4.2 && <5, hArduino >=0.9, mtl >=2.2,
easyplot >=1.0,
process,
glib >= 0.11 && < 0.14,
gtk >= 0.11 && < 0.14,
hmatrix >= 0.10,
plot < 0.3,
plot-gtk,
hmatrix-gsl-stats,
linda,
colour,
array
--plot-gtk -any, plot -any, colour -any, hmatrix == 0.16.0.6, array -any,
--random >= 1.0.1.1, storable-complex >= 0.2.1, primitive >= 0.5.0.1,
--vector >= 0.10.9.1, hmatrix >= 0.15.2.0, hmatrix-gsl-stats >= 0.2, hstatistics >= 0.2.5.1,
--linda -any
if flag(threaded)
buildable: True
ghc-options: -threaded
main-is: arduinoPlot.hs
buildable: True
hs-source-dirs: src
最后是我的测试代码:
module DynamicPlot (
dynamicMain
) where
import Data.Colour.Names
import qualified Data.Array.IArray as A
import Numeric.Vector
import Numeric.Matrix
import Numeric.GSL.Statistics
import Graphics.Rendering.Plot
import Graphics.Rendering.Plot.Gtk
ln = 25
ts = linspace ln (0,1)
rs = randomVector 0 Gaussian ln
ss = sin (15*2*pi*ts)
ds = 0.25*rs + ss
es = constant (0.25*(stddev rs)) ln
fs :: Double -> Double
fs = sin . (15*2*pi*)
ms :: Matrix Double
ms = buildMatrix 64 64 (\(x,y) -> sin (2*2*pi*(fromIntegral x)/64) * cos (5*2*pi*(fromIntegral y)/64))
figure = do
withTextDefaults $ setFontFamily "OpenSymbol"
withTitle $ setText "Testing plot package:"
withSubTitle $ do
setText "with 1 second of a 15Hz sine wave"
setFontSize 10
setPlots 1 2
withPlot (1,1) $ do
setDataset (ts,[point (ds,es,"Sampled data") (Bullet,green)
,line (fs,"15 Hz sinusoid") blue])
addAxis XAxis (Side Lower) $ do
setGridlines Major True
withAxisLabel $ setText "time (s)"
addAxis YAxis (Side Lower) $ do
setGridlines Major True
withAxisLabel $ setText "amplitude"
addAxis XAxis (Value 0) $ return ()
setRangeFromData XAxis Lower
setRange YAxis Lower (-1.25) 1.25
setLegend True NorthEast Inside
withLegendFormat $ setFontSize 6
我用于开发:
- cabal 沙盒与 cabal 版本 1.20.0.3
- arch linux
- ghc 7.8.3
- leksah 0.14.1.0(但 cabal 从命令行构建也失败了)
有谁知道为什么我会在问题的开头提到错误?
更新
可能示例代码已过时,导致我遇到错误。我修复了一些问题并获得了运行版本:
module Main (
main
) where
-- imports needed for threaded GTK
--
import Control.Concurrent
import Control.Concurrent.MVar
import Graphics.UI.Gtk
import System.Exit
import Graphics.UI.Gtk
import Control.Monad.Trans(liftIO)
-- imports needed for plot
--
import Numeric.GSL.Statistics
import Graphics.Rendering.Plot
import Graphics.Rendering.Plot.Gtk
import Numeric.LinearAlgebra
main = do
-- init
--
_ <- initGUI
exit <- newEmptyMVar
-- display figure
--
figure1 <- displayFigure
threadDelay 10000000; -- wait 10 seconds
putStrLn "finished"
displayFigure :: IO PlotHandle
displayFigure = display figure
--
-- corrected example code
--
ln = 25
ts = linspace ln (0,1)
rs = randomVector 0 Gaussian ln
ss = sin (15*2*pi*ts)
ds = 0.25*rs + ss
es = constant (0.25*(stddev rs)) ln
fs :: Double -> Double
fs = sin . (15*2*pi*)
ms :: Matrix Double
ms = buildMatrix 64 64 (\(x,y) -> sin (2*2*pi*(fromIntegral x)/64) * cos (5*2*pi*(fromIntegral y)/64))
figure = do
withTextDefaults $ setFontFamily "OpenSymbol"
withTitle $ setText "Testing plot package:"
withSubTitle $ do
setText "with 1 second of a 15Hz sine wave"
setFontSize 10
setPlots 1 2
withPlot (1,1) $ do
setDataset (ts,[point (ds,es,"Sampled data") (Bullet,green)
,line (fs,"15 Hz sinusoid") blue])
addAxis XAxis (Side Lower) $ do
setGridlines Major True
withAxisLabel $ setText "time (s)"
addAxis YAxis (Side Lower) $ do
setGridlines Major True
withAxisLabel $ setText "amplitude"
addAxis XAxis (Value 0) $ return ()
--
--
-- WHY DO THE NEXT TWO LINES LEAD TO A TYPE ERROR ?
--
--
--setRangeFromData XAxis Lower
--setRange YAxis Lower (-1.25) 1.25
setLegend True NorthEast Inside
withLegendFormat $ setFontSize 6
这是 cabal 文件:
-- Instructions on how to write this file are in the Cabal
-- documentation, which can be found here:
-- http://haskell.org/cabal/release/cabal-latest/doc/users-guide/
name: gtkPlotTest
version: 1.0.0.2
license: BSD3
license-file: LICENSE
copyright: (c) Simon Marlow
author: Simon Marlow
maintainer: Simon Marlow <marlowsd@gmail.com>
bug-reports: mailto:marlowsd@gmail.com
stability: stable
homepage: http://www.haskell.org/hello/
synopsis: Hello World, an example package
category: Console, Text
cabal-version: >= 1.6
build-type: Simple
Description:
This is an implementation of the classic "Hello World" program in
Haskell, as an example of how to create a minimal Haskell
application using Cabal and Hackage. Please submit any suggestions and
improvements.
source-repository head
type: darcs
location: http://darcs.haskell.org/hello/
flag threaded
default: False
executable gtkPlotTest
hs-source-dirs: src
main-is: gtkPlottest.hs
build-depends: base >= 4.2 && < 5,
gtk -any,
hmatrix -any,
plot -any,
plot-gtk,
hmatrix-gsl-stats,
linda,
colour,
array,
mtl
--hmatrix-static -any
-- gtk >= -any -- 0.11 && < 0.14,
-- plot any -- < 0.3,
-- if flag(threaded)
ghc-options: -threaded
还有两个问题。
首先以下两行会导致类型错误,如果我不将它们注释掉(参见上面的代码):
--
--
-- WHY DO THE NEXT TWO LINES LEAD TO A TYPE ERROR ?
--
--setRangeFromData XAxis Lower
--setRange YAxis Lower (-1.25) 1.25
其次,main 函数不是正确的 GTK 函数:我只是等待一段时间并关闭窗口。我希望窗口无限期地显示,并且在按下窗口的“x”按钮时,窗口会以适当的退出信号关闭。
这是main 函数的解决方法:
...
figure1 <- displayFigure
threadDelay 10000000; -- wait 10 seconds
putStrLn "finished"
【问题讨论】:
-
not in scope错误总是意味着:那个东西没有在你导入的模块的任何地方定义。看看你能不能在 Hayoo 上找到名字,然后导入正确的模块。 -
@leftaroundabout 示例代码可能已经过时,Hayoo 在某些情况下会有所帮助。您是否看到代码中最后两个未解决问题的任何解决方案?我正在运行一个工作版本。
-
至于第二个问题,如何很好的退出,
Graphics.Rendering.Plot.Gtk是通过GHCi交互显示,使用destroy figure会破坏窗口。如果您想将绘图合并到独立的可执行文件中,请使用Graphics.UI.Gtk.Plot.plotNew和onDestroy。查看destroy的源代码,了解如何将window传递给onDestroy。 -
这两行的类型错误是因为你需要将
Linear添加到setRangeFromData XAxis Lower Linear和setRange YAxis Lower Linear (-1.25) (1.25)。抱歉,API 更改时我没有更新示例文件。