Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Can't import System.Random

Tags:

haskell

I have installed Haskell on MacOS Mojave via the instructions found here, i.e. using the stack command. However,

import System.Random

brought in ghci the error message Could not find module ‘System.Random’. By searching for a solution, I came accross this discussion on Stackoverflow, and I followed the suggestion posted there by Michael Snoyman to try the command

stack install random

before entering ghci again. This command produced tons of output, one part relating to System.Random :

random> configure

random> Warning: random.cabal:15:2: Tabs used as indentation at 15:2, 16:2, 17:2

random> Configuring random-1.1...

random> build

random> Preprocessing library for random-1.1..

random> Building library for random-1.1..

random> [1 of 1] Compiling System.Random

random>

random> /private/var/folders/bg/zjbyc9fj64d5kr98_x5bfjtm0000gn/T/stack946/random-1.1/System/Random.hs:43:1: warning: [-Wtabs]

random> Tab character found here, and in 74 further locations.

random> Please use spaces instead.

random> |

random> 43 | (

random> | ^^^^^^^^

Now I some questions:

(1) What exactly was I doing with this stack install command? I know that it is risky to run a command without knowing well what it is supposed to do, but truth is this is exactly what I did. My guess is that this is fetching some libraries (similarly to Gems in Ruby or CPAN in Perl) and add them to the Haskell implementation. But if these are "official" libraries, why then do I get a syntax error? Am I supposed to run expand on the sources which had been downloaded, to get rid of the tabs?

(2) Is this really the official way to get System.Random into the installation, or is there a better way to get the Random module? I would have expected that this kind of module is already included in the distribution, like many other modules are.

(3) What can I do now to get my Random numbers? Because I still get the error that the module can't be found (not surprisingly, if the source code of the random library has syntax errors)

like image 211
user1934428 Avatar asked Jan 06 '20 19:01

user1934428


Video Answer


1 Answers

Do the following 2 steps to get stack and System.Random installed

brew install haskell-stack
stack ghci --package random
Configuring GHCi with the following packages:
GHCi, version 8.6.5: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /private/var/folders/5d/b7qfjxcd27v_3pgvdxzz1vr8nncyxt/T/haskell-stack-ghci/2a3bbd58/ghci-script
Prelude> import System.Random
Prelude System.Random>

stackage is a curated set of packages from hackage and stack is a build system toolset. Yes it's CPAN like but not system-wide, like a Python's virtualenv.

like image 58
Sergey Romanovsky Avatar answered Sep 26 '22 08:09

Sergey Romanovsky