pretty-types

A small pretty printing DSL for complex types.

https://github.com/sheyll/pretty-types#readme

Version on this page:0.2.3.1
LTS Haskell 21.25:0.4.0.0
Stackage Nightly 2023-06-21:0.4.0.0
Latest on Hackage:0.4.0.0

See all snapshots pretty-types appears in

BSD-3-Clause licensed by Sven Heyll
Maintained by [email protected]
This version can be pinned in stack with:pretty-types-0.2.3.1@sha256:5e4b9a3345f4c38fbae84861aa8b684f0ae6b9c21c38eb316129b7ac1b63b770,3675

Module documentation for 0.2.3.1

Depends on 3 packages(full list with versions):

Build Status Hackage

Type Pretty Printing

A tiny eDSL, not unlike ErrorMessage for TypeErrors to pretty print types, especially uninhabited types.

This Example will render this table:

+-------+-----+------------+
|  col 1|col 2|       col 3|
+-------+-----+------------+
|   2423|  451|       21234|
| 242322|   42|         n/a|
|      0| 4351|      623562|
|   4351|  n/a|         n/a|
|      0| 4351|      623562|
+-------+-----+------------+

From this code:

module Main (main) where

import Data.Type.Pretty
import GHC.TypeLits
import Data.Kind
import Data.Proxy

main :: IO ()
main = putStrLn $ showPretty (Proxy :: Proxy TestTable)

type TestTable =
  'MyTable         '[MyCol "col 1" 7, MyCol "col 2" 5, MyCol "col 3" 12]
          '[ MyRow '[2423           ,451             ,21234]
           , MyRow '[242322         ,42]
           , MyRow '[0              ,4351            ,623562]
           , MyRow '[4351]
           , MyRow '[0              ,4351            ,623562]
           ]

data MyTable = MyTable [Type] [Type]
data MyCol :: Symbol -> Nat -> Type
data MyRow :: [Nat] -> Type


type instance ToPretty ('MyTable cols rows) =
           PrettyManyIn (PutStr "+") (RowSepLine cols)
      <$$> PrettyManyIn (PutStr "|") (TableHeading cols)
      <$$> PrettyManyIn (PutStr "+") (RowSepLine cols)
      <$$> PrettyHigh   (TableRows cols rows)
      <$$> PrettyManyIn (PutStr "+") (RowSepLine cols)

type family TableHeading (cols :: [Type]) :: [PrettyType]
type instance TableHeading '[] = '[]
type instance TableHeading (MyCol title width ': r) =
  PutStrW width title  ': TableHeading  r

type family
   RowSepLine (cols :: [Type]) :: [PrettyType] where
   RowSepLine '[] = '[]
   RowSepLine (MyCol title width ': r) =
     PrettyOften width (PutStr "-") ': RowSepLine  r

type family
  TableRows (cols :: [Type]) (rows :: [Type]) :: [PrettyType] where
  TableRows cols '[] = '[]
  TableRows cols (MyRow cells ': rest ) =
    PrettyManyIn (PutStr "|") (TableCells cols cells) ': TableRows cols rest

type family
  TableCells (cols :: [Type]) (cells :: [Nat]) :: [PrettyType] where
  TableCells '[] cells = '[]
  TableCells (MyCol title width ': cols) (value ': cells) =
    PutNatW width value ':  TableCells cols cells
  TableCells (MyCol title width ': cols) '[] =
    PutStrW width "n/a" ':  TableCells cols '[]

Changes

Version 0.2.3.1

  • Switch to Stackage LTS-7.0

Version 0.2.3.0

  • Added parameterized pretty printing, similar to type-level pretty-printing functions

Version 0.2.2.1

  • Fixed several @since haddock comments

Version 0.2.2.0

  • Added a ToPretty instance for ‘Tagged’

Version 0.2.1.0

  • Added ToPretty instances for type level strings and numbers
  • Added ToPretty instances for types of kind Maybe and Bool
  • Added ToPretty instances for some standard number types (Word*, Int*)

Version 0.2.0.0

  • Added indentation support
  • Added <:> combinator

Version 0.1.0.0

  • Initial Version