Gradient Ascent

a web-log about computers, math, hacks, games, and life.

by Youwen Wu | |

why I made my blog in haskell

a purely functional...blog?

2024-05-25
(last updated: 2024-05-25T12:00:00Z)

Welcome! This is the first post on gradient ascent and also one that tests all of the features.

gradient ascent

I’ll be writing about computers, code, math, video games, and whatever else here.

A monad is just a monoid in the category of endofunctors, what’s the problem?

haskell?

This entire blog is generated with hakyll. It’s a library for generating static sites for Haskell, a purely functional programming language. It’s a library because it doesn’t come with as many batteries included as tools like Hugo or Astro. You set up most of the site yourself by calling the library from Haskell.

Here’s a brief excerpt:

main :: IO ()
main = hakyllWith config $ do
    forM_
        [ "CNAME"
        , "favicon.ico"
        , "robots.txt"
        , "_config.yml"
        , "images/*"
        , "out/*"
        , "fonts/*"
        ]
        $ \f -> match f $ do
            route idRoute
            compile copyFileCompiler

The code highlighting is also generated by hakyll.


why?

Haskell is a purely functional language with no mutable state. Its syntax actually makes it pretty elegant for declaring routes and “rendering” pipelines.

I originally wanted to build this entire blog myself. I had a working version with the Svelte framework, complete with GFM rendering, table of contents, KaTeX math, code highlighting, static generation, and other goodies. However, it seemed like a little too much work to maintain. I switched to hakyll because

  1. Haskell is cool.
  2. It comes with enough features that I don’t feel like I have to build everything from scratch.
  3. It comes with Pandoc, a Haskell library for converting between markdown formats. It’s probably more powerful than anything you could do in nodejs. It renders all of the markdown to HTML as well as the math.
    1. It supports KaTeX as well as MathML. I’m a little disappointed with the KaTeX though. It doesn’t directly render it, but simply injects the KaTeX files and renders it client-side.

speaking of math

We can have math inline, like so: ex2dx=π\int_\infty^\infty \, e^{-x^2}\,dx = \sqrt{\pi}. This site ships semantic MathML math with its HTML, and the MathJax script to the client.

It’d be nice if MathML could just be used and supported across all browsers, but unfortunately we still aren’t quite there yet. Firefox is the only one where everything looks 80% of the way to LaTeX. On Safari and Chrome, even simple equations like π\sqrt{\pi} render improperly.

Pros of MathML:

Cons:

This site has MathJax render all of the math so it looks nice and standardized across browsers, but the math still displays regardless (like say if MathJax couldn’t load due to slow network) because of MathML. Best of both worlds.

Let’s try it now. Here’s a simple theorem:

an+bncn{a,b,c}n3 a^n + b^n \ne c^n \, \forall\,\left\{ a,\,b,\,c \right\} \in \mathbb{Z} \land n \ge 3

The proof is trivial and will be left as an exercise to the reader.

seems a little overengineered

Probably is. Not as much as the old one, though.