haskellsvghaskell-diagrams

Haskell diagrams: generating SVG without compiling?


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.


Solution

  • 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"