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".
I am using MacOS Ventura 13.4.1 (c) (22F770820d)
My shell is zsh
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:
Install stack:
curl -sSL https://get.haskellstack.org/ | shPrepend
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.
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:
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 scriptcommand 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 diagramsargument to build and use thediagramspackage, but that generated some errors about hidden modules together with some advice for additional packages (diagrams-libanddiagrams-svg) that might be needed. It seems likely that newer versions of thediagramspackage have been split up across multiple package.Ultimately, the actual commands I ran were:
and this successfully generated a
diagram1.svgfile.The mostly full transcript with only a few boring parts removed is: