21

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)

Community
  • 1
  • 1
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • 1
    First do like `~$ stack repl --package random` – Redu Jan 06 '20 at 19:44
  • 1
    What is included with "the distribution" varies because there is no one distribution of the GHC compiler. It had been packaged in many forms with greater or fewer packages included and various times. – Thomas M. DuBuisson Jan 07 '20 at 01:42

1 Answers1

23

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.

pxeger
  • 148
  • 1
  • 12
Sergey Romanovsky
  • 4,216
  • 4
  • 25
  • 27
  • Since I (obviously) have already installed `stack`, and the `stack install random` produced the errors I posted, is it advisable to repeat all three steps in exactly that sequence? What is the difference between starting ghci using `stack ghci`, and by just invoking `ghci`? – user1934428 Jan 07 '20 at 06:59
  • 1
    1. Those aren't errors, it's just a warning, you can ignore it. – Sergey Romanovsky Jan 07 '20 at 17:37
  • 2
    2. difference between `stack ghci` and `ghci` is in set of libraries available for `ghci` process. stack wraps ghci, sets up library paths which are installed in a sandbox. It's similar to python virtualenv. stack creates a sandbox for every project when you do `stack setup`. Please read [quick start guide](https://docs.haskellstack.org/en/stable/README/#quick-start-guide) – Sergey Romanovsky Jan 07 '20 at 17:43
  • Thank you very much. Doing `stack ghci` did the trick. And thanks a lot for the _stack_ quick start guide. I certainly will read it. – user1934428 Jan 08 '20 at 09:10
  • `stack install` should only be used for building packages that have executable targets that you want to use. Example `stack install hindent`, this command will build the package and copy a binary to local `bin` so you can run it in a shell. In order to make package available for the ghci session you should use `--package` argument. – lehins Jan 19 '20 at 21:57
  • I am currently running 2 versions of ghci. How do you install a specific version? – XLVII Sep 10 '22 at 07:11
  • @XLVII I think this should answer your question: https://stackoverflow.com/questions/7751222/multiple-side-by-side-versions-with-homebrew – Sergey Romanovsky Sep 15 '22 at 22:15