type-of-html: High performance type driven html generation.

[ bsd3, html, language, library, text, web ] [ Propose Tags ]

This library makes most invalid html documents compile time errors and uses advanced type level features to realise compile time computations.

[Skip to Readme]
Change log ChangeLog.md
Dependencies base (==4.10.*), bytestring, text [details]
License BSD-3-Clause
Copyright 2017, Florian Knupfer
Author Florian Knupfer
Maintainer fknupfer@gmail.com
Category Language
Home page https://github.com/knupfer/type-of-html
Source repo head: git clone https://github.com/knupfer/type-of-html
Uploaded by knupfer at Tue Sep 12 19:27:20 UTC 2017
Distributions LTSHaskell:, NixOS:, Stackage:, openSUSE:
Downloads 2827 total (110 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]
Hackage Matrix CI




Maintainer's Corner

For package maintainers and hackage trustees

Readme for type-of-html-

[back to package description]

Type of html

Type of html is a library for generating html in a highly performant, modular and type safe manner.

Please look at the documentation of the module for an overview of the api: Html

Note that you need at least ghc 8.2.


Part of the html spec is encoded at the typelevel, turning a lot of mistakes into type errors.

Let's check out the /type safety/ in ghci:

>>> td_ (tr_ "a")

<interactive>:1:1: error:
    • 'Tr is not a valid child of 'Td
    • In the expression: td_ (tr_ "a")
      In an equation for ‘it’: it = td_ (tr_ "a")

<interactive>:1:6: error:
    • 'Tr can't contain a string
    • In the first argument of ‘td_’, namely ‘(tr_ "a")’
      In the expression: td_ (tr_ "a")
      In an equation for ‘it’: it = td_ (tr_ "a")

>>> tr_ (td_ "a")


>>> td_A (A.coords_ "a") "b"

<interactive>:1:1: error:
    • 'CoordsA is not a valid attribute of 'Td
    • In the expression: td_A (A.coords_ "a") "b"
      In an equation for ‘it’: it = td_A (A.coords_ "a") "b"

>>> td_A (A.id_ "a") "b"
<td id="a">b</td>

Every parent child relation of html elements is checked against the specification of html and non conforming elements result in compile errors.

The same is true for html attributes.

The checking is a bit lenient at the moment:

  • some elements can't contain itself as any descendant: at the moment we look only at direct children. This allows some (quite exotic) invalid html documents.
  • some elements change their permitted content based on attributes: we always allow content as if all relevant attributes are set.

Never the less: these cases are seldom. In the vast majority of cases you're only allowed to construct valid html. These restrictions aren't fundamental, they could be turned into compile time errors. Perhaps a future version will be even more strict.


Html documents are just ordinary haskell values which can be composed or abstracted over:

>>> let table = table_ . map (tr_ . map td_)
>>> :t table
table :: ('Td ?> a) => [[a]] -> 'Table > ['Tr > ['Td > a]]
>>> table [["A","B"],["C"]]
>>> import Data.Char
>>> html_ . body_ . table $ map (\c -> [[c], show $ ord c]) ['a'..'d']

And here's an example module:

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds     #-}

module Main where

import Html

import qualified Html.Attribute as A

main :: IO ()
  = print
  . page
  $ map td_ [1..(10::Int)]

  :: 'Tr ?> a
  => a
  -> ('Div :@: ('ClassA := String # 'IdA := String))
        ( 'Div > String
        # 'Div > String
        # 'Table > 'Tr > a
page tds =
  div_A (A.class_ "qux" # A.id_ "baz")
    ( div_ "foo"
    # div_ "bar"
    # table_ (tr_ tds)

Please note that the type of 'page' is inferable, so ask ghc-mod or whatever you use to write it for you. If you choose not to write the types, you don't need the language extensions. I strongly suggest that you don't write signatures for bigger documents.

All text will be automatically html escaped:

>>> i_ "&"

>>> div_A (A.id_ ">") ()
<div id="&gt;"></div>

If you want to opt out, wrap your types into the 'Raw' constructor. This will increase performance, but can be only used with trusted input. You can use this e.g. to embed some blaze-html code into type-of-html.

>>> i_ (Raw "</i><script></script><i>")


Type of html is a lot faster than blaze html or than lucid.

Look at the following benchmarks:

Remember this benchmark from blaze?


This is comparing blaze with type of html:


To look at the exact code of this benchmark look here in the repo. The big table benchmark here is only a 4x4 table. Using a 1000x10 table like on the blaze homepage yields even better relative performance (~9 times faster), but would make the other benchmarks unreadable.

How is this possible? We supercompile lots of parts of the generation process. This is possible thanks to the new features of GHC 8.2: AppendSymbol. We represent tags as kinds and map these tags to (a :: [Symbol]) and then fold all neighbouring Proxies with AppendSymbol. Afterwards we retrieve the Proxies with symbolVal which will be embedded in the executable as Addr#. All this happens at compile time. At runtime we do only generate the content and append Builders.

For example, if you write:

renderText $ tr_ (td_ "test")

The compiler does optimize it to the following (well, unpackCString# doesn't exist for Builder, so it's slightly more complicated):

decodeUtf8 $ toLazyByteString
  (  Data.ByteString.Builder.unpackCString# "<tr><td>"#
  <> escape (Data.Text.unpackCString# "test"#)
  <> Data.ByteString.Builder.unpackCString# "</tr>"#

If you write

renderBuilder $ div_ (div_ ())

The compiler does optimize it to the following:

Data.ByteString.Builder.unpackCString# "<div><div></div></div>"#

This sort of compiletime optimization isn't for free, it'll increase compilation times.

Comparision to lucid and blaze-html

Advantages of 'type-of-html':

  • more or less 5 times faster
  • a lot higher type safety: a lot of invalid documents are not inhabited
  • fewer dependencies

Disadvantages of 'type-of-html':

  • a bit noisy syntax (don't write types!)
  • sometimes unusual type error messages
  • compile times (1 min for a medium sized page, with -O0 only ~4sec)
  • needs at least ghc 8.2

I'd generally recommend that you put your documents into an extra module to avoid frequent recompilations. Additionally you can use type-of-html within an blaze-html document and vice versa. This allows you to gradually migrate, or only write the hotpath in a more efficient representation.

Example usage

{-# OPTIONS_GHC -fno-warn-missing-signatures #-}

module Main where

import Html

import Data.Text.Lazy.IO as TL

main :: IO ()
main = TL.putStrLn $ renderText example

example =
    ( body_
      ( h1_
        ( img_
        # strong_ "0"
      # div_
        ( div_ "1"
      # div_
        ( form_
          ( fieldset_
            ( div_
              ( div_
                ( label_ "a"
                # select_
                  ( option_ "b"
                  # option_ "c"
                # div_ "d"
            # button_ (i_ "e")