1
votes

I tried the Fibonacci demo from the diagrams gallery but, as many of their examples do, it requires compilation and then the mainWith function takes various options, including a file name for SVG output. I'd like to get the same result of an .svg output file, but from within GHCi, without having to compile first.

While this Q&A is about the same issue, the solution there uses API calls that no longer seem to work, such as SizeSpec2D and mkSizeSpec.

1
So what are you asking – how to fool around with diagrams in an REPL, or how to generate SVG files within some existing Haskell application? - leftaroundabout
How to generate svg files from inside GHCi. Seems that if I manually set the args in GHCi (:set args ...) and then run main it will work, but I'd like to do this from within the .hs file itself. - guthrie
The type of mkSizeSpec has changed a few diagrams versions ago, as SizeSpec is now parameterised over the number of dimensions. The solution in the other Q&A should work if you adjust it to account for that. - duplode
@guthrie yeah but why do you want that? The point of an REPL is to have direct feedback, but GHCi can't show you SVGs. (IHaskell can.) - leftaroundabout
@duplode - Thanks, I saw that, but hadn't yet figured out the mapping. And mkSizeSpec2D is still listed in Diagrams.TwoD.Size, although including it doesn't seem to expose it. Just working my way through this library... - guthrie

1 Answers

2
votes

You can :load the .lhs file into GHCi and then use the :main command to run it.

$ cd $(mktemp -d)                                                                                                                                                                                                                     

$ wget -q https://archives.haskell.org/projects.haskell.org/diagrams/gallery/FibCalls.lhs

$ stack ghci --package diagrams-lib diagrams-svg diagrams-contrib

λ> :load FibCalls.lhs
[1 of 1] Compiling Main             ( FibCalls.lhs, interpreted )
Ok, one module loaded.

λ> :main -o out.svg

λ> :quit
Leaving GHCi.

$ head -n2 out.svg 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"