2

The program below, diagram1.hs, is from the PDF "What I Wish I Knew When Learning Haskell" by Stephen Diehl; see the chapter titled "Graphics".

  1. I am using MacOS Ventura 13.4.1 (c) (22F770820d)

  2. My shell is zsh

  3. The project1 folder was completely empty except for file, diagram1.hs

import Diagrams.Prelude
import Diagrams.Backend.SVG.CmdLine

sierpinski :: Int -> Diagram SVG
sierpinski 1 = eqTriangle 1
sierpinski n =
      s
     ===
  (s ||| s) # centerX
  where
    s = sierpinski (n - 1)

example :: Diagram SVG
example = sierpinski 5 # fc black

main :: IO ()
main = defaultMain example
-- $ runhaskell diagram1.hs -w 256 -h 256 -o diagram1.svg%       

On reddit.com, a user suggested:

One option would be:

  1. Install stack:

     curl -sSL https://get.haskellstack.org/ | sh
    
  2. Prepend stack exec --package diagrams -- to the command you have there:

    stack exec --package diagrams -- runhaskell diagram1.hs -w 256 -h 256 -o diagram1.svg
    

His step 1 resulted in this:

admin@admins-MacBook-Pro-2 haskellProjects % curl -sSL https://get.haskellstack.org/ | sh

Stack version 2.11.1 already appears to be installed at:
  /Users/admin/.ghcup/bin/stack

Use 'stack upgrade' or your OS's package manager to upgrade,
or pass '-f' to this script to over-write the existing binary, e.g.:
  curl -sSL https://get.haskellstack.org/ | sh -s - -f

To install to a different location, pass '-d DESTDIR', e.g.:
  curl -sSL https://get.haskellstack.org/ | sh -s - -d /opt/stack/bin
admin@admins-MacBook-Pro-2 haskellProjects % 

His step 2 resulted in this:

admin@admins-MacBook-Pro-2 project1 % stack exec --package diagrams -- runhaskell diagram1.hs -w 256 -h 256 -o diagram1.svg
ghc: panic! (the 'impossible' happened)
  (GHC version 8.6.5 for x86_64-apple-darwin):
    Prelude.chr: bad argument: 2650800131

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug


Error: [S-6374]
       While building simple Setup.hs (scroll up to its section to see the error) using:
       /Users/admin/.ghcup/ghc/8.6.5/bin/ghc -rtsopts -threaded 
       -clear-package-db -global-package-db -hide-all-packages -package base 
       -main-is StackSetupShim.mainOverride -package Cabal-2.4.0.1 
       /Users/admin/.stack/setup-exe-src/setup-6HauvNHV.hs 
       /Users/admin/.stack/setup-exe-src/setup-shim-6HauvNHV.hs -o 
       /Users/admin/.stack/setup-exe-cache/x86_64-osx/tmp-Cabal-simple_6HauvNHV_2.4.0.1_ghc-8.6.5
       Process exited with code: ExitFailure 1 
admin@admins-MacBook-Pro-2 project1 % 

I have written quite a number of programs in Haskell, but have never used cabal, stack or package.

I tried the reddit user's suggestion (see above), which resulted in failure (see above). Before that, I had experimented with stack and cabal, but my attempts failed. The cabal and stack User Manuals do not seem to help much.

duplode
  • 33,731
  • 7
  • 79
  • 150
R. Adams
  • 31
  • 3
  • 2
    `GHC version 8.6.5` That's an old GHC version, you should find something newer. Especially since you hit a bug ("the 'impossible' happened) which is likely fixed. Pick your package manager of choice or use ghcup and install a new compiler before continuing. – Thomas M. DuBuisson Jul 23 '23 at 23:11
  • 1
    Agreed with Thomas on 8.6.5 being old. However, if you're using stack, his proposed fix probably isn't right -- stack has its own mechanisms for choosing GHC versions. Take a look in the stack documentation; I believe the thing you want to do is switch resolvers. (But I have never used stack in any way more complicated than running `stack build` for a project where somebody else set everything up, so take my suggestion with its own grain of salt.) – Daniel Wagner Jul 24 '23 at 00:41
  • 1
    It might be a good idea to nuke all Haskell tooling currently installed and setting up fresh. Note that [GHCup](https://www.haskell.org/ghcup/) is now the most convenient way of installing the basics. – leftaroundabout Jul 24 '23 at 01:27
  • Thanks to the three of you, above; those are good explanations and suggestions. K. A. Buhr, below, has also provided some good information, which I am going to follow tomorrow. – R. Adams Jul 24 '23 at 04:57

1 Answers1

3

I'm using Linux rather than MacOS, but I think the same general advice applies.

It looks like you have a version of Stack that's been previously installed by GHCup, but the installation is broken for some reason.

You might find it helpful to start from scratch by getting rid of any of the following directories that exist:

/Users/admin/.ghcup
/Users/admin/.stack
/Users/admin/.cabal

There is nothing important in these directories that can't be regenerated, so it's safe to remove them, but if you're nervous about doing that, just rename them to get them out of the way.

Then, you should be able to either follow the Redditor's instructions to install Stack, or perhaps reinstall GHCup, which can manage your Stack installation.

Here's my transcript on my Linux desktop for starting from scratch and running your example program, which you may find helpful. After clearing out my old installation, I used GHCup to reinstall Stack, and then I used the stack script command to run your program. I used "resolver" lts-21.4, which is the most recent long-term support resolver listed on https://www.stackage.org/. Originally, I only supplied a --package diagrams argument to build and use the diagrams package, but that generated some errors about hidden modules together with some advice for additional packages (diagrams-lib and diagrams-svg) that might be needed. It seems likely that newer versions of the diagrams package have been split up across multiple package.

Ultimately, the actual commands I ran were:

cd
rm -rf .stack .gchup .cabal
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
mkdir src/haskell/project1
cd src/haskell/project1
<<create diagram1.hs>>
stack script --resolver lts-21.4 --package diagrams --package diagrams-lib \
    --package diagrams-svg diagram1.hs -- -w 256 -h 256 -o diagram1.svg

and this successfully generated a diagram1.svg file.

The mostly full transcript with only a few boring parts removed is:

~$ cd
~$ rm -rf .stack .ghcup .cabal
~$ # See instructions on www.haskell.org/ghcup
~$ curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

Welcome to Haskell!

[...]

Press ENTER to proceed or ctrl-c to abort.
Note that this script can be re-run at any given time.

-------------------------------------------------------------------------------

Detected bash shell on your system...
Do you want ghcup to automatically add the required PATH variable to "/u/buhr/.bashrc"?

[P] Yes, prepend  [A] Yes, append  [N] No  [?] Help (default is "P").

y
-------------------------------------------------------------------------------
Do you want to install haskell-language-server (HLS)?
HLS is a language-server that provides IDE-like functionality
and can integrate with different editors, such as Vim, Emacs, VS Code, Atom, ...
Also see https://haskell-language-server.readthedocs.io/en/stable/

[Y] Yes  [N] No  [?] Help (default is "N").

n
-------------------------------------------------------------------------------
Do you want to enable better integration of stack with GHCup?
This means that stack won't install its own GHC versions, but uses GHCup's.
For more information see:
  https://docs.haskellstack.org/en/stable/yaml_configuration/#ghc-installation-customisation-experimental
If you want to keep stacks vanilla behavior, answer 'No'.

[Y] Yes  [N] No  [?] Help (default is "Y").

y

[...]

If you are new to Haskell, check out https://www.haskell.org/ghcup/steps/
~$ cd src/haskell
~/src/haskell$ mkdir project1
~/src/haskell$ cd project1
~/src/haskell/project1$ cat >diagram1.hs
import Diagrams.Prelude
import Diagrams.Backend.SVG.CmdLine

sierpinski :: Int -> Diagram SVG
sierpinski 1 = eqTriangle 1
sierpinski n =
      s
     ===
  (s ||| s) # centerX
  where
    s = sierpinski (n - 1)

example :: Diagram SVG
example = sierpinski 5 # fc black

main :: IO ()
main = defaultMain example
~/src/haskell/project1$ stack script --resolver lts-21.4 --package diagrams --package diagrams-lib --package diagrams-svg diagram1.hs -- -w 256 -h 256 -o diagram1.svg
Using resolver: lts-21.4 specified on command line
[...lots of build output the first time...]
[...this goes faster the second time you run it...]
~/src/haskell/project1$ ls -l
total 28
-rw-r--r-- 1 buhr buhr   311 Jul 23 22:34 diagram1.hs
-rw-r--r-- 1 buhr buhr 21236 Jul 23 22:44 diagram1.svg
~/src/haskell/project1$
K. A. Buhr
  • 45,621
  • 3
  • 45
  • 71
  • Thank you for the excellent detail! I will begin again, tomorrow. – R. Adams Jul 24 '23 at 04:59
  • Dear K. A. Buhr, your steps listed above appear to have resulted in success!!! I am very pleased. I now have a diagram1.svg file. But what can I do with it? It appears to be an HTTP document, correct? Can I open it in a Web browser? – R. Adams Jul 24 '23 at 23:45
  • 1
    Okay, I figured it out: I used the Safari web browser, selected Open File..., found and selected the diagram1.svg file, and pressed the Open button. That yielded a real cool-looking diagram of triangles within a triangle! Thank you, again. – R. Adams Jul 24 '23 at 23:55