2017-02-13 68 views
1

我正在Ubuntu 16.04堆棧ghci版本8.我想在Haskell中導入System.Random,但似乎我有麻煩,可能涉及Cabal。我已經看到了類似的帖子這個網站上,但他們通常會說,這是因爲舊的版本,但我使用的版本8如何安裝system.random。也許在堆棧版本8 cabal問題

我得到的錯誤:

Failed to load interface for ‘System.Random’ 
Use -v to see a list of the files searched for. 

還告訴我,我有一個系統問題。隨機。在網上看,我發現我需要先安裝Cabal。

當我鍵入

Cabal --version 

我得到

cabal-install version 1.22.6.0 
using version 1.22.5.0 of the Cabal library 

告訴我,我已經擁有它。所以,當我嘗試用

sudo apt-get install cabal-install cabal update 

更新我得到

Reading package lists... Done 
Building dependency tree  
Reading state information... Done 
E: Unable to locate package cabal 
E: Unable to locate package update 

這是奇怪的。如果我不理它,並嘗試通過

cabal install random 

反正安裝隨機我得到

Resolving dependencies... 
All the requested packages are already installed: 
random-1.1 
Use --reinstall if you want to reinstall anyway. 

這樣看來我也有陰謀的隨機包裝。但我無法加載程序

import System.Random 

命令。

編輯:看來我有堆棧ghci版本8.0.1和ghci版本7.10.3。 System.random在ghci中加載,但不是堆棧ghci。

當我嘗試在堆棧中加載文件時,我也會得到以下內容。進入

stack ghci R.hs 

我得到

Warning: Couldn't find a component for file target /home/aa/workspace/share/haskell/chenw/hw4/R.hs. Attempting to load anyway. 
Configuring GHCi with the following packages: 
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help 
Loaded GHCi configuration from /home/aa/.ghc/ghci.conf 
Loaded GHCi configuration from /tmp/ghci19674/ghci-script 

這是否意味着有什麼毛病我安裝堆棧的?

+0

'命令和apt-get安裝陰謀安裝小集團update'失敗並不奇怪。該命令是'cabal update';它與apt-get無關。 – Ryan

+0

@Ryan,我剛剛嘗試過「cabal update && cabal install cabal-install」,但它仍然不起作用。 – domoremath

+0

https://docs.haskellstack.org/en/stable/GUIDE/#adding-dependencies。 'stack install'也可能工作。 – Ryan

回答

3

如果你想使用stack,並假設你有一個工作stack,你不需要通過apt-get安裝任何東西。

首先,更新stack

stack update 

確保您有正確的resolverversion number爲您的包裹

  • 如果你想在全球範圍內使用的堆棧,即,stack ghci <source.hs>。檢查~/.stack/global-project/stack.yaml
  • 如果您使用stack管理項目,即。 stack new <project>,stack build。檢查項目的stack.yaml

要使用stack全局(任何地方)

  • 通過stack ghci:load <source.hs>
  • 編譯源代碼安裝隨機使用GHCI stack install random
  • 通過stack ghc <source.hs>
  • 運行您的程序通過stack exec <executable_name>

您可以使用--resolver=強制堆棧使用特定的解析器。

示例:使用最新的LTS

$ stack --resolver=lts setup 
$ stack --resolver=lts install random 
$ stack --resolver=lts ghci 

要在項目目錄(stack.yaml存在)使用stack,你不需要手動安裝包;如果需要,stack會爲你安裝它,當時stack build。但是,你必須編輯<project.cabal>

build-depends:  base >= 4.7 && < 5 
        , random