1

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.

guthrie
  • 4,529
  • 4
  • 26
  • 31
  • 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 Mar 26 '18 at 21:53
  • 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 Mar 26 '18 at 22:23
  • [The type of `mkSizeSpec` has changed a few *diagrams* versions ago](https://hackage.haskell.org/package/diagrams-lib-1.4.2/docs/Diagrams-Size.html#v:mkSizeSpec), 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 Mar 26 '18 at 22:40
  • 1
    @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](https://github.com/gibiansky/IHaskell) can.) – leftaroundabout Mar 26 '18 at 23:48
  • @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 Mar 27 '18 at 02:20
  • @guthrie `mkSizeSpec2D` appears to be reexported by `Diagrams.TwoD` and, transitively, by `Diagrams` and `Diagrams.Prelude`. Other than the signature change, it should work the same as before. – duplode Mar 27 '18 at 02:27
  • @leftaroundabout - yes; the Haskell file spawns a viewer, which is then run by GHCi from main. – guthrie Mar 27 '18 at 13:29

1 Answers1

2

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"
Chris Martin
  • 30,334
  • 10
  • 78
  • 137