prettyprinter-interp-0.1.0.0: Efficient interpolation for Prettyprinter
Copyright(C) 2022 Peter Lebbing
LicenseBSD2 (see the file LICENSE)
MaintainerPeter Lebbing <peter@digitalbrains.com>
Safe HaskellNone
LanguageHaskell2010

Prettyprinter.Interpolate

Description

Efficient interpolation for Prettyprinter

This module provides efficient interpolation of string-interpolate quasi quoters when used with prettyprinters Documents.

The normal quasi quoters from string-interpolate do work when used as a Doc. Newlines are even converted to Prettyprinter.line. However, this method is inefficient. The following code functions correctly:

 {-# LANGUAGE OverloadedStrings #-}
 {-# LANGUAGE QuasiQuotes #-}

 module Main where

 import Data.String.Interpolate
 import Data.Text (Text)
 import Prettyprinter

 f :: Text
 f = "world"

 g :: Doc ()
 g = [i|Hello #{f}!|]

 main :: IO ()
 main = print g
 

However, what happens under the hood is that f is converted to String, the interpolated string is built manipulating Strings, and then the output is converted to Text in prettyprinter. The following code is much better:

 g = pretty ([i|Hello #{f}!|] :: Text)
 

Now, the interpolated string is constructed as Text, and this is passed cleanly into Doc which also uses Text as its underlying type for representation of text. At no point is f converted to String, and the string construction benefits from the performance of Text. And again, newlines are converted to line.

This module defines wrapper quasi quoters that automatically perform the pretty invocation, and can simply be used as:

 g = [di|Hello #{f}!|]
 
Synopsis

Documentation

di :: QuasiQuoter Source #

Wrapper around the i quasi quoter, producing a Doc

Newlines in the text are converted to line.

__di :: QuasiQuoter Source #

Wrapper around the __i quasi quoter, producing a Doc

Newlines in the text are converted to line.

diii :: QuasiQuoter Source #

Wrapper around the iii quasi quoter, producing a Doc

Newlines in the text are converted to line.

d__i'E :: QuasiQuoter Source #

Wrapper around the __i'E quasi quoter, producing a Doc

Newlines in the text are converted to line.

d__i'L :: QuasiQuoter Source #

Wrapper around the __i'L quasi quoter, producing a Doc

Newlines in the text are converted to line.

diii'E :: QuasiQuoter Source #

Wrapper around the iii'E quasi quoter, producing a Doc

Newlines in the text are converted to line.

diii'L :: QuasiQuoter Source #

Wrapper around the iii'L quasi quoter, producing a Doc

Newlines in the text are converted to line.